-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·85 lines (80 loc) · 2.65 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
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
78
79
80
81
82
83
84
85
#!/usr/bin/env node
'use strict'
if (require.main === module) {
const repdeps = require('./index')
const getStdin = require('get-stdin')
const { parent, regex, replacement, deletion } = require('yargs')
.usage(`Usage: $0 [-p <parent>] [-x <regex>] [-r <replacement>] [-d]\n
A JSON dependency structure is read from stdin, transformed, and written to stdout.`)
.example('$0 -p foo', 'Replace "foo/..." paths with "foo"')
.example('$0 -p foo -d', 'Delete "foo/..." paths')
.example('$0 -x \'^(?!foo/).*$\' -d', 'Delete all paths except "foo/..."')
.example('$0 -x \'([^/]*).*\' -r \'$1\'', 'Replace all paths by their first component')
.epilog(`Either a parent (-p) or a regex (-x) is required, but not both.
Either a replacement (-r) or a deletion (-d) is required, but not both.
If only parent is specified, replacement is assumed and defaults to parent.`)
.showHelpOnFail(false, 'Specify --help for available options')
.strict()
.option('parent', {
alias: 'p',
demand: false,
describe: 'Parent for selecting paths',
nargs: 1,
type: 'string'
})
.option('regex', {
alias: 'x',
conflicts: 'parent',
demand: false,
describe: 'Regex for selecting paths',
nargs: 1,
type: 'string'
})
.option('replacement', {
alias: 'r',
demand: false,
nargs: 1,
describe: 'Replacement path',
type: 'string'
})
.option('deletion', {
alias: 'd',
conflicts: 'replacement',
default: undefined,
demand: false,
describe: 'Delete selected paths',
requiresArg: false,
type: 'boolean'
})
.check(function (argv) {
if (argv.regex === undefined) {
// Require either parent or regex
if (argv.parent === undefined) {
throw (new Error('One of parent (-p) or regex (-x) must be specified'))
}
} else {
// Require either replacement or deletion
if (argv.replacement === undefined && !argv.deletion) {
throw (new Error('One of replacement (-r) or deletion (-d) must be specified'))
}
}
return true
})
.argv
getStdin()
.then(str => {
const deps = JSON.parse(str)
return parent
? deletion
? repdeps.deleteChildren(deps, parent)
: repdeps.replaceChildren(deps, parent, replacement)
: deletion
? repdeps.deleteMatchingPaths(deps, new RegExp(regex))
: repdeps.replaceMatchingPaths(deps, new RegExp(regex), replacement)
})
.then(result => process.stdout.write(JSON.stringify(result)))
.catch(err => {
console.error(err)
process.exit(1)
})
}