Skip to content

Commit d07e9f7

Browse files
committed
Switch from standard to eslint + prettier
1 parent d0f8069 commit d07e9f7

26 files changed

+387
-402
lines changed

.eslintrc.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
env:
2+
node: true
3+
extends: problems
4+
plugins:
5+
- prettier
6+
rules:
7+
prettier/prettier:
8+
- error
9+
- { semi: false, singleQuote: true }
10+
no-console: off

index.js

Lines changed: 118 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ const version = () => {
4747

4848
const argv = require('yargs')
4949
.usage(
50-
`${chalk.bold.red(logo)}
50+
`${chalk.bold.red(logo)}
5151
Usage:
5252
5353
$0 [input.css] [OPTIONS] [--output|-o output.css] [--watch]`
54-
)
54+
)
5555
.option('o', {
5656
alias: 'output',
5757
desc: 'Output file',
@@ -93,14 +93,15 @@ Usage:
9393
type: 'boolean'
9494
})
9595
.option('poll', {
96-
desc: 'Use polling for file watching. Can optionally pass polling interval; default 100 ms'
96+
desc:
97+
'Use polling for file watching. Can optionally pass polling interval; default 100 ms'
9798
})
9899
.option('x', {
99100
alias: 'ext',
100101
desc: 'Override the output file extension',
101102
type: 'string',
102-
coerce (ext) {
103-
if (ext.indexOf('.') !== 0) return '.' + ext
103+
coerce(ext) {
104+
if (ext.indexOf('.') !== 0) return `.${ext}`
104105
return ext
105106
}
106107
})
@@ -111,7 +112,8 @@ Usage:
111112
})
112113
.option('b', {
113114
alias: 'base',
114-
desc: 'Mirror the directory structure relative to this path in the output directory, this only works together with --dir',
115+
desc:
116+
'Mirror the directory structure relative to this path in the output directory, this only works together with --dir',
115117
type: 'string'
116118
})
117119
.option('c', {
@@ -120,25 +122,29 @@ Usage:
120122
type: 'string'
121123
})
122124
.alias('m', 'map')
123-
.describe('m', 'Create an external sourcemap')
124-
.describe('no-map', 'Disable the default inline sourcemaps')
125-
.version(version).alias('v', 'version')
126-
.help('h').alias('h', 'help')
125+
.describe('m', 'Create an external sourcemap')
126+
.describe('no-map', 'Disable the default inline sourcemaps')
127+
.version(version)
128+
.alias('v', 'version')
129+
.help('h')
130+
.alias('h', 'help')
127131
.example('$0 input.css -o output.css', 'Basic usage')
128-
.example('cat input.css | $0 -u autoprefixer > output.css', 'Piping input & output')
132+
.example(
133+
'cat input.css | $0 -u autoprefixer > output.css',
134+
'Piping input & output'
135+
)
129136
.epilog(
130-
`If no input files are passed, it reads from stdin. If neither -o, --dir, or --replace is passed, it writes to stdout.
137+
`If no input files are passed, it reads from stdin. If neither -o, --dir, or --replace is passed, it writes to stdout.
131138
132139
If there are multiple input files, the --dir or --replace option must be passed.
133140
134141
For more details, please see https://github.com/postcss/postcss-cli`
135-
)
136-
.argv
142+
).argv
137143

138-
let dir = argv.dir
144+
const dir = argv.dir
139145

140146
let input = argv._
141-
let output = argv.output
147+
const output = argv.output
142148

143149
if (argv.map) argv.map = { inline: false }
144150

@@ -152,13 +158,13 @@ let config = {
152158
stringifier: argv.stringifier ? require(argv.stringifier) : undefined
153159
},
154160
plugins: argv.use
155-
? argv.use.map((plugin) => {
156-
try {
157-
return require(plugin)()
158-
} catch (e) {
159-
error(`Plugin Error: Cannot find module '${plugin}'`)
160-
}
161-
})
161+
? argv.use.map(plugin => {
162+
try {
163+
return require(plugin)()
164+
} catch (e) {
165+
return error(`Plugin Error: Cannot find module '${plugin}'`)
166+
}
167+
})
162168
: []
163169
}
164170

