Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(cli): don't ask questions during publish when --non-interactive is specified (#5002) #5108

Merged
merged 2 commits into from
Feb 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions __tests__/commands/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> => {
return runRun([], {nonInteractive: true, newVersion}, 'no-args', async (config, reporter): ?Promise<void> => {
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<void> => {
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<void> => {
return runRun([], {newVersion, gitTagVersion}, 'no-args', async (config): ?Promise<void> => {
const pkg = await fs.readJson(path.join(config.cwd, 'package.json'));
Expand Down
5 changes: 4 additions & 1 deletion __tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
12 changes: 11 additions & 1 deletion src/cli/commands/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ async function getCredentials(
return {username, email};
}

export async function getToken(config: Config, reporter: Reporter, name: string = ''): Promise<() => Promise<void>> {
export async function getToken(
config: Config,
reporter: Reporter,
name: string = '',
flags: Object = {},
): Promise<() => Promise<void>> {
const auth = config.registries.npm.getAuth(name);
if (auth) {
config.registries.npm.setToken(auth);
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
5 changes: 5 additions & 0 deletions src/cli/commands/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions src/reporters/lang/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.',

Expand Down