@hyrious/npm-diff
v0.1.3
Published
libnpmdiff, but detects renames and works on browser
Downloads
3
Readme
@hyrious/npm-diff
libnpmdiff, but detects renames and works on browser
Usage
CLI
Use npm diff
please.
API
In Node.js or with bundlers:
import { diff } from '@hyrious/npm-diff'
console.log(await diff(['[email protected]', '[email protected]']))
In browser:
<script src="https://cdn.jsdelivr.net/npm/pako"></script>
<script src="https://cdn.jsdelivr.net/npm/diff"></script>
<script src="https://cdn.jsdelivr.net/npm/@hyrious/npm-diff"></script>
<script>
const { diff } = npmdiff
console.log(await diff(...))
</script>
Or:
<script type="module">
import { diff } from 'https://esm.sh/@hyrious/npm-diff'
console.log(await diff(...))
</script>
diff([ a, b ], opts?) → Promise<String>
Fetches the registry tarballs and compare files between a spec a
and spec b
.
The spec is usually described in <pkg-name>@<version>
.
Options
tagVersionPrefix
{String} Prefix used to define version numbers. Defaults tov
.diffUnified
{Number} How many lines to print around each hunk. Defaults to3
.diffIgnoreAllSpace
{Boolean} Whether or not should ignore changes in whitespace (very useful to avoid indentation changes extra diff lines). Defaults tofalse
.diffFiles
{Array<String>} If set only prints patches for the files listed in this array (also accepts globs). Defaults toundefined
.diffNameOnly
{Boolean} Prints only file names and no patch diffs. Defaults tofalse
.diffNoPrefix
{Boolean} If true then skips printing any prefixes in filenames. Defaults tofalse
.diffSrcPrefix
{String} Prefix to be used in the filenames froma
. Defaults toa/
.diffDstPrefix
{String} Prefix to be used in the filenames fromb
. Defaults tob/
.diffText
{Boolean} Should treat all files as text and try to print diff for binary files. Defaults tofalse
.cache_get
{Function} User-defined cache getter before fetching tarballs, it takes an object{ name, version }
and returns aPromise
that fullfils with aUint8Array
containing the tarball. Defaults toundefined
, which means each tarball will be fetched without cache.cache_set
{Function} User-defined cache setter after fetching tarballs, it takes an object{ name, version }
and aUint8Array
which is the tarball content. Defaults toundefined
.signal
{AbortSignal} Abort signal to cancel all the progress. Defaults toundefined
.
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.
More Examples
Store packages in indexedDB, see idb-keyval
import * as idb from 'idb-keyval'
const patch = await diff(['[email protected]', '[email protected]'], {
cache_get: ({ name, version }) => idb.get(`${name}@${version}`),
cache_set: ({ name, version }, tarball) => idb.set(`${name}@${version}`, tarball),
})
Render patch to html, see diff2html
const patch = await diff(['[email protected]', '[email protected]'])
const ui = new Diff2HtmlUI($('#diff'), patch, {
drawFileList: true,
outputFormat: 'line-by-line',
})
ui.draw()
ui.highlightCode()
License
MIT © hyrious