-
-
Notifications
You must be signed in to change notification settings - Fork 945
Expand file tree
/
Copy pathiodd
More file actions
executable file
·55 lines (43 loc) · 1.56 KB
/
iodd
File metadata and controls
executable file
·55 lines (43 loc) · 1.56 KB
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
55
#!/usr/bin/env node
import { program } from 'commander';
import { spawn } from 'node:child_process';
import fs from 'node:fs/promises';
import path from 'node:path';
program
.name('iodd')
.description(
'(I)nstall (o)ptional (d)ev (d)ependencies from package.json#optionalDevDependencies'
)
.option('-v, --verbose', 'Output npm install output to stdout/stderr')
.option('-r, --required', 'Exit with non-zero code if dependencies fail to install')
.argument('[packagePath]', 'Path to package.json file', './package.json')
.action(main);
async function main(packagePath, options) {
// Get list of optional dependencies from package.json
const json = await fs.readFile(path.join(process.cwd(), packagePath));
const packageJson = JSON.parse(json);
const { optionalDevDependencies: deps } = packageJson;
if (!deps) {
console.error(`No optional dependencies found in ${packagePath}`);
process.exit(1);
}
const packageRefs = Object.entries(deps).map(([name, version]) => `${name}@${version}`);
// Install optional dependencies with child_process running npm
const args = ['install', '--no-save', ...packageRefs];
console.log('Running: ', 'npm', args.join(' '));
const cp = spawn('npm', args);
if (options.verbose) {
cp.stdout.pipe(process.stdout);
cp.stderr.pipe(process.stderr);
}
const exitCode = await new Promise((resolve) => {
cp.on('close', resolve);
});
if (exitCode !== 0) {
console.error('Dependencies failed to install');
if (options.required) {
process.exit(exitCode);
}
}
}
program.parseAsync();