diff --git a/packages/semver/src/generators/install/index.spec.ts b/packages/semver/src/generators/install/index.spec.ts index 69f0f0316..9b51855c6 100644 --- a/packages/semver/src/generators/install/index.spec.ts +++ b/packages/semver/src/generators/install/index.spec.ts @@ -13,9 +13,6 @@ jest.mock('inquirer'); const defaultOptions: SchemaOptions = { syncVersions: false, - push: true, - branch: 'main', - remote: 'origin', enforceConventionalCommits: true, projects: [], }; @@ -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', () => { @@ -114,7 +113,9 @@ describe('Install generator', () => { references: [], }); - jest.spyOn(inquirer, 'prompt').mockResolvedValue({ projects: ['lib1'] }); + jest + .spyOn(inquirer, 'prompt') + .mockResolvedValue({ projects: ['lib1'] }); }); afterEach(() => @@ -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); @@ -299,6 +328,5 @@ describe('Install generator', () => { expect(packageJson.devDependencies.husky).toBeUndefined(); }); }); - }); }); diff --git a/packages/semver/src/generators/install/index.ts b/packages/semver/src/generators/install/index.ts index 7088b22e3..53ec8edfc 100644 --- a/packages/semver/src/generators/install/index.ts +++ b/packages/semver/src/generators/install/index.ts @@ -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); diff --git a/packages/semver/src/generators/install/schema.json b/packages/semver/src/generators/install/schema.json index 183e46e37..260257c24 100644 --- a/packages/semver/src/generators/install/schema.json +++ b/packages/semver/src/generators/install/schema.json @@ -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", diff --git a/packages/semver/src/generators/install/schema.ts b/packages/semver/src/generators/install/schema.ts index 3d7ce0d74..2d190dd25 100644 --- a/packages/semver/src/generators/install/schema.ts +++ b/packages/semver/src/generators/install/schema.ts @@ -1,8 +1,6 @@ export interface SchemaOptions { syncVersions: boolean; - push: boolean; - remote: string; - branch: string; + baseBranch?: string; projects?: string[]; enforceConventionalCommits?: boolean; skipInstall?: boolean; diff --git a/packages/semver/src/generators/install/utils/workspace.ts b/packages/semver/src/generators/install/utils/workspace.ts index 15ebd62be..01e69b1b6 100644 --- a/packages/semver/src/generators/install/utils/workspace.ts +++ b/packages/semver/src/generators/install/utils/workspace.ts @@ -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 { +export async function updateWorkspaceFromPrompt( + tree: Tree, + options: SchemaOptions +): Promise { const projects = listProjects(tree); const answers = await createPrompt(projects); - return updateProjects(tree, (projectName) => + return updateProjects(tree, options, (projectName) => answers.projects.includes(projectName) ); } @@ -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 ); }