Skip to content

Commit e30fa56

Browse files
committed
feat: enable reading githubActions from fs
1 parent 530fb76 commit e30fa56

File tree

2 files changed

+46
-34
lines changed

2 files changed

+46
-34
lines changed

Diff for: lib/github-actions/index.js

+44-31
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,54 @@
22

33
const Nv = require('@pkgjs/nv');
44

5+
6+
const internals = {};
7+
8+
9+
internals.parseActionsSetupNode = function * (workflow) {
10+
11+
for (const job of Object.values(workflow.jobs)) {
12+
13+
const nodeSteps = job.steps.filter(({ uses }) => uses && uses.startsWith('actions/setup-node'));
14+
for (const step of nodeSteps) {
15+
const nodeVersion = step.with && step.with['node-version'];
16+
17+
if (!nodeVersion) {
18+
// Docs say: "The node-version input is optional. If not supplied, the node version that is PATH will be used."
19+
// Therefore we cannot reliably detect a specific version, but we do want to let the user know
20+
yield 'not-set';
21+
continue;
22+
}
23+
24+
const matrixMatch = nodeVersion.match(/^\${{\s+matrix.(?<matrixVarName>.*)\s+}}$/);
25+
if (matrixMatch) {
26+
const matrix = job.strategy.matrix[matrixMatch.groups.matrixVarName];
27+
28+
yield * matrix;
29+
continue;
30+
}
31+
32+
const envMatch = nodeVersion.match(/^\${{\s+env.(?<envVarName>.*)\s+}}$/);
33+
if (envMatch) {
34+
const envValue = workflow.env[envMatch.groups.envVarName];
35+
36+
yield envValue;
37+
continue;
38+
}
39+
40+
yield nodeVersion;
41+
}
42+
}
43+
};
44+
45+
546
exports.detect = async (meta) => {
647

748
const files = await meta.loadFolder('.github/workflows');
849
const rawSet = new Set();
950

1051
if (!files.length) {
52+
// explicitly return no `githubActions` - this is different to finding actions and detecting no Node.js versions
1153
return;
1254
}
1355

@@ -19,37 +61,8 @@ exports.detect = async (meta) => {
1961

2062
const workflow = await meta.loadFile(`.github/workflows/${file}`, { yaml: true });
2163

22-
for (const job of Object.values(workflow.jobs)) {
23-
24-
const nodeSteps = job.steps.filter(({ uses }) => uses && uses.startsWith('actions/setup-node'));
25-
for (const step of nodeSteps) {
26-
const nodeVersion = step.with && step.with['node-version'];
27-
28-
if (!nodeVersion) {
29-
// @todo - no node version defined - use default? what is the default?
30-
continue;
31-
}
32-
33-
const matrixMatch = nodeVersion.match(/^\${{\s+matrix.(?<matrixVarName>.*)\s+}}$/);
34-
if (matrixMatch) {
35-
const matrix = job.strategy.matrix[matrixMatch.groups.matrixVarName];
36-
37-
for (const version of matrix) {
38-
rawSet.add(version);
39-
}
40-
41-
continue;
42-
}
43-
44-
const envMatch = nodeVersion.match(/^\${{\s+env.(?<envVarName>.*)\s+}}$/);
45-
if (envMatch) {
46-
rawSet.add(workflow.env[envMatch.groups.envVarName]);
47-
48-
continue;
49-
}
50-
51-
rawSet.add(nodeVersion);
52-
}
64+
for (const version of internals.parseActionsSetupNode(workflow)) {
65+
rawSet.add(version);
5366
}
5467
}
5568

Diff for: lib/loader/path.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ exports.create = async (path) => {
2525

2626
return simpleGit.revparse(['HEAD']);
2727
},
28-
loadFolder: () => {
28+
loadFolder: (path) => {
2929

30-
// @todo
31-
return [];
30+
return Fs.readdirSync(path);
3231
},
3332
loadFile: (filename, options = {}) => {
3433

0 commit comments

Comments
 (0)