mirror of https://github.com/nodejs/corepack.git
refactor: separate library and cli entry point (#250)
This commit is contained in:
parent
25fa62354b
commit
df89e38200
30
mkshims.ts
30
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.`);
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -0,0 +1,3 @@
|
|||
import {runMain} from './main';
|
||||
|
||||
runMain(process.argv.slice(2));
|
||||
|
|
@ -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));
|
||||
|
|
@ -0,0 +1 @@
|
|||
export {runMain} from './main';
|
||||
Loading…
Reference in New Issue