Skip to content

Commit 3af17e0

Browse files
authored
feat: add npm-shrinkwrap.json file support (#59)
1 parent d0c6b31 commit 3af17e0

File tree

8 files changed

+230
-22
lines changed

8 files changed

+230
-22
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: example-shrinkwrap
2+
on: [push, pull_request]
3+
jobs:
4+
test:
5+
runs-on: ubuntu-latest
6+
steps:
7+
- uses: actions/checkout@v1
8+
- uses: bahmutov/npm-install@HEAD
9+
with:
10+
working-directory: examples/shrinkwrap
11+
- run: npm t
12+
working-directory: examples/shrinkwrap

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Example | Status
99
--- | ---
1010
[main](.github/workflows/main.yml) | ![this repo](https://github.com/bahmutov/npm-install/workflows/main/badge.svg?branch=master)
1111
[basic](.github/workflows/example-basic.yml) | ![basic example](https://github.com/bahmutov/npm-install/workflows/example-basic/badge.svg?branch=master)
12+
[shrinkwrap](.github/workflows/example-shrinkwrap.yml) | ![shrinkwrap example](https://github.com/bahmutov/npm-install/workflows/example-shrinkwrap/badge.svg?branch=master)
1213
[Yarn](.github/workflows/example-yarn.yml) | ![yarn example](https://github.com/bahmutov/npm-install/workflows/example-yarn/badge.svg?branch=master)
1314
[without lock file](.github/workflows/example-without-lock-file.yml) | ![without lockfile example](https://github.com/bahmutov/npm-install/workflows/example-without-lock-file/badge.svg?branch=master)
1415
[subfolders](.github/workflows/example-subfolders.yml) | ![subfolders example](https://github.com/bahmutov/npm-install/workflows/example-subfolders/badge.svg?branch=master)
@@ -98,7 +99,7 @@ jobs:
9899
99100
### Use lock file
100101
101-
By default, this action will use a lock file like `package-lock.json` or `yarn.lock`. You can set `useLockFile: false` to use just `package.json` which might be better for [building libraries](https://twitter.com/mikeal/status/1202298796274700288).
102+
By default, this action will use a lock file like `package-lock.json`, `npm-shrinkwrap.json` or `yarn.lock`. You can set `useLockFile: false` to use just `package.json` which might be better for [building libraries](https://twitter.com/mikeal/status/1202298796274700288).
102103

103104
```yml
104105
- uses: bahmutov/npm-install@v1

dist/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2927,11 +2927,19 @@ const getLockFilename = usePackageLock => workingDirectory => {
29272927
const useYarn = fs.existsSync(yarnFilename)
29282928
core.debug(`yarn lock file "${yarnFilename}" exists? ${useYarn}`)
29292929

2930+
const npmShrinkwrapFilename = path.join(
2931+
workingDirectory,
2932+
'npm-shrinkwrap.json'
2933+
)
29302934
const packageLockFilename = path.join(workingDirectory, 'package-lock.json')
2935+
const npmFilename =
2936+
!useYarn && fs.existsSync(npmShrinkwrapFilename)
2937+
? npmShrinkwrapFilename
2938+
: packageLockFilename
29312939

29322940
const result = {
29332941
useYarn,
2934-
lockFilename: useYarn ? yarnFilename : packageLockFilename
2942+
lockFilename: useYarn ? yarnFilename : npmFilename
29352943
}
29362944
return result
29372945
}

examples/shrinkwrap/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const chalk = require('chalk')
2+
console.log(chalk.cyanBright('chalk is working!'))

examples/shrinkwrap/npm-shrinkwrap.json

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

examples/shrinkwrap/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "example-shrinkwrap",
3+
"private": true,
4+
"version": "1.0.0",
5+
"description": "npm-install with npm-shrinkwrap.json example",
6+
"main": "index.js",
7+
"scripts": {
8+
"test": "node ."
9+
},
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"chalk": "3.0.0"
14+
}
15+
}

index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,19 @@ const getLockFilename = usePackageLock => workingDirectory => {
125125
const useYarn = fs.existsSync(yarnFilename)
126126
core.debug(`yarn lock file "${yarnFilename}" exists? ${useYarn}`)
127127

128+
const npmShrinkwrapFilename = path.join(
129+
workingDirectory,
130+
'npm-shrinkwrap.json'
131+
)
128132
const packageLockFilename = path.join(workingDirectory, 'package-lock.json')
133+
const npmFilename =
134+
!useYarn && fs.existsSync(npmShrinkwrapFilename)
135+
? npmShrinkwrapFilename
136+
: packageLockFilename
129137

130138
const result = {
131139
useYarn,
132-
lockFilename: useYarn ? yarnFilename : packageLockFilename
140+
lockFilename: useYarn ? yarnFilename : npmFilename
133141
}
134142
return result
135143
}

test/action-spec.js

Lines changed: 65 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ describe('action', () => {
7979

8080
context('does not find Yarn', function() {
8181
const yarnFilename = path.join(cwd, 'yarn.lock')
82+
const npmShrinkwrapFilename = path.join(cwd, 'npm-shrinkwrap.json')
8283
const packageLockFilename = path.join(cwd, 'package-lock.json')
8384
const npmCachePaths = [path.join(homedir, '.npm')]
8485
const pathToNpm = '/path/to/npm'
85-
const cacheKey = 'npm-platform-arch-hash-from-package-lock-file'
8686

8787
beforeEach(function() {
8888
sandbox
@@ -100,31 +100,77 @@ describe('action', () => {
100100
.withArgs('npm')
101101
.resolves(pathToNpm)
102102

103-
sandbox
104-
.stub(hasha, 'fromFileSync')
105-
.withArgs(packageLockFilename)
106-
.returns('hash-from-package-lock-file')
107-
108103
const cacheHit = false
109104
this.restoreCache = sandbox.stub(cache, 'restoreCache').resolves(cacheHit)
110105
this.saveCache = sandbox.stub(cache, 'saveCache').resolves()
111106
})
112107

113-
it('uses package lock and NPM', async function() {
114-
await action.npmInstallAction()
108+
context('finds npm-shrinkwrap.json', async function() {
109+
const cacheKey = 'npm-platform-arch-hash-from-npm-shrinkwrap-file'
115110

116-
expect(this.restoreCache).to.be.calledOnceWithExactly(
117-
npmCachePaths,
118-
cacheKey,
119-
[cacheKey]
120-
)
121-
expect(this.exec).to.be.calledOnceWithExactly(quote(pathToNpm), ['ci'], {
122-
cwd
111+
beforeEach(function() {
112+
fs.existsSync.withArgs(npmShrinkwrapFilename).returns(true)
113+
114+
sandbox
115+
.stub(hasha, 'fromFileSync')
116+
.withArgs(npmShrinkwrapFilename)
117+
.returns('hash-from-npm-shrinkwrap-file')
118+
})
119+
120+
it('uses npm-shrinkwrap.json and NPM', async function() {
121+
await action.npmInstallAction()
122+
123+
expect(this.restoreCache).to.be.calledOnceWithExactly(
124+
npmCachePaths,
125+
cacheKey,
126+
[cacheKey]
127+
)
128+
expect(this.exec).to.be.calledOnceWithExactly(
129+
quote(pathToNpm),
130+
['ci'],
131+
{
132+
cwd
133+
}
134+
)
135+
expect(this.saveCache).to.be.calledOnceWithExactly(
136+
npmCachePaths,
137+
cacheKey
138+
)
139+
})
140+
})
141+
142+
context('finds package-lock.json', async function() {
143+
const cacheKey = 'npm-platform-arch-hash-from-package-lock-file'
144+
145+
beforeEach(function() {
146+
fs.existsSync.withArgs(npmShrinkwrapFilename).returns(false)
147+
148+
sandbox
149+
.stub(hasha, 'fromFileSync')
150+
.withArgs(packageLockFilename)
151+
.returns('hash-from-package-lock-file')
152+
})
153+
154+
it('uses package-lock.json and NPM', async function() {
155+
await action.npmInstallAction()
156+
157+
expect(this.restoreCache).to.be.calledOnceWithExactly(
158+
npmCachePaths,
159+
cacheKey,
160+
[cacheKey]
161+
)
162+
expect(this.exec).to.be.calledOnceWithExactly(
163+
quote(pathToNpm),
164+
['ci'],
165+
{
166+
cwd
167+
}
168+
)
169+
expect(this.saveCache).to.be.calledOnceWithExactly(
170+
npmCachePaths,
171+
cacheKey
172+
)
123173
})
124-
expect(this.saveCache).to.be.calledOnceWithExactly(
125-
npmCachePaths,
126-
cacheKey
127-
)
128174
})
129175
})
130176

0 commit comments

Comments
 (0)