libnpmdiff
Advanced tools
Comparing version 1.0.1 to 2.0.0
21
index.js
@@ -6,14 +6,17 @@ const pacote = require('pacote') | ||
const argsError = () => | ||
Object.assign( | ||
new TypeError('libnpmdiff needs two arguments to compare'), | ||
{ code: 'EDIFFARGS' } | ||
) | ||
const diff = async (specs, opts = {}) => { | ||
const { prefix: path } = opts | ||
if (specs.length !== 2) | ||
throw argsError() | ||
const aManifest = await pacote.manifest(specs.a, opts) | ||
const [ | ||
aManifest, | ||
bManifest, | ||
] = | ||
await Promise.all(specs.map(spec => pacote.manifest(spec, opts))) | ||
// when using a single argument the spec to compare from is going to be | ||
// figured out from reading the current location package | ||
if (!specs.b) | ||
specs.b = `file:${path || '.'}` | ||
const bManifest = await pacote.manifest(specs.b, opts) | ||
const versions = { | ||
@@ -20,0 +23,0 @@ a: aManifest.version, |
@@ -10,5 +10,4 @@ const EOL = '\n' | ||
let res = '' | ||
const diffOpts = opts.diffOpts || {} | ||
const srcPrefix = diffOpts.noPrefix ? '' : diffOpts.srcPrefix || 'a/' | ||
const dstPrefix = diffOpts.noPrefix ? '' : diffOpts.dstPrefix || 'b/' | ||
const srcPrefix = opts.diffNoPrefix ? '' : opts.diffSrcPrefix || 'a/' | ||
const dstPrefix = opts.diffNoPrefix ? '' : opts.diffDstPrefix || 'b/' | ||
@@ -38,3 +37,3 @@ for (const filename of files.values()) { | ||
if (diffOpts.nameOnly) { | ||
if (opts.diffNameOnly) { | ||
res += `${filename}${EOL}` | ||
@@ -76,4 +75,4 @@ continue | ||
{ | ||
context: diffOpts.context || 3, | ||
ignoreWhitespace: diffOpts.ignoreWhitespace, | ||
context: opts.diffContext === 0 ? 0 : opts.diffContext || 3, | ||
ignoreWhitespace: opts.diffIgnoreWhitespace, | ||
} | ||
@@ -80,0 +79,0 @@ ).replace( |
@@ -8,4 +8,3 @@ const { basename, extname } = require('path') | ||
const shouldPrintPatch = (path, opts = {}) => { | ||
const { text } = opts.diffOpts || {} | ||
if (text) | ||
if (opts.diffText) | ||
return true | ||
@@ -12,0 +11,0 @@ |
@@ -13,31 +13,53 @@ const tar = require('tar') | ||
filter: (path, entry) => { | ||
const fileMatch = () => | ||
(!filterFiles.length || | ||
filterFiles.some(f => | ||
minimatch( | ||
normalizeMatch(path), | ||
`{package/,}${normalizeMatch(f)}`, | ||
{ matchBase: true } | ||
))) | ||
// expands usage of simple path filters, e.g: lib or src/ | ||
const folderMatch = () => | ||
filterFiles.some(f => | ||
normalizeMatch(path).startsWith(normalizeMatch(f)) || | ||
normalizeMatch(path).startsWith(`package/${normalizeMatch(f)}`)) | ||
if ( | ||
entry.type !== 'File' || | ||
(filterFiles.length && | ||
!filterFiles.some(f => | ||
minimatch(normalizeMatch(path), `{package/,}${normalizeMatch(f)}`))) | ||
) | ||
filterFiles.length && | ||
entry.type === 'Directory' && | ||
folderMatch() | ||
) { | ||
// if simple folder was found, add that as a glob to list of filters | ||
filterFiles.push(`${path}**`) | ||
return false | ||
} | ||
const key = path.replace(/^[^/]+\/?/, '') | ||
files.add(key) | ||
if ( | ||
entry.type === 'File' && | ||
fileMatch() | ||
) { | ||
const key = path.replace(/^[^/]+\/?/, '') | ||
files.add(key) | ||
// should skip reading file when using --name-only option | ||
let content | ||
try { | ||
entry.setEncoding('utf8') | ||
content = entry.concat() | ||
} catch (e) { | ||
/* istanbul ignore next */ | ||
throw Object.assign( | ||
new Error('failed to read files'), | ||
{ code: 'EDIFFUNTAR' } | ||
) | ||
// should skip reading file when using --name-only option | ||
let content | ||
try { | ||
entry.setEncoding('utf8') | ||
content = entry.concat() | ||
} catch (e) { | ||
/* istanbul ignore next */ | ||
throw Object.assign( | ||
new Error('failed to read files'), | ||
{ code: 'EDIFFUNTAR' } | ||
) | ||
} | ||
refs.set(`${prefix}${key}`, { | ||
content, | ||
mode: `100${entry.mode.toString(8)}`, | ||
}) | ||
return true | ||
} | ||
refs.set(`${prefix}${key}`, { | ||
content, | ||
mode: `100${entry.mode.toString(8)}`, | ||
}) | ||
return true | ||
}, | ||
@@ -55,5 +77,4 @@ }) | ||
const arr = [].concat(tarballs) | ||
const { files: _files } = opts.diffOpts || {} | ||
const filterFiles = _files || [] | ||
const filterFiles = opts.diffFiles || [] | ||
@@ -60,0 +81,0 @@ for (const i of arr) { |
{ | ||
"name": "libnpmdiff", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"description": "The registry diff", | ||
@@ -61,5 +61,5 @@ "repository": "https://github.com/npm/libnpmdiff", | ||
"minimatch": "^3.0.4", | ||
"pacote": "^11.1.14", | ||
"pacote": "^11.2.3", | ||
"tar": "^6.1.0" | ||
} | ||
} |
@@ -23,6 +23,6 @@ # libnpmdiff | ||
const patch = await libdiff({ | ||
a: 'abbrev@1.1.0', | ||
b: 'abbrev@1.1.1' | ||
}) | ||
const patch = await libdiff([ | ||
'abbrev@1.1.0', | ||
'abbrev@1.1.1' | ||
]) | ||
console.log( | ||
@@ -74,8 +74,6 @@ patch | ||
#### `> libnpmdif({ a, b }, [opts]) -> Promise<String>` | ||
#### `> libnpmdif([ a, b ], [opts]) -> Promise<String>` | ||
Fetches the registry tarballs and compare files between a spec `a` and spec `b`. **npm** spec types are usually described in `<pkg-name>@<version>` form but multiple other types are alsos supported, for more info on valid specs take a look at [`npm-package-arg`](https://github.com/npm/npm-package-arg). | ||
If only spec `a` is provided, then it's going to try and compare that specified spec against the current project directory in the local file system (cwd may be set via `opts.prefix` option). | ||
**Options**: | ||
@@ -85,14 +83,16 @@ | ||
- `tagVersionPrefix <Sring>`: What prefix should be used to define version numbers. Defaults to `v` | ||
- `prefix <String>`: The path to use as current working directory if trying to read from local file system when using only a single comparison parameter `a`. Defaults to `.`. | ||
- `diffContext <Number>`: How many lines of code to print before/after each diff. Defaults to `3`. | ||
- `diffFiles <Array<String>>`: If set only prints patches for the files listed in this array (also accepts globs). Defaults to `undefined`. | ||
- `diffIgnoreWhitespace <Boolean>`: Whether or not should ignore changes in whitespace (very useful to avoid indentation changes extra diff lines). Defaults to `false`. | ||
- `diffNameOnly <Boolean>`: Prints only file names and no patch diffs. Defaults to `false`. | ||
- `diffNoPrefix <Boolean>`: If true then skips printing any prefixes in filenames. Defaults to `false`. | ||
- `diffSrcPrefix <String>`: Prefix to be used in the filenames from `a`. Defaults to `a/`. | ||
- `diffDstPrefix <String>`: Prefix to be used in the filenames from `b`. Defaults to `b/`. | ||
- `diffText <Boolean>`: Should treat all files as text and try to print diff for binary files. Defaults to `false`. | ||
- ...`cache`, `registry` and other common options accepted by [pacote](https://github.com/npm/pacote#options) | ||
- `diffOpts <Object>`: Object containing extra options on how to format the diff patch output: | ||
- `context <Number>`: How many lines of code to print before/after each diff. Defaults to `3`. | ||
- `files <Array<String>>`: If set only prints patches for the files listed in this array (also accepts globs). Defaults to `undefined`. | ||
- `ignoreWhitespace <Boolean>`: Whether or not should ignore changes in whitespace (very useful to avoid indentation changes extra diff lines). Defaults to `false`. | ||
- `nameOnly <Boolean>`: Prints only file names and no patch diffs. Defaults to `false`. | ||
- `noPrefix <Boolean>`: If true then skips printing any prefixes in filenames. Defaults to `false`. | ||
- `srcPrefix <String>`: Prefix to be used in the filenames from `a`. Defaults to `a/`. | ||
- `dstPrefix <String>`: Prefix to be used in the filenames from `b`. Defaults to `b/`. | ||
- `text <Boolean>`: Should treat all files as text and try to print diff for binary files. Defaults to `false`. | ||
Returns a `Promise` that fullfils with a `String` containing the resulting patch diffs. | ||
Throws an error if either `a` or `b` are missing or if trying to diff more than two specs. | ||
## LICENSE | ||
@@ -99,0 +99,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12441
235
Updatedpacote@^11.2.3