Skip to content

Commit

Permalink
refactor: warn about long arguments string only once
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj authored and okonet committed Jul 1, 2019
1 parent bcd52ac commit b71b9c8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 25 deletions.
3 changes: 2 additions & 1 deletion src/generateTasks.js
@@ -1,8 +1,9 @@
'use strict'

const path = require('path')
const micromatch = require('micromatch')
const pathIsInside = require('path-is-inside')
const path = require('path')

const { getConfig } = require('./getConfig')

const debug = require('debug')('lint-staged:gen-tasks')
Expand Down
21 changes: 0 additions & 21 deletions src/resolveTaskFn.js
Expand Up @@ -26,14 +26,6 @@ function execLinter(bin, args, execaOptions) {
return execa(bin, args, { ...execaOptions })
}

/**
* https://serverfault.com/questions/69430/what-is-the-maximum-length-of-a-command-line-in-mac-os-x
* https://support.microsoft.com/en-us/help/830473/command-prompt-cmd-exe-command-line-string-limitation
* https://unix.stackexchange.com/a/120652
*/
const MAX_ARG_LENGTH =
(process.platform === 'darwin' && 262144) || (process.platform === 'win32' && 8191) || 131072

const successMsg = linter => `${symbols.success} ${linter} passed!`

/**
Expand Down Expand Up @@ -104,19 +96,6 @@ module.exports = function resolveTaskFn(options) {
const tasks = linters.map(command => {
const { bin, args } = findBin(command)

const argLength = args.join(' ').length
if (argLength > MAX_ARG_LENGTH) {
console.warn(dedent`${symbols.warning} ${chalk.yellow(
`${chalk.bold(
command
)} received an argument string of ${argLength} characters, and might not run correctly on your platform.
It is recommended to use functions as linters and split your command based on the number of staged files. For more info, please read:
https://github.com/okonet/lint-staged#using-js-functions-to-customize-linter-commands
`
)}
`)
}

// If `linter` is a function, args already include `pathsToLint`.
const argsWithPaths = fnLinter ? args : args.concat(pathsToLint)

Expand Down
30 changes: 27 additions & 3 deletions src/runAll.js
@@ -1,15 +1,27 @@
'use strict'

const generateTasks = require('./generateTasks')
const git = require('./gitWorkflow')
const getStagedFiles = require('./getStagedFiles')
const chalk = require('chalk')
const dedent = require('dedent')
const Listr = require('listr')
const has = require('lodash/has')
const symbols = require('log-symbols')

const generateTasks = require('./generateTasks')
const getStagedFiles = require('./getStagedFiles')
const git = require('./gitWorkflow')
const makeCmdTasks = require('./makeCmdTasks')
const resolveGitDir = require('./resolveGitDir')

const debug = require('debug')('lint-staged:run')

/**
* https://serverfault.com/questions/69430/what-is-the-maximum-length-of-a-command-line-in-mac-os-x
* https://support.microsoft.com/en-us/help/830473/command-prompt-cmd-exe-command-line-string-limitation
* https://unix.stackexchange.com/a/120652
*/
const MAX_ARG_LENGTH =
(process.platform === 'darwin' && 262144) || (process.platform === 'win32' && 8191) || 131072

/**
* Executes all tasks and either resolves or rejects the promise
* @param config {Object}
Expand Down Expand Up @@ -39,6 +51,18 @@ module.exports = async function runAll(config) {

debug('Loaded list of staged files in git:\n%O', files)

const argLength = files.join(' ').length
if (argLength > MAX_ARG_LENGTH) {
console.warn(
dedent`${symbols.warning} ${chalk.yellow(
`lint-staged generated an argument string of ${argLength} characters, and commands might not run correctly on your platform.
It is recommended to use functions as linters and split your command based on the number of staged files. For more info, please read:
https://github.com/okonet/lint-staged#using-js-functions-to-customize-linter-commands
`
)}`
)
}

const tasks = (await generateTasks(config, gitDir, files)).map(task => ({
title: `Running tasks for ${task.pattern}`,
task: async () =>
Expand Down

0 comments on commit b71b9c8

Please sign in to comment.