Skip to content

Commit

Permalink
add version
Browse files Browse the repository at this point in the history
  • Loading branch information
ecraig12345 committed Nov 18, 2024
1 parent 2bb973e commit 8b01729
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/__tests__/changefile/getQuestionsForPackage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ describe('getQuestionsForPackage', () => {
expect(questions![0].choices).toEqual([
{
title:
' [1mPatch[22m - bug fixes; new features; backwards-compatible API changes (ok in patches for v0.x packages)',
' [1mPatch[22m - bug fixes; new features; backwards-compatible API changes (ok in patches for version < 1)',
value: 'patch',
},
{
title: ' [1mMinor[22m - breaking changes; major feature (ok in minor versions for v0.x packages)',
title: ' [1mMinor[22m - breaking changes; major feature (ok in minors for version < 1)',
value: 'minor',
},
{ title: ' None - this change does not affect the published package in any way', value: 'none' },
Expand Down
12 changes: 6 additions & 6 deletions src/__tests__/changefile/promptForChange.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ describe('promptForChange', () => {
await waitForPrompt();

// verify asking for first package
expect(logs.mocks.log).toHaveBeenLastCalledWith('Please describe the changes for: foo');
expect(logs.mocks.log).toHaveBeenLastCalledWith('Please describe the changes for: foo (currently v1.0.0)');
// choose custom options for this package
stdin.emitKey({ name: 'down' });
await stdin.sendByChar('\n');
stdin.emitKey({ name: 'down' });
await stdin.sendByChar('\n');

// verify asking for second package
expect(logs.mocks.log).toHaveBeenLastCalledWith('Please describe the changes for: bar');
expect(logs.mocks.log).toHaveBeenLastCalledWith('Please describe the changes for: bar (currently v1.0.0)');
// choose defaults
await stdin.sendByChar('\n\n');

Expand Down Expand Up @@ -120,11 +120,11 @@ describe('promptForChange', () => {
await waitForPrompt();

// use defaults for first package
expect(logs.mocks.log).toHaveBeenLastCalledWith('Please describe the changes for: foo');
expect(logs.mocks.log).toHaveBeenLastCalledWith('Please describe the changes for: foo (currently v1.0.0)');
await stdin.sendByChar('\n\n');

// cancel for second package
expect(logs.mocks.log).toHaveBeenLastCalledWith('Please describe the changes for: bar');
expect(logs.mocks.log).toHaveBeenLastCalledWith('Please describe the changes for: bar (currently v1.0.0)');
stdin.emitKey({ name: 'c', ctrl: true });

// nothing is returned
Expand All @@ -148,12 +148,12 @@ describe('promptForChange', () => {
await waitForPrompt();

// enter a valid type for foo
expect(logs.mocks.log).toHaveBeenLastCalledWith('Please describe the changes for: foo');
expect(logs.mocks.log).toHaveBeenLastCalledWith('Please describe the changes for: foo (currently v1.0.0)');
expect(stdout.getOutput()).toMatch(/enter any type/);
await stdin.sendByChar('patch\n');

// enter an invalid type for bar
expect(logs.mocks.log).toHaveBeenCalledWith('Please describe the changes for: bar');
expect(logs.mocks.log).toHaveBeenCalledWith('Please describe the changes for: bar (currently v1.0.0)');
await stdin.sendByChar('invalid\n');

expect(await changeFilesPromise).toBeUndefined();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ describe('promptForChange _promptForPackageChange', () => {
expect.objectContaining({ name: 'comment', type: 'autocomplete' }),
];

/** Info for the default package used in tests */
const pkgInfo = defaultQuestionsParams.packageInfos[pkg];

/** Wait for the prompt to finish rendering (simulates real user input) */
const waitForPrompt = () => new Promise(resolve => process.nextTick(resolve));

Expand All @@ -61,7 +64,7 @@ describe('promptForChange _promptForPackageChange', () => {
});

it('returns an empty object and logs nothing if there are no questions', async () => {
const answers = await _promptForPackageChange([], pkg);
const answers = await _promptForPackageChange([], pkgInfo);
expect(answers).toEqual({});
expect(logs.mocks.log).not.toHaveBeenCalled();
});
Expand All @@ -70,13 +73,15 @@ describe('promptForChange _promptForPackageChange', () => {
const questions = getQuestionsForPackage(defaultQuestionsParams);
expect(questions).toEqual(expectedQuestions);

const answersPromise = _promptForPackageChange(questions!, pkg);
const answersPromise = _promptForPackageChange(questions!, pkgInfo);

// input: press enter twice to use defaults (with a pause in between to simulate real user input)
await stdin.sendByChar('\n\n');
const answers = await answersPromise;

expect(logs.getMockLines('log')).toMatchInlineSnapshot(`"Please describe the changes for: foo"`);
expect(logs.getMockLines('log')).toMatchInlineSnapshot(
`"Please describe the changes for: foo (currently v1.0.0)"`
);
expect(stdout.getOutput()).toMatchInlineSnapshot(`
"? Change type » - Use arrow-keys. Return to submit.
> Patch - bug fixes; no API changes
Expand All @@ -99,7 +104,7 @@ describe('promptForChange _promptForPackageChange', () => {
});
expect(questions).toEqual(expectedQuestions.slice(1));

const answerPromise = _promptForPackageChange(questions!, pkg);
const answerPromise = _promptForPackageChange(questions!, pkgInfo);
await waitForPrompt();
expect(stdout.lastOutput()).toMatchInlineSnapshot(`
"? Describe changes (type or choose one) »
Expand All @@ -112,7 +117,9 @@ describe('promptForChange _promptForPackageChange', () => {
await stdin.sendByChar('abc\n');
const answers = await answerPromise;

expect(logs.getMockLines('log')).toMatchInlineSnapshot(`"Please describe the changes for: foo"`);
expect(logs.getMockLines('log')).toMatchInlineSnapshot(
`"Please describe the changes for: foo (currently v1.0.0)"`
);
expect(stdout.getOutput()).toMatchInlineSnapshot(`
"? Describe changes (type or choose one) » a
? Describe changes (type or choose one) » ab
Expand All @@ -130,7 +137,7 @@ describe('promptForChange _promptForPackageChange', () => {
});
expect(questions).toEqual(expectedQuestions.slice(1));

const answerPromise = _promptForPackageChange(questions!, pkg);
const answerPromise = _promptForPackageChange(questions!, pkgInfo);
await waitForPrompt();
expect(stdout.lastOutput()).toMatchInlineSnapshot(`
"? Describe changes (type or choose one) »
Expand All @@ -144,7 +151,9 @@ describe('promptForChange _promptForPackageChange', () => {
await stdin.sendByChar('\n');
const answers = await answerPromise;

expect(logs.getMockLines('log')).toMatchInlineSnapshot(`"Please describe the changes for: foo"`);
expect(logs.getMockLines('log')).toMatchInlineSnapshot(
`"Please describe the changes for: foo (currently v1.0.0)"`
);
expect(stdout.getOutput()).toMatchInlineSnapshot(`
"? Describe changes (type or choose one) » abc
√ Describe changes (type or choose one) » abc"
Expand All @@ -160,7 +169,7 @@ describe('promptForChange _promptForPackageChange', () => {
});
expect(questions).toEqual(expectedQuestions.slice(1));

const answerPromise = _promptForPackageChange(questions!, pkg);
const answerPromise = _promptForPackageChange(questions!, pkgInfo);
await waitForPrompt();
expect(stdout.lastOutput()).toMatchInlineSnapshot(`
"? Describe changes (type or choose one) »
Expand All @@ -173,7 +182,9 @@ describe('promptForChange _promptForPackageChange', () => {
stdin.send('abc\n');
const answers = await answerPromise;

expect(logs.getMockLines('log')).toMatchInlineSnapshot(`"Please describe the changes for: foo"`);
expect(logs.getMockLines('log')).toMatchInlineSnapshot(
`"Please describe the changes for: foo (currently v1.0.0)"`
);
expect(stdout.getOutput()).toMatchInlineSnapshot(`""`);
expect(answers).toEqual({ comment: 'abc' });
});
Expand All @@ -183,7 +194,7 @@ describe('promptForChange _promptForPackageChange', () => {
const questions = getQuestionsForPackage({ ...defaultQuestionsParams, recentMessages });
expect(questions).toEqual(expectedQuestions);

const answerPromise = _promptForPackageChange(questions!, pkg);
const answerPromise = _promptForPackageChange(questions!, pkgInfo);

// arrow down to select the third type
stdin.emitKey({ name: 'down' });
Expand Down Expand Up @@ -233,7 +244,7 @@ describe('promptForChange _promptForPackageChange', () => {
});
expect(questions).toEqual(expectedQuestions.slice(1));

const answerPromise = _promptForPackageChange(questions!, pkg);
const answerPromise = _promptForPackageChange(questions!, pkgInfo);

// type "ba" and press enter to select "bar"
await stdin.sendByChar('ba\n');
Expand Down Expand Up @@ -264,7 +275,7 @@ describe('promptForChange _promptForPackageChange', () => {
});
expect(questions).toEqual(expectedQuestions.slice(1));

const answerPromise = _promptForPackageChange(questions!, pkg);
const answerPromise = _promptForPackageChange(questions!, pkgInfo);

// type "b", press backspace to delete it, press enter to select foo
await stdin.sendByChar('b');
Expand Down Expand Up @@ -294,7 +305,7 @@ describe('promptForChange _promptForPackageChange', () => {
const questions = getQuestionsForPackage(defaultQuestionsParams);
expect(questions).toEqual(expectedQuestions);

const answerPromise = _promptForPackageChange(questions!, pkg);
const answerPromise = _promptForPackageChange(questions!, pkgInfo);

// answer the first question
await stdin.sendByChar('\n');
Expand All @@ -305,7 +316,7 @@ describe('promptForChange _promptForPackageChange', () => {
const answers = await answerPromise;

expect(logs.getMockLines('log')).toMatchInlineSnapshot(`
"Please describe the changes for: foo
"Please describe the changes for: foo (currently v1.0.0)
Cancelled, no change files are written"
`);

Expand Down
10 changes: 5 additions & 5 deletions src/changefile/promptForChange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import prompts from 'prompts';
import { ChangeFileInfo, ChangeType } from '../types/ChangeInfo';
import { BeachballOptions } from '../types/BeachballOptions';
import { isValidChangeType } from '../validation/isValidChangeType';
import { PackageGroups, PackageInfos } from '../types/PackageInfo';
import { PackageGroups, PackageInfo, PackageInfos } from '../types/PackageInfo';
import { getQuestionsForPackage } from './getQuestionsForPackage';

type ChangePromptResponse = { type?: ChangeType; comment?: string };
Expand All @@ -19,7 +19,7 @@ export async function promptForChange(params: {
email: string | null;
options: Pick<BeachballOptions, 'message' | 'type' | 'dependentChangeType'>;
}): Promise<ChangeFileInfo[] | undefined> {
const { changedPackages, email, options } = params;
const { changedPackages, packageInfos, email, options } = params;
if (!changedPackages.length) {
return;
}
Expand All @@ -43,7 +43,7 @@ export async function promptForChange(params: {
// Now prompt for each package
const packageChangeInfo: ChangeFileInfo[] = [];
for (let pkg of changedPackages) {
const response = await _promptForPackageChange(packageQuestions[pkg], pkg);
const response = await _promptForPackageChange(packageQuestions[pkg], packageInfos[pkg]);
if (!response) {
return; // user cancelled
}
Expand All @@ -64,7 +64,7 @@ export async function promptForChange(params: {
*/
export async function _promptForPackageChange(
questions: prompts.PromptObject[],
pkg: string
pkg: PackageInfo
): Promise<ChangePromptResponse | undefined> {
if (!questions.length) {
// This MUST return an empty object rather than nothing, because returning nothing means the
Expand All @@ -73,7 +73,7 @@ export async function _promptForPackageChange(
}

console.log('');
console.log(`Please describe the changes for: ${pkg}`);
console.log(`Please describe the changes for: ${pkg.name} (currently v${pkg.version})`);

let isCancelled = false;
const onCancel = () => {
Expand Down

0 comments on commit 8b01729

Please sign in to comment.