Skip to content

Commit

Permalink
fix(semver): ignore enforceConventionalCommits if config not found
Browse files Browse the repository at this point in the history
  • Loading branch information
edbzn committed Nov 18, 2023
1 parent f0ad2b5 commit 029f60e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 17 deletions.
45 changes: 38 additions & 7 deletions packages/semver/src/generators/install/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
readJson,
writeJson,
type Tree,
logger,
} from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import * as inquirer from 'inquirer';
Expand All @@ -17,6 +18,9 @@ const defaultOptions: SchemaOptions = {
syncVersions: false,
enforceConventionalCommits: true,
projects: [],
skipInstall: false,
baseBranch: 'main',
preset: 'conventionalcommits',
};

describe('@jscutlery/semver:install', () => {
Expand Down Expand Up @@ -73,7 +77,7 @@ describe('@jscutlery/semver:install', () => {
const lib1 = findJson(tree, projectName1, 'project.json');
const lib2 = findJson(tree, projectName2, 'project.json');

expect(inquirer.prompt).toBeCalledWith(
expect(inquirer.prompt).toHaveBeenCalledWith(
expect.objectContaining({
name: 'projects',
type: 'checkbox',
Expand All @@ -87,6 +91,7 @@ describe('@jscutlery/semver:install', () => {
expect.objectContaining({
version: {
executor: '@jscutlery/semver:version',
options: expect.toBeObject(),
},
}),
);
Expand All @@ -100,12 +105,13 @@ describe('@jscutlery/semver:install', () => {
const lib1 = findJson(tree, projectName1, 'project.json');
const lib2 = findJson(tree, projectName2, 'project.json');

expect(inquirer.prompt).not.toBeCalled();
expect(inquirer.prompt).not.toHaveBeenCalled();
expect(lib1.targets.version).toBeUndefined();
expect(lib2.targets).toEqual(
expect.objectContaining({
version: {
executor: '@jscutlery/semver:version',
options: expect.toBeObject(),
},
}),
);
Expand Down Expand Up @@ -154,7 +160,10 @@ describe('@jscutlery/semver:install', () => {

describe('--preset option', () => {
it('should install conventional config', async () => {
await install(tree, { ...defaultOptions, preset: 'conventional' });
await install(tree, {
...defaultOptions,
preset: 'conventionalcommits',
});

const packageJson = readJson(tree, 'package.json');
const lib1 = findJson(tree, projectName1, 'project.json');
Expand All @@ -167,7 +176,9 @@ describe('@jscutlery/semver:install', () => {
expect.objectContaining({
version: {
executor: '@jscutlery/semver:version',
options: expect.objectContaining({ preset: 'conventional' }),
options: expect.objectContaining({
preset: 'conventionalcommits',
}),
},
}),
);
Expand All @@ -194,22 +205,27 @@ describe('@jscutlery/semver:install', () => {
});

it('should install angular config', async () => {
await install(tree, { ...defaultOptions, preset: 'conventional' });
await install(tree, {
...defaultOptions,
preset: 'conventionalcommits',
});

const lib1 = findJson(tree, projectName1, 'project.json');

expect(lib1.targets).toEqual(
expect.objectContaining({
version: {
executor: '@jscutlery/semver:version',
options: expect.objectContaining({ preset: 'conventional' }),
options: expect.objectContaining({
preset: 'conventionalcommits',
}),
},
}),
);
});

it('extends conventional commitlint config', async () => {
await install(tree, { ...options, preset: 'conventional' });
await install(tree, { ...options, preset: 'conventionalcommits' });

const commitlintConfig = readJson(tree, '.commitlintrc.json');

Expand Down Expand Up @@ -237,6 +253,21 @@ describe('@jscutlery/semver:install', () => {
preset: 'angular',
};

it('skip if config not found', async () => {
const warnSpy = jest.spyOn(logger, 'warn').mockImplementation();

await install(tree, { ...options, preset: 'atom' });

const packageJson = readJson(tree, 'package.json');
expect(packageJson.devDependencies).not.toContainKeys([
'@commitlint/cli',
'husky',
]);
expect(tree.exists('.commitlintrc.json')).toBeFalse();
expect(tree.exists('.husky')).toBeFalse();
expect(warnSpy).toHaveBeenCalled();
});

it('add commitlint to package.json devDepencencies', async () => {
await install(tree, options);

Expand Down
8 changes: 4 additions & 4 deletions packages/semver/src/generators/install/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import type { Preset } from '../../executors/common/preset';

export interface SchemaOptions {
syncVersions: boolean;
baseBranch?: string;
baseBranch: string;
projects?: string[];
enforceConventionalCommits?: boolean;
skipInstall?: boolean;
enforceConventionalCommits: boolean;
skipInstall: boolean;
commitMessageFormat?: string;
preset?: Preset;
preset: Preset;
}
29 changes: 23 additions & 6 deletions packages/semver/src/generators/install/utils/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
readJson,
updateJson,
type Tree,
logger,
} from '@nx/devkit';
import { constants } from 'fs';
import type { SchemaOptions } from '../schema';
Expand All @@ -21,6 +22,15 @@ export interface PackageJsonPart<T> {

export function addDependencies(tree: Tree, options: SchemaOptions) {
if (options.enforceConventionalCommits) {
const preset = _getCommitlintConfig(options);

if (preset === null) {
logger.warn(
`No commitlint config found for ${options.preset} preset, --enforceConventionalCommits option ignored.`,
);
return tree;
}

_addCommitlintConfig(tree, options);
_addHuskyConfig(tree);
_addHuskyConfigMsg(tree);
Expand All @@ -35,7 +45,8 @@ function _addDevDependencies(tree: Tree, options: SchemaOptions) {
{},
{
'@commitlint/cli': '^17.0.0',
[_getCommitlintConfig(options)]: '^17.0.0',
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
[_getCommitlintConfig(options)!]: '^17.0.0',
husky: '^8.0.0',
},
);
Expand All @@ -58,7 +69,8 @@ function _addCommitlintConfig(tree: Tree, options: SchemaOptions) {
'.commitlintrc.json',
JSON.stringify(
{
extends: [_getCommitlintConfig(options)],
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
extends: [_getCommitlintConfig(options)!],
rules: {},
},
null,
Expand Down Expand Up @@ -98,8 +110,13 @@ function _addHuskyConfigMsg(tree: Tree) {
}
}

function _getCommitlintConfig(options: SchemaOptions) {
return options.preset === 'angular'
? '@commitlint/config-angular'
: '@commitlint/config-conventional';
function _getCommitlintConfig(options: SchemaOptions): string | null {
switch (options.preset) {
case 'angular':
return '@commitlint/config-angular';
case 'conventionalcommits':
return '@commitlint/config-conventional';
default:
return null;
}
}

0 comments on commit 029f60e

Please sign in to comment.