feat: add nameVerFromPkgSnapshot()

This commit is contained in:
Zoltan Kochan
2018-03-17 23:30:09 +02:00
parent b8431aac34
commit db2953aa53
5 changed files with 45 additions and 0 deletions

View File

@@ -55,6 +55,8 @@ Writes the current shrinkwrap file only. Fails if there is no `node_modules` dir
Prunes a shrinkwrap file. Prunning means removing packages that are not referenced.
### `nameVerFromPkgSnapshot(relDepPath, pkgSnapshot): {name: string, version: string}`
## License
[MIT](LICENSE)

View File

@@ -3,6 +3,7 @@ export * from './read'
export * from './types'
import existsWanted from './existsWanted'
import nameVerFromPkgSnapshot from './nameVerFromPkgSnapshot'
import prune from './prune'
import write, {
writeCurrentOnly,
@@ -11,6 +12,7 @@ import write, {
export {
existsWanted,
nameVerFromPkgSnapshot,
prune,
write,
writeWantedOnly,

View File

@@ -0,0 +1,21 @@
import * as dp from 'dependency-path'
import {PackageSnapshot} from './types'
export default (
relDepPath: string,
pkgSnapshot: PackageSnapshot,
) => {
if (!pkgSnapshot.name) {
const pkgInfo = dp.parse(relDepPath)
return {
// tslint:disable:no-string-literal
name: pkgInfo['name'],
version: pkgInfo['version'],
// tslint:enable:no-string-literal
}
}
return {
name: pkgSnapshot.name,
version: pkgSnapshot.version,
}
}

View File

@@ -1,2 +1,3 @@
import './prune'
import './read'
import './nameVerFromPkgSnapshot'

View File

@@ -0,0 +1,19 @@
import {nameVerFromPkgSnapshot} from 'pnpm-shrinkwrap'
import test = require('tape')
test('nameVerFromPkgSnapshot()', (t) => {
t.deepEqual(nameVerFromPkgSnapshot('/some-weird-path', {
name: 'foo',
version: '1.0.0',
}), {
name: 'foo',
version: '1.0.0',
})
t.deepEqual(nameVerFromPkgSnapshot('/foo/1.0.0', {}), {
name: 'foo',
version: '1.0.0',
})
t.end()
})