From b816057514dca81e238458e4504f7d8eab831c95 Mon Sep 17 00:00:00 2001 From: legobt <6wbvkn0j@anonaddy.me> Date: Tue, 13 Aug 2024 02:51:25 +0000 Subject: [PATCH] feat: allow running tool in offline mode by setting --fetch-remote=false --- src/initial-parameters.test.ts | 24 +++++++++++++++++------- src/initial-parameters.ts | 2 ++ src/main.test.ts | 3 +++ src/main.ts | 11 +++++++++-- src/monorepo-workflow-operations.ts | 5 ++++- src/package.ts | 4 ++++ src/project.test.ts | 7 +++++-- src/project.ts | 4 ++++ src/repo.test.ts | 2 +- 9 files changed, 49 insertions(+), 13 deletions(-) diff --git a/src/initial-parameters.test.ts b/src/initial-parameters.test.ts index 9f4299c..499207d 100644 --- a/src/initial-parameters.test.ts +++ b/src/initial-parameters.test.ts @@ -36,12 +36,13 @@ describe('initial-parameters', () => { reset: true, backport: false, defaultBranch: 'main', + fetchRemote: true, }); jest .spyOn(envModule, 'getEnvironmentVariables') .mockReturnValue({ EDITOR: undefined }); when(jest.spyOn(projectModule, 'readProject')) - .calledWith('/path/to/project', { stderr }) + .calledWith('/path/to/project', { stderr, fetchRemote: true }) .mockResolvedValue(project); const initialParameters = await determineInitialParameters({ @@ -56,6 +57,7 @@ describe('initial-parameters', () => { reset: true, releaseType: 'ordinary', defaultBranch: 'main', + fetchRemote: true, }); }); @@ -72,6 +74,7 @@ describe('initial-parameters', () => { reset: true, backport: false, defaultBranch: 'main', + fetchRemote: true, }); jest .spyOn(envModule, 'getEnvironmentVariables') @@ -88,6 +91,7 @@ describe('initial-parameters', () => { expect(readProjectSpy).toHaveBeenCalledWith('/path/to/cwd/project', { stderr, + fetchRemote: true, }); }); @@ -102,12 +106,13 @@ describe('initial-parameters', () => { reset: true, backport: false, defaultBranch: 'main', + fetchRemote: true, }); jest .spyOn(envModule, 'getEnvironmentVariables') .mockReturnValue({ EDITOR: undefined }); when(jest.spyOn(projectModule, 'readProject')) - .calledWith('/path/to/project', { stderr }) + .calledWith('/path/to/project', { stderr, fetchRemote: true }) .mockResolvedValue(project); const initialParameters = await determineInitialParameters({ @@ -132,12 +137,13 @@ describe('initial-parameters', () => { reset: true, backport: false, defaultBranch: 'main', + fetchRemote: true, }); jest .spyOn(envModule, 'getEnvironmentVariables') .mockReturnValue({ EDITOR: undefined }); when(jest.spyOn(projectModule, 'readProject')) - .calledWith('/path/to/project', { stderr }) + .calledWith('/path/to/project', { stderr, fetchRemote: true }) .mockResolvedValue(project); const initialParameters = await determineInitialParameters({ @@ -162,12 +168,13 @@ describe('initial-parameters', () => { reset: true, backport: false, defaultBranch: 'main', + fetchRemote: true, }); jest .spyOn(envModule, 'getEnvironmentVariables') .mockReturnValue({ EDITOR: undefined }); when(jest.spyOn(projectModule, 'readProject')) - .calledWith('/path/to/project', { stderr }) + .calledWith('/path/to/project', { stderr, fetchRemote: true }) .mockResolvedValue(project); const initialParameters = await determineInitialParameters({ @@ -190,12 +197,13 @@ describe('initial-parameters', () => { reset: false, backport: false, defaultBranch: 'main', + fetchRemote: true, }); jest .spyOn(envModule, 'getEnvironmentVariables') .mockReturnValue({ EDITOR: undefined }); when(jest.spyOn(projectModule, 'readProject')) - .calledWith('/path/to/project', { stderr }) + .calledWith('/path/to/project', { stderr, fetchRemote: true }) .mockResolvedValue(project); const initialParameters = await determineInitialParameters({ @@ -218,12 +226,13 @@ describe('initial-parameters', () => { reset: false, backport: true, defaultBranch: 'main', + fetchRemote: true, }); jest .spyOn(envModule, 'getEnvironmentVariables') .mockReturnValue({ EDITOR: undefined }); when(jest.spyOn(projectModule, 'readProject')) - .calledWith('/path/to/project', { stderr }) + .calledWith('/path/to/project', { stderr, fetchRemote: true }) .mockResolvedValue(project); const initialParameters = await determineInitialParameters({ @@ -246,12 +255,13 @@ describe('initial-parameters', () => { reset: false, backport: false, defaultBranch: 'main', + fetchRemote: true, }); jest .spyOn(envModule, 'getEnvironmentVariables') .mockReturnValue({ EDITOR: undefined }); when(jest.spyOn(projectModule, 'readProject')) - .calledWith('/path/to/project', { stderr }) + .calledWith('/path/to/project', { stderr, fetchRemote: true }) .mockResolvedValue(project); const initialParameters = await determineInitialParameters({ diff --git a/src/initial-parameters.ts b/src/initial-parameters.ts index 0e7da44..3754550 100644 --- a/src/initial-parameters.ts +++ b/src/initial-parameters.ts @@ -21,6 +21,7 @@ type InitialParameters = { reset: boolean; releaseType: ReleaseType; defaultBranch: string; + fetchRemote: boolean; }; /** @@ -64,5 +65,6 @@ export async function determineInitialParameters({ reset: args.reset, defaultBranch: args.defaultBranch, releaseType: args.backport ? 'backport' : 'ordinary', + fetchRemote: args.fetchRemote, }; } diff --git a/src/main.test.ts b/src/main.test.ts index 485b8ec..0a797b9 100644 --- a/src/main.test.ts +++ b/src/main.test.ts @@ -20,6 +20,7 @@ describe('main', () => { reset: true, defaultBranch: 'main', releaseType: 'backport', + fetchRemote: true, }); const followMonorepoWorkflowSpy = jest .spyOn(monorepoWorkflowOperations, 'followMonorepoWorkflow') @@ -40,6 +41,7 @@ describe('main', () => { defaultBranch: 'main', stdout, stderr, + fetchRemote: true, }); }); @@ -55,6 +57,7 @@ describe('main', () => { reset: false, defaultBranch: 'main', releaseType: 'backport', + fetchRemote: true, }); const followMonorepoWorkflowSpy = jest .spyOn(monorepoWorkflowOperations, 'followMonorepoWorkflow') diff --git a/src/main.ts b/src/main.ts index 1b50f67..c80398c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -25,8 +25,14 @@ export async function main({ stdout: Pick; stderr: Pick; }) { - const { project, tempDirectoryPath, reset, releaseType, defaultBranch } = - await determineInitialParameters({ argv, cwd, stderr }); + const { + project, + tempDirectoryPath, + reset, + releaseType, + defaultBranch, + fetchRemote, + } = await determineInitialParameters({ argv, cwd, stderr }); if (project.isMonorepo) { stdout.write( @@ -40,6 +46,7 @@ export async function main({ defaultBranch, stdout, stderr, + fetchRemote, }); } else { stdout.write( diff --git a/src/monorepo-workflow-operations.ts b/src/monorepo-workflow-operations.ts index ff8a017..6dd02a5 100644 --- a/src/monorepo-workflow-operations.ts +++ b/src/monorepo-workflow-operations.ts @@ -59,6 +59,7 @@ import { * @param args.defaultBranch - The name of the default branch in the repository. * @param args.stdout - A stream that can be used to write to standard out. * @param args.stderr - A stream that can be used to write to standard error. + * @param args.fetchRemote - Whether to synchronize local tags with remote. */ export async function followMonorepoWorkflow({ project, @@ -68,6 +69,7 @@ export async function followMonorepoWorkflow({ defaultBranch, stdout, stderr, + fetchRemote, }: { project: Project; tempDirectoryPath: string; @@ -76,6 +78,7 @@ export async function followMonorepoWorkflow({ defaultBranch: string; stdout: Pick; stderr: Pick; + fetchRemote?: boolean; }) { const { version: newReleaseVersion, firstRun } = await createReleaseBranch({ project, @@ -83,7 +86,7 @@ export async function followMonorepoWorkflow({ }); if (firstRun) { - await updateChangelogsForChangedPackages({ project, stderr }); + await updateChangelogsForChangedPackages({ project, stderr, fetchRemote }); await commitAllChanges( project.directoryPath, `Initialize Release ${newReleaseVersion}`, diff --git a/src/package.ts b/src/package.ts index 15f5f52..6f55c50 100644 --- a/src/package.ts +++ b/src/package.ts @@ -305,16 +305,19 @@ export async function formatChangelog(changelog: string) { * @param args.project - The project. * @param args.package - A particular package in the project. * @param args.stderr - A stream that can be used to write to standard error. + * @param args.fetchRemote - Whether to synchronize local tags with remote. * @returns The result of writing to the changelog. */ export async function updatePackageChangelog({ project: { repositoryUrl }, package: pkg, stderr, + fetchRemote, }: { project: Pick; package: Package; stderr: Pick; + fetchRemote?: boolean | undefined; }): Promise { let changelogContent; @@ -341,6 +344,7 @@ export async function updatePackageChangelog({ repoUrl: repositoryUrl, tagPrefixes: [`${pkg.validatedManifest.name}@`, 'v'], formatter: formatChangelog, + fetchRemote, }); if (newChangelogContent) { diff --git a/src/project.test.ts b/src/project.test.ts index 517a8ea..e26d159 100644 --- a/src/project.test.ts +++ b/src/project.test.ts @@ -63,7 +63,7 @@ describe('project', () => { .calledWith(projectDirectoryPath) .mockResolvedValue(projectRepositoryUrl); when(jest.spyOn(repoModule, 'getTagNames')) - .calledWith(projectDirectoryPath) + .calledWith(projectDirectoryPath, true) .mockResolvedValue(projectTagNames); when(jest.spyOn(packageModule, 'readMonorepoRootPackage')) .calledWith({ @@ -111,7 +111,10 @@ describe('project', () => { ); expect( - await readProject(projectDirectoryPath, { stderr }), + await readProject(projectDirectoryPath, { + stderr, + fetchRemote: true, + }), ).toStrictEqual({ directoryPath: projectDirectoryPath, repositoryUrl: projectRepositoryUrl, diff --git a/src/project.ts b/src/project.ts index ac9ac28..4ee6729 100644 --- a/src/project.ts +++ b/src/project.ts @@ -139,17 +139,20 @@ export async function readProject( * @param args - The arguments. * @param args.project - The project. * @param args.stderr - A stream that can be used to write to standard error. + * @param args.fetchRemote - Whether to synchronize local tags with remote. * @returns The result of writing to the changelog. */ export async function updateChangelogsForChangedPackages({ project, stderr, + fetchRemote, }: { project: Pick< Project, 'directoryPath' | 'repositoryUrl' | 'workspacePackages' >; stderr: Pick; + fetchRemote?: boolean | undefined; }): Promise { await Promise.all( Object.values(project.workspacePackages) @@ -161,6 +164,7 @@ export async function updateChangelogsForChangedPackages({ project, package: pkg, stderr, + fetchRemote, }), ), ); diff --git a/src/repo.test.ts b/src/repo.test.ts index 6754646..0a43c95 100644 --- a/src/repo.test.ts +++ b/src/repo.test.ts @@ -108,7 +108,7 @@ describe('repo', () => { }) .mockResolvedValue(['tag1', 'tag2', 'tag3']); - expect(await getTagNames('/path/to/repo')).toStrictEqual([ + expect(await getTagNames('/path/to/repo', false)).toStrictEqual([ 'tag1', 'tag2', 'tag3',