Skip to content
This repository has been archived by the owner on Jul 7, 2022. It is now read-only.

Commit

Permalink
feat(precommit): Update precommit script
Browse files Browse the repository at this point in the history
Update precommit script to newer version lint-staged which provides a way to explicitly provide a
config file. This means the cosmicconfig hack is not needed anymore.
  • Loading branch information
adambrgmn committed Dec 19, 2017
1 parent 2331828 commit 2199128
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 59 deletions.
42 changes: 42 additions & 0 deletions src/scripts/precommit.js
Original file line number Diff line number Diff line change
@@ -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);
}
20 changes: 0 additions & 20 deletions src/scripts/precommit/index.js

This file was deleted.

39 changes: 0 additions & 39 deletions src/scripts/precommit/lint-staged.js

This file was deleted.

12 changes: 12 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -163,4 +174,5 @@ module.exports = {
pkg,
resolveBin,
resolveFransScripts,
getPackageManagerBin,
};

0 comments on commit 2199128

Please sign in to comment.