-
Notifications
You must be signed in to change notification settings - Fork 59
/
bin.js
executable file
·89 lines (75 loc) · 2.06 KB
/
bin.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
86
87
88
89
#!/usr/bin/env node
const fmt = require('./')
const fs = require('fs')
const stdin = require('stdin')
const argv = require('minimist')(process.argv.slice(2), {
boolean: ['help', 'stdin', 'version', 'write'],
alias: {
h: 'help',
w: 'write',
v: 'version'
}
})
// running `standard-format -` is equivalent to `standard-format --stdin`
if (argv._[0] === '-') {
argv.stdin = true
argv._.shift()
}
if (argv.help) {
console.log(function () {
/*
standard-format - Auto formatter for the easier cases in standard
Usage:
standard-format <flags> [FILES...]
If FILES is omitted, then all JavaScript source files (*.js) in the current
working directory are checked, recursively.
These paths are automatically excluded:
node_modules/, .git/, *.min.js, bundle.js
Flags:
--stdin Read file text from stdin.
-w, --write Directly modify input files.
-v, --version Show current version.
-h, --help Show usage information.
Readme: https://github.com/maxogden/standard-format
*/
}.toString().split(/\n/).slice(2, -2).join('\n'))
process.exit(0)
}
if (argv.version) {
console.log(require('./package.json').version)
process.exit(0)
}
function processFile (transformed) {
if (argv.write && transformed.name !== 'stdin') {
fs.writeFileSync(transformed.name, transformed.data)
} else {
process.stdout.write(transformed.data)
}
}
function getFiles (done) {
const args = argv._
if (argv.stdin) {
return stdin(function (file) {
return done(null, [{ name: 'stdin', data: file }])
})
} else if (args.length === 0) {
return fmt.load(done)
} else {
return done(null, args.map(function (file) {
return { name: file, data: fs.readFileSync(file).toString() }
}))
}
}
getFiles(function (err, files) {
if (err) return error(err)
files.forEach(function (file) {
try {
file.data = fmt.transform(file.data)
processFile(file)
} catch (e) { error(file.name + ': ' + e) }
})
})
function error (err) {
console.error(err)
process.exit(1)
}