Skip to content

Commit

Permalink
use pnpm overrides instead of root syncpack (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
turbocrime committed Jul 9, 2024
1 parent 144c2fa commit 801ad3c
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 66 deletions.
25 changes: 25 additions & 0 deletions .pnpmfile.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Developers may wish to use pnpm.overrides to specify local tarballs or other
* customization, but pnpm currently does not respect overrides during peer
* dependency resolution.
*
* @see https://github.com/pnpm/pnpm/issues/4214
*
* @param pkg manifest of package currently being read
*/
const overridePeerDependencies = (pkg, context) => {
const overrides = require('./package.json').pnpm?.overrides ?? {};
for (const dep of Object.keys(pkg.peerDependencies ?? {})) {
if (dep in overrides) pkg.peerDependencies[dep] = overrides[dep];
}
return pkg;
};

module.exports = {
hooks: {
readPackage(pkg, context) {
pkg = overridePeerDependencies(pkg, context);
return pkg;
},
},
};
10 changes: 0 additions & 10 deletions .syncpackrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"dependencyTypes": [
"dev",
"peer",
"pnpmOverrides",
"prod"
],
"versionGroups": [
Expand Down Expand Up @@ -47,15 +46,6 @@
"snapTo": [
"prax-wallet"
]
},
{
"label": "Control @penumbra-zone packages from root",
"dependencies": [
"@penumbra-zone/*"
],
"snapTo": [
"prax-wallet"
]
}
]
}
18 changes: 4 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"private": true,
"license": "(MIT OR Apache-2.0)",
"packageManager": "[email protected]+sha512.f549b8a52c9d2b8536762f99c0722205efc5af913e77835dbccc3b0b0b2ca9e7dc8022b78062c17291c48e88749c70ce88eb5a74f1fa8c4bf5e18bb46c8bd83a",
"type": "module",
"scripts": {
"add:tgz": "tsx scripts/add-tgz.ts",
"all-check": "pnpm clean && pnpm install && pnpm lint && pnpm build && pnpm test",
"buf-update": "pnpm update --latest \"@buf/*\" \"@bufbuild/*\" \"@connectrpc/*\" && pnpm syncpack fix-mismatches && pnpm install",
"build": "turbo build",
Expand All @@ -20,6 +22,7 @@
"lint:prettier": "prettier --check .",
"lint:rust": "turbo lint:rust",
"lint:syncpack": "syncpack lint",
"postadd:tgz": "$npm_execpath install",
"postinstall": "syncpack list-mismatches",
"pretest": "playwright install",
"test": "turbo test",
Expand All @@ -35,20 +38,7 @@
"@bufbuild/protobuf": "^1.10.0",
"@connectrpc/connect": "^1.4.0",
"@connectrpc/connect-web": "^1.4.0",
"@penumbra-zone/bech32m": "^6.1.1",
"@penumbra-zone/client": "^11.0.0",
"@penumbra-zone/crypto-web": "^12.0.0",
"@penumbra-zone/getters": "^11.0.0",
"@penumbra-zone/keys": "^4.2.1",
"@penumbra-zone/perspective": "^13.0.0",
"@penumbra-zone/protobuf": "^5.4.0",
"@penumbra-zone/query": "^14.0.0",
"@penumbra-zone/services": "^16.0.0",
"@penumbra-zone/storage": "^13.0.0",
"@penumbra-zone/transport-chrome": "^5.0.1",
"@penumbra-zone/transport-dom": "^7.2.1",
"@penumbra-zone/types": "^15.0.0",
"@penumbra-zone/wasm": "^16.0.0"
"tsx": "^4.16.2"
},
"devDependencies": {
"@buf/connectrpc_eliza.bufbuild_es": "1.10.0-20230913231627-233fca715f49.1",
Expand Down
1 change: 1 addition & 0 deletions packages/context/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@penumbra-labs/registry": "9.4.0",
"@penumbra-zone/bech32m": "^6.1.1",
"@penumbra-zone/crypto-web": "^12.0.0",
"@penumbra-zone/getters": "^11.0.0",
"@penumbra-zone/protobuf": "^5.4.0",
"@penumbra-zone/query": "^14.0.0",
"@penumbra-zone/storage": "^13.0.0",
Expand Down
1 change: 1 addition & 0 deletions packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@penumbra-zone/bech32m": "^6.1.1",
"@penumbra-zone/getters": "^11.0.0",
"@penumbra-zone/perspective": "^13.0.0",
"@penumbra-zone/protobuf": "^5.4.0",
"@penumbra-zone/types": "^15.0.0",
"@penumbra-zone/wasm": "^16.0.0",
"@radix-ui/react-avatar": "^1.0.4",
Expand Down
65 changes: 23 additions & 42 deletions pnpm-lock.yaml

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

66 changes: 66 additions & 0 deletions scripts/add-tgz.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { type SpawnOptionsWithoutStdio, spawn } from 'node:child_process';
import fs from 'node:fs/promises';
import { basename, resolve } from 'node:path';
import { argv, exit } from 'node:process';
import url, { URL } from 'node:url';

// ----- utils
const usage = (reason?: string) => {
console.warn(reason);
console.warn(`Usage: ${basename(argv[1])} <tarball0.tgz> [tarball1.tgz] [tarball2.tgz] ...`);
console.warn("Each tarball should have an accompanying 'package.json' in the same directory.");
return 1;
};

const cmd = (exec: string, args?: string[], options?: SpawnOptionsWithoutStdio) =>
new Promise<void>((resolve, reject) => {
console.log(exec, ...(args ?? []));
const run = spawn(exec, args ?? [], { ...options, stdio: 'inherit' });
run.on('error', reject);
run.on('exit', code => (code ? exit(code) : resolve()));
});

const readJson = async (fileUrl: URL): Promise<unknown> =>
JSON.parse((await fs.readFile(fileUrl)).toString());

const isPackageJsonWithName = (packageJson: unknown): packageJson is { name: string } =>
typeof packageJson === 'object' &&
packageJson !== null &&
'name' in packageJson &&
typeof packageJson.name === 'string';

// ----- main

const tarballs = argv.slice(2);

if (!tarballs.length) exit(usage('Missing tarball filenames'));
if (!tarballs.every(fn => fn.endsWith('.tgz')))
exit(usage("Tarball filenames must end with '.tgz'"));

// absolute file URLs
const tarballUrls = tarballs.map(tarball => url.pathToFileURL(resolve(tarball)));

// create record of package name to tarball path
const overrides: Record<string, URL> = Object.fromEntries(
await Promise.all(
tarballUrls.map(async tarballUrl => {
// TODO: the correct package.json is inside the tarball

const tarballPackageJson = await readJson(
// assume package.json exists in same directory as the tarball
// (default behavior of npm pack)
new URL('package.json', tarballUrl),
);

if (!isPackageJsonWithName(tarballPackageJson))
throw new TypeError(`Invalid package.json adjacent to ${tarballUrl}`);

return [tarballPackageJson.name, tarballUrl];
}),
),
);

for (const [name, tarballUrl] of Object.entries(overrides)) {
await cmd('pnpm', ['pkg', 'set', `pnpm.overrides.${name}=${tarballUrl}`]);
await cmd('pnpm', ['pkg', 'set', `pnpm.peerDependencyRules.allowAny[].=${name}`]);
}

0 comments on commit 801ad3c

Please sign in to comment.