Skip to content

Commit

Permalink
Restore a working linking setup
Browse files Browse the repository at this point in the history
The purpose of this is to restore a working setup where we can link
all glimmer-vm to ember.js without publishing.

Previously this was just a script that `yarn link` on both sides.

It seems like we tried to use `yalc` at some point.

Neither of those other solutions fully works with the constraints:

* supports pnpm
* supports pnpm workspace
* supports `publishConfig`
* resolve internal dependencies (like `@glimmer/encoder`) when
  linked

This script makes it work with all of the above.

Usage:

1. pnpm install, develop, make changes, etc
2. pnpm build
3. pnpm link:all
4. pnpm link:glimmer-vm from ember.js
5. validate, iterate, re-run pnpm build/pnpm link:all as needed
6. pnpm unlink from ember.js
7. pnpm unlink:all here

See also emberjs/ember.js#20590
  • Loading branch information
chancancode committed Dec 8, 2023
1 parent bb7faf4 commit 3b9789a
Show file tree
Hide file tree
Showing 13 changed files with 467 additions and 134 deletions.
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
/bower_components/
/node_modules/
**/node_modules/
**/.yalc/

# misc
/coverage/
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/dist/
**/dist
/control-dist/
/linkable-dist/
node_modules/
**/node_modules/
**/.turbo
Expand All @@ -13,5 +14,4 @@ tmp/
*.pdf
instrumentation.*.json
.cache
**/.yalc
**/*.tgz
1 change: 0 additions & 1 deletion benchmark/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"./benchmarks/krausest/lib/**/*.js",
"./benchmarks/krausest/vite.config.mts"
],
"exclude": ["**/.yalc"],
"references": [
{
"path": "../packages/@glimmer/tsconfig.json"
Expand Down
116 changes: 116 additions & 0 deletions bin/link-all.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { readFile, writeFile, unlink } from 'node:fs/promises';
import { join } from 'node:path';
import chalk from 'chalk';
import { execa } from 'execa';
import { mkdirp } from 'mkdirp';
import { rimraf } from 'rimraf';
import { x as untar } from 'tar';
import { packages } from './packages.mjs';
import { writeFileSync } from 'node:fs';

const dest = new URL('../linkable-dist', import.meta.url).pathname;
const pkgs = packages('@glimmer');

await mkdirp(dest);
await rimraf(dest + '/*.tgz', { glob: true });

const pack = pkgs.map(async (pkg) => {
try {
await execa('pnpm', ['pack', '--pack-destination', dest], {
cwd: pkg.path,
});

console.log(chalk.green(`Successfully packed ${pkg.name}`));
} catch (error: unknown) {
let message = `Failed to pack ${pkg.name}`;

if (error instanceof Error) {
message += `\n\n${error.stack}`;
}

throw new Error(message);
}
});

await Promise.all(pack);

const unpack = pkgs.map(async (pkg) => {
try {
const pkgDest = join(dest, pkg.name);

await mkdirp(pkgDest);
await rimraf(pkgDest + '/**/*');

const tarball = join(dest, pkg.name.replace('@', '').replace('/', '-') + `-${pkg.version}.tgz`);

await untar({
file: tarball,
strip: 1,
cwd: pkgDest,
});

await unlink(tarball);

// https://github.com/pnpm/pnpm/issues/881
const packageJsonPath = join(pkgDest, 'package.json');
const packageJson = JSON.parse(await readFile(packageJsonPath, { encoding: 'utf8' }));
delete packageJson.devDependencies;
await writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2), {
encoding: 'utf8',
});

console.log(chalk.green(`Successfully unpacked ${pkg.name}`));
} catch (error: unknown) {
let message = `Failed to unpack ${pkg.name}`;

if (error instanceof Error) {
message += `\n\n${error.stack}`;
}

throw new Error(message);
}
});

await Promise.all(unpack);

const packageJson = `{
"name": "linkable-dist",
"private": true,
"overrides": {
${pkgs.map((pkg) => ` "${pkg.name}": "workspace:*"`).join(',\n')}
}
}
`;

const workspaceYaml = 'packages:\n' + pkgs.map((pkg) => ` - '${pkg.name}'\n`).join('');

