Skip to content

Commit bc3ce25

Browse files
committed
fix(write-workflow): leveraged the core writer and switched to native fs.mkdir
1 parent 8a4d9a4 commit bc3ce25

11 files changed

Lines changed: 124 additions & 106 deletions

File tree

package-lock.json

Lines changed: 8 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,9 @@
5454
},
5555
"dependencies": {
5656
"@form8ion/core": "^4.0.0",
57-
"@form8ion/github-workflows-core": "^5.0.0",
57+
"@form8ion/github-workflows-core": "^5.1.0",
5858
"@form8ion/javascript-core": "^11.0.0",
5959
"js-yaml": "^4.0.0",
60-
"make-dir": "^4.0.0",
6160
"semver": "^7.3.7"
6261
},
6362
"devDependencies": {

src/lifter/lifter.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {promises as fs} from 'fs';
2-
import {dump, load} from 'js-yaml';
2+
import {load} from 'js-yaml';
3+
import {writeWorkflowFile} from '@form8ion/github-workflows-core';
34

45
import {scaffold as scaffoldBadges} from '../badges/index.js';
56
import {lift as liftJobs} from '../jobs/index.js';
@@ -11,17 +12,18 @@ export default async function ({projectRoot, results: {branchesToVerify}, vcs, r
1112
const {engines} = JSON.parse(await fs.readFile(`${projectRoot}/package.json`, 'utf-8'));
1213
const existingBranches = existingConfig.on.push.branches;
1314

14-
await fs.writeFile(
15-
pathToConfig,
16-
dump({
15+
await writeWorkflowFile({
16+
projectRoot,
17+
name: 'node-ci',
18+
config: {
1719
...existingConfig,
1820
...branchesToVerify && {
1921
on: {...existingConfig.on, push: {branches: mergeBranches(existingBranches, branchesToVerify)}}
2022
},
2123
permissions: {contents: 'read'},
2224
jobs: liftJobs({jobs: existingConfig.jobs, engines, runner})
23-
})
24-
);
25+
}
26+
});
2527

2628
return {badges: scaffoldBadges({vcs})};
2729
}

src/lifter/lifter.test.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {promises as fs} from 'node:fs';
22
import jsYaml from 'js-yaml';
3+
import {writeWorkflowFile} from '@form8ion/github-workflows-core';
34

45
import {afterEach, beforeEach, describe, it, vi, expect} from 'vitest';
56
import any from '@travi/any';
@@ -12,14 +13,14 @@ import lift from './lifter.js';
1213

1314
vi.mock('node:fs');
1415
vi.mock('js-yaml');
16+
vi.mock('@form8ion/github-workflows-core');
1517
vi.mock('./branches/merge-branches');
1618
vi.mock('../jobs/index.js');
1719
vi.mock('../badges');
1820

1921
describe('lifter', () => {
2022
const projectRoot = any.string();
2123
const rawExistingConfig = any.string();
22-
const dumpedConfig = any.string();
2324
const existingJobs = any.listOf(any.simpleObject);
2425
const liftedJobs = any.listOf(any.simpleObject);
2526
const enginesDefinition = any.simpleObject();
@@ -49,12 +50,13 @@ describe('lifter', () => {
4950
jobs: existingJobs
5051
};
5152
when(jsYaml.load).calledWith(rawExistingConfig).mockReturnValue(existingConfig);
52-
when(jsYaml.dump)
53-
.calledWith({...existingConfig, permissions: {contents: 'read'}, jobs: liftedJobs})
54-
.mockReturnValue(dumpedConfig);
5553

5654
expect(await lift({projectRoot, results: any.simpleObject(), vcs, runner})).toEqual({badges: badgesResults});
57-
expect(fs.writeFile).toHaveBeenCalledWith(`${projectRoot}/.github/workflows/node-ci.yml`, dumpedConfig);
55+
expect(writeWorkflowFile).toHaveBeenCalledWith({
56+
projectRoot,
57+
name: 'node-ci',
58+
config: {...existingConfig, permissions: {contents: 'read'}, jobs: liftedJobs}
59+
});
5860
});
5961

6062
it('should append the additional branches to the existing list', async () => {
@@ -68,17 +70,18 @@ describe('lifter', () => {
6870
const mergedBranches = any.listOf(any.word);
6971
when(jsYaml.load).calledWith(rawExistingConfig).mockReturnValue(existingConfig);
7072
when(mergeBranchList).calledWith(existingBranches, branchesToVerify).mockReturnValue(mergedBranches);
71-
when(jsYaml.dump)
72-
.calledWith({
73+
74+
expect(await lift({projectRoot, results: {...any.simpleObject(), branchesToVerify}, vcs, runner}))
75+
.toEqual({badges: badgesResults});
76+
expect(writeWorkflowFile).toHaveBeenCalledWith({
77+
projectRoot,
78+
name: 'node-ci',
79+
config: {
7380
...existingConfig,
7481
on: {...existingConfig.on, push: {branches: mergedBranches}},
7582
permissions: {contents: 'read'},
7683
jobs: liftedJobs
77-
})
78-
.mockReturnValue(dumpedConfig);
79-
80-
expect(await lift({projectRoot, results: {...any.simpleObject(), branchesToVerify}, vcs, runner}))
81-
.toEqual({badges: badgesResults});
82-
expect(fs.writeFile).toHaveBeenCalledWith(`${projectRoot}/.github/workflows/node-ci.yml`, dumpedConfig);
84+
}
85+
});
8386
});
8487
});
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
import {promises as fs} from 'fs';
2-
import {dump} from 'js-yaml';
1+
import {promises as fs} from 'node:fs';
2+
import {writeWorkflowFile} from '@form8ion/github-workflows-core';
33

4-
import makeDir from 'make-dir';
54
import {nvmrcVerification} from '../jobs/scaffolder.js';
65

76
export default async function ({projectRoot, runner}) {
8-
return fs.writeFile(
9-
`${await makeDir(`${projectRoot}/.github/workflows`)}/node-ci.yml`,
10-
dump({
7+
await fs.mkdir(`${projectRoot}/.github/workflows`, {recursive: true});
8+
9+
return writeWorkflowFile({
10+
projectRoot,
11+
name: 'node-ci',
12+
config: {
1113
name: 'Node.js CI',
1214
on: {
1315
push: {branches: ['master']},
@@ -19,6 +21,6 @@ export default async function ({projectRoot, runner}) {
1921
},
2022
permissions: {contents: 'read'},
2123
jobs: {verify: nvmrcVerification({runner})}
22-
})
23-
);
24+
}
25+
});
2426
}
Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
import {promises as fs} from 'fs';
2-
import makeDir from 'make-dir';
3-
import {dump} from 'js-yaml';
1+
import {promises as fs} from 'node:fs';
2+
import {writeWorkflowFile} from '@form8ion/github-workflows-core';
43

54
import any from '@travi/any';
65
import {afterEach, describe, expect, it, vi} from 'vitest';
76
import {when} from 'jest-when';
87

8+
import {nvmrcVerification} from '../jobs/scaffolder.js';
99
import scaffoldConfig from './config-scaffolder.js';
1010

1111
vi.mock('fs');
12-
vi.mock('make-dir');
1312
vi.mock('js-yaml');
13+
vi.mock('@form8ion/github-workflows-core');
14+
vi.mock('../jobs/scaffolder.js');
1415

1516
describe('config scaffolder', () => {
1617
afterEach(() => {
@@ -20,37 +21,29 @@ describe('config scaffolder', () => {
2021
it('should define the workflow file', async () => {
2122
const projectRoot = any.string();
2223
const projectType = any.word();
23-
const pathToCreatedWorkflowsDirectory = any.string();
24-
const dumpedYaml = any.string();
2524
const runner = any.word();
26-
when(makeDir).calledWith(`${projectRoot}/.github/workflows`).mockResolvedValue(pathToCreatedWorkflowsDirectory);
27-
when(dump).calledWith({
28-
name: 'Node.js CI',
29-
on: {
30-
push: {branches: ['master']},
31-
pull_request: {types: ['opened', 'synchronize']}
32-
},
33-
env: {
34-
FORCE_COLOR: 1,
35-
NPM_CONFIG_COLOR: 'always'
36-
},
37-
permissions: {contents: 'read'},
38-
jobs: {
39-
verify: {
40-
'runs-on': runner,
41-
steps: [
42-
{uses: 'actions/checkout@v3'},
43-
{name: 'Setup node', uses: 'actions/setup-node@v3', with: {'node-version-file': '.nvmrc', cache: 'npm'}},
44-
{run: 'npm clean-install'},
45-
{run: 'corepack npm audit signatures'},
46-
{run: 'npm test'}
47-
]
48-
}
49-
}
50-
}).mockReturnValue(dumpedYaml);
25+
const verifyJob = any.simpleObject();
26+
when(nvmrcVerification).calledWith({runner}).mockReturnValue(verifyJob);
5127

5228
await scaffoldConfig({projectRoot, projectType, runner});
5329

54-
expect(fs.writeFile).toHaveBeenCalledWith(`${pathToCreatedWorkflowsDirectory}/node-ci.yml`, dumpedYaml);
30+
expect(fs.mkdir).toHaveBeenCalledWith(`${projectRoot}/.github/workflows`, {recursive: true});
31+
expect(writeWorkflowFile).toHaveBeenCalledWith({
32+
projectRoot,
33+
name: 'node-ci',
34+
config: {
35+
name: 'Node.js CI',
36+
on: {
37+
push: {branches: ['master']},
38+
pull_request: {types: ['opened', 'synchronize']}
39+
},
40+
env: {
41+
FORCE_COLOR: 1,
42+
NPM_CONFIG_COLOR: 'always'
43+
},
44+
permissions: {contents: 'read'},
45+
jobs: {verify: verifyJob}
46+
}
47+
});
5548
});
5649
});

test/integration/features/step_definitions/ci-steps.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {promises as fs} from 'node:fs';
2-
import makeDir from 'make-dir';
3-
import {dump, load} from 'js-yaml';
2+
import {load} from 'js-yaml';
43
import {
54
scaffoldCheckoutStep,
65
scaffoldDependencyInstallationStep,
76
scaffoldNodeSetupStep,
8-
scaffoldVerificationStep
7+
scaffoldVerificationStep,
8+
writeWorkflowFile
99
} from '@form8ion/github-workflows-core';
1010

1111
import {Before, Given, Then} from '@cucumber/cucumber';
@@ -19,18 +19,19 @@ Before(async function () {
1919
});
2020

2121
Given('a CI workflow exists', async function () {
22-
const workflowsDirectory = await makeDir(pathToWorkflowsDirectory);
22+
await fs.mkdir(pathToWorkflowsDirectory, {recursive: true});
2323

24-
await fs.writeFile(
25-
`${workflowsDirectory}/node-ci.yml`,
26-
dump({
24+
await writeWorkflowFile({
25+
projectRoot: this.projectRoot,
26+
name: 'node-ci',
27+
config: {
2728
on: {
2829
push: {branches: this.existingBranches},
2930
pull_request: this.prTriggerConfig
3031
},
3132
jobs: {}
32-
})
33-
);
33+
}
34+
});
3435
});
3536

3637
Then('the ci config remains unchanged', async function () {

test/integration/features/step_definitions/install-steps.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {promises as fs} from 'fs';
2-
import {dump, load} from 'js-yaml';
2+
import {load} from 'js-yaml';
3+
import {writeWorkflowFile} from '@form8ion/github-workflows-core';
34

45
import {Given, Then} from '@cucumber/cucumber';
56
import any from '@travi/any';
@@ -25,7 +26,7 @@ Given('the legacy action is in use for installing dependencies', async function
2526

2627
ciWorkflow.jobs[this.existingJobName] = {steps: this.existingJobSteps};
2728

28-
await fs.writeFile(pathToCiWorkflow, dump(ciWorkflow));
29+
await writeWorkflowFile({projectRoot: this.projectRoot, name: 'node-ci', config: ciWorkflow});
2930
});
3031

3132
Then('the legacy action is replaced with direct installation', async function () {

0 commit comments

Comments
 (0)