mirror of
https://github.com/pnpm/pnpm.git
synced 2026-05-02 13:04:57 -04:00
feat: rename public/private to wanted/current shrinkwrap
This commit is contained in:
16
README.md
16
README.md
@@ -16,9 +16,11 @@ npm i pnpm-shrinkwrap
|
||||
|
||||
## API
|
||||
|
||||
### `read(pkgPath, opts) => Promise<Shrinkwrap>`
|
||||
### `readWanted(pkgPath, opts) => Promise<Shrinkwrap>`
|
||||
|
||||
Reads the public `shrinkwrap.yaml` file from the root of the package.
|
||||
Alias: `read`
|
||||
|
||||
Reads the `shrinkwrap.yaml` file from the root of the package.
|
||||
|
||||
#### Arguments
|
||||
|
||||
@@ -26,13 +28,15 @@ Reads the public `shrinkwrap.yaml` file from the root of the package.
|
||||
* `opts.ignoreIncompatible` - *Boolean* - `false` by default. If `true`, throws an error
|
||||
if the shrinkwrap file format is not compatible with the current library.
|
||||
|
||||
### `readPrivate(pkgPath, opts) => Promise<Shrinkwrap>`
|
||||
### `readCurrent(pkgPath, opts) => Promise<Shrinkwrap>`
|
||||
|
||||
Same as `read()` but for the private shrinkwrap file at `node_modules/.shrinkwrap.yaml`.
|
||||
Alias: `readPrivate`
|
||||
|
||||
### `write(pkgPath, shrinkwrap, privateShrinkwrap) => Promise<void>`
|
||||
Reads the shrinkwrap file from `node_modules/.shrinkwrap.yaml`.
|
||||
|
||||
Writes the public private shrinkwrap files. When they are empty, removes them.
|
||||
### `write(pkgPath, wantedShrinkwrap, currentShrinkwrap) => Promise<void>`
|
||||
|
||||
Writes the wanted/current shrinkwrap files. When they are empty, removes them.
|
||||
|
||||
### `prune(shrinkwrap, package) => Promise<Shrinkwrap>`
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import path = require('path')
|
||||
|
||||
export const SHRINKWRAP_FILENAME = 'shrinkwrap.yaml'
|
||||
export const PRIVATE_SHRINKWRAP_FILENAME = path.join('node_modules', '.shrinkwrap.yaml')
|
||||
export const WANTED_SHRINKWRAP_FILENAME = 'shrinkwrap.yaml'
|
||||
export const CURRENT_SHRINKWRAP_FILENAME = path.join('node_modules', '.shrinkwrap.yaml')
|
||||
|
||||
// Although .0 versions are supported, a bump to 3.1 would be a breaking change
|
||||
// because comver version support was added after releasing version 3.
|
||||
|
||||
16
src/read.ts
16
src/read.ts
@@ -1,8 +1,8 @@
|
||||
import path = require('path')
|
||||
import logger from './logger'
|
||||
import {
|
||||
SHRINKWRAP_FILENAME,
|
||||
PRIVATE_SHRINKWRAP_FILENAME,
|
||||
WANTED_SHRINKWRAP_FILENAME,
|
||||
CURRENT_SHRINKWRAP_FILENAME,
|
||||
} from './constants'
|
||||
import {Shrinkwrap} from './types'
|
||||
import loadYamlFile = require('load-yaml-file')
|
||||
@@ -27,23 +27,27 @@ class ShrinkwrapBreakingChangeError extends PnpmError {
|
||||
filename: string
|
||||
}
|
||||
|
||||
export async function readPrivate (
|
||||
export const readPrivate = readCurrent
|
||||
|
||||
export async function readCurrent (
|
||||
pkgPath: string,
|
||||
opts: {
|
||||
ignoreIncompatible: boolean,
|
||||
}
|
||||
): Promise<Shrinkwrap | null> {
|
||||
const shrinkwrapPath = path.join(pkgPath, PRIVATE_SHRINKWRAP_FILENAME)
|
||||
const shrinkwrapPath = path.join(pkgPath, CURRENT_SHRINKWRAP_FILENAME)
|
||||
return await _read(shrinkwrapPath, opts)
|
||||
}
|
||||
|
||||
export async function read (
|
||||
export const read = readWanted
|
||||
|
||||
export async function readWanted (
|
||||
pkgPath: string,
|
||||
opts: {
|
||||
ignoreIncompatible: boolean,
|
||||
}
|
||||
): Promise<Shrinkwrap | null> {
|
||||
const shrinkwrapPath = path.join(pkgPath, SHRINKWRAP_FILENAME)
|
||||
const shrinkwrapPath = path.join(pkgPath, WANTED_SHRINKWRAP_FILENAME)
|
||||
return await _read(shrinkwrapPath, opts)
|
||||
}
|
||||
|
||||
|
||||
33
src/write.ts
33
src/write.ts
@@ -3,7 +3,10 @@ import writeFileAtomicCB = require('write-file-atomic')
|
||||
import thenify = require('thenify')
|
||||
import rimraf = require('rimraf-then')
|
||||
import yaml = require('js-yaml')
|
||||
import {SHRINKWRAP_FILENAME, PRIVATE_SHRINKWRAP_FILENAME} from './constants'
|
||||
import {
|
||||
WANTED_SHRINKWRAP_FILENAME,
|
||||
CURRENT_SHRINKWRAP_FILENAME,
|
||||
} from './constants'
|
||||
import {Shrinkwrap} from './types'
|
||||
import mkdirp = require('mkdirp-promise')
|
||||
import logger from './logger'
|
||||
@@ -18,29 +21,29 @@ const SHRINKWRAP_YAML_FORMAT = {
|
||||
|
||||
export default function write (
|
||||
pkgPath: string,
|
||||
shrinkwrap: Shrinkwrap,
|
||||
privateShrinkwrap: Shrinkwrap
|
||||
wantedShrinkwrap: Shrinkwrap,
|
||||
currentShrinkwrap: Shrinkwrap
|
||||
) {
|
||||
const shrinkwrapPath = path.join(pkgPath, SHRINKWRAP_FILENAME)
|
||||
const privateShrinkwrapPath = path.join(pkgPath, PRIVATE_SHRINKWRAP_FILENAME)
|
||||
const wantedShrinkwrapPath = path.join(pkgPath, WANTED_SHRINKWRAP_FILENAME)
|
||||
const currentShrinkwrapPath = path.join(pkgPath, CURRENT_SHRINKWRAP_FILENAME)
|
||||
|
||||
// empty shrinkwrap is not saved
|
||||
if (Object.keys(shrinkwrap.specifiers).length === 0) {
|
||||
if (Object.keys(wantedShrinkwrap.specifiers).length === 0) {
|
||||
return Promise.all([
|
||||
rimraf(shrinkwrapPath),
|
||||
rimraf(privateShrinkwrapPath),
|
||||
rimraf(wantedShrinkwrapPath),
|
||||
rimraf(currentShrinkwrapPath),
|
||||
])
|
||||
}
|
||||
|
||||
const yamlDoc = yaml.safeDump(shrinkwrap, SHRINKWRAP_YAML_FORMAT)
|
||||
const yamlDoc = yaml.safeDump(wantedShrinkwrap, SHRINKWRAP_YAML_FORMAT)
|
||||
|
||||
// in most cases the `shrinkwrap.yaml` and `node_modules/.shrinkwrap.yaml` are equal
|
||||
// in those cases the YAML document can be stringified only once for both files
|
||||
// which is more efficient
|
||||
if (shrinkwrap === privateShrinkwrap) {
|
||||
if (wantedShrinkwrap === currentShrinkwrap) {
|
||||
return Promise.all([
|
||||
writeFileAtomic(shrinkwrapPath, yamlDoc),
|
||||
mkdirp(path.dirname(privateShrinkwrapPath)).then(() => writeFileAtomic(privateShrinkwrapPath, yamlDoc)),
|
||||
writeFileAtomic(wantedShrinkwrapPath, yamlDoc),
|
||||
mkdirp(path.dirname(currentShrinkwrapPath)).then(() => writeFileAtomic(currentShrinkwrapPath, yamlDoc)),
|
||||
])
|
||||
}
|
||||
|
||||
@@ -48,10 +51,10 @@ export default function write (
|
||||
'To fix this, run `pnpm install`. From pnpm version 2, named installations and uninstallations will fail ' +
|
||||
'when the content of `node_modules` won\'t match what the `shrinkwrap.yaml` expects.')
|
||||
|
||||
const privateYamlDoc = yaml.safeDump(privateShrinkwrap, SHRINKWRAP_YAML_FORMAT)
|
||||
const currentYamlDoc = yaml.safeDump(currentShrinkwrap, SHRINKWRAP_YAML_FORMAT)
|
||||
|
||||
return Promise.all([
|
||||
writeFileAtomic(shrinkwrapPath, yamlDoc),
|
||||
mkdirp(path.dirname(privateShrinkwrapPath)).then(() => writeFileAtomic(privateShrinkwrapPath, privateYamlDoc)),
|
||||
writeFileAtomic(wantedShrinkwrapPath, yamlDoc),
|
||||
mkdirp(path.dirname(currentShrinkwrapPath)).then(() => writeFileAtomic(currentShrinkwrapPath, currentYamlDoc)),
|
||||
])
|
||||
}
|
||||
|
||||
27
test/read.ts
27
test/read.ts
@@ -1,25 +1,36 @@
|
||||
import {read, readPrivate} from '../src'
|
||||
import {
|
||||
readWanted,
|
||||
readCurrent,
|
||||
readPrivate,
|
||||
read,
|
||||
} from '../src'
|
||||
import test = require('tape')
|
||||
import path = require('path')
|
||||
|
||||
process.chdir(__dirname)
|
||||
|
||||
test('read()', async t => {
|
||||
test('backward compatibility', t => {
|
||||
t.equal(readWanted, read)
|
||||
t.equal(readCurrent, readPrivate)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('readWanted()', async t => {
|
||||
{
|
||||
const shr = await read(path.join('fixtures', '1'), {
|
||||
const shr = await readWanted(path.join('fixtures', '1'), {
|
||||
ignoreIncompatible: false,
|
||||
})
|
||||
t.equal(shr!.shrinkwrapVersion, 3, 'converted version to shrinkwrapVersion')
|
||||
}
|
||||
{
|
||||
const shr = await read(path.join('fixtures', '2'), {
|
||||
const shr = await readWanted(path.join('fixtures', '2'), {
|
||||
ignoreIncompatible: false,
|
||||
})
|
||||
t.equal(shr!.shrinkwrapVersion, 3)
|
||||
}
|
||||
|
||||
try {
|
||||
const shr = await read(path.join('fixtures', '3'), {
|
||||
const shr = await readWanted(path.join('fixtures', '3'), {
|
||||
ignoreIncompatible: false,
|
||||
})
|
||||
t.fail()
|
||||
@@ -29,15 +40,15 @@ test('read()', async t => {
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('readPrivate()', async t => {
|
||||
test('readCurrent()', async t => {
|
||||
{
|
||||
const shr = await readPrivate(path.join('fixtures', '1'), {
|
||||
const shr = await readCurrent(path.join('fixtures', '1'), {
|
||||
ignoreIncompatible: false,
|
||||
})
|
||||
t.equal(shr!.shrinkwrapVersion, 3, 'converted version to shrinkwrapVersion')
|
||||
}
|
||||
{
|
||||
const shr = await readPrivate(path.join('fixtures', '2'), {
|
||||
const shr = await readCurrent(path.join('fixtures', '2'), {
|
||||
ignoreIncompatible: false,
|
||||
})
|
||||
t.equal(shr!.shrinkwrapVersion, 3)
|
||||
|
||||
Reference in New Issue
Block a user