Skip to content

Commit

Permalink
feat: add npm-shrinkwrap.json file support (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
thinca authored Dec 7, 2020
1 parent d0c6b31 commit 3af17e0
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 22 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/example-shrinkwrap.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: example-shrinkwrap
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: bahmutov/npm-install@HEAD
with:
working-directory: examples/shrinkwrap
- run: npm t
working-directory: examples/shrinkwrap
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Example | Status
--- | ---
[main](.github/workflows/main.yml) | ![this repo](https://github.com/bahmutov/npm-install/workflows/main/badge.svg?branch=master)
[basic](.github/workflows/example-basic.yml) | ![basic example](https://github.com/bahmutov/npm-install/workflows/example-basic/badge.svg?branch=master)
[shrinkwrap](.github/workflows/example-shrinkwrap.yml) | ![shrinkwrap example](https://github.com/bahmutov/npm-install/workflows/example-shrinkwrap/badge.svg?branch=master)
[Yarn](.github/workflows/example-yarn.yml) | ![yarn example](https://github.com/bahmutov/npm-install/workflows/example-yarn/badge.svg?branch=master)
[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)
[subfolders](.github/workflows/example-subfolders.yml) | ![subfolders example](https://github.com/bahmutov/npm-install/workflows/example-subfolders/badge.svg?branch=master)
Expand Down Expand Up @@ -98,7 +99,7 @@ jobs:
### Use lock file
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).
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).

```yml
- uses: bahmutov/npm-install@v1
Expand Down
10 changes: 9 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2927,11 +2927,19 @@ const getLockFilename = usePackageLock => workingDirectory => {
const useYarn = fs.existsSync(yarnFilename)
core.debug(`yarn lock file "${yarnFilename}" exists? ${useYarn}`)

const npmShrinkwrapFilename = path.join(
workingDirectory,
'npm-shrinkwrap.json'
)
const packageLockFilename = path.join(workingDirectory, 'package-lock.json')
const npmFilename =
!useYarn && fs.existsSync(npmShrinkwrapFilename)
? npmShrinkwrapFilename
: packageLockFilename

const result = {
useYarn,
lockFilename: useYarn ? yarnFilename : packageLockFilename
lockFilename: useYarn ? yarnFilename : npmFilename
}
return result
}
Expand Down
2 changes: 2 additions & 0 deletions examples/shrinkwrap/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const chalk = require('chalk')
console.log(chalk.cyanBright('chalk is working!'))
116 changes: 116 additions & 0 deletions examples/shrinkwrap/npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions examples/shrinkwrap/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "example-shrinkwrap",
"private": true,
"version": "1.0.0",
"description": "npm-install with npm-shrinkwrap.json example",
"main": "index.js",
"scripts": {
"test": "node ."
},
"author": "",
"license": "ISC",
"dependencies": {
"chalk": "3.0.0"
}
}
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,19 @@ const getLockFilename = usePackageLock => workingDirectory => {
const useYarn = fs.existsSync(yarnFilename)
core.debug(`yarn lock file "${yarnFilename}" exists? ${useYarn}`)

const npmShrinkwrapFilename = path.join(
workingDirectory,
'npm-shrinkwrap.json'
)
const packageLockFilename = path.join(workingDirectory, 'package-lock.json')
const npmFilename =
!useYarn && fs.existsSync(npmShrinkwrapFilename)
? npmShrinkwrapFilename
: packageLockFilename

const result = {
useYarn,
lockFilename: useYarn ? yarnFilename : packageLockFilename
lockFilename: useYarn ? yarnFilename : npmFilename
}
return result
}
Expand Down
84 changes: 65 additions & 19 deletions test/action-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ describe('action', () => {

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

beforeEach(function() {
sandbox
Expand All @@ -100,31 +100,77 @@ describe('action', () => {
.withArgs('npm')
.resolves(pathToNpm)

sandbox
.stub(hasha, 'fromFileSync')
.withArgs(packageLockFilename)
.returns('hash-from-package-lock-file')

const cacheHit = false
this.restoreCache = sandbox.stub(cache, 'restoreCache').resolves(cacheHit)
this.saveCache = sandbox.stub(cache, 'saveCache').resolves()
})

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

expect(this.restoreCache).to.be.calledOnceWithExactly(
npmCachePaths,
cacheKey,
[cacheKey]
)
expect(this.exec).to.be.calledOnceWithExactly(quote(pathToNpm), ['ci'], {
cwd
beforeEach(function() {
fs.existsSync.withArgs(npmShrinkwrapFilename).returns(true)

sandbox
.stub(hasha, 'fromFileSync')
.withArgs(npmShrinkwrapFilename)
.returns('hash-from-npm-shrinkwrap-file')
})

it('uses npm-shrinkwrap.json and NPM', async function() {
await action.npmInstallAction()

expect(this.restoreCache).to.be.calledOnceWithExactly(
npmCachePaths,
cacheKey,
[cacheKey]
)
expect(this.exec).to.be.calledOnceWithExactly(
quote(pathToNpm),
['ci'],
{
cwd
}
)
expect(this.saveCache).to.be.calledOnceWithExactly(
npmCachePaths,
cacheKey
)
})
})

context('finds package-lock.json', async function() {
const cacheKey = 'npm-platform-arch-hash-from-package-lock-file'

beforeEach(function() {
fs.existsSync.withArgs(npmShrinkwrapFilename).returns(false)

sandbox
.stub(hasha, 'fromFileSync')
.withArgs(packageLockFilename)
.returns('hash-from-package-lock-file')
})

it('uses package-lock.json and NPM', async function() {
await action.npmInstallAction()

expect(this.restoreCache).to.be.calledOnceWithExactly(
npmCachePaths,
cacheKey,
[cacheKey]
)
expect(this.exec).to.be.calledOnceWithExactly(
quote(pathToNpm),
['ci'],
{
cwd
}
)
expect(this.saveCache).to.be.calledOnceWithExactly(
npmCachePaths,
cacheKey
)
})
expect(this.saveCache).to.be.calledOnceWithExactly(
npmCachePaths,
cacheKey
)
})
})

Expand Down

0 comments on commit 3af17e0

Please sign in to comment.