From 4d80aec4661ed8dbc15623f6813a8b428e71dfee Mon Sep 17 00:00:00 2001 From: Elizabeth Craig Date: Mon, 18 Nov 2024 06:09:55 -0800 Subject: [PATCH] add version --- .../changefile/getQuestionsForPackage.test.ts | 4 +- .../changefile/promptForChange.test.ts | 12 +++--- ...ptForChange_promptForPackageChange.test.ts | 39 ++++++++++++------- src/changefile/getQuestionsForPackage.ts | 4 +- src/changefile/promptForChange.ts | 10 ++--- 5 files changed, 40 insertions(+), 29 deletions(-) diff --git a/src/__tests__/changefile/getQuestionsForPackage.test.ts b/src/__tests__/changefile/getQuestionsForPackage.test.ts index d0aaed2a..571492f5 100644 --- a/src/__tests__/changefile/getQuestionsForPackage.test.ts +++ b/src/__tests__/changefile/getQuestionsForPackage.test.ts @@ -53,11 +53,11 @@ describe('getQuestionsForPackage', () => { expect(questions![0].choices).toEqual([ { title: - ' Patch - bug fixes; new features; backwards-compatible API changes (ok in patches for v0.x packages)', + ' Patch - bug fixes; new features; backwards-compatible API changes (ok in patches for version < 1)', value: 'patch', }, { - title: ' Minor - breaking changes; major feature (ok in minor versions for v0.x packages)', + title: ' Minor - 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' }, diff --git a/src/__tests__/changefile/promptForChange.test.ts b/src/__tests__/changefile/promptForChange.test.ts index c9bcb950..e4e2b0ca 100644 --- a/src/__tests__/changefile/promptForChange.test.ts +++ b/src/__tests__/changefile/promptForChange.test.ts @@ -81,7 +81,7 @@ 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'); @@ -89,7 +89,7 @@ describe('promptForChange', () => { 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'); @@ -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 @@ -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(); diff --git a/src/__tests__/changefile/promptForChange_promptForPackageChange.test.ts b/src/__tests__/changefile/promptForChange_promptForPackageChange.test.ts index a96074e6..51089103 100644 --- a/src/__tests__/changefile/promptForChange_promptForPackageChange.test.ts +++ b/src/__tests__/changefile/promptForChange_promptForPackageChange.test.ts @@ -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)); @@ -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(); }); @@ -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 @@ -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) » @@ -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 @@ -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) » @@ -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" @@ -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) » @@ -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' }); }); @@ -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' }); @@ -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'); @@ -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'); @@ -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'); @@ -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" `); diff --git a/src/changefile/getQuestionsForPackage.ts b/src/changefile/getQuestionsForPackage.ts index 3a14a19c..1aa54244 100644 --- a/src/changefile/getQuestionsForPackage.ts +++ b/src/changefile/getQuestionsForPackage.ts @@ -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: { diff --git a/src/changefile/promptForChange.ts b/src/changefile/promptForChange.ts index 87340c66..f62cf03c 100644 --- a/src/changefile/promptForChange.ts +++ b/src/changefile/promptForChange.ts @@ -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 }; @@ -19,7 +19,7 @@ export async function promptForChange(params: { email: string | null; options: Pick; }): Promise { - const { changedPackages, email, options } = params; + const { changedPackages, packageInfos, email, options } = params; if (!changedPackages.length) { return; } @@ -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 } @@ -64,7 +64,7 @@ export async function promptForChange(params: { */ export async function _promptForPackageChange( questions: prompts.PromptObject[], - pkg: string + pkg: PackageInfo ): Promise { if (!questions.length) { // This MUST return an empty object rather than nothing, because returning nothing means the @@ -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 = () => {