-
Notifications
You must be signed in to change notification settings - Fork 7
/
cli.js
69 lines (58 loc) · 1.5 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
#!/usr/bin/env node
'use strict'
main()
function main () {
const resolveCwd = require('resolve-cwd')
const localCLI = resolveCwd('prettier-standard-formatter/cli')
if (localCLI && localCLI !== __filename) {
require(localCLI)
return
}
run()
}
function run () {
const fs = require('fs')
const globby = require('globby')
const meow = require('meow')
const recursive = require('recursive-readdir')
const prettierStandard = require('./')
const DEFAULT_IGNORE_LIST = ['.git', 'node_modules', '!*.js']
const format = path => {
fs.readFile(path, 'utf-8', (err, sourceCode) => {
if (err) throw err
prettierStandard.format(sourceCode).then(output => {
fs.writeFile(path, output, 'utf-8', err => {
if (err) throw err
console.log(path)
})
})
})
}
const processPaths = paths => {
paths.forEach(path => {
if (!fs.lstatSync(path).isDirectory()) {
format(path)
} else {
recursive(path, DEFAULT_IGNORE_LIST, (err, files) => {
if (err) throw err
files.forEach(format)
})
}
})
}
const cli = meow(
`
Usage
$ prettier-standard-formatter [<file|glob> ...]
Examples
$ prettier-standard-formatter
$ prettier-standard-formatter index.js
$ prettier-standard-formatter foo.js bar.js
$ prettier-standard-formatter index.js src/**/*.js
`
)
if (!cli.input.length) {
cli.showHelp(1)
}
globby(cli.input).then(processPaths)
}