Skip to content

Commit df89e38

Browse files
authored
refactor: separate library and cli entry point (#250)
1 parent 25fa623 commit df89e38

File tree

5 files changed

+22
-24
lines changed

5 files changed

+22
-24
lines changed

mkshims.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ import path from 'path';
55
import {Engine} from './sources/Engine';
66
import {SupportedPackageManagerSet} from './sources/types';
77

8-
function shouldGenerateShim(name: string) {
9-
// No filtering needed at the moment
10-
return true;
11-
}
12-
138
const engine = new Engine();
149

1510
const distDir = path.join(__dirname, `dist`);
@@ -23,26 +18,33 @@ fs.mkdirSync(shimsDir, {recursive: true});
2318
fs.mkdirSync(physicalNodewinDir, {recursive: true});
2419

2520
async function main() {
21+
const corepackPath = path.join(distDir, `corepack.js`);
22+
fs.writeFileSync(corepackPath, [
23+
`#!/usr/bin/env node`,
24+
`require('./lib/corepack.cjs').runMain(process.argv.slice(2));`,
25+
].join(`\n`));
26+
fs.chmodSync(corepackPath, 0o755);
27+
2628
for (const packageManager of SupportedPackageManagerSet) {
2729
const binSet = engine.getBinariesFor(packageManager);
2830

2931
for (const binaryName of binSet) {
3032
const entryPath = path.join(distDir, `${binaryName}.js`);
3133
const entryScript = [
32-
`#!/usr/bin/env node\n`,
33-
`require('./corepack').runMain(['${binaryName}', ...process.argv.slice(2)]);\n`,
34-
].join(``);
34+
`#!/usr/bin/env node`,
35+
`require('./lib/corepack.cjs').runMain(['${binaryName}', ...process.argv.slice(2)]);`,
36+
].join(`\n`);
3537

3638
fs.writeFileSync(entryPath, entryScript);
3739
fs.chmodSync(entryPath, 0o755);
3840
}
3941
}
4042

41-
for (const binaryName of fs.readdirSync(distDir)) {
42-
if (shouldGenerateShim(binaryName) === false)
43+
for (const entry of fs.readdirSync(distDir, {withFileTypes: true})) {
44+
if (entry.isDirectory())
4345
continue;
4446

45-
await cmdShim(path.join(distDir, binaryName), path.join(shimsDir, path.basename(binaryName, `.js`)), {createCmdFile: true});
47+
await cmdShim(path.join(distDir, entry.name), path.join(shimsDir, path.basename(entry.name, `.js`)), {createCmdFile: true});
4648
}
4749

4850
// The Node distribution doesn't support symlinks, so they copy the shims into
@@ -60,11 +62,11 @@ async function main() {
6062
stat: (p: string, cb: () => void) => fs.stat(remapPath(p), cb),
6163
});
6264

63-
for (const binaryName of fs.readdirSync(distDir)) {
64-
if (shouldGenerateShim(binaryName) === false)
65+
for (const entry of fs.readdirSync(distDir, {withFileTypes: true})) {
66+
if (entry.isDirectory())
6567
continue;
6668

67-
await cmdShim(path.join(virtualNodewinDir, `dist/${binaryName}`), path.join(physicalNodewinDir, path.basename(binaryName, `.js`)), {createCmdFile: true, fs: easyStatFs});
69+
await cmdShim(path.join(virtualNodewinDir, `dist/${entry.name}`), path.join(physicalNodewinDir, path.basename(entry.name, `.js`)), {createCmdFile: true, fs: easyStatFs});
6870
}
6971

7072
console.log(`All shims have been generated.`);

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@
5151
},
5252
"scripts": {
5353
"build": "rm -rf dist shims && run build:bundle && ts-node ./mkshims.ts",
54-
"build:bundle": "esbuild ./sources/_entryPoint.ts --bundle --platform=node --target=node14.14.0 --external:corepack --outfile='./dist/corepack.js' --resolve-extensions='.ts,.mjs,.js'",
55-
"corepack": "ts-node ./sources/_entryPoint.ts",
54+
"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'",
55+
"corepack": "ts-node ./sources/_cli.ts",
5656
"lint": "eslint .",
5757
"prepack": "yarn build",
5858
"postpack": "rm -rf dist shims",

sources/_cli.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import {runMain} from './main';
2+
3+
runMain(process.argv.slice(2));

sources/_entryPoint.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

sources/_lib.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {runMain} from './main';

0 commit comments

Comments
 (0)