-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
cli.ts
54 lines (42 loc) Β· 1.48 KB
/
cli.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import {NodeFS} from '@yarnpkg/fslib';
import crossSpawn from 'cross-spawn';
import {dynamicRequire} from './dynamicRequire';
import {generateSdk} from './generateSdk';
const [,, name, ...rest] = process.argv;
if (name === `--help` || name === `-h`)
help(false);
else if (name === `--sdk` && rest.length === 0)
sdk();
else if (typeof name !== `undefined` && name[0] !== `-`)
run(name, rest);
else
help(true);
function help(error: boolean) {
const logFn = error ? console.error : console.log;
process.exitCode = error ? 1 : 0;
logFn(`Usage: yarn pnpify --sdk`);
logFn(`Usage: yarn pnpify <program> [...argv]`);
logFn();
logFn(`Setups a TypeScript sdk for use within your VSCode editor instance`);
logFn(`More info at https://next.yarnpkg.com/advanced/pnpify`);
}
function sdk() {
const {getPackageInformation, topLevel} = dynamicRequire(`pnpapi`);
const {packageLocation} = getPackageInformation(topLevel);
const projectRoot = NodeFS.toPortablePath(packageLocation);
generateSdk(projectRoot).catch(error => {
console.error(error.stack);
process.exitCode = 1;
});
}
function run(name: string, argv: Array<string>) {
let {NODE_OPTIONS} = process.env;
NODE_OPTIONS = `${NODE_OPTIONS || ``} --require ${dynamicRequire.resolve(`@yarnpkg/pnpify`)}`.trim();
const child = crossSpawn(name, argv, {
env: {...process.env, NODE_OPTIONS},
stdio: `inherit`,
});
child.on(`exit`, code => {
process.exitCode = code !== null ? code : 1;
});
}