Skip to content

Commit

Permalink
feat(semver): add --baseBranch option to install generator
Browse files Browse the repository at this point in the history
  • Loading branch information
edbzn committed Nov 4, 2021
1 parent 3aeea5c commit 0aa8626
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 18 deletions.
44 changes: 36 additions & 8 deletions packages/semver/src/generators/install/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ jest.mock('inquirer');

const defaultOptions: SchemaOptions = {
syncVersions: false,
push: true,
branch: 'main',
remote: 'origin',
enforceConventionalCommits: true,
projects: [],
};
Expand All @@ -27,9 +24,11 @@ describe('Install generator', () => {
jest.spyOn(fs, 'mkdirSync').mockImplementation(() => undefined);
jest
.spyOn(fs, 'writeFileSync')
.mockImplementation((_path: number | PathLike, _content: string | ArrayBufferView) => {
tree.write(_path as string, _content as string);
});
.mockImplementation(
(_path: number | PathLike, _content: string | ArrayBufferView) => {
tree.write(_path as string, _content as string);
}
);
});

describe('Workspace Version 1', () => {
Expand Down Expand Up @@ -114,7 +113,9 @@ describe('Install generator', () => {
references: [],
});

jest.spyOn(inquirer, 'prompt').mockResolvedValue({ projects: ['lib1'] });
jest
.spyOn(inquirer, 'prompt')
.mockResolvedValue({ projects: ['lib1'] });
});

afterEach(() =>
Expand Down Expand Up @@ -165,6 +166,34 @@ describe('Install generator', () => {
);
});

it('should forward --baseBranch option to all projects', async () => {
jest
.spyOn(inquirer, 'prompt')
.mockResolvedValue({ projects: ['lib1', 'lib2'] });

await install(tree, { ...options, baseBranch: 'master' });

const lib1 = readJson(tree, 'libs/lib1/project.json');
const lib2 = readJson(tree, 'libs/lib2/project.json');

expect(lib1.targets).toEqual(
expect.objectContaining({
version: {
executor: '@jscutlery/semver:version',
options: { baseBranch: 'master' },
},
})
);
expect(lib2.targets).toEqual(
expect.objectContaining({
version: {
executor: '@jscutlery/semver:version',
options: { baseBranch: 'master' },
},
})
);
});

it('should not touch nx.json', async () => {
await install(tree, options);

Expand Down Expand Up @@ -299,6 +328,5 @@ describe('Install generator', () => {
expect(packageJson.devDependencies.husky).toBeUndefined();
});
});

});
});
2 changes: 1 addition & 1 deletion packages/semver/src/generators/install/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default async function install(tree: Tree, options: SchemaOptions) {
} else {
options.projects && options.projects.length > 0
? updateWorkspaceFromSchema(tree, options)
: await updateWorkspaceFromPrompt(tree);
: await updateWorkspaceFromPrompt(tree, options);
}

addDependencies(tree, options);
Expand Down
4 changes: 4 additions & 0 deletions packages/semver/src/generators/install/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
"description": "Which projects should be versioned independently",
"type": "array"
},
"baseBranch": {
"description": "Configure base branch",
"type": "string"
},
"enforceConventionalCommits": {
"description": "Install & configure commitlint, husky & commitizen",
"type": "boolean",
Expand Down
4 changes: 1 addition & 3 deletions packages/semver/src/generators/install/schema.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
export interface SchemaOptions {
syncVersions: boolean;
push: boolean;
remote: string;
branch: string;
baseBranch?: string;
projects?: string[];
enforceConventionalCommits?: boolean;
skipInstall?: boolean;
Expand Down
27 changes: 21 additions & 6 deletions packages/semver/src/generators/install/utils/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,36 @@ export function listProjects(tree: Tree): ProjectDefinition[] {

export function updateProjects(
tree: Tree,
options: SchemaOptions,
predicate: (projectName: string) => boolean
) {
getProjects(tree).forEach((project, projectName) => {
if (predicate(projectName)) {
project.targets!.version = {
executor: '@jscutlery/semver:version'

const targets = project.targets ?? {};
targets.version = {
executor: '@jscutlery/semver:version',
};

if (options.baseBranch) {
targets.version.options = {
baseBranch: options.baseBranch
}
}

updateProjectConfiguration(tree, projectName, project);
}
});
}

export async function updateWorkspaceFromPrompt(tree: Tree): Promise<void> {
export async function updateWorkspaceFromPrompt(
tree: Tree,
options: SchemaOptions
): Promise<void> {
const projects = listProjects(tree);
const answers = await createPrompt(projects);

return updateProjects(tree, (projectName) =>
return updateProjects(tree, options, (projectName) =>
answers.projects.includes(projectName)
);
}
Expand All @@ -43,7 +56,9 @@ export function updateWorkspaceFromSchema(
tree: Tree,
options: SchemaOptions
): void {
return updateProjects(tree, (projectName) =>
options.projects?.includes(projectName) as boolean
return updateProjects(
tree,
options,
(projectName) => options.projects?.includes(projectName) as boolean
);
}

0 comments on commit 0aa8626

Please sign in to comment.