-
Notifications
You must be signed in to change notification settings - Fork 0
/
glob.js
70 lines (62 loc) · 1.94 KB
/
glob.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
#!/usr/bin/env node
var program = require('commander')
var package = require('./package')
program
.version(package.version)
.description(`Match files using the patterns the shell uses, like stars and stuff.\n
This is a [glob implementation in JavaScript](https://www.npmjs.com/package/glob) for command line use.`)
.option('-j, --json', 'JSON encode matches (default separates by newline)')
.option('-s, --delimiter [separator]', 'Separate matches by delimiter (default separator is newline)')
.option('-o, --output [filename]', 'Write to file (write to stdout by default)')
.option('-c, --cwd [directory]', 'The current working directory in which to search')
.option('-n, --nodir', 'Do not match directories, only files (Note: to match only directories, simply put a / at the end of the pattern)')
.parse(process.argv);
let pattern = program.args[0] || null
let options = {
//
}
options.delimiter = (() => {
if(program.json) {
if(program.delimiter) {
console.error('Can not specify delimiter when using --json')
process.exit()
}
} else {
return program.delimiter || '\n'
}
})()
if(program.cwd)
options.cwd = program.cwd
if(program.nodir)
options.nodir = true
if(!pattern) {
console.error('Missing glob pattern argument.')
program.outputHelp()
} else {
require('glob')(pattern, options, (error, paths) => {
if (error)
throw error.message
let data = paths
if (program.json) {
data = JSON.stringify(data, false, 3)
} else {
data = ''
for (let path of paths) {
data += `${path}${options.delimiter}`
}
data = data.trim()
}
if (program.output) {
try {
require('fs').writeFileSync(program.output, data)
} catch (exception) {
console.error('Error writing file.')
console.error(exception.message)
program.outputHelp()
}
}
else {
console.log(data)
}
})
}