Skip to content

Commit 05dc361

Browse files
authored
Add support for gh actions using bahmutov/npm-install (#14)
* feat(yaml-parser): replace bahmutov/npm-install for npm ci if uses cache * test(yaml-parser): add coverage for bahmutov/npm-install use case
1 parent bb97fdb commit 05dc361

File tree

5 files changed

+48
-7
lines changed

5 files changed

+48
-7
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: Node.js CI
2+
"on":
3+
push:
4+
branches:
5+
- master
6+
- "dependabot/**"
7+
pull_request:
8+
types:
9+
- opened
10+
- synchronize
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: bahmutov/npm-install@v1
16+
- run: npm test
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: Node.js CI
2+
"on":
3+
push:
4+
branches:
5+
- master
6+
- "dependabot/**"
7+
pull_request:
8+
types:
9+
- opened
10+
- synchronize
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: bahmutov/npm-install@v1
16+
- run: npm test

tests/utils/github-action-fixtures/pending-to-add-cache/example5/actual.yml renamed to tests/utils/github-action-fixtures/pending-to-add-cache/replace-bahmutov-npm-install/actual.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: Node.js CI
2-
'on':
2+
"on":
33
push:
44
branches:
55
- master

tests/utils/github-action-fixtures/pending-to-add-cache/example5/expected.yml renamed to tests/utils/github-action-fixtures/pending-to-add-cache/replace-bahmutov-npm-install/expected.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ jobs:
1818
with:
1919
node-version: 12.x
2020
cache: npm
21-
- uses: bahmutov/npm-install@v1
21+
- run: npm ci
2222
- run: npm test

utils/yaml-parser.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import prettier from "prettier";
33

44
const { parseDocument } = YAML;
55

6+
const usesBahmutovNpmInstall = (hasCache, stepUses) =>
7+
hasCache && stepUses && stepUses.includes("bahmutov/npm-install");
8+
69
/**
710
* @param {string} cache
811
*
@@ -27,11 +30,15 @@ export function getAddCacheToSetupNodeFunction(cache) {
2730

2831
for (const { value: job } of jobs.items) {
2932
const steps = job.get("steps");
33+
let jobHasCache = false;
3034
for (const step of steps.items) {
3135
const stepUses = step.get("uses");
3236
const stepWith = step.get("with");
3337

34-
if (
38+
if (usesBahmutovNpmInstall(jobHasCache, stepUses)) {
39+
step.set("run", "npm ci");
40+
step.delete("uses")
41+
} else if (
3542
stepUses &&
3643
stepUses.includes("actions/setup-node") &&
3744
(!stepWith || !stepWith.get("cache"))
@@ -47,13 +54,15 @@ export function getAddCacheToSetupNodeFunction(cache) {
4754
}
4855

4956
cacheAdded = true;
57+
jobHasCache = true;
5058
}
5159
}
5260
}
5361

54-
return cacheAdded ? prettier.format(
55-
yamlDocument.toString({lineWidth: 0}), {
56-
parser: 'yaml'
57-
}) : null;
62+
return cacheAdded
63+
? prettier.format(yamlDocument.toString({ lineWidth: 0 }), {
64+
parser: "yaml",
65+
})
66+
: null;
5867
};
5968
}

0 commit comments

Comments
 (0)