Skip to content

Commit

Permalink
refactor: separate library and cli entry point (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
merceyz authored May 19, 2023
1 parent 25fa623 commit df89e38
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 24 deletions.
30 changes: 16 additions & 14 deletions mkshims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand All @@ -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
Expand All @@ -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.`);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions sources/_cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {runMain} from './main';

runMain(process.argv.slice(2));
8 changes: 0 additions & 8 deletions sources/_entryPoint.ts

This file was deleted.

1 change: 1 addition & 0 deletions sources/_lib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {runMain} from './main';

0 comments on commit df89e38

Please sign in to comment.