-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
😅 Add a hackneyed initial draft of "check JSR metadata and publish on…
…ly if local version higher" feature in the GHA automation
- Loading branch information
Showing
12 changed files
with
267 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"name": "@axhxrx/assert-never", | ||
"version": "0.1.1", | ||
"version": "0.1.7", | ||
"exports": "./mod.ts" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@axhxrx/date", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"exports": "./mod.ts", | ||
"importMap": "import_map.json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@axhxrx/detect-runtime", | ||
"version": "0.1.2", | ||
"version": "0.1.7", | ||
"exports": "./index.ts", | ||
"importMap": "import_map.json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
export type JsrScopeName = `@${string}`; | ||
|
||
/** | ||
Fetch metadata about JSR package. E.g.: | ||
```ts | ||
const metadata = await jsrMetadataFetch('axhxrx', 'date'); | ||
// an example of what the looks like: | ||
const example = { | ||
scope: "axhxrx", | ||
name: "date", | ||
latest: "0.1.6", | ||
versions: { | ||
"0.1.4": {}, | ||
"0.1.1": { | ||
yanked: true, | ||
}, | ||
"0.1.5": {}, | ||
"0.1.2": {}, | ||
"0.1.0": {}, | ||
"0.1.6": {}, | ||
}, | ||
} | ||
```ts | ||
*/ | ||
export const jsrMetadataFetch = async (scopeName: JsrScopeName, packageName: string): Promise<any> => | ||
{ | ||
const trimmedScopeName = scopeName.substring(1); | ||
const url = `https://jsr.io/@${trimmedScopeName}/${packageName}/meta.json`; | ||
const response = await fetch(url, { | ||
headers: { | ||
'User-Agent': 'JSR Metadata Fetcher', | ||
Accept: 'application/json', | ||
}, | ||
}); | ||
if (!response.ok) | ||
{ | ||
throw new Error(`Failed to fetch JSR metadata from ${url}`); | ||
} | ||
const result = await response.json(); | ||
return result; | ||
}; | ||
|
||
import * as semver from "@std/semver"; | ||
|
||
/** | ||
Returns `true` if the JSR metadata `latest` version string indicates an older version when compared to `localVersionString`, `false` otherwise. Comparison is done by `@std/semver` and it throws if you (or, for that matter, JSR) pass in a bad version string. | ||
@throws some error if something goes wrong | ||
*/ | ||
export const jsrVersionIsOlderThan = async (scopeName: JsrScopeName, packageName: string, otherVersionString: string, jsrMetadata?: {latest: string }): Promise<any> => { | ||
const metadata = await jsrMetadataFetch(scopeName, packageName); | ||
const jsrVersionString = metadata.latest; | ||
|
||
const jsrVersion = semver.parse(jsrVersionString); | ||
const localVersion = semver.parse(otherVersionString); | ||
|
||
const isOlder = semver.lessThan(jsrVersion, localVersion); | ||
return isOlder; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
Clunky hack first draft! 🚧 | ||
*/ | ||
console.log('ARGS', Deno.args); | ||
|
||
import { join } from 'https://deno.land/[email protected]/path/mod.ts'; | ||
const cwd = Deno.cwd(); | ||
|
||
const firstArg = Deno.args[0]; | ||
const secondArg = Deno.args[1]; | ||
const thirdArg = Deno.args[2]; | ||
|
||
let scopeName: JsrScopeName; | ||
let packageName: string; | ||
let localVersionString: string; | ||
|
||
if (firstArg && secondArg && thirdArg) | ||
{ | ||
console.log('taking arguments from command line'); | ||
scopeName = '@' + firstArg; | ||
packageName = secondArg; | ||
localVersionString = thirdArg; | ||
} | ||
else if (firstArg || secondArg || thirdArg) | ||
{ | ||
console.log("ERR_INVALID_ARGS: it's an all-or-nothing affair"); | ||
console.log( | ||
'Usage: deno run --allow-read --allow-net local-version-is-newer.deno.ts SCOPE_NAME PACKAGE_NAME LOCAL_VERSION_STRING', | ||
); | ||
Deno.exit(1); | ||
} | ||
else | ||
{ | ||
const pathToJsrMetadata = firstArg ?? join(cwd, 'jsr.json'); | ||
|
||
console.log(`🌮🌮🌮 local-version-is-newer.deno.ts running in ${cwd}`); | ||
console.log(`Will attempt to read "${cwd}/jsr.json" to check local version...`); | ||
|
||
const localJsrMetadata = Deno.readTextFileSync(pathToJsrMetadata); | ||
const localJsrMetadataJson = JSON.parse(localJsrMetadata); | ||
localVersionString = localJsrMetadataJson.version; | ||
} | ||
|
||
if (!localVersionString) | ||
{ | ||
throw new Error(`No version found in "${cwd}/jsr.json"...`); | ||
} | ||
|
||
if (typeof localVersionString !== 'string' || localVersionString.length < 5 | ||
|| localVersionString.split('.').length !== 3) | ||
{ | ||
throw new Error(`Weird unusable version: "${localVersionString}"...`); | ||
} | ||
console.log(`Local version: ${localVersionString}`); | ||
|
||
import { jsrMetadataFetch, JsrScopeName, jsrVersionIsOlderThan } from './mod.ts'; | ||
|
||
const remoteMetadata = await jsrMetadataFetch('@axhxrx', 'date'); | ||
|
||
if (!remoteMetadata) | ||
{ | ||
throw new Error(`No metadata found for "@axhxrx/date"...`); | ||
} | ||
|
||
console.log(`Remote version: ${remoteMetadata.latest}`); | ||
|
||
const isOld = await jsrVersionIsOlderThan('@axhxrx', 'date', localVersionString, remoteMetadata); | ||
|
||
console.log('JSR version is old:', isOld); | ||
|
||
if (!isOld) | ||
{ | ||
console.log('Doing nothing and exiting with exit status 1'); | ||
Deno.exit(1); | ||
} | ||
else | ||
{ | ||
console.log('Will exit with status 0'); | ||
Deno.exit(0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./jsrMetadataFetch.ts"; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { it, describe, expect} from 'bun:test' | ||
|
||
import { jsrMetadataFetch, jsrVersionIsOlderThan } from "./mod.ts"; | ||
describe('JsrMetadata ', () => { | ||
it('can fetch', () => { | ||
const md = jsrMetadataFetch('@axhxrx', 'date'); | ||
}); | ||
|
||
it('can check if JSR version is old', async () => { | ||
const md = jsrMetadataFetch('@axhxrx', 'date'); | ||
const isOld = await jsrVersionIsOlderThan('@axhxrx', 'date', '0.1.0'); | ||
expect(isOld).toBe(false); | ||
const isOld2 = await jsrVersionIsOlderThan('@axhxrx', 'date', '999.1.0'); | ||
expect(isOld2).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters