-
Notifications
You must be signed in to change notification settings - Fork 22
/
cli.js
50 lines (42 loc) · 1.47 KB
/
cli.js
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
#!/usr/bin/env node
import { execSync } from 'node:child_process';
import fs from 'node:fs';
import path from 'node:path';
import { hideBin } from 'yargs/helpers';
import yargs from 'yargs/yargs';
import { PACKAGES_ROOT, errorHandler, getJSON } from './scripts/helpers/index.js';
const argvNoBin = hideBin(process.argv);
const argv = yargs(argvNoBin)
.command('$0 <reflect> <package>', 'reflect the command', (yargs) => {
yargs.positional('reflect', {
describe: 'npm command to reflect',
type: 'string'
});
yargs.positional('package', {
describe: 'Package or element name',
type: 'string'
});
})
.demandCommand()
.help().argv;
const options = argvNoBin.slice(2);
// Element or package
try {
const isElement = !fs.existsSync(path.resolve(PACKAGES_ROOT, argv.package));
const workspace = isElement ? 'elements' : argv.package;
const elementName = isElement ? argv.package : undefined;
// For workspace package real name is required
const packageName = (await getJSON(path.resolve(PACKAGES_ROOT, workspace, 'package.json'), import.meta))
.name;
const command = ['npm', 'run', argv.reflect, `--workspace=${packageName}`];
// Add element name to command
if (workspace === 'elements') {
command.push(elementName ? elementName : 'elements');
}
options.length > 0 && command.push('--');
command.push(...options);
execSync(command.join(' '), { stdio: 'inherit' });
} catch (error) {
errorHandler(error);
process.exit(1);
}