-
Notifications
You must be signed in to change notification settings - Fork 18
/
clear-package-json.js
executable file
·71 lines (63 loc) · 1.85 KB
/
clear-package-json.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
67
68
69
70
71
#!/usr/bin/env node
import { clearPackageJSON } from './core.js'
import { getConfig } from './get-config.js'
import {
parseListArg,
readJSON,
readJSONFromStdin,
writeJSON
} from './utils.js'
const HELP =
'npx clear-package-json <input> [options]\n' +
'\n' +
'Options:\n' +
' --help Show help\n' +
' --version Show version number\n' +
' --fields One or more exclude package.json fields\n' +
' --output, -o Output file name'
async function handleOptions() {
const options = {}
let input, output
for (let i = 2; i < process.argv.length; i++) {
if (process.argv[i] === '--help') {
process.stdout.write(HELP + '\n')
process.exit(0)
} else if (process.argv[i] === '--version') {
process.stdout.write(require('./package.json').version + '\n')
process.exit(0)
} else if (process.argv[i] === '-o' || process.argv[i] === '--output') {
output = process.argv[i + 1]
i += 1
} else if (process.argv[i] === '--fields') {
options.fields = parseListArg(process.argv[i + 1])
i += 1
} else {
input = process.argv[i]
}
}
if (!input) {
process.stderr.write(
HELP + '\n\nNot enough non-option arguments: got 0, need at least 1'
)
process.exit(1)
}
if (!options.fields) {
let config = await getConfig()
options.fields = config.fields
}
return [input, output, options]
}
async function run() {
const [input, output, options] = await handleOptions()
const packageJson = await (input ? readJSON(input) : readJSONFromStdin())
const cleanPackageJSON = clearPackageJSON(packageJson, options.fields)
if (output) {
await writeJSON(output, cleanPackageJSON)
} else {
process.stdout.write(`${JSON.stringify(cleanPackageJSON, null, ' ')}\n`)
}
}
run().catch(error => {
process.stderr.write(error.stack + '\n')
process.exit(1)
})