Skip to content

Commit a28463d

Browse files
luftywiranda13okonet
authored andcommitted
style: Add prettier, fix eslint configuration and prettify all files (lint-staged#231)
* chore(eslint): list `coverage` dir in `.eslintignore` The files in this directory shouldn't be checked by ESLint * chore(eslint): fix inconcistencies The problem are mainly caused by `tabWidth`: * The default is '2', not overridden by eslint-config-okonet * In this project's eslintrc we override it in `indent` rule which of course will fight with `prettier` default. * Also deleted some unnecessary overrides in `prettier` field because they're already the default out of the box * chore(eslint): rename `.eslintrc` to be `.eslintrc.json` This will make IDE applies proper syntax highlighting * style: run `lint:fix` script * chore: run `lint` script before `test` * Revert "chore(eslint): fix inconcistencies" This reverts commit 1fb420c. * chore(eslint): remove `indent` rule * style: run `lint:fix` script * chore(editorconfig): update `indent_size` * ci: run installation with npm v3 and above * chore: install `prettier` pin to v1.5.3
1 parent 18eadf2 commit a28463d

20 files changed

+623
-694
lines changed

.editorconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ root = true
33

44
[*]
55
indent_style = space
6-
indent_size = 4
6+
indent_size = 2
77
end_of_line = lf
88
charset = utf-8
99
trim_trailing_whitespace = true

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
coverage

.eslintrc .eslintrc.json

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
"es6": true
1010
},
1111
"rules": {
12-
"indent": [2, 4],
1312
"strict": 0,
1413
"import/no-extraneous-dependencies": [2, {
1514
"devDependencies": ["**/*.spec.js"]

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ node_js:
66
- '8'
77
- '6'
88
- '4'
9+
before_install: if [[ `npm -v` < 3* ]]; then npm i -g npm@^3; fi
910
before_script:
1011
- npm prune
1112
after_success:

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"lint": "eslint .",
1313
"lint:fix": "npm run lint -- --fix",
1414
"pre-commit": "remove-lockfiles && node index.js",
15+
"pretest": "npm run lint",
1516
"test": "jest --coverage",
1617
"deps": "npm-check -s",
1718
"deps:update": "npm-check -u"
@@ -76,6 +77,7 @@
7677
"jsonlint-cli": "^1.0.1",
7778
"npm-check": "^5.2.2",
7879
"pre-commit": "^1.1.3",
80+
"prettier": "1.5.3",
7981
"remove-lockfiles": "^0.2.1"
8082
},
8183
"config": {

src/calcChunkSize.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@
2626
* @returns {number} The chunk size
2727
*/
2828
module.exports = function calcChunkSize(paths, idealChunkSize) {
29-
/* What is the longest file path? */
30-
const maxPathLen = paths.reduce(
31-
(maxLen, filePath) => Math.max(maxLen, filePath.length),
32-
20 // safe initial value
33-
)
29+
/* What is the longest file path? */
30+
const maxPathLen = paths.reduce(
31+
(maxLen, filePath) => Math.max(maxLen, filePath.length),
32+
20 // safe initial value
33+
)
3434

35-
/* In the worst case scenario, */
36-
/* how many files can we process in a single command? */
37-
/* For windows systems, command length is limited to 8192 */
38-
const maxAllowedChunkSize = Math.floor(8000 / maxPathLen)
35+
/* In the worst case scenario, */
36+
/* how many files can we process in a single command? */
37+
/* For windows systems, command length is limited to 8192 */
38+
const maxAllowedChunkSize = Math.floor(8000 / maxPathLen)
3939

40-
/* Configured chunk size / default - idealChunkSize */
41-
return Math.min(paths.length, maxAllowedChunkSize, idealChunkSize)
40+
/* Configured chunk size / default - idealChunkSize */
41+
return Math.min(paths.length, maxAllowedChunkSize, idealChunkSize)
4242
}

src/findBin.js

+18-22
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,18 @@
33
const npmWhich = require('npm-which')(process.cwd())
44

55
module.exports = function findBin(cmd, packageJson, options) {
6-
/*
6+
/*
77
* If package.json has script with cmd defined
88
* we want it to be executed first
99
*/
10-
if (packageJson.scripts && packageJson.scripts[cmd] !== undefined) {
11-
// Support for scripts from package.json
12-
const args = [
13-
'run',
14-
options && options.verbose ? undefined : '--silent',
15-
cmd
16-
].filter(Boolean)
10+
if (packageJson.scripts && packageJson.scripts[cmd] !== undefined) {
11+
// Support for scripts from package.json
12+
const args = ['run', options && options.verbose ? undefined : '--silent', cmd].filter(Boolean)
1713

18-
return { bin: 'npm', args }
19-
}
14+
return { bin: 'npm', args }
15+
}
2016

21-
/*
17+
/*
2218
* If cmd wasn't found in package.json scripts
2319
* we'll try to locate the binary in node_modules/.bin
2420
* and if this fails in $PATH.
@@ -36,17 +32,17 @@ module.exports = function findBin(cmd, packageJson, options) {
3632
* }
3733
*/
3834

39-
const parts = cmd.split(' ')
40-
let bin = parts[0]
41-
const args = parts.splice(1)
35+
const parts = cmd.split(' ')
36+
let bin = parts[0]
37+
const args = parts.splice(1)
4238

43-
try {
44-
/* npm-which tries to resolve the bin in local node_modules/.bin */
45-
/* and if this fails it look in $PATH */
46-
bin = npmWhich.sync(bin)
47-
} catch (err) {
48-
throw new Error(`${ bin } could not be found. Try \`npm install ${ bin }\`.`)
49-
}
39+
try {
40+
/* npm-which tries to resolve the bin in local node_modules/.bin */
41+
/* and if this fails it look in $PATH */
42+
bin = npmWhich.sync(bin)
43+
} catch (err) {
44+
throw new Error(`${bin} could not be found. Try \`npm install ${bin}\`.`)
45+
}
5046

51-
return { bin, args }
47+
return { bin, args }
5248
}

src/generateTasks.js

+22-17
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,26 @@ const minimatch = require('minimatch')
55
const readConfigOption = require('./readConfigOption')
66

77
module.exports = function generateTasks(config, files) {
8-
const linters = config.linters !== undefined ? config.linters : config
9-
const resolve = file => files[file]
10-
return Object.keys(linters)
11-
.map((pattern) => {
12-
const commands = linters[pattern]
13-
const globOptions = readConfigOption(config, 'globOptions', {})
14-
const filter = minimatch.filter(pattern, Object.assign({
15-
matchBase: true,
16-
dot: true
17-
}, globOptions))
18-
const fileList = Object.keys(files).filter(filter).map(resolve)
19-
return {
20-
pattern,
21-
commands,
22-
fileList
23-
}
24-
})
8+
const linters = config.linters !== undefined ? config.linters : config
9+
const resolve = file => files[file]
10+
return Object.keys(linters).map(pattern => {
11+
const commands = linters[pattern]
12+
const globOptions = readConfigOption(config, 'globOptions', {})
13+
const filter = minimatch.filter(
14+
pattern,
15+
Object.assign(
16+
{
17+
matchBase: true,
18+
dot: true
19+
},
20+
globOptions
21+
)
22+
)
23+
const fileList = Object.keys(files).filter(filter).map(resolve)
24+
return {
25+
pattern,
26+
commands,
27+
fileList
28+
}
29+
})
2530
}

src/index.js

+72-75
Original file line numberDiff line numberDiff line change
@@ -9,99 +9,96 @@ const appRoot = require('app-root-path')
99
const Listr = require('listr')
1010
const cosmiconfig = require('cosmiconfig')
1111

12-
const packageJson = require(appRoot.resolve('package.json')) // eslint-disable-line
12+
const packageJson = require(appRoot.resolve('package.json')); // eslint-disable-line
1313
const runScript = require('./runScript')
1414
const generateTasks = require('./generateTasks')
1515
const readConfigOption = require('./readConfigOption')
1616

1717
// Force colors for packages that depend on https://www.npmjs.com/package/supports-color
1818
// but do this only in TTY mode
1919
if (process.stdout.isTTY) {
20-
process.env.FORCE_COLOR = true
20+
process.env.FORCE_COLOR = true
2121
}
2222

2323
cosmiconfig('lint-staged', {
24-
rc: '.lintstagedrc',
25-
rcExtensions: true
24+
rc: '.lintstagedrc',
25+
rcExtensions: true
2626
})
27-
.then((result) => {
28-
// result.config is the parsed configuration object
29-
// result.filepath is the path to the config file that was found
30-
const config = result.config
27+
.then(result => {
28+
// result.config is the parsed configuration object
29+
// result.filepath is the path to the config file that was found
30+
const config = result.config
3131

32-
const verbose = config.verbose
33-
// Output config in verbose mode
34-
if (verbose) console.log(config)
35-
const concurrent = readConfigOption(config, 'concurrent', true)
36-
const renderer = verbose ? 'verbose' : 'update'
37-
const gitDir = config.gitDir ? path.resolve(config.gitDir) : process.cwd()
38-
sgf.cwd = gitDir
32+
const verbose = config.verbose
33+
// Output config in verbose mode
34+
if (verbose) console.log(config)
35+
const concurrent = readConfigOption(config, 'concurrent', true)
36+
const renderer = verbose ? 'verbose' : 'update'
37+
const gitDir = config.gitDir ? path.resolve(config.gitDir) : process.cwd()
38+
sgf.cwd = gitDir
3939

40-
sgf('ACM', (err, files) => {
41-
if (err) {
42-
console.error(err)
43-
process.exit(1)
44-
}
45-
46-
const resolvedFiles = {}
47-
files.forEach((file) => {
48-
const absolute = path.resolve(gitDir, file.filename)
49-
const relative = path.relative(gitDir, absolute)
50-
resolvedFiles[relative] = absolute
51-
})
52-
53-
const tasks = generateTasks(config, resolvedFiles)
54-
.map(task => ({
55-
title: `Running tasks for ${ task.pattern }`,
56-
task: () => (
57-
new Listr(
58-
runScript(
59-
task.commands,
60-
task.fileList,
61-
packageJson,
62-
{ gitDir, verbose, config }
63-
), {
64-
// In sub-tasks we don't want to run concurrently
65-
// and we want to abort on errors
66-
concurrent: false,
67-
exitOnError: true
68-
}
69-
)
70-
),
71-
skip: () => {
72-
if (task.fileList.length === 0) {
73-
return `No staged files match ${ task.pattern }`
74-
}
75-
return false
76-
}
77-
}))
40+
sgf('ACM', (err, files) => {
41+
if (err) {
42+
console.error(err)
43+
process.exit(1)
44+
}
7845

46+
const resolvedFiles = {}
47+
files.forEach(file => {
48+
const absolute = path.resolve(gitDir, file.filename)
49+
const relative = path.relative(gitDir, absolute)
50+
resolvedFiles[relative] = absolute
51+
})
7952

80-
if (tasks.length) {
81-
new Listr(tasks, {
82-
concurrent,
83-
renderer,
84-
exitOnError: !concurrent // Wait for all errors when running concurrently
85-
})
86-
.run()
87-
.catch((error) => {
88-
if (Array.isArray(error.errors)) {
89-
error.errors.forEach((lintError) => {
90-
console.error(lintError.message)
91-
})
92-
} else {
93-
console.log(error.message)
94-
}
95-
process.exit(1)
96-
})
53+
const tasks = generateTasks(config, resolvedFiles).map(task => ({
54+
title: `Running tasks for ${task.pattern}`,
55+
task: () =>
56+
new Listr(
57+
runScript(task.commands, task.fileList, packageJson, {
58+
gitDir,
59+
verbose,
60+
config
61+
}),
62+
{
63+
// In sub-tasks we don't want to run concurrently
64+
// and we want to abort on errors
65+
concurrent: false,
66+
exitOnError: true
9767
}
68+
),
69+
skip: () => {
70+
if (task.fileList.length === 0) {
71+
return `No staged files match ${task.pattern}`
72+
}
73+
return false
74+
}
75+
}))
76+
77+
if (tasks.length) {
78+
new Listr(tasks, {
79+
concurrent,
80+
renderer,
81+
exitOnError: !concurrent // Wait for all errors when running concurrently
9882
})
83+
.run()
84+
.catch(error => {
85+
if (Array.isArray(error.errors)) {
86+
error.errors.forEach(lintError => {
87+
console.error(lintError.message)
88+
})
89+
} else {
90+
console.log(error.message)
91+
}
92+
process.exit(1)
93+
})
94+
}
9995
})
100-
.catch((parsingError) => {
101-
console.error(`Could not parse lint-staged config.
96+
})
97+
.catch(parsingError => {
98+
console.error(`Could not parse lint-staged config.
10299
Make sure you have created it. See https://github.com/okonet/lint-staged#readme.
103100
104-
${ parsingError }
101+
${parsingError}
105102
`)
106-
process.exit(1)
107-
})
103+
process.exit(1)
104+
})

src/readConfigOption.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
* @returns {*}
1212
*/
1313
module.exports = function readConfigOption(config, key, defaultValue) {
14-
if (typeof config !== 'undefined' && typeof config[key] !== 'undefined') {
15-
return config[key]
16-
}
14+
if (typeof config !== 'undefined' && typeof config[key] !== 'undefined') {
15+
return config[key]
16+
}
1717

18-
return defaultValue
18+
return defaultValue
1919
}
20-

0 commit comments

Comments
 (0)