Skip to content

Commit

Permalink
feat: allow running tool in offline mode by setting --fetch-remote=false
Browse files Browse the repository at this point in the history
  • Loading branch information
legobeat committed Aug 13, 2024
1 parent 2e76a77 commit a3504fc
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 13 deletions.
24 changes: 17 additions & 7 deletions src/initial-parameters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -56,6 +57,7 @@ describe('initial-parameters', () => {
reset: true,
releaseType: 'ordinary',
defaultBranch: 'main',
fetchRemote: true,
});
});

Expand All @@ -72,6 +74,7 @@ describe('initial-parameters', () => {
reset: true,
backport: false,
defaultBranch: 'main',
fetchRemote: true,
});
jest
.spyOn(envModule, 'getEnvironmentVariables')
Expand All @@ -88,6 +91,7 @@ describe('initial-parameters', () => {

expect(readProjectSpy).toHaveBeenCalledWith('/path/to/cwd/project', {
stderr,
fetchRemote: true,
});
});

Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -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({
Expand Down
2 changes: 2 additions & 0 deletions src/initial-parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type InitialParameters = {
reset: boolean;
releaseType: ReleaseType;
defaultBranch: string;
fetchRemote: boolean;
};

/**
Expand Down Expand Up @@ -64,5 +65,6 @@ export async function determineInitialParameters({
reset: args.reset,
defaultBranch: args.defaultBranch,
releaseType: args.backport ? 'backport' : 'ordinary',
fetchRemote: args.fetchRemote,
};
}
3 changes: 3 additions & 0 deletions src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('main', () => {
reset: true,
defaultBranch: 'main',
releaseType: 'backport',
fetchRemote: true,
});
const followMonorepoWorkflowSpy = jest
.spyOn(monorepoWorkflowOperations, 'followMonorepoWorkflow')
Expand All @@ -40,6 +41,7 @@ describe('main', () => {
defaultBranch: 'main',
stdout,
stderr,
fetchRemote: true,
});
});

Expand All @@ -55,6 +57,7 @@ describe('main', () => {
reset: false,
defaultBranch: 'main',
releaseType: 'backport',
fetchRemote: true,
});
const followMonorepoWorkflowSpy = jest
.spyOn(monorepoWorkflowOperations, 'followMonorepoWorkflow')
Expand Down
11 changes: 9 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@ export async function main({
stdout: Pick<WriteStream, 'write'>;
stderr: Pick<WriteStream, 'write'>;
}) {
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(
Expand All @@ -40,6 +46,7 @@ export async function main({
defaultBranch,
stdout,
stderr,
fetchRemote,
});
} else {
stdout.write(
Expand Down
5 changes: 4 additions & 1 deletion src/monorepo-workflow-operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -68,6 +69,7 @@ export async function followMonorepoWorkflow({
defaultBranch,
stdout,
stderr,
fetchRemote,
}: {
project: Project;
tempDirectoryPath: string;
Expand All @@ -76,14 +78,15 @@ export async function followMonorepoWorkflow({
defaultBranch: string;
stdout: Pick<WriteStream, 'write'>;
stderr: Pick<WriteStream, 'write'>;
fetchRemote?: boolean;
}) {
const { version: newReleaseVersion, firstRun } = await createReleaseBranch({
project,
releaseType,
});

if (firstRun) {
await updateChangelogsForChangedPackages({ project, stderr });
await updateChangelogsForChangedPackages({ project, stderr, fetchRemote });
await commitAllChanges(
project.directoryPath,
`Initialize Release ${newReleaseVersion}`,
Expand Down
4 changes: 4 additions & 0 deletions src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,16 +301,19 @@ export 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<Project, 'directoryPath' | 'repositoryUrl'>;
package: Package;
stderr: Pick<WriteStream, 'write'>;
fetchRemote?: boolean | undefined;
}): Promise<void> {
let changelogContent;

Expand All @@ -337,6 +340,7 @@ export async function updatePackageChangelog({
repoUrl: repositoryUrl,
tagPrefixes: [`${pkg.validatedManifest.name}@`, 'v'],
formatter: formatChangelog,
fetchRemote,

Check failure on line 343 in src/package.ts

View workflow job for this annotation

GitHub Actions / Build (16.x)

Argument of type '{ changelogContent: string; isReleaseCandidate: false; projectRootDirectory: string; repoUrl: string; tagPrefixes: [string, string]; formatter: (changelog: string) => string; fetchRemote: boolean | undefined; }' is not assignable to parameter of type 'UpdateChangelogOptions'.

Check failure on line 343 in src/package.ts

View workflow job for this annotation

GitHub Actions / Build (18.x)

Argument of type '{ changelogContent: string; isReleaseCandidate: false; projectRootDirectory: string; repoUrl: string; tagPrefixes: [string, string]; formatter: (changelog: string) => string; fetchRemote: boolean | undefined; }' is not assignable to parameter of type 'UpdateChangelogOptions'.

Check failure on line 343 in src/package.ts

View workflow job for this annotation

GitHub Actions / Build (20.x)

Argument of type '{ changelogContent: string; isReleaseCandidate: false; projectRootDirectory: string; repoUrl: string; tagPrefixes: [string, string]; formatter: (changelog: string) => string; fetchRemote: boolean | undefined; }' is not assignable to parameter of type 'UpdateChangelogOptions'.
});

if (newChangelogContent) {
Expand Down
7 changes: 5 additions & 2 deletions src/project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -111,7 +111,10 @@ describe('project', () => {
);

expect(
await readProject(projectDirectoryPath, { stderr }),
await readProject(projectDirectoryPath, {
stderr,
fetchRemote: true,
}),
).toStrictEqual({
directoryPath: projectDirectoryPath,
repositoryUrl: projectRepositoryUrl,
Expand Down
4 changes: 4 additions & 0 deletions src/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,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<WriteStream, 'write'>;
fetchRemote?: boolean | undefined;
}): Promise<void> {
await Promise.all(
Object.values(project.workspacePackages)
Expand All @@ -158,6 +161,7 @@ export async function updateChangelogsForChangedPackages({
project,
package: pkg,
stderr,
fetchRemote,
}),
),
);
Expand Down
2 changes: 1 addition & 1 deletion src/repo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit a3504fc

Please sign in to comment.