/
cli.js
194 lines (171 loc) · 4.78 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env node
var fs = require('fs')
var path = require('path')
var stdin = require('stdin')
var pkg = require('../package.json')
var stylefmt = require('../')
var minimist = require('minimist')
var argv = minimist(process.argv.slice(2), {
boolean: [
'help',
'version'
],
alias: {
b: 'config-basedir',
c: 'config',
d: 'diff',
h: 'help',
i: 'ignore-path',
r: 'recursive',
v: 'version',
}
})
var postcss = require('postcss')
var scss = require('postcss-scss')
if (argv.v) {
console.log(pkg.version)
process.exit()
}
if (argv.h) {
console.log('Usage: stylefmt [options] input-name [output-name]')
console.log('')
console.log('Options:')
console.log('')
console.log(' -b, --config-basedir Path to the directory that relative paths defining "extends"')
console.log(' -c, --config Path to a specific configuration file (JSON, YAML, or CommonJS)')
console.log(' -d, --diff Output diff against original file')
console.log(' -r, --recursive Format list of space seperated files(globs) in place')
console.log(' -v, --version Output the version number')
console.log(' -h, --help Output usage information')
console.log(' -i, --ignore-path Path to a file containing patterns that describe files to ignore.')
console.log(' --stdin-filename A filename to assign stdin input.')
process.exit()
}
var options = {}
if (argv.c) {
options.configFile = argv.c
}
if (argv.b) {
options.configBasedir = (path.isAbsolute(argv.b))
? argv.b
: path.resolve(process.cwd(), argv.b)
}
if (argv.i) {
options.ignorePath = argv.i
}
if (argv.r) {
var globby = require('globby')
globby([path.join(argv.r)].concat(argv._)).then(processMultipleFiles)
} else if (argv._[0]) {
var input = argv._[0]
var fullPath = path.resolve(process.cwd(), input)
var output = argv._[1] || argv._[0]
var css = fs.readFileSync(fullPath, 'utf-8')
postcss([stylefmt(options)])
.process(css, {
from: fullPath,
syntax: scss
})
.then(function (result) {
var formatted = result.css
if (argv.d) {
console.log(handleDiff(input, css, formatted))
} else {
if (css !== formatted) {
fs.writeFile(output, formatted, function (err) {
if (err) {
throw err
}
})
}
}
})
} else {
stdin(function (css) {
options.codeFilename = argv['stdin-filename']
postcss([stylefmt(options)])
.process(css, {
from: options.codeFilename,
syntax: scss
})
.then(function (result) {
process.stdout.write(result.css)
})
})
}
function processMultipleFiles (files) {
files = files.filter(isCss).sort()
if(!files.length){
console.error("Files glob patterns specified did not match any css files.")
return
}
Promise.all(files.map(function (file) {
var fullPath = path.resolve(process.cwd(), file)
var css = fs.readFileSync(fullPath, 'utf-8')
return postcss([stylefmt(options)])
.process(css, {
from: fullPath,
syntax: scss
})
.then(function (result) {
var formatted = result.css
if (argv.d) {
return handleDiff(file, css, formatted)
} else if (css !== formatted) {
fs.writeFile(fullPath, formatted, function (err) {
if (err) {
throw err
}
})
return file
}
})
})).then(function (messages) {
if (argv.d) {
console.log(messages.join('\n\n'))
} else {
messages = messages.filter(function (file){
return file
})
if(messages.length){
messages = messages.join(', ') + '\n\n' + messages.length
} else {
messages = 'No'
}
console.log(messages + ' files are formatted.')
}
})
}
function isCss (filePath) {
return /^\.css|\.scss|\.wxss$/i.test(path.extname(filePath))
}
function handleDiff (file, original, formatted) {
var diff
var chalk = require('chalk')
if (original === formatted) {
diff = 'There is no difference with the original file.'
}
if (chalk.supportsColor) {
file = chalk.blue(file)
if(diff) {
diff = chalk.gray(diff)
} else {
var JsDiff = require('diff')
diff = JsDiff.createPatch(file, original, formatted)
diff = diff.split('\n').splice(4).map(function (line) {
if (line[0] === '+') {
line = chalk.green(line)
} else if (line[0] === '-') {
line = chalk.red(line)
} else if (line.match(/^@@\s+.+?\s+@@/) || '\\ No newline at end of file' === line) {
line = ''
}
return chalk.gray(line)
})
diff = diff.join('\n').trim()
}
} else if (!diff) {
diff = formatted
}
return file + '\n' + diff
}