From 8ce294c3b0c8fd6aab349a7c7819b4016c0a1d15 Mon Sep 17 00:00:00 2001 From: Avi Sharvit Date: Fri, 2 Aug 2019 11:19:38 +0300 Subject: [PATCH] feat(cli-options): add --noDefaults option Using --noDefaults option will set all the default Boolean options to false --- src/app/index.test.js | 52 ++++++++++++++++++++++++++++++++++++++++++- src/app/options.js | 4 ++++ src/app/prompter.js | 25 ++++++++++++++++----- src/app/prompts.js | 2 ++ 4 files changed, 76 insertions(+), 7 deletions(-) diff --git a/src/app/index.test.js b/src/app/index.test.js index 9258852..fa805c0 100644 --- a/src/app/index.test.js +++ b/src/app/index.test.js @@ -33,7 +33,6 @@ test('default files', () => { assert.file([ '.git', '.babelrc', - '.commitlintrc.json', '.editorconfig', '.eslintignore', '.eslintrc', @@ -54,9 +53,49 @@ test('default files', () => { 'other/roadmap.md', // travisCI '.travis.yml', + // semanticRelease + '.commitlintrc.json', + ]); + + assert.noFile([ + '_babelrc', + '_editorconfig', + '_eslintignore', + '_eslintrc', + '_gitattributes', + '_gitignore', + '_travis.yml', + '_npmignore', + '_github/issue_template.md', + '_github/pull_request_template.md', + '_commitlintrc.json', + ]); + }); +}); + +test('default files with --noDefaults', () => { + return runAppGenerator() + .withOptions({ noDefaults: true }) + .withPrompts(requiredPrompts) + .then(() => { + assert.file([ + '.git', + '.babelrc', + '.editorconfig', + '.eslintignore', + '.eslintrc', + '.gitattributes', + '.gitignore', + '.npmignore', + 'license', + 'package.json', + 'readme.md', + 'src/index.js', + 'src/index.test.js', ]); assert.noFile([ + '.commitlintrc.json', '_babelrc', '_editorconfig', '_eslintignore', @@ -68,6 +107,17 @@ test('default files', () => { '_github/issue_template.md', '_github/pull_request_template.md', '_commitlintrc.json', + // githubTemplates + 'contributing.md', + '.github/issue_template.md', + '.github/pull_request_template.md', + 'other/code_of_conduct.md', + 'other/examples.md', + 'other/roadmap.md', + // travisCI + '.travis.yml', + // semanticRelease + '.commitlintrc.json', ]); }); }); diff --git a/src/app/options.js b/src/app/options.js index d47eccc..cf0d1b8 100644 --- a/src/app/options.js +++ b/src/app/options.js @@ -45,6 +45,10 @@ const options = { desc: 'Your github-token, needed when using with --createGithubRepository or --semanticRelease (https://github.com/sharvit/generator-node-mdl#about-passwords-and-tokens)', }, + noDefaults: { + type: Boolean, + desc: 'Set all the default Boolean optins to false', + }, }; export default options; diff --git a/src/app/prompter.js b/src/app/prompter.js index 27719a0..6414e36 100644 --- a/src/app/prompter.js +++ b/src/app/prompter.js @@ -25,7 +25,9 @@ export default class Prompter { await this._promptGeneral(); await this._promptGithub(); await this._promptTravis(); + await this._promptCoveralls(); await this._promptNpm(); + await this._promptSemanticRelease(); await this._promptPasswords(); return this.props; @@ -78,7 +80,9 @@ export default class Prompter { ]); this._updatePropsWithAnswers({ travisCI }); + } + async _promptCoveralls() { const { coveralls } = await this.generator.prompt([ this._buildPrompt('coveralls', prompts.coveralls), ]); @@ -87,13 +91,21 @@ export default class Prompter { } async _promptNpm() { - const { npmDeploy, semanticRelease } = await this.generator.prompt([ + const { npmDeploy } = await this.generator.prompt([ this._buildPrompt('npmDeploy', prompts.npmDeploy), - this._buildPrompt('semanticRelease', prompts.semanticRelease), ]); this._updatePropsWithAnswers({ npmDeploy: npmDeploy || false, + }); + } + + async _promptSemanticRelease() { + const { semanticRelease } = await this.generator.prompt([ + this._buildPrompt('semanticRelease', prompts.semanticRelease), + ]); + + this._updatePropsWithAnswers({ semanticRelease: semanticRelease || false, }); } @@ -279,9 +291,14 @@ export default class Prompter { _buildPrompt(name, prompt) { const result = { name, message: prompt.desc }; + if (prompt.default) result.default = prompt.default; + if (prompt.store) result.store = prompt.store; + if (prompt.filter) result.filter = prompt.filter; + switch (prompt.type) { case Boolean: result.type = 'confirm'; + result.default = this.props.noDefaults ? false : result.default; break; case String: default: @@ -289,10 +306,6 @@ export default class Prompter { break; } - if (prompt.default) result.default = prompt.default; - if (prompt.store) result.store = prompt.store; - if (prompt.filter) result.filter = prompt.filter; - result.when = () => { if (this.props[name] !== undefined) return false; diff --git a/src/app/prompts.js b/src/app/prompts.js index 9816332..6ed5707 100644 --- a/src/app/prompts.js +++ b/src/app/prompts.js @@ -58,12 +58,14 @@ const prompts = { ...options.npmDeploy, required: true, store: true, + dependOn: ['travisCI'], help: () => '\nNeed to have an npm account: https://www.npmjs.com/', }, semanticRelease: { ...options.semanticRelease, required: true, store: true, + dependOn: ['travisCI', 'npmDeploy'], help: () => '\nLearn more about semantic-release: https://semantic-release.gitbook.io/semantic-release/', },