|
| 1 | +import fs from "node:fs"; |
| 2 | +import path from "node:path"; |
| 3 | + |
| 4 | +import { runNpm } from "./utils.mjs"; |
| 5 | + |
| 6 | +const bindingsDirectory = path.resolve("bindings/js"); |
| 7 | +const nativePackagesDirectory = path.join(bindingsDirectory, "npm"); |
| 8 | +const outputDirectory = path.resolve("package-artifacts/npm"); |
| 9 | +const packagePath = path.join(bindingsDirectory, "package.json"); |
| 10 | +const readmePath = path.join(bindingsDirectory, "README.md"); |
| 11 | + |
| 12 | +const nativePackages = fs |
| 13 | + .readdirSync(nativePackagesDirectory, { withFileTypes: true }) |
| 14 | + .filter((entry) => entry.isDirectory()) |
| 15 | + .map((entry) => path.join(nativePackagesDirectory, entry.name)) |
| 16 | + .sort(); |
| 17 | + |
| 18 | +if (nativePackages.length === 0) { |
| 19 | + throw new Error(`No native packages found in ${nativePackagesDirectory}`); |
| 20 | +} |
| 21 | + |
| 22 | +fs.mkdirSync(outputDirectory, { recursive: true }); |
| 23 | + |
| 24 | +for (const nativePackage of nativePackages) { |
| 25 | + const hasNativeAddon = fs |
| 26 | + .readdirSync(nativePackage, { withFileTypes: true }) |
| 27 | + .some((entry) => entry.isFile() && entry.name.endsWith(".node")); |
| 28 | + if (!hasNativeAddon) { |
| 29 | + throw new Error(`Missing native addon in ${nativePackage}`); |
| 30 | + } |
| 31 | + |
| 32 | + runNpm([ |
| 33 | + "pack", |
| 34 | + nativePackage, |
| 35 | + "--pack-destination", |
| 36 | + outputDirectory, |
| 37 | + ]); |
| 38 | +} |
| 39 | + |
| 40 | +const originalPackage = fs.readFileSync(packagePath, "utf8"); |
| 41 | +const originalReadme = fs.readFileSync(readmePath, "utf8"); |
| 42 | +const rootPackage = JSON.parse(originalPackage); |
| 43 | +rootPackage.optionalDependencies = {}; |
| 44 | + |
| 45 | +for (const nativePackage of nativePackages) { |
| 46 | + const manifest = JSON.parse( |
| 47 | + fs.readFileSync(path.join(nativePackage, "package.json"), "utf8"), |
| 48 | + ); |
| 49 | + rootPackage.optionalDependencies[manifest.name] = manifest.version; |
| 50 | +} |
| 51 | + |
| 52 | +try { |
| 53 | + fs.writeFileSync( |
| 54 | + packagePath, |
| 55 | + `${JSON.stringify(rootPackage, null, 2)}\n`, |
| 56 | + ); |
| 57 | + runNpm([ |
| 58 | + "pack", |
| 59 | + bindingsDirectory, |
| 60 | + "--ignore-scripts", |
| 61 | + "--pack-destination", |
| 62 | + outputDirectory, |
| 63 | + ]); |
| 64 | + |
| 65 | + rootPackage.name = "shell-use"; |
| 66 | + fs.writeFileSync( |
| 67 | + packagePath, |
| 68 | + `${JSON.stringify(rootPackage, null, 2)}\n`, |
| 69 | + ); |
| 70 | + fs.writeFileSync( |
| 71 | + readmePath, |
| 72 | + originalReadme.replaceAll("@microsoft/shell-use", "shell-use"), |
| 73 | + ); |
| 74 | + runNpm([ |
| 75 | + "pack", |
| 76 | + bindingsDirectory, |
| 77 | + "--ignore-scripts", |
| 78 | + "--pack-destination", |
| 79 | + outputDirectory, |
| 80 | + ]); |
| 81 | +} finally { |
| 82 | + fs.writeFileSync(packagePath, originalPackage); |
| 83 | + fs.writeFileSync(readmePath, originalReadme); |
| 84 | +} |
0 commit comments