-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts-nodejs.js
executable file
·66 lines (60 loc) · 1.63 KB
/
scripts-nodejs.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env node
import * as util from 'node:util'
import { spawn } from 'child_process'
const { values, positionals } = util.parseArgs({
args: process.argv.slice(2),
allowPositionals: true,
options: {
level: {
type: 'string',
},
help: {
type: 'boolean',
},
},
})
// TODO: --verbose
const helpText = `hyperupcall-scripts-nodejs <SUBCOMMAND> [--help]
SUBCOMMANDS:
format [check|fix]
lint [check|fix] --level none|dev|commit|release
`
if (!positionals[0] || !positionals[1]) {
process.stdout.write(helpText)
process.exit(1)
}
if (positionals[0] === 'format') {
if (positionals[1] == 'check') {
run(['prettier', '--check', '--ignore-unknown', '.'])
} else if (positionals[1] === 'fix') {
run(['prettier', '--write', '--ignore-unknown', '.'])
} else {
throw new Error(`Invalid: ${positionals[1]}`)
}
} else if (positionals[0] === 'lint') {
if (positionals[1] === 'check') {
run(['eslint', '.'])
} else if (positionals[1] === 'fix') {
run(['eslint', '--fix', '.'])
} else {
throw new Error(`Invalid: ${positionals[1]}`)
}
} else {
throw new Error(`Invalid: ${positionals[0]}`)
}
async function run(/** @type {string[]} */ command) {
const child = spawn(command[0], command.slice(1), {
env: {
...process.env,
/**
* This experimental flag is enabled by default on some Node versions. It causes
* with Prettier, giving warnings like `[warn] Ignored unknown option { __esModule: true }`
* Disable for now.
*/
NODE_OPTIONS: '--no-experimental-require-module',
HYPERUPCALL_FORMAT_LEVEL: values.level ?? 'dev',
},
})
child.stdout.pipe(process.stdout)
child.stderr.pipe(process.stderr)
}