Skip to content

Commit 830c1d3

Browse files
committedOct 20, 2024
feat(commitlint): enable migrating js config to json
1 parent ff42f74 commit 830c1d3

File tree

8 files changed

+82
-13
lines changed

8 files changed

+82
-13
lines changed
 

‎src/commitlint/lifter.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1-
export default function () {
1+
import {promises as fs} from 'node:fs';
2+
3+
import {fileExists} from '@form8ion/core';
4+
5+
import scaffoldCommitlint from './scaffolder.js';
6+
7+
export default async function ({projectRoot, configs}) {
8+
if (await fileExists(`${projectRoot}/.commitlintrc.js`)) {
9+
await fs.unlink(`${projectRoot}/.commitlintrc.js`);
10+
11+
return scaffoldCommitlint({projectRoot, config: configs.commitlint});
12+
}
13+
14+
if (await fileExists(`${projectRoot}/.commitlintrc.cjs`)) {
15+
await fs.unlink(`${projectRoot}/.commitlintrc.cjs`);
16+
17+
return scaffoldCommitlint({projectRoot, config: configs.commitlint});
18+
}
19+
220
return {};
321
}

‎src/commitlint/lifter.test.js

+42-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,49 @@
1-
import {describe, expect, it} from 'vitest';
1+
import {promises as fs} from 'node:fs';
2+
import {fileExists} from '@form8ion/core';
23

4+
import {describe, expect, it, vi} from 'vitest';
5+
import any from '@travi/any';
6+
import {when} from 'jest-when';
7+
8+
import scaffoldCommitlint from './scaffolder.js';
39
import liftCommitlint from './lifter.js';
410

11+
vi.mock('node:fs');
12+
vi.mock('@form8ion/core');
13+
vi.mock('./scaffolder.js');
14+
515
describe('commitlint lifter', () => {
16+
const projectRoot = any.string();
17+
const commitlintConfig = any.simpleObject();
18+
const configs = {commitlint: commitlintConfig};
19+
620
it('should return an empty result when config does not need the format to be changed', async () => {
7-
expect(await liftCommitlint()).toEqual({});
21+
when(fileExists).calledWith(`${projectRoot}/.commitlintrc.js`).mockResolvedValue(false);
22+
when(fileExists).calledWith(`${projectRoot}/.commitlintrc.cjs`).mockResolvedValue(false);
23+
24+
expect(await liftCommitlint({projectRoot, configs})).toEqual({});
25+
26+
expect(scaffoldCommitlint).not.toHaveBeenCalled();
27+
expect(fs.unlink).not.toHaveBeenCalled();
28+
});
29+
30+
it('should migrate the config to json format if currently using js format', async () => {
31+
const scaffoldResult = any.simpleObject();
32+
when(fileExists).calledWith(`${projectRoot}/.commitlintrc.js`).mockResolvedValue(true);
33+
when(fileExists).calledWith(`${projectRoot}/.commitlintrc.cjs`).mockResolvedValue(false);
34+
when(scaffoldCommitlint).calledWith({projectRoot, config: commitlintConfig}).mockResolvedValue(scaffoldResult);
35+
36+
expect(await liftCommitlint({projectRoot, configs})).toEqual(scaffoldResult);
37+
expect(fs.unlink).toHaveBeenCalledWith(`${projectRoot}/.commitlintrc.js`);
38+
});
39+
40+
it('should migrate the config to json format if currently using cjs format', async () => {
41+
const scaffoldResult = any.simpleObject();
42+
when(fileExists).calledWith(`${projectRoot}/.commitlintrc.js`).mockResolvedValue(false);
43+
when(fileExists).calledWith(`${projectRoot}/.commitlintrc.cjs`).mockResolvedValue(true);
44+
when(scaffoldCommitlint).calledWith({projectRoot, config: commitlintConfig}).mockResolvedValue(scaffoldResult);
45+
46+
expect(await liftCommitlint({projectRoot, configs})).toEqual(scaffoldResult);
47+
expect(fs.unlink).toHaveBeenCalledWith(`${projectRoot}/.commitlintrc.cjs`);
848
});
949
});

‎src/lifter.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import {applyEnhancers} from '@form8ion/core';
33
import * as semanticReleasePlugin from './semantic-release/index.js';
44
import * as commitlintPlugin from './commitlint/index.js';
55

6-
export default async function ({projectRoot}) {
6+
export default async function ({projectRoot, configs}) {
77
return applyEnhancers({
8-
options: {projectRoot},
8+
options: {projectRoot, configs},
99
enhancers: {
1010
'semantic-release': semanticReleasePlugin,
1111
commitlint: commitlintPlugin

‎src/lifter.test.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,18 @@ vi.mock('@form8ion/core');
1313
describe('lifter', () => {
1414
it('should apply the enhancers', async () => {
1515
const projectRoot = any.string();
16+
const configs = any.simpleObject();
1617
const enhancerResults = any.simpleObject();
1718
when(applyEnhancers)
1819
.calledWith({
19-
options: {projectRoot},
20+
options: {projectRoot, configs},
2021
enhancers: {
2122
'semantic-release': semanticReleasePlugin,
2223
commitlint: commitlintPlugin
2324
}
2425
})
2526
.mockResolvedValue(enhancerResults);
2627

27-
expect(await lift({projectRoot})).toEqual(enhancerResults);
28+
expect(await lift({projectRoot, configs})).toEqual(enhancerResults);
2829
});
2930
});

‎src/tester-test.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import any from '@travi/any';
33
import sinon from 'sinon';
44

55
import * as semanticReleaseTester from './semantic-release/tester.js';
6+
import * as commitlintTester from './commitlint/tester.js';
67
import testForCommitConvention from './tester.js';
78

89
suite('predicate', () => {
@@ -14,18 +15,28 @@ suite('predicate', () => {
1415
sandbox = sinon.createSandbox();
1516

1617
sandbox.stub(semanticReleaseTester, 'default');
18+
sandbox.stub(commitlintTester, 'default');
1719
});
1820

1921
teardown(() => sandbox.restore());
2022

2123
test('that `true` is returned when semantic-release use is detected', async () => {
2224
semanticReleaseTester.default.withArgs({projectRoot}).resolves(true);
25+
commitlintTester.default.withArgs({projectRoot}).resolves(false);
2326

2427
assert.isTrue(await testForCommitConvention({projectRoot}));
2528
});
2629

27-
test('that `false` is returned when semantic-release use is not detected', async () => {
30+
test('that `true` is returned when commitlint use is detected', async () => {
2831
semanticReleaseTester.default.withArgs({projectRoot}).resolves(false);
32+
commitlintTester.default.withArgs({projectRoot}).resolves(true);
33+
34+
assert.isTrue(await testForCommitConvention({projectRoot}));
35+
});
36+
37+
test('that `false` is returned when neither semantic-release nor commitlint use is detected', async () => {
38+
semanticReleaseTester.default.withArgs({projectRoot}).resolves(false);
39+
commitlintTester.default.withArgs({projectRoot}).resolves(false);
2940

3041
assert.isFalse(await testForCommitConvention({projectRoot}));
3142
});

‎src/tester.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {test as semanticReleaseIsInUse} from './semantic-release/index.js';
2+
import {test as commitlintIsInUse} from './commitlint/index.js';
23

3-
export default function ({projectRoot}) {
4-
return semanticReleaseIsInUse({projectRoot});
4+
export default async function ({projectRoot}) {
5+
return await semanticReleaseIsInUse({projectRoot}) || commitlintIsInUse({projectRoot});
56
}

‎test/integration/features/lift/commitlint.feature

-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
Feature: Lift Commitlint
22

3-
@wip
43
Scenario: js config exists
54
Given commitlint is configured with a "js" extension
65
When the project is lifted
76
Then commitlint will be configured
87
But other commitlint config formats do not exist
98

10-
@wip
119
Scenario: cjs config exists
1210
Given commitlint is configured with a "cjs" extension
1311
When the project is lifted

‎test/integration/features/step_definitions/common-steps.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ When('the project is lifted', async function () {
103103
});
104104

105105
if (await test({projectRoot: this.projectRoot})) {
106-
await lift({projectRoot: this.projectRoot});
106+
await lift({projectRoot: this.projectRoot, configs: {commitlint: {name: this.commilintConfigName}}});
107107
}
108108
});
109109

0 commit comments

Comments
 (0)