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 941d1f8 commit 4d80aec
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 29 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 @@ -53,11 +53,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 @@ -81,15 +81,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 @@ -118,11 +118,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 @@ -146,12 +146,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 @@ -41,6 +41,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 @@ -59,7 +62,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 @@ -68,13 +71,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 @@ -97,7 +102,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 @@ -110,7 +115,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 @@ -128,7 +135,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 @@ -142,7 +149,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 @@ -158,7 +167,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 @@ -171,7 +180,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 @@ -181,7 +192,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 @@ -231,7 +242,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 @@ -262,7 +273,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 @@ -292,7 +303,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 @@ -303,7 +314,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
4 changes: 2 additions & 2 deletions src/changefile/getQuestionsForPackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ const defaultChangeTypeDescriptions: ChangeTypeDescriptions = {
prerelease: 'bump prerelease version',
patch: {
general: 'bug fixes; no API changes',
v0: 'bug fixes; new features; backwards-compatible API changes (ok in patches for v0.x packages)',
v0: 'bug fixes; new features; backwards-compatible API changes (ok in patches for version < 1)',
},
minor: {
general: 'new feature; backwards-compatible API changes',
v0: 'breaking changes; major feature (ok in minor versions for v0.x packages)',
v0: 'breaking changes; major feature (ok in minors for version < 1)',
},
none: 'this change does not affect the published package in any way',
major: {
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 4d80aec

Please sign in to comment.