UNPKG

1.34 kBJavaScriptView Raw
1'use strict'
2
3const chalk = require('chalk')
4const dedent = require('dedent')
5const has = require('lodash/has')
6
7const warn = msg => {
8 console.warn(chalk.yellowBright.bold(msg))
9}
10
11/**
12 * Checks if the given command or binary name is present in the package.json scripts. This would be
13 * called if and when resolving a binary fails in `findBin`.
14 *
15 * @param {Object} pkg package.json
16 * @param {string} cmd
17 * @param {string} binName
18 * @param {Array<string>} args
19 * @throws {Error} If a script is found in the pkg for the given `cmd` or `binName`.
20 */
21module.exports = function checkPkgScripts(pkg, cmd, binName, args) {
22 if (pkg && pkg.scripts) {
23 const { scripts } = pkg
24 let scriptName
25 let script
26 if (has(scripts, cmd)) {
27 scriptName = cmd
28 script = scripts[cmd]
29 } else if (has(scripts, binName)) {
30 scriptName = binName
31 script = scripts[binName]
32 } else {
33 return
34 }
35
36 const argsStr = args && args.length ? args.join(' ') : ''
37 warn(dedent`
38 \`lint-staged\` no longer supports running scripts defined in package.json.
39
40 The same behavior can be achieved by changing the command to any of the following:
41 - \`npm run ${scriptName} -- ${argsStr}\`
42 - \`${script} ${argsStr}\`
43 `)
44 throw new Error(`Could not resolve binary for \`${cmd}\``)
45 }
46}