Skip to content

Commit

Permalink
😅 Add a hackneyed initial draft of "check JSR metadata and publish on…
Browse files Browse the repository at this point in the history
…ly if local version higher" feature in the GHA automation
  • Loading branch information
masonmark committed Jun 16, 2024
1 parent 942ef89 commit d1d485f
Show file tree
Hide file tree
Showing 12 changed files with 267 additions and 6 deletions.
12 changes: 10 additions & 2 deletions .github/workflows/jsr-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ jobs:
steps:
- uses: actions/checkout@v4

- name: Publish package
- name: Publish libs/ts/assert-never
working-directory: libs/ts/assert-never
run: deno run --allow-read --allow-net ../jsr-metadata/local-version-is-newer.deno.ts && npx jsr publish

- name: Publish libs/ts/date
working-directory: libs/ts/date
run: npx jsr publish
run: deno run --allow-read --allow-net ../jsr-metadata/local-version-is-newer.deno.ts && npx jsr publish

- name: Publish libs/ts/detect-runtime
working-directory: libs/ts/detect-runtime
run: deno run --allow-read --allow-net ../jsr-metadata/local-version-is-newer.deno.ts && npx jsr publish

8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ Can we construct a megarepo that contains multiple, interdependent TypeScript li

## Change log

### miscellany

- "deno.disablePaths" settings do not support globs, so to avoid Deno language server annoyingess, we must specify every single incompatible file individually (e.g. "libs/ts/detect-runtime/tests.bun.spec.ts") — obvious candidate for automation

- add a hackneyed initial draft of "check JSR metadata and publish only if local version higher" feature in the GHA automation

### 3️⃣ lib 3: `@axhxrx/detect-runtime`

Next, add a junk lib that imports the first two. This one will also import `left-pad`, of NPM fame. 😉 That's my test for "can use old NPM package".
Expand Down Expand Up @@ -79,6 +85,8 @@ error: npm package '@jsr/libs__logger' does not exist.

I think this might be some unrelated bug in Deno/JSR though. I'll tackle that separately.

UPDATE: Yeah, the .npmrc file was missing somehow... fixed by adding that back.


### 2️⃣ lib 2: `axhxrx/date`

Expand Down
Binary file modified bun.lockb
Binary file not shown.
88 changes: 87 additions & 1 deletion deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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"
}
2 changes: 1 addition & 1 deletion libs/ts/date/jsr.jsonc → libs/ts/date/jsr.json
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"
}
2 changes: 1 addition & 1 deletion libs/ts/detect-runtime/jsr.json
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"
}
60 changes: 60 additions & 0 deletions libs/ts/jsr-metadata/jsrMetadataFetch.ts
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;
}
80 changes: 80 additions & 0 deletions libs/ts/jsr-metadata/local-version-is-newer.deno.ts
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);
}
2 changes: 2 additions & 0 deletions libs/ts/jsr-metadata/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./jsrMetadataFetch.ts";

16 changes: 16 additions & 0 deletions libs/ts/jsr-metadata/tests.bun.spec.ts
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);
});
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"dependencies": {
"@libs/logger": "npm:@jsr/libs__logger",
"@std/semver": "npm:@jsr/std__semver",
"jsr": "^0.12.4",
"left-pad": "^1.3.0"
}
Expand Down

0 comments on commit d1d485f

Please sign in to comment.