Skip to content

Commit

Permalink
fix(run): yarn run should never prompt when non-interactive (#5694)
Browse files Browse the repository at this point in the history
**Summary**

This pr fixes a bug ([#5655](#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.
```
  • Loading branch information
peijiesim authored and BYK committed Apr 26, 2018
1 parent 12d8f7f commit 63c56c7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
10 changes: 0 additions & 10 deletions __tests__/commands/__snapshots__/run.js.snap
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
20 changes: 19 additions & 1 deletion __tests__/commands/run.js
Expand Up @@ -62,7 +62,6 @@ test('lists all available commands with no arguments', (): Promise<void> =>
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);
Expand All @@ -71,6 +70,25 @@ test('lists all available commands with no arguments', (): Promise<void> =>
expect(reporter.getBuffer()).toEqual(rprtr.getBuffer());
}));

test('lists all available commands with no arguments and --non-interactive', (): Promise<void> =>
runRun([], {nonInteractive: true}, 'no-args', (config, reporter): ?Promise<void> => {
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<void> =>
runRun(['build'], {}, 'spaces', async (config): ?Promise<void> => {
const pkg = await fs.readJson(path.join(config.cwd, 'package.json'));
Expand Down
16 changes: 8 additions & 8 deletions src/cli/commands/run.js
Expand Up @@ -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 {
Expand All @@ -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'));
}
Expand Down

0 comments on commit 63c56c7

Please sign in to comment.