From df89e382003fc9edf14ced83a5832e20f873c878 Mon Sep 17 00:00:00 2001 From: Kristoffer K Date: Fri, 19 May 2023 10:08:16 +0200 Subject: [PATCH] refactor: separate library and cli entry point (#250) --- mkshims.ts | 30 ++++++++++++++++-------------- package.json | 4 ++-- sources/_cli.ts | 3 +++ sources/_entryPoint.ts | 8 -------- sources/_lib.ts | 1 + 5 files changed, 22 insertions(+), 24 deletions(-) create mode 100644 sources/_cli.ts delete mode 100644 sources/_entryPoint.ts create mode 100644 sources/_lib.ts diff --git a/mkshims.ts b/mkshims.ts index 16ab417..fde6dac 100644 --- a/mkshims.ts +++ b/mkshims.ts @@ -5,11 +5,6 @@ import path from 'path'; import {Engine} from './sources/Engine'; import {SupportedPackageManagerSet} from './sources/types'; -function shouldGenerateShim(name: string) { - // No filtering needed at the moment - return true; -} - const engine = new Engine(); const distDir = path.join(__dirname, `dist`); @@ -23,26 +18,33 @@ fs.mkdirSync(shimsDir, {recursive: true}); fs.mkdirSync(physicalNodewinDir, {recursive: true}); async function main() { + const corepackPath = path.join(distDir, `corepack.js`); + fs.writeFileSync(corepackPath, [ + `#!/usr/bin/env node`, + `require('./lib/corepack.cjs').runMain(process.argv.slice(2));`, + ].join(`\n`)); + fs.chmodSync(corepackPath, 0o755); + for (const packageManager of SupportedPackageManagerSet) { const binSet = engine.getBinariesFor(packageManager); for (const binaryName of binSet) { const entryPath = path.join(distDir, `${binaryName}.js`); const entryScript = [ - `#!/usr/bin/env node\n`, - `require('./corepack').runMain(['${binaryName}', ...process.argv.slice(2)]);\n`, - ].join(``); + `#!/usr/bin/env node`, + `require('./lib/corepack.cjs').runMain(['${binaryName}', ...process.argv.slice(2)]);`, + ].join(`\n`); fs.writeFileSync(entryPath, entryScript); fs.chmodSync(entryPath, 0o755); } } - for (const binaryName of fs.readdirSync(distDir)) { - if (shouldGenerateShim(binaryName) === false) + for (const entry of fs.readdirSync(distDir, {withFileTypes: true})) { + if (entry.isDirectory()) continue; - await cmdShim(path.join(distDir, binaryName), path.join(shimsDir, path.basename(binaryName, `.js`)), {createCmdFile: true}); + await cmdShim(path.join(distDir, entry.name), path.join(shimsDir, path.basename(entry.name, `.js`)), {createCmdFile: true}); } // The Node distribution doesn't support symlinks, so they copy the shims into @@ -60,11 +62,11 @@ async function main() { stat: (p: string, cb: () => void) => fs.stat(remapPath(p), cb), }); - for (const binaryName of fs.readdirSync(distDir)) { - if (shouldGenerateShim(binaryName) === false) + for (const entry of fs.readdirSync(distDir, {withFileTypes: true})) { + if (entry.isDirectory()) continue; - await cmdShim(path.join(virtualNodewinDir, `dist/${binaryName}`), path.join(physicalNodewinDir, path.basename(binaryName, `.js`)), {createCmdFile: true, fs: easyStatFs}); + await cmdShim(path.join(virtualNodewinDir, `dist/${entry.name}`), path.join(physicalNodewinDir, path.basename(entry.name, `.js`)), {createCmdFile: true, fs: easyStatFs}); } console.log(`All shims have been generated.`); diff --git a/package.json b/package.json index 9f38e36..735b9e5 100644 --- a/package.json +++ b/package.json @@ -51,8 +51,8 @@ }, "scripts": { "build": "rm -rf dist shims && run build:bundle && ts-node ./mkshims.ts", - "build:bundle": "esbuild ./sources/_entryPoint.ts --bundle --platform=node --target=node14.14.0 --external:corepack --outfile='./dist/corepack.js' --resolve-extensions='.ts,.mjs,.js'", - "corepack": "ts-node ./sources/_entryPoint.ts", + "build:bundle": "esbuild ./sources/_lib.ts --bundle --platform=node --target=node14.14.0 --external:corepack --outfile='./dist/lib/corepack.cjs' --resolve-extensions='.ts,.mjs,.js'", + "corepack": "ts-node ./sources/_cli.ts", "lint": "eslint .", "prepack": "yarn build", "postpack": "rm -rf dist shims", diff --git a/sources/_cli.ts b/sources/_cli.ts new file mode 100644 index 0000000..a546c98 --- /dev/null +++ b/sources/_cli.ts @@ -0,0 +1,3 @@ +import {runMain} from './main'; + +runMain(process.argv.slice(2)); diff --git a/sources/_entryPoint.ts b/sources/_entryPoint.ts deleted file mode 100644 index 80ca9b3..0000000 --- a/sources/_entryPoint.ts +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env node -import {runMain} from './main'; - -// Used by the generated shims -export {runMain}; - -if (process.mainModule === module) - runMain(process.argv.slice(2)); diff --git a/sources/_lib.ts b/sources/_lib.ts new file mode 100644 index 0000000..eeab174 --- /dev/null +++ b/sources/_lib.ts @@ -0,0 +1 @@ +export {runMain} from './main';