Skip to content

Commit 9e83bdc

Browse files
committed
Automatically detect local packages
1 parent c37213c commit 9e83bdc

File tree

1 file changed

+25
-82
lines changed

1 file changed

+25
-82
lines changed

scripts/demos-inject-local.ts

Lines changed: 25 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -3,96 +3,30 @@ import { promises as fs } from 'node:fs';
33
import path from 'node:path';
44
import { execSync } from 'node:child_process';
55
import inquirer from 'inquirer';
6+
import { findWorkspacePackages } from '@pnpm/workspace.find-packages';
7+
8+
// Get workspace packages and create a mapping from package name to local path
9+
const workspacePackages = await findWorkspacePackages(path.resolve('.'), {
10+
patterns: ['./packages/*']
11+
});
12+
const packagePaths = {};
13+
workspacePackages.forEach((pkg) => {
14+
const pkgName = pkg.manifest.name!;
15+
if (pkgName.startsWith('@powersync/')) {
16+
packagePaths[pkgName] = pkg.rootDirRealPath;
17+
}
18+
});
619

720
// Intercepts the resolution of `@powersync/*` packages (plus peer dependencies) and
821
// replaces them with local versions.
9-
//
10-
// TODO Find out if this can be done with links instead of needing to
11-
// use bundles
12-
13-
/*
14-
const PNPMFILE_CJS = `const fs = require('fs');
15-
const path = require('path');
16-
17-
module.exports = {
18-
hooks: {
19-
async readPackage(pkg, context) {
20-
const workspacePackages = await fs.promises.readdir('../../packages');
21-
22-
const workspaceOverrides = {};
23-
workspacePackages.forEach((workspacePkg) => {
24-
workspaceOverrides['@powersync/' + workspacePkg] = path.resolve('../../packages/' + workspacePkg);
25-
});
26-
27-
const getPeerDeps = async (pkgName) => {
28-
const workspacePath = workspaceOverrides[pkgName];
29-
if (!workspacePath) return {};
30-
try {
31-
await fs.promises.access(workspacePath);
32-
const content = await fs.promises.readFile(path.join(workspacePath, 'package.json'), 'utf8');
33-
return JSON.parse(content).peerDependencies || {};
34-
} catch (e) {
35-
return {};
36-
}
37-
};
38-
39-
// Recursively find and add missing workspace peers
40-
// Loops until no new workspace dependencies are added to handle chains (A -> B -> C)
41-
let changed = true;
42-
while (changed) {
43-
changed = false;
44-
const allDeps = { ...pkg.dependencies, ...pkg.devDependencies };
45-
46-
for (const depName of Object.keys(allDeps)) {
47-
if (workspaceOverrides[depName]) {
48-
const peers = await getPeerDeps(depName);
49-
50-
for (const [peerName, peerVersion] of Object.entries(peers)) {
51-
if (workspaceOverrides[peerName] && !allDeps[peerName]) {
52-
if (!pkg.dependencies) pkg.dependencies = {};
53-
54-
pkg.dependencies[peerName] = peerVersion;
55-
56-
// Update our temp list so the loop sees it next time
57-
allDeps[peerName] = peerVersion;
58-
changed = true;
59-
context.log(\`Autofilled workspace peer \${peerName} (required by \${depName})\`);
60-
}
61-
}
62-
}
63-
}
64-
}
65-
66-
const replaceWithWorkspace = (dependencies) => {
67-
if (!dependencies) return;
68-
Object.keys(workspaceOverrides).forEach((name) => {
69-
if (dependencies[name]) {
70-
dependencies[name] = \`file:\${workspaceOverrides[name]}\`;
71-
72-
if (!pkg.dependenciesMeta) pkg.dependenciesMeta = {};
73-
if (!pkg.dependenciesMeta[name]) pkg.dependenciesMeta[name] = {};
74-
pkg.dependenciesMeta[name].injected = true;
75-
}
76-
});
77-
};
78-
79-
replaceWithWorkspace(pkg.dependencies);
80-
replaceWithWorkspace(pkg.devDependencies);
81-
82-
return pkg;
83-
}
84-
}
85-
}
86-
`;
87-
*/
88-
8922
const PNPMFILE_CJS = `const fs = require("fs");
9023
const path = require("path");
9124
9225
module.exports = {
9326
hooks: {
9427
readPackage(pkg) {
95-
const getLocalPath = (name) => \`../../packages/\${name.split("/")[1]}\`;
28+
const localPaths = ${JSON.stringify(packagePaths)};
29+
const getLocalPath = (name) => localPaths[name];
9630
9731
const injectPeers = (manifestPath) => {
9832
try {
@@ -215,6 +149,7 @@ const main = async () => {
215149
)
216150
.option('-p, --pattern <pattern>', 'specify a pattern matching which demos to select')
217151
.option('-d, --directory <path>', 'specify directory to search for demos in', 'demos')
152+
.option('-a, --all', 'run for all demos non-interactively', false)
218153
.option('-i, --install', 'run `pnpm install` after injecting', false)
219154
.option('-f, --force', 'overwrite existing `.pnpmfile.cjs` files if found', false);
220155

@@ -227,7 +162,15 @@ const main = async () => {
227162
const options = program.opts();
228163

229164
const allDemos = await getDemosAll(options);
230-
const demos = options.pattern ? filterDemosPattern(allDemos, options) : await filterDemosUser(allDemos, options);
165+
let demos: string[];
166+
167+
if (options.all) {
168+
demos = allDemos;
169+
} else if (options.pattern) {
170+
demos = filterDemosPattern(allDemos, options);
171+
} else {
172+
demos = await filterDemosUser(allDemos, options);
173+
}
231174

232175
await injectPackagesAll(demos, options);
233176
if (options.install) {

0 commit comments

Comments
 (0)