Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 3 additions & 86 deletions bin/link-build.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { writeFileSync } from 'node:fs';
import { readFile, unlink, writeFile } 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';

Expand Down Expand Up @@ -36,84 +31,6 @@ const pack = pkgs.map(async (pkg) => {

await Promise.all(pack);

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

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

const tarball = join(dist, 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;
writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2), {
encoding: 'utf8',
});

console.log(chalk.green(`Successfully unpacked ${pkg.name}`));
} catch (error) {
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": "glint-monorepo-built",
"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(dist, 'package.json'), packageJson, { encoding: 'utf8' });
await writeFile(join(dist, 'pnpm-workspace.yaml'), workspaceYaml, { encoding: 'utf8' });

await execa('pnpm', ['install'], {
cwd: dist,
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(dist, pkg.name);

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

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

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

throw new Error(message);
}
}
console.log(
chalk.green(`Successfully packed all packages. Ready for linking in external project.`),
);
37 changes: 30 additions & 7 deletions bin/link-install.mjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { readFileSync } from 'node:fs';
import { readFileSync, writeFileSync } from 'node:fs';
import path from 'node:path';
import chalk from 'chalk';
import { execa } from 'execa';
import { glob } from 'glob';
import assert from 'node:assert';

const rootDir = new URL('..', import.meta.url).pathname;

const CWD = process.cwd();

const friendlyCWD = CWD.replace(process.env.HOME, '~');
const friendlyRoot = rootDir.replace(process.env.HOME, '~');

assert(
CWD !== rootDir,
`Cannot run link-install from the glint monorepo. Must be ran from an external project`,
Expand All @@ -27,26 +28,48 @@ function shouldLink(dep) {
return dep.startsWith('@glint/');
}

const tars = glob.sync('*.tgz', {
cwd: path.join(rootDir, 'dist'),
});

console.log(tars);

const link = packageJsonPaths.map(async (packageJsonPath) => {
const packagePath = path.dirname(packageJsonPath);

try {
const packageJson = JSON.parse(await readFileSync(packageJsonPath, { encoding: 'utf8' }));

const friendlyCWD = CWD.replace(process.env.HOME, '~');
const friendlyRoot = rootDir.replace(process.env.HOME, '~');
console.log(`Gathering packages from ${chalk.gray(friendlyRoot)}`);

for (const [dep] of [
...Object.entries(packageJson.dependencies ?? {}),
...Object.entries(packageJson.devDependencies ?? {}),
]) {
if (shouldLink(dep)) {
let tarified = dep.replace('@', '').replace('/', '-');
let tar = tars.find((x) => x.startsWith(tarified));

if (!tar) {
console.warn(`Could not find mapping to ${dep} from ${packagePath} using ${tarified}`);
continue;
}

let tarPath = path.join(rootDir, 'dist', tar);
let relativeTarPath = path.relative(packagePath, tarPath);

// eslint-disable-next-line no-console
console.log(`Linking ${chalk.yellow(dep)} within ${chalk.grey(friendlyCWD)}`);
await execa('pnpm', ['link', '--global', dep], { cwd: packagePath });
console.log(
`Linking ${chalk.yellow(dep)} within ${chalk.grey(friendlyCWD)} to ${chalk.green(relativeTarPath)}`,
);

packageJson.pnpm ||= {};
packageJson.pnpm.overrides ||= {};
Object.assign(packageJson.pnpm.overrides, {});
}
}

writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
} catch (error) {
let message = `Failed to link ${packagePath}`;

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"execa": "^9.5.2",
"mkdirp": "^3.0.1",
"rimraf": "^6.0.1",
"tar": "^7.4.3",
"@glimmer/component": "^2.0.0",
"@glint/tsserver-plugin": "workspace:*",
"@typescript-eslint/eslint-plugin": "^5.42.1",
Expand Down
53 changes: 0 additions & 53 deletions pnpm-lock.yaml

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

Loading