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 {Engine} from './sources/Engine';
|
||||||
import {SupportedPackageManagerSet} from './sources/types';
|
import {SupportedPackageManagerSet} from './sources/types';
|
||||||
|
|
||||||
function shouldGenerateShim(name: string) {
|
|
||||||
// No filtering needed at the moment
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
const engine = new Engine();
|
const engine = new Engine();
|
||||||
|
|
||||||
const distDir = path.join(__dirname, `dist`);
|
const distDir = path.join(__dirname, `dist`);
|
||||||
|
|
@ -23,26 +18,33 @@ fs.mkdirSync(shimsDir, {recursive: true});
|
||||||
fs.mkdirSync(physicalNodewinDir, {recursive: true});
|
fs.mkdirSync(physicalNodewinDir, {recursive: true});
|
||||||
|
|
||||||
async function main() {
|
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) {
|
for (const packageManager of SupportedPackageManagerSet) {
|
||||||
const binSet = engine.getBinariesFor(packageManager);
|
const binSet = engine.getBinariesFor(packageManager);
|
||||||
|
|
||||||
for (const binaryName of binSet) {
|
for (const binaryName of binSet) {
|
||||||
const entryPath = path.join(distDir, `${binaryName}.js`);
|
const entryPath = path.join(distDir, `${binaryName}.js`);
|
||||||
const entryScript = [
|
const entryScript = [
|
||||||
`#!/usr/bin/env node\n`,
|
`#!/usr/bin/env node`,
|
||||||
`require('./corepack').runMain(['${binaryName}', ...process.argv.slice(2)]);\n`,
|
`require('./lib/corepack.cjs').runMain(['${binaryName}', ...process.argv.slice(2)]);`,
|
||||||
].join(``);
|
].join(`\n`);
|
||||||
|
|
||||||
fs.writeFileSync(entryPath, entryScript);
|
fs.writeFileSync(entryPath, entryScript);
|
||||||
fs.chmodSync(entryPath, 0o755);
|
fs.chmodSync(entryPath, 0o755);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const binaryName of fs.readdirSync(distDir)) {
|
for (const entry of fs.readdirSync(distDir, {withFileTypes: true})) {
|
||||||
if (shouldGenerateShim(binaryName) === false)
|
if (entry.isDirectory())
|
||||||
continue;
|
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
|
// 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),
|
stat: (p: string, cb: () => void) => fs.stat(remapPath(p), cb),
|
||||||
});
|
});
|
||||||
|
|
||||||
for (const binaryName of fs.readdirSync(distDir)) {
|
for (const entry of fs.readdirSync(distDir, {withFileTypes: true})) {
|
||||||
if (shouldGenerateShim(binaryName) === false)
|
if (entry.isDirectory())
|
||||||
continue;
|
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.`);
|
console.log(`All shims have been generated.`);
|
||||||
|
|
|
||||||
|
|
@ -51,8 +51,8 @@
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "rm -rf dist shims && run build:bundle && ts-node ./mkshims.ts",
|
"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'",
|
"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/_entryPoint.ts",
|
"corepack": "ts-node ./sources/_cli.ts",
|
||||||
"lint": "eslint .",
|
"lint": "eslint .",
|
||||||
"prepack": "yarn build",
|
"prepack": "yarn build",
|
||||||
"postpack": "rm -rf dist shims",
|
"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