Skip to content

Commit

Permalink
feat: lint staged files on precommit, do nothing on push (#218)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: splits husky option into a precommit/prepush
  • Loading branch information
brandonocasey authored Aug 22, 2018
1 parent 9a79cc7 commit c6deb82
Show file tree
Hide file tree
Showing 6 changed files with 834 additions and 851 deletions.
9 changes: 2 additions & 7 deletions generators/app/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ module.exports = {
basic: 'Basic plugin (function-based)'
}),

HUSKY_CHOICES: objectToChoices({
lint: 'Check code quality',
test: 'Check code quality and run tests',
none: 'Nothing'
}),

LICENSE_NAMES,

LICENSE_CHOICES: objectToChoices(LICENSE_NAMES),
Expand All @@ -52,7 +46,8 @@ module.exports = {
PROMPT_DEFAULTS: {
docs: false,
css: false,
husky: 'lint',
prepush: false,
precommit: true,
lang: false,
license: LICENSE_DEFAULT,
pluginType: 'advanced',
Expand Down
14 changes: 9 additions & 5 deletions generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,15 @@ module.exports = class extends Generator {
`,
default: defaults.lang
}, {
type: 'list',
name: 'husky',
message: 'What should be done before you `git push`?',
default: defaults.husky,
choices: constants.HUSKY_CHOICES
type: 'confirm',
name: 'precommit',
message: 'Should we lint changed files before allowing `git commit`',
default: defaults.precommit
}, {
type: 'confirm',
name: 'prepush',
message: 'Should we run tests before allowing `git push`',
default: defaults.prepush
}];

return prompts.filter(p => !_.includes(this._promptsToFilter, p.name));
Expand Down
50 changes: 39 additions & 11 deletions generators/app/package-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,38 @@ const packageJSON = (current, context) => {

// In case husky was previously installed, but is now "none", we can
// remove it from the package.json entirely.
if (context.husky === 'none') {
delete result.devDependencies.husky;
if (!context.precommit) {
delete result.scripts.precommit;
}

if (!context.prepush) {
delete result.scripts.prepush;
} else {
_.assign(result.devDependencies, getGeneratorVersions(['husky']));
result.scripts.prepush = `npm run ${context.husky}`;
}

if (!context.prepush && !context.precommit) {
delete result.devDependencies.husky;
delete result.devDependencies['lint-staged'];
}

if (context.precommit) {
_.assign(result.devDependencies, getGeneratorVersions(['husky', 'lint-staged']));
result.scripts.precommit = 'lint-staged';

result['lint-staged'] = result['lint-staged'] || {};
_.assign(result['lint-staged'], {
'*.js': [
'vjsstandard --fix',
'git add'
]
});
}

if (context.prepush) {
if (!result.devDependencies.husky) {
_.assign(result.devDependencies, getGeneratorVersions(['husky']));
}

result.scripts.prepush = 'npm run test';
}

// Support the documentation tooling option.
Expand All @@ -203,8 +229,14 @@ const packageJSON = (current, context) => {
'docs:toc': 'doctoc README.md'
});

if (context.husky !== 'none') {
result.scripts.precommit = 'npm run docs:toc && git add README.md';
if (context.precommit) {
result['lint-staged'] = result['lint-staged'] || {};
_.assign(result['lint-staged'], {
'README.md': [
'npm run docs:toc',
'git add'
]
});
}

_.assign(result.devDependencies, getGeneratorVersions(['doctoc', 'jsdoc']));
Expand All @@ -216,10 +248,6 @@ const packageJSON = (current, context) => {
'watch:css': 'npm run build:css -- -w'
});

if (context.husky !== 'none') {
result.scripts.precommit = 'npm run docs:toc && git add README.md';
}

_.assign(result.devDependencies, getGeneratorVersions([
'postcss-cli',
'videojs-generate-postcss-config'
Expand Down
Loading

0 comments on commit c6deb82

Please sign in to comment.