@@ -169,21 +175,27 @@ Promise.resolve()
169175
.then(() => {
170176
if (input && input.length) return globber(input)
171177

172-
if (argv.replace || argv.dir) error('Input Error: Cannot use --dir or --replace when reading from stdin')
178+
if (argv.replace || argv.dir) {
179+
error(
180+
'Input Error: Cannot use --dir or --replace when reading from stdin'
181+
)
182+
}
173183

174184
if (argv.watch) {
175185
error('Input Error: Cannot run in watch mode when reading from stdin')
176186
}
177187

178188
return ['stdin']
179189
})
180-
.then((i) => {
190+
.then(i => {
181191
if (!i || !i.length) {
182192
error('Input Error: You must pass a valid list of files to parse')
183193
}
184194

185195
if (i.length > 1 && !argv.dir && !argv.replace) {
186-
error('Input Error: Must use --dir or --replace with multiple input files')
196+
error(
197+
'Input Error: Must use --dir or --replace with multiple input files'
198+
)
187199
}
188200

189201
if (i[0] !== 'stdin') i = i.map(i => path.resolve(i))
@@ -192,21 +204,20 @@ Promise.resolve()
192204

193205
return files(input)
194206
})
195-
.then((results) => {
207+
.then(results => {
196208
if (argv.watch) {
197-
const watcher = chokidar.watch(
198-
input.concat(dependencies(results)),
199-
{
200-
usePolling: argv.poll,
201-
interval: argv.poll && typeof argv.poll === 'number' ? argv.poll : 100
202-
}
203-
)
209+
const watcher = chokidar.watch(input.concat(dependencies(results)), {
210+
usePolling: argv.poll,
211+
interval: argv.poll && typeof argv.poll === 'number' ? argv.poll : 100
212+
})
204213

205214
if (config.file) watcher.add(config.file)
206215

207216
watcher
208-
.on('ready', (file) => console.warn(chalk.bold.cyan('Waiting for file changes...')))
209-
.on('change', (file) => {
217+
.on('ready', () => {
218+
console.warn(chalk.bold.cyan('Waiting for file changes...'))
219+
})
220+
.on('change', file => {
210221
let recompile = []
211222

212223
if (~input.indexOf(file)) recompile.push(file)
@@ -218,47 +229,51 @@ Promise.resolve()
218229
if (!recompile.length) recompile = input
219230

220231
return files(recompile)
221-
.then((results) => watcher.add(dependencies(results)))
222-
.then(() => console.warn(chalk.bold.cyan('Waiting for file changes...')))
232+
.then(results => watcher.add(dependencies(results)))
233+
.then(() => {
234+
console.warn(chalk.bold.cyan('Waiting for file changes...'))
235+
})
223236
.catch(error)
224237
})
225238
}
226239
})
227240
.catch(error)
228241

229-
function rc (ctx, path) {
242+
function rc(ctx, path) {
230243
if (argv.use) return Promise.resolve()
231244

232245
return postcssrc(ctx, path)
233-
.then((rc) => {
246+
.then(rc => {
234247
if (rc.options.from || rc.options.to) {
235-
error('Config Error: Can not set from or to options in config file, use CLI arguments instead')
248+
error(
249+
'Config Error: Can not set from or to options in config file, use CLI arguments instead'
250+
)
236251
}
237252
config = rc
238253
})
239-
.catch((err) => {
254+
.catch(err => {
240255
if (err.message.indexOf('No PostCSS Config found') === -1) throw err
241256
})
242257
}
243258

244-
function files (files) {
245-
if (typeof files === 'string') files = [ files ]
259+
function files(files) {
260+
if (typeof files === 'string') files = [files]
246261

247-
return Promise.all(files.map((file) => {
248-
if (file === 'stdin') {
249-
return stdin()
250-
.then((content) => {
262+
return Promise.all(
263+
files.map(file => {
264+
if (file === 'stdin') {
265+
return stdin().then(content => {
251266
if (!content) return error('Input Error: Did not receive any STDIN')
252267
return css(content, 'stdin')
253268
})
254-
}
269+
}
255270

256-
return read(file)
257-
.then((content) => css(content, file))
258-
}))
271+
return read(file).then(content => css(content, file))
272+
})
273+
)
259274
}
260275

261-
function css (css, file) {
276+
function css(css, file) {
262277
const ctx = { options: config.options }
263278

264279
if (file !== 'stdin') {
@@ -271,7 +286,8 @@ function css (css, file) {
271286
if (!argv.config) argv.config = path.dirname(file)
272287
}
273288

274-
const relativePath = file !== 'stdin' ? path.relative(path.resolve(), file) : file
289+
const relativePath =
290+
file !== 'stdin' ? path.relative(path.resolve(), file) : file
275291

276292
if (!argv.config) argv.config = process.cwd()
277293

@@ -282,94 +298,95 @@ function css (css, file) {
282298

283299
return rc(ctx, argv.config)
284300
.then(() => {
285-
let options = config.options
301+
const options = config.options
286302

287303
if (file === 'stdin' && output) file = output
288304

289305
// TODO: Unit test this
290306
options.from = file === 'stdin' ? path.join(process.cwd(), 'stdin') : file
291307

292308
if (output || dir || argv.replace) {
293-
options.to = output || (argv.replace ? file : path.join(dir, argv.base ? file.replace(path.resolve(argv.base), '') : path.basename(file)))
309+
const base = argv.base
310+
? file.replace(path.resolve(argv.base), '')
311+
: path.basename(file)
312+
options.to = output || (argv.replace ? file : path.join(dir, base))
294313

295314
if (argv.ext) {
296-
options.to = options.to
297-
.replace(path.extname(options.to), argv.ext)
315+
options.to = options.to.replace(path.extname(options.to), argv.ext)
298316
}
299317

300318
options.to = path.resolve(options.to)
301319
}
302320

303321
if (!options.to && config.options.map && !config.options.map.inline) {
304322
spinner.fail()
305-
error('Output Error: Cannot output external sourcemaps when writing to STDOUT')
323+
error(
324+
'Output Error: Cannot output external sourcemaps when writing to STDOUT'
325+
)
306326
}
307327

308-
return postcss(config.plugins)
309-
.process(css, options)
310-
.then((result) => {
311-
const tasks = []
312-
313-
if (options.to) {
314-
tasks.push(fs.outputFile(options.to, result.css))
315-
316-
if (result.map) {
317-
tasks.push(
318-
fs.outputFile(
319-
options.to
320-
.replace(
321-
path.extname(options.to),
322-
path.extname(options.to) + '.map'
323-
),
324-
result.map
325-
)
328+
return postcss(config.plugins).process(css, options).then(result => {
329+
const tasks = []
330+
331+
if (options.to) {
332+
tasks.push(fs.outputFile(options.to, result.css))
333+
334+
if (result.map) {
335+
tasks.push(
336+
fs.outputFile(
337+
options.to.replace(
338+
path.extname(options.to),
339+
`${path.extname(options.to)}.map`
340+
),
341+
result.map
326342
)
327-
}
328-
} else {
329-
spinner.text = chalk.bold.green(
330-
`Finished ${relativePath} (${prettyHrtime(process.hrtime(time))})`
331343
)
332-
spinner.succeed()
333-
return process.stdout.write(result.css, 'utf8')
334344
}
345+
} else {
346+
spinner.text = chalk.bold.green(
347+
`Finished ${relativePath} (${prettyHrtime(process.hrtime(time))})`
348+
)
349+
spinner.succeed()
350+
return process.stdout.write(result.css, 'utf8')
351+
}
335352

336-
return Promise.all(tasks)
337-
.then(() => {
338-
spinner.text = chalk.bold.green(
339-
`Finished ${relativePath} (${prettyHrtime(process.hrtime(time))})`
340-
)
341-
if (result.warnings().length) {
342-
spinner.fail()
343-
console.warn(reporter(result))
344-
} else spinner.succeed()
353+
return Promise.all(tasks).then(() => {
354+
spinner.text = chalk.bold.green(
355+
`Finished ${relativePath} (${prettyHrtime(process.hrtime(time))})`
356+
)
357+
if (result.warnings().length) {
358+
spinner.fail()
359+
console.warn(reporter(result))
360+
} else spinner.succeed()
345361

346-
return result
347-
})
362+
return result
348363
})
349-
}).catch((err) => {
364+
})
365+
})
366+
.catch(err => {
350367
spinner.fail()
351368
throw err
352369
})
353370
}
354371

355-
function dependencies (results) {
356-
if (!Array.isArray(results)) results = [ results ]
372+
function dependencies(results) {
373+
if (!Array.isArray(results)) results = [results]
357374

358375
const messages = []
359376

360-
results.forEach((result) => {
377+
results.forEach(result => {
361378
if (result.messages <= 0) return
362379

363380
result.messages
364-
.filter((msg) => msg.type === 'dependency' ? msg : '')
381+
.filter(msg => (msg.type === 'dependency' ? msg : ''))
365382
.map(depGraph.add)
366-
.forEach((dependency) => messages.push(dependency.file))
383+
.forEach(dependency => messages.push(dependency.file))
367384
})
368385

369386
return messages
370387
}
371388

372-
function error (err) {
389+
function error(err) {
373390
if (typeof err === 'string') {
374391
spinner.fail(chalk.bold.red(err))
375392
} else if (err.name === 'CssSyntaxError') {

0 commit comments

Comments
 (0)