Skip to content

Commit

Permalink
Use cosmiconfig for lint-staged config. Closes #64
Browse files Browse the repository at this point in the history
  • Loading branch information
okonet committed Oct 7, 2016
1 parent 385c37d commit 2aff2e4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/generateTasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const minimatch = require('minimatch')

module.exports = function generateTasks (config, filePaths) {
module.exports = function generateTasks(config, filePaths) {
const linters = config.linters !== undefined ? config.linters : config
return Object.keys(linters)
.map((pattern) => {
Expand Down
69 changes: 44 additions & 25 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,57 @@

process.env.FORCE_COLOR = true // Force colors for packages that depend on https://www.npmjs.com/package/supports-color

const path = require('path')
const sgf = require('staged-git-files')
const appRoot = require('app-root-path')
const Listr = require('listr')
const path = require('path')
const cosmiconfig = require('cosmiconfig')

const config = require(appRoot.resolve('package.json')) // eslint-disable-line
const packageJson = require(appRoot.resolve('package.json')) // eslint-disable-line
const runScript = require('./runScript')
const resolvePaths = require('./resolvePaths')
const generateTasks = require('./generateTasks')

// If git root is defined -> Set git root as sgf's cwd
if (config['lint-staged']['git-root'] !== undefined) {
sgf.cwd = path.resolve(config['lint-staged']['git-root'])
}

sgf('ACM', (err, files) => {
if (err) {
console.error(err)
}

const tasks = generateTasks(config['lint-staged'], resolvePaths(files))
.map(task => ({
title: `Running tasks for ${ task.pattern }`,
task: () => (new Listr(runScript(task.commands, task.fileList, config)))
}))


if (tasks.length) {
new Listr(tasks).run().catch((error) => {
console.error(error)
process.exit(1)
})
}
cosmiconfig(packageJson.name, {
rc: 'lintstaged'
})
.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

// If git-root is defined -> set git root as sgf's cwd
if ('git-root' in config) {
sgf.cwd = path.resolve(config['git-root'])
}

sgf('ACM', (err, files) => {

This comment has been minimized.

Copy link
@liweinan0423

liweinan0423 Apr 28, 2017

If I am correct ACM means added, created and modified. Is it possible to make this configurable? Because sometimes we only want to commit some but not all of the files that are modified in one commit.

This comment has been minimized.

Copy link
@okonet

okonet Apr 28, 2017

Author Collaborator

I don't understand what you mean by:

Because sometimes we only want to commit some but not all of the files that are modified in one commit.

If you want to commit some files, you should only add those files to the commit index. lint-staged will only execute tasks on these files.

How would you like to modify this? In order to do what? There is #150 where you can add your input. Here is a bad place to discuss.

This comment has been minimized.

Copy link
@liweinan0423

liweinan0423 via email Apr 28, 2017

if (err) {
console.error(err)
}

const tasks = generateTasks(config, resolvePaths(files))
.map(task => ({
title: `Running tasks for ${ task.pattern }`,
task: () => (new Listr(runScript(task.commands, task.fileList, config)))
}))


if (tasks.length) {
new Listr(tasks).run().catch((error) => {
console.error(error)
process.exit(1)
})
}
})
})
.catch((parsingError) => {
// do something constructive
console.error(`Could not parse lint-staged config.
Make sure you have created it. See ${ packageJson.homepage }.
${ parsingError }
`)
process.exit(1)
})

0 comments on commit 2aff2e4

Please sign in to comment.