diff --git a/__tests__/commands/version.js b/__tests__/commands/version.js index d19033a855..78480ae763 100644 --- a/__tests__/commands/version.js +++ b/__tests__/commands/version.js @@ -50,6 +50,24 @@ test('run version with no arguments, --new-version flag where version is same as }); }); +test('run version with --non-interactive and --new-version should succeed', (): Promise => { + return runRun([], {nonInteractive: true, newVersion}, 'no-args', async (config, reporter): ?Promise => { + const pkg = await fs.readJson(path.join(config.cwd, 'package.json')); + + expect(pkg.version).toEqual(newVersion); + }); +}); + +test('run version with --non-interactive and without --new-version should fail', async (): Promise => { + let thrown = false; + try { + await runRun([], {nonInteractive: true}, 'no-args'); + } catch (e) { + thrown = true; + } + expect(thrown).toEqual(true); +}); + test('run version and make sure all lifecycle steps are executed', (): Promise => { return runRun([], {newVersion, gitTagVersion}, 'no-args', async (config): ?Promise => { const pkg = await fs.readJson(path.join(config.cwd, 'package.json')); diff --git a/__tests__/index.js b/__tests__/index.js index 88541afc5b..c91fbedab4 100644 --- a/__tests__/index.js +++ b/__tests__/index.js @@ -208,7 +208,10 @@ test.concurrent('should show version of yarn with -v', async () => { }); test.concurrent('should run version command', async () => { - await expectAnErrorMessage(execCommand('version', [], 'run-version'), "Can't answer a question unless a user TTY"); + await expectAnErrorMessage( + execCommand('version', [], 'run-version'), + 'You must specify a new version with --new-version when running with --non-interactive.', + ); }); test.concurrent('should run --version command', async () => { diff --git a/src/cli/commands/login.js b/src/cli/commands/login.js index 2b24434c75..d95d52ec60 100644 --- a/src/cli/commands/login.js +++ b/src/cli/commands/login.js @@ -36,7 +36,12 @@ async function getCredentials( return {username, email}; } -export async function getToken(config: Config, reporter: Reporter, name: string = ''): Promise<() => Promise> { +export async function getToken( + config: Config, + reporter: Reporter, + name: string = '', + flags: Object = {}, +): Promise<() => Promise> { const auth = config.registries.npm.getAuth(name); if (auth) { config.registries.npm.setToken(auth); @@ -55,6 +60,11 @@ export async function getToken(config: Config, reporter: Reporter, name: string }; } + // make sure we're not running in non-interactive mode before asking for login + if (flags.nonInteractive || config.nonInteractive) { + throw new MessageError(reporter.lang('nonInteractiveNoToken')); + } + // const creds = await getCredentials(config, reporter); if (!creds) { diff --git a/src/cli/commands/publish.js b/src/cli/commands/publish.js index ee16431e4b..1be22fd14a 100644 --- a/src/cli/commands/publish.js +++ b/src/cli/commands/publish.js @@ -134,7 +134,7 @@ export async function run(config: Config, reporter: Reporter, flags: Object, arg // reporter.step(2, 4, reporter.lang('loggingIn')); - const revoke = await getToken(config, reporter, pkg.name); + const revoke = await getToken(config, reporter, pkg.name, flags); // reporter.step(3, 4, reporter.lang('publishing')); diff --git a/src/cli/commands/version.js b/src/cli/commands/version.js index c75ae40b07..b063a021ac 100644 --- a/src/cli/commands/version.js +++ b/src/cli/commands/version.js @@ -74,6 +74,11 @@ export async function setVersion( // wasn't passed a version arg so ask interactively while (!newVersion) { + // make sure we're not running in non-interactive mode before asking for new version + if (flags.nonInteractive || config.nonInteractive) { + throw new MessageError(reporter.lang('nonInteractiveNoVersionSpecified')); + } + newVersion = await reporter.question(reporter.lang('newVersion')); if (!required && !newVersion) { diff --git a/src/reporters/lang/en.js b/src/reporters/lang/en.js index ac12f341b5..b3b299db15 100644 --- a/src/reporters/lang/en.js +++ b/src/reporters/lang/en.js @@ -308,6 +308,10 @@ const messages = { published: 'Published.', publishing: 'Publishing', + nonInteractiveNoVersionSpecified: + 'You must specify a new version with --new-version when running with --non-interactive.', + nonInteractiveNoToken: "No token found and can't prompt for login when running with --non-interactive.", + infoFail: 'Received invalid response from npm.', malformedRegistryResponse: 'Received malformed response from registry for $0. The registry may be down.',