-
-
Notifications
You must be signed in to change notification settings - Fork 412
/
index.js
71 lines (59 loc) · 2.14 KB
/
index.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
/* global process */
/* eslint no-console: 0 */
'use strict'
const path = require('path')
const sgf = require('staged-git-files')
const appRoot = require('app-root-path')
const Listr = require('listr')
const cosmiconfig = require('cosmiconfig')
const packageJson = require(appRoot.resolve('package.json')) // eslint-disable-line
const runScript = require('./runScript')
const resolvePaths = require('./resolvePaths')
const generateTasks = require('./generateTasks')
// Force colors for packages that depend on https://www.npmjs.com/package/supports-color
// but do this only in TTY mode
if (process.stdout.isTTY) {
process.env.FORCE_COLOR = true
}
cosmiconfig('lint-staged', {
rc: '.lintstagedrc',
rcExtensions: true
})
.then((result) => {
// result.config is the parsed configuration object
// result.filepath is the path to the config file that was found
const config = result.config
const concurrent = config.concurrent || true
const gitDir = config.gitDir ? path.resolve(config.gitDir) : process.cwd()
// If gitDir is defined -> set git root as sgf's cwd
if (gitDir !== process.cwd()) {
sgf.cwd = gitDir
}
sgf('ACM', (err, files) => {
if (err) {
console.error(err)
}
const tasks = generateTasks(config, resolvePaths(files, gitDir))
.map(task => ({
title: `Running tasks for ${ task.pattern }`,
task: () => (
new Listr(
runScript(task.commands, task.fileList, packageJson, gitDir)
)
)
}))
if (tasks.length) {
new Listr(tasks, { concurrent }).run().catch((error) => {
console.error(error.message)
process.exit(1)
})
}
})
})
.catch((parsingError) => {
console.error(`Could not parse lint-staged config.
Make sure you have created it. See https://github.com/okonet/lint-staged#readme.
${ parsingError }
`)
process.exit(1)
})