await writeFile(join(dest, 'package.json'), packageJson, { encoding: 'utf8' });
await writeFile(join(dest, 'pnpm-workspace.yaml'), workspaceYaml, { encoding: 'utf8' });

await execa('pnpm', ['install'], {
cwd: dest,
stdio: 'inherit',
});

console.log(chalk.green(`Successfully installed packages`));

// Seems like there are race conditions in pnpm if we try to do these concurrently
for (const pkg of pkgs) {
try {
const pkgDest = join(dest, pkg.name);

await execa('pnpm', ['link', '--global'], {
cwd: pkgDest,
});

console.log(chalk.green(`Successfully linked ${pkg.name}`));
} catch (error: unknown) {
let message = `Failed to link ${pkg.name}`;

if (error instanceof Error) {
message += `\n\n${error.stack}`;
}

throw new Error(message);
}
}
5 changes: 4 additions & 1 deletion bin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
"@types/js-yaml": "^4.0.9",
"@types/node": "^20.9.4",
"@types/puppeteer-chromium-resolver": "workspace:^",
"@types/tar": "^6.1.10",
"chalk": "^5.2.0",
"execa": "^7.1.1",
"glob": "^10.2.3",
"js-yaml": "^4.1.0",
"puppeteer-chromium-resolver": "^20.0.0"
"mkdirp": "^3.0.1",
"puppeteer-chromium-resolver": "^20.0.0",
"tar": "^6.2.0"
},
"devDependencies": {
"eslint": "^8.54.0",
Expand Down
23 changes: 23 additions & 0 deletions bin/unlink-all.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import chalk from 'chalk';
import { execa } from 'execa';
import { rimraf } from 'rimraf';
import { packages } from './packages.mjs';

for (const pkg of packages('@glimmer')) {
try {
await execa('pnpm', ['uninstall', '--global', pkg.name]);

console.log(chalk.green(`Successfully unlinked ${pkg.name}`));
} catch (error: unknown) {
let message = `Failed to unlink ${pkg.name}`;

if (error instanceof Error) {
message += `\n\n${error.stack}`;
}

throw new Error(message);
}
}

const dest = new URL('../linkable-dist', import.meta.url).pathname;
await rimraf(dest);
13 changes: 0 additions & 13 deletions bin/yalc-publish.mts

This file was deleted.

70 changes: 0 additions & 70 deletions lib/local-linker/index.js

This file was deleted.

13 changes: 0 additions & 13 deletions lib/local-linker/package.json

This file was deleted.

9 changes: 0 additions & 9 deletions lib/tsconfig.json

This file was deleted.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"build": "dotenv -- turbo build",
"build:control": "rollup -c rollup.config.mjs",
"build:flags": "RETAIN_FLAGS=true ember build --env production --suppress-sizes",
"link:all": "esyes ./bin/link-all.mts",
"lint": "npm-run-all lint:*",
"lint:format": "prettier -c .",
"lint:files": "turbo lint",
Expand All @@ -31,7 +32,8 @@
"test:node": "node bin/run-node-tests.mjs",
"test:babel-plugins": "yarn workspace @glimmer/vm-babel-plugins test",
"test:smoke": "SMOKE_TESTS=true ember test",
"test:types": "node bin/run-types-tests.mjs"
"test:types": "node bin/run-types-tests.mjs",
"unlink:all": "esyes ./bin/unlink-all.mts"
},
"pnpm": {
"overrides": {
Expand Down Expand Up @@ -94,6 +96,7 @@
"eslint-plugin-qunit": "^8.0.1",
"eslint-plugin-simple-import-sort": "^10.0.0",
"eslint-plugin-unused-imports": "^3.0.0",
"esyes": "^1.0.1",
"execa": "^7.1.1",
"fast-glob": "^3.2.12",
"glob": "^10.2.3",
Expand Down Expand Up @@ -167,7 +170,7 @@
"node": ">=16.0.0"
},
"volta": {
"node": "20.1.0",
"node": "20.9.0",
"pnpm": "8.5.0"
}
}
Loading

0 comments on commit 3b9789a

Please sign in to comment.