-
-
Notifications
You must be signed in to change notification settings - Fork 412
/
index.js
98 lines (87 loc) Β· 3.38 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
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
/* 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 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 verbose = config.verbose
// Output config in verbose mode
if (verbose) console.log(config)
const concurrent = typeof config.concurrent !== 'undefined' ? config.concurrent : true
const renderer = verbose ? 'verbose' : 'update'
const gitDir = config.gitDir ? path.resolve(config.gitDir) : process.cwd()
sgf.cwd = gitDir
sgf('ACM', (err, files) => {
if (err) {
console.error(err)
}
const resolvedFiles = {}
files.forEach((file) => {
const absolute = path.resolve(gitDir, file.filename)
const relative = path.relative(gitDir, absolute)
resolvedFiles[relative] = absolute
})
const tasks = generateTasks(config, resolvedFiles)
.map(task => ({
title: `Running tasks for ${ task.pattern }`,
task: () => (
new Listr(
runScript(
task.commands,
task.fileList,
packageJson,
{ gitDir, verbose }
), {
// In sub-tasks we don't want to run concurrently
// and we want to abort on errors
concurrent: false,
exitOnError: true
}
)
)
}))
if (tasks.length) {
new Listr(tasks, {
concurrent,
renderer,
exitOnError: !concurrent // Wait for all errors when running concurrently
})
.run()
.catch((error) => {
if (Array.isArray(error.errors)) {
error.errors.forEach((lintError) => {
console.error(lintError.message)
})
} else {
console.log(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)
})