|
| 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