From 63c56c713175d1db384d76b81dabe7d53e93119a Mon Sep 17 00:00:00 2001 From: peijiesim Date: Thu, 26 Apr 2018 11:11:48 -0700 Subject: [PATCH] fix(run): yarn run should never prompt when non-interactive (#5694) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Summary** This pr fixes a bug ([#5655](https://github.com/yarnpkg/yarn/issues/5655)) in which `yarn run --non-interactive` prints a `Error: No command specified` message and also suppresses the message `question Which command would you like to run?:` when running non-interactively. **Test plan** ``` > yarn-local run --non-interactive yarn run v1.6.0 info Commands available from binary scripts: acorn, atob, babylon, browserslist, commitizen, detect-libc, errno, escodegen, esgenerate, eslint, esparse, esvalidate, flow, git-cz, git-release-notes, gulp, gunzip-maybe, handlebars, import-local-fixture, jest, jest-runtime, js-yaml, jsesc, jsinspect, json5, loose-envify, miller-rabin, mkdirp, node-pre-gyp, nopt, prettier, rc, regjsparser, rimraf, sane, semver, sha.js, shjs, sshpk-conv, sshpk-sign, sshpk-verify, strip-indent, strip-json-comments, uglifyjs, user-home, uuid, watch, webpack, which info Project commands - build gulp build - build-bundle node ./scripts/build-webpack.js - build-chocolatey powershell ./scripts/build-chocolatey.ps1 - build-deb ./scripts/build-deb.sh - build-dist bash ./scripts/build-dist.sh - build-win-installer scripts\build-windows-installer.bat - changelog git-release-notes $(git describe --tags --abbrev=0 $(git describe --tags --abbrev=0)^)..$(git describe --tags --abbrev=0) scripts/changelog.md - commit git-cz - dupe-check yarn jsinspect ./src - lint eslint . && flow check - pkg-tests yarn --cwd packages/pkg-tests jest yarn.test.js - prettier eslint src __tests__ --fix - release-branch ./scripts/release-branch.sh - test yarn lint && yarn test-only - test-coverage node --max_old_space_size=4096 node_modules/jest/bin/jest.js --coverage --verbose - test-only node --max_old_space_size=4096 node_modules/jest/bin/jest.js --verbose - watch gulp watch ✨ Done in 0.28s. ``` --- __tests__/commands/__snapshots__/run.js.snap | 10 ---------- __tests__/commands/run.js | 20 +++++++++++++++++++- src/cli/commands/run.js | 16 ++++++++-------- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/__tests__/commands/__snapshots__/run.js.snap b/__tests__/commands/__snapshots__/run.js.snap index 0b22fa9b6d..2fa7a6139e 100644 --- a/__tests__/commands/__snapshots__/run.js.snap +++ b/__tests__/commands/__snapshots__/run.js.snap @@ -2,11 +2,6 @@ exports[`returns noBinAvailable with no bins 1`] = ` Array [ - Object { - "data": "No command specified.", - "error": true, - "type": "error", - }, Object { "data": "There are no binary scripts available.", "error": true, @@ -44,11 +39,6 @@ Array [ exports[`returns noScriptsAvailable with no scripts 1`] = ` Array [ - Object { - "data": "No command specified.", - "error": true, - "type": "error", - }, Object { "data": "Commands available from binary scripts: cat-names", "error": false, diff --git a/__tests__/commands/run.js b/__tests__/commands/run.js index 085441ebcc..2bad11ef51 100644 --- a/__tests__/commands/run.js +++ b/__tests__/commands/run.js @@ -62,7 +62,6 @@ test('lists all available commands with no arguments', (): Promise => const bins = ['cat-names']; // Emulate run output - rprtr.error(rprtr.lang('commandNotSpecified')); rprtr.info(`${rprtr.lang('binCommands')}${bins.join(', ')}`); rprtr.info(rprtr.lang('possibleCommands')); rprtr.list('possibleCommands', scripts, hints); @@ -71,6 +70,25 @@ test('lists all available commands with no arguments', (): Promise => expect(reporter.getBuffer()).toEqual(rprtr.getBuffer()); })); +test('lists all available commands with no arguments and --non-interactive', (): Promise => + runRun([], {nonInteractive: true}, 'no-args', (config, reporter): ?Promise => { + const rprtr = new reporters.BufferReporter({stdout: null, stdin: null}); + const scripts = ['build', 'prestart', 'start']; + const hints = { + build: "echo 'building'", + prestart: "echo 'prestart'", + start: 'node index.js', + }; + const bins = ['cat-names']; + + // Emulate run output + rprtr.info(`${rprtr.lang('binCommands')}${bins.join(', ')}`); + rprtr.info(rprtr.lang('possibleCommands')); + rprtr.list('possibleCommands', scripts, hints); + + expect(reporter.getBuffer()).toEqual(rprtr.getBuffer()); + })); + test('runs script containing spaces', (): Promise => runRun(['build'], {}, 'spaces', async (config): ?Promise => { const pkg = await fs.readJson(path.join(config.cwd, 'package.json')); diff --git a/src/cli/commands/run.js b/src/cli/commands/run.js index a7b2dbd19a..30b62d61b5 100644 --- a/src/cli/commands/run.js +++ b/src/cli/commands/run.js @@ -113,8 +113,6 @@ export async function run(config: Config, reporter: Reporter, flags: Object, arg // list possible scripts if none specified if (args.length === 0) { - reporter.error(reporter.lang('commandNotSpecified')); - if (binCommands.length) { reporter.info(`${reporter.lang('binCommands') + binCommands.join(', ')}`); } else { @@ -124,12 +122,14 @@ export async function run(config: Config, reporter: Reporter, flags: Object, arg if (pkgCommands.length) { reporter.info(`${reporter.lang('possibleCommands')}`); reporter.list('possibleCommands', pkgCommands, cmdHints); - await reporter - .question(reporter.lang('commandQuestion')) - .then( - answer => runCommand(answer.trim().split(' ')), - () => reporter.error(reporter.lang('commandNotSpecified')), - ); + if (!flags.nonInteractive) { + await reporter + .question(reporter.lang('commandQuestion')) + .then( + answer => runCommand(answer.trim().split(' ')), + () => reporter.error(reporter.lang('commandNotSpecified')), + ); + } } else { reporter.error(reporter.lang('noScriptsAvailable')); }