-
Notifications
You must be signed in to change notification settings - Fork 18
/
clear-package-json.js
77 lines (72 loc) · 1.52 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
72
73
74
75
76
77
#!/usr/bin/env node
const chalk = require('chalk')
const yargs = require('yargs')
const {
clearPackageJSON
} = require('./core')
const {
readJson,
readJsonFromStdin,
writeJson
} = require('./utils')
const getConfig = require('./get-config')
const { isTTY } = process.stdin
const { argv } = yargs
.usage('$0')
.command(
isTTY
? '$0 <input> [options]'
: '$0 [options]',
'Clear package.json file',
yargsCommand => {
if (isTTY) {
yargsCommand
.positional('input', {
type: 'string',
desc: 'Input package.json file'
})
.require('input')
}
}
)
.option('fields', {
type: 'array',
desc: 'One or more exclude package.json fields'
})
.option('output', {
alias: 'o',
type: 'string',
desc: 'Output file name'
})
const options = {
fields: argv.fields
}
function handleOptions () {
if (!options.fields) {
return getConfig().then(config => {
options.fields = config.fields
})
}
return Promise.resolve()
}
handleOptions()
.then(() => (
isTTY
? readJson(argv.input)
: readJsonFromStdin()
))
.then(packageJson => {
const cleanPackageJSON = clearPackageJSON(packageJson, options.fields)
if (argv.output) {
return writeJson(
argv.output,
cleanPackageJSON,
{ spaces: 2 }
)
}
process.stdout.write(`${ JSON.stringify(cleanPackageJSON, null, ' ') }\n`)
})
.catch(error => {
console.error(chalk.red(error))
process.exit()
})