diff --git a/src/scripts/precommit.js b/src/scripts/precommit.js new file mode 100644 index 0000000..eddebe0 --- /dev/null +++ b/src/scripts/precommit.js @@ -0,0 +1,42 @@ +const path = require('path'); +const spawn = require('cross-spawn'); +const { + isOptedIn, + resolveBin, + hasFile, + hasPkgProp, + getPackageManagerBin, +} = require('../utils'); + +const here = p => path.join(__dirname, p); +const hereRelative = p => here(p).replace(process.cwd(), '.'); + +const [, , ...args] = process.argv; + +const useBuiltinConfig = + !args.includes('--config') && + !hasFile('.lintstagedrc') && + !hasFile('lintstaged.config.js') && + !hasPkgProp('lintstaged'); + +const config = useBuiltinConfig + ? ['--config', hereRelative('../config/lintstagedrc.js')] + : []; + +const lintStagedResult = spawn.sync(resolveBin('lint-staged'), [...config], { + stdio: 'inherit', +}); + +if (lintStagedResult.status !== 0 || !isOptedIn('pre-commit')) { + process.exit(lintStagedResult.status); +} else { + const validateResult = spawn.sync( + getPackageManagerBin(), + ['run', 'validate'], + { + stdio: 'inherit', + }, + ); + + process.exit(validateResult.status); +} diff --git a/src/scripts/precommit/index.js b/src/scripts/precommit/index.js deleted file mode 100644 index a2276f4..0000000 --- a/src/scripts/precommit/index.js +++ /dev/null @@ -1,20 +0,0 @@ -const spawn = require('cross-spawn'); -const { isOptedIn } = require('../../utils'); - -const [executor, ...args] = process.argv; - -const lintStagedResult = spawn.sync( - executor, - [require.resolve('./lint-staged')].concat(args), - { stdio: 'inherit' }, -); - -if (lintStagedResult.status !== 0 || !isOptedIn('pre-commit')) { - process.exit(lintStagedResult.status); -} else { - const validateResult = spawn.sync('npm', ['run', 'validate'], { - stdio: 'inherit', - }); - - process.exit(validateResult.status); -} diff --git a/src/scripts/precommit/lint-staged.js b/src/scripts/precommit/lint-staged.js deleted file mode 100644 index ce25efa..0000000 --- a/src/scripts/precommit/lint-staged.js +++ /dev/null @@ -1,39 +0,0 @@ -const path = require('path'); -const resolve = require('resolve'); -const { hasPkgProp, hasFile } = require('../../utils'); - -const useBuiltinConfig = - !hasFile('.lintstagedrc') && - !hasFile('lint-staged.config.js') && - !hasPkgProp('lint-staged'); - -const lintStagedPath = require.resolve('lint-staged'); -const cosmiconfigPath = resolve.sync('cosmiconfig', { - basedir: path.dirname(lintStagedPath), -}); -const realCosmicConfig = require(cosmiconfigPath); - -// lint-staged uses cosmiconfig to find its configuration -// and it has no other way to provide config -// (via a node API or command-line flag) -// So, we're doing this require cache magic to provide our own -// config so folks don't have to have that in their package.json -function fakeCosmiconfig(...args) { - if (args[0] === 'lint-staged') { - return { - load() { - return Promise.resolve({ - config: require('../../config/lintstagedrc'), - }); - }, - }; - } - - return realCosmicConfig(...args); -} - -if (useBuiltinConfig) { - require.cache[cosmiconfigPath] = { exports: fakeCosmiconfig }; -} - -require(lintStagedPath); diff --git a/src/utils.js b/src/utils.js index 600f919..1935d71 100644 --- a/src/utils.js +++ b/src/utils.js @@ -143,6 +143,17 @@ function isOptedIn(key, t = true, f = false) { return contents.includes(key) ? t : f; } +const getPackageManagerBin = () => { + try { + resolveBin('yarn'); + } catch (err) { + return 'npm'; + } + + if (hasFile('yarn.lock')) return 'yarn'; + return 'npm'; +}; + module.exports = { appDirectory, envIsSet, @@ -163,4 +174,5 @@ module.exports = { pkg, resolveBin, resolveFransScripts, + getPackageManagerBin, };