Skip to content

Commit e6aa96d

Browse files
AgentEnderFrozenPandaz
authored andcommitted
chore(repo): improve copy-built-package script (#34432)
Makes copy-built-package script a bit more ergonomic and discoverable. Adds some interactive UI for picking package / repo if they are not specified. (cherry picked from commit 5142079)
1 parent da7b1e0 commit e6aa96d

7 files changed

Lines changed: 957 additions & 260 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"lint-pnpm-lock": "eslint pnpm-lock.yaml",
2828
"migrate-to-pnpm-version": "node ./scripts/migrate-to-pnpm-version.js",
2929
"analyze-docs": "node tools/scripts/analyze-docs.mjs",
30-
"update-all-repos": "nx run update-repos:update-all-repos --parallel 5"
30+
"update-all-repos": "nx run update-repos:update-all-repos --parallel 5",
31+
"copy-built-package": "tsx scripts/copy-built-package"
3132
},
3233
"devDependencies": {
3334
"@actions/core": "^1.10.0",

scripts/copy-built-package.ts

Lines changed: 0 additions & 259 deletions
This file was deleted.

scripts/copy-built-package/copy.ts

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import { hash } from 'crypto';
2+
import fs, { existsSync, readFileSync } from 'fs';
3+
import { dirname, join, relative } from 'path';
4+
import { globSync } from 'tinyglobby';
5+
import { ProgressDisplay } from './progress';
6+
7+
export const WORKSPACE_ROOT = join(__dirname, '../..');
8+
9+
export function copyBuiltPackage(
10+
pkg: { pkgRoot: string; npmName: string },
11+
outputWorkspace: string
12+
) {
13+
const pathInDist = join(WORKSPACE_ROOT, 'dist', pkg.pkgRoot);
14+
const copyRoot = existsSync(pathInDist) ? pathInDist : pkg.pkgRoot;
15+
16+
if (copyRoot === pkg.pkgRoot) {
17+
console.warn(`No build artifacts in "${pathInDist}, checking project root`);
18+
}
19+
20+
// Copy all files from dist/packages/<pkg> to the output package root
21+
const files = globSync(`**/*.js`, {
22+
cwd: copyRoot,
23+
dot: true,
24+
ignore: ['**/node_modules/**'],
25+
});
26+
27+
if (!files.length) {
28+
throw new Error(
29+
`Unable to find built artifacts for ${pkg.npmName}. Did you forget to build?`
30+
);
31+
}
32+
33+
const outputPkgRoot = join(outputWorkspace, 'node_modules', pkg.npmName);
34+
fs.mkdirSync(outputPkgRoot, { recursive: true });
35+
36+
// Get native files count for accurate total
37+
const nativeFiles = getNativeFiles(pkg.pkgRoot);
38+
const totalFiles = files.length + nativeFiles.length;
39+
const progress = new ProgressDisplay(totalFiles);
40+
41+
files.forEach((file) => {
42+
const destFile = join(outputPkgRoot, file);
43+
const destDir = dirname(destFile);
44+
const copySrc = join(copyRoot, file);
45+
const relativeFilePath = relative(copyRoot, copySrc);
46+
progress.update(relativeFilePath);
47+
fs.mkdirSync(destDir, { recursive: true });
48+
safeCopyFileSync(copySrc, destFile, progress);
49+
});
50+
51+
// Copy native files
52+
copyNativeFiles(pkg.pkgRoot, outputPkgRoot, nativeFiles, progress);
53+
progress.finish();
54+
}
55+
56+
function getNativeFiles(pkgRoot: string): string[] {
57+
const copyRoot = join(WORKSPACE_ROOT, pkgRoot);
58+
return globSync(`**/*.{node,wasm,js,mjs,cjs}`, {
59+
ignore: [
60+
'**/node_modules/**',
61+
'src/command-line/migrate/run-migration-process.js',
62+
],
63+
cwd: copyRoot,
64+
});
65+
}
66+
67+
function copyNativeFiles(
68+
pkgRoot: string,
69+
outputPkgRoot: string,
70+
nativeFiles: string[],
71+
progress: ProgressDisplay
72+
) {
73+
const copyRoot = join(WORKSPACE_ROOT, pkgRoot);
74+
nativeFiles.forEach((file) => {
75+
const destFile = join(outputPkgRoot, file);
76+
const destDir = dirname(destFile);
77+
const copySrc = join(copyRoot, file);
78+
const relativeFilePath = relative(copyRoot, copySrc);
79+
progress.update(relativeFilePath);
80+
fs.mkdirSync(destDir, { recursive: true });
81+
safeCopyFileSync(copySrc, destFile, progress);
82+
});
83+
}
84+
85+
function safeCopyFileSync(
86+
src: string,
87+
dest: string,
88+
progress: ProgressDisplay
89+
) {
90+
const sha = hash('sha256', readFileSync(src));
91+
for (let i = 0; i < 3; i++) {
92+
try {
93+
fs.copyFileSync(src, dest);
94+
const destSha = hash('sha256', readFileSync(dest));
95+
if (sha === destSha) {
96+
return;
97+
} else {
98+
progress.insertBefore(
99+
`Hash mismatch when copying ${src} to ${dest}, retrying...`
100+
);
101+
}
102+
} catch (e) {
103+
progress.insertBefore(
104+
`Error copying file ${src} to ${dest}: ${(e as Error).message}, retrying...`
105+
);
106+
}
107+
}
108+
throw new Error(`Failed to copy ${src} to ${dest} after 3 attempts.`);
109+
}

0 commit comments

Comments
 (0)