Skip to content

Commit

Permalink
add support in @neon-rs/manifest for optional prefixes in npm prebuil…
Browse files Browse the repository at this point in the history
…d package names

- update optional `{ neon: { type: "library", prefix?: string, ... } }` field to package.json
- update codegen of load.cts to load package name with prefix if needed
  • Loading branch information
dherman committed Nov 15, 2024
1 parent 7e0c676 commit 211a902
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 5 deletions.
55 changes: 55 additions & 0 deletions src/manifest/fixtures/empty-platforms-with-prefix/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"name": "empty-platforms-with-prefix",
"private": false,
"version": "0.1.17",
"description": "A simple Neon library with a prefix and no platforms.",
"type": "module",
"exports": {
".": {
"import": {
"types": "./types/index.d.mts",
"default": "./lib/index.mjs"
},
"require": {
"types": "./types/index.d.cts",
"default": "./lib/index.cjs"
}
}
},
"types": "./types/index.d.cts",
"main": "./lib/index.cjs",
"files": [
"lib/index.cjs",
"lib/load.cjs",
"lib/index.mjs",
"types/index.d.cts",
"types/index.d.mts"
],
"scripts": {
"test": "cargo test",
"cargo-build": "cargo build --message-format=json > cargo.log",
"cross-build": "cross build --message-format=json > cross.log",
"postcargo-build": "neon dist -v < cargo.log",
"postcross-build": "neon dist -v -m /target < cross.log",
"debug": "npm run cargo-build --",
"build": "npm run cargo-build -- --release",
"cross": "npm run cross-build -- --release",
"prepack": "neon update-platforms -v 2>update-platforms.log",
"version": "neon bump -v --binaries platforms"
},
"author": "David Herman <[email protected]>",
"license": "MIT",
"devDependencies": {
"@neon-rs/cli": "^0.0.186"
},
"dependencies": {
"@neon-rs/load": "^0.0.186"
},
"neon": {
"type": "library",
"org": "@dherman",
"prefix": "empty-platforms-with-prefix-",
"platforms": {},
"load": "./lib/load.cjs"
}
}
2 changes: 1 addition & 1 deletion src/manifest/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@neon-rs/manifest",
"private": false,
"version": "0.1.3",
"version": "0.2.0",
"description": "Library for working with Neon package configuration.",
"exports": {
".": {
Expand Down
4 changes: 2 additions & 2 deletions src/manifest/src/cache/npm/npm.cts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export class NPMCacheCfg implements CacheCfg {
[],
js.callExpression(
js.identifier('require'),
[js.literal(`${cfg.org}/${platform}`)]
[js.literal(`${cfg.org}/${cfg.prefix ?? ''}${platform}`)]
)
)
);
Expand All @@ -123,7 +123,7 @@ export class NPMCacheCfg implements CacheCfg {

packageNames(): string[] {
const cfg = this.manifest.cfg();
return Object.keys(this.manifest.allPlatforms()).map(key => `${cfg.org}/${key}`);
return Object.keys(this.manifest.allPlatforms()).map(key => `${cfg.org}/${cfg.prefix ?? ''}${key}`);
}

updatePlatforms(lib: LibraryManifest): boolean {
Expand Down
6 changes: 6 additions & 0 deletions src/manifest/src/library/library.cts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { NPMCacheCfg } from "../cache/npm/npm.cjs";
export interface LibraryCfg {
type: "library";
org: string;
prefix?: string;
platforms: PlatformFamily;
load?: string;
}
Expand All @@ -26,6 +27,11 @@ function assertIsLibraryCfg(json: unknown): asserts json is LibraryCfg {
throw new TypeError(`expected "neon.load" to be a string, found ${json.load}`);
}
}
if ('prefix' in json) {
if (typeof json.prefix !== 'string' && typeof json.prefix !== 'undefined') {
throw new TypeError(`expected "neon.prefix" to be a string, found ${json.prefix}`);
}
}
}

function isEmptyFamily(family: PlatformFamily): boolean {
Expand Down
16 changes: 16 additions & 0 deletions src/manifest/src/test/manifest.cts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ describe("simple manifest", () => {
assert.strictEqual(lib.version, '0.1.17');
});

async function testPrefixPlatforms(lib: LibraryManifest, name: string, prefix: string, version: string) {
const contents = await fs.readFile(path.join(lib.dir, 'lib', 'load.cjs'), 'utf8');
assert.include(contents, `'darwin-arm64': () => require('@${name}/${prefix}darwin-arm64')`);
assert.include(contents, `'darwin-x64': () => require('@${name}/${prefix}darwin-x64')`);
}

async function testEmptyPlatforms(lib: LibraryManifest, name: string, version: string) {
await lib.addNodePlatform('darwin-arm64');
assert.isTrue(lib.hasUnsavedChanges());
Expand Down Expand Up @@ -92,4 +98,14 @@ describe("simple manifest", () => {
const json: any = lib.toJSON();
assert.strictEqual(json.optionalDependencies['@empty-object-platforms/darwin-arm64'], lib.version);
});

it("handles prefixes", async () => {
const lib = await library('empty-platforms-with-prefix');
await lib.addPlatformPreset('macos');
await lib.saveChanges(() => {});
const reloaded = await LibraryManifest.load(lib.dir);
assert.strictEqual(reloaded.cfg().platforms, 'macos');
assert.strictEqual(reloaded.cfg().prefix, 'empty-platforms-with-prefix-');
assert.strictEqual(reloaded.cfg().org, '@dherman');
});
});
6 changes: 4 additions & 2 deletions src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
"version": "0.1.81",
"workspaces": [
"cli",
"install"
"install",
"manifest"
],
"author": "Dave Herman <[email protected]>",
"license": "MIT",
"scripts": {
"dist": "npm run dist:cli && npm run dist:install",
"dist": "npm run dist:cli && npm run dist:install && npm run dist:manifest",
"dist:cli": "cd cli && npm run dist",
"dist:install": "cd install && npm run dist",
"dist:manifest": "cd manifest && npm run dist",
"version": "node ../dist/cli bump -v --workspaces"
},
"dependencies": {
Expand Down

0 comments on commit 211a902

Please sign in to comment.