diff options
author | Ruy Adorno <ruyadorno@hotmail.com> | 2020-05-17 08:30:11 +0300 |
---|---|---|
committer | nlf <quitlahok@gmail.com> | 2021-01-28 23:50:12 +0300 |
commit | d011266b733367aad283ccbfb9d2b19442c3405f (patch) | |
tree | 689c45b149cd77b4dab6d12b2c09bfa9c40d8aaa /node_modules/@npmcli | |
parent | bb7329def2631308a82144f050955de913a5c8e4 (diff) |
feat: add npm diffruyadorno/npm-diff
- As proposed in RFC: https://github.com/npm/rfcs/pull/144
PR-URL: https://github.com/npm/cli/pull/1319
Credit: @ruyadorno
Close: #1319
Reviewed-by: @isaacs
Diffstat (limited to 'node_modules/@npmcli')
-rw-r--r-- | node_modules/@npmcli/disparity-colors/CHANGELOG.md | 6 | ||||
-rw-r--r-- | node_modules/@npmcli/disparity-colors/LICENSE | 15 | ||||
-rw-r--r-- | node_modules/@npmcli/disparity-colors/README.md | 49 | ||||
-rw-r--r-- | node_modules/@npmcli/disparity-colors/index.js | 34 | ||||
-rw-r--r-- | node_modules/@npmcli/disparity-colors/package.json | 60 |
5 files changed, 164 insertions, 0 deletions
diff --git a/node_modules/@npmcli/disparity-colors/CHANGELOG.md b/node_modules/@npmcli/disparity-colors/CHANGELOG.md new file mode 100644 index 000000000..216d1db90 --- /dev/null +++ b/node_modules/@npmcli/disparity-colors/CHANGELOG.md @@ -0,0 +1,6 @@ +# Changelog + +## 1.0.0 + +- Initial release + diff --git a/node_modules/@npmcli/disparity-colors/LICENSE b/node_modules/@npmcli/disparity-colors/LICENSE new file mode 100644 index 000000000..dedcd7d2f --- /dev/null +++ b/node_modules/@npmcli/disparity-colors/LICENSE @@ -0,0 +1,15 @@ +The ISC License + +Copyright (c) npm Inc. + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR +IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/node_modules/@npmcli/disparity-colors/README.md b/node_modules/@npmcli/disparity-colors/README.md new file mode 100644 index 000000000..a89be36d8 --- /dev/null +++ b/node_modules/@npmcli/disparity-colors/README.md @@ -0,0 +1,49 @@ +# @npmcli/disparity-colors + +[![NPM version](https://img.shields.io/npm/v/@npmcli/disparity-colors)](https://www.npmjs.com/package/@npmcli/disparity-colors) +[![Build Status](https://img.shields.io/github/workflow/status/npm/disparity-colors/node-ci)](https://github.com/npm/disparity-colors) +[![License](https://img.shields.io/github/license/npm/disparity-colors)](https://github.com/npm/disparity-colors/blob/master/LICENSE) + +Spiritual sucessor to [disparity](https://www.npmjs.com/package/disparity). Colorizes [Diff Unified format](https://en.wikipedia.org/wiki/Diff#Unified_format) output using [ansi-styles](https://www.npmjs.com/package/ansi-styles). + +## Install + +`npm install @npmcli/disparity-colors` + +## Usage: + +```js +const colorize = require('@npmcli/disparity-colors') +mapWorkspaces(`--- a/src/index.js ++++ b/src/index.js +@@ -1,4 +1,5 @@ + "use strict"; ++"use foo"; + + const os = require("os"); +`) +// [33m--- a/src/index.js[39m +// [33m+++ b/src/index.js[39m +// [35m@@ -1,4 +1,5 @@[39m +// "use strict"; +// [32m+"use foo";[39m +// +// const os = require("os"); +``` + +## API: + +### `colorize(str, opts = {}) -> String` + +- `str`: A [Diff Unified format](https://en.wikipedia.org/wiki/Diff#Unified_format) string +- `opts`: + - `headerLength`: A **Number** defining how many lines should be colorized as header + +#### Returns + +A **String** including the appropriate [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles) + +## LICENSE + +[ISC](./LICENSE) + diff --git a/node_modules/@npmcli/disparity-colors/index.js b/node_modules/@npmcli/disparity-colors/index.js new file mode 100644 index 000000000..a02b8f884 --- /dev/null +++ b/node_modules/@npmcli/disparity-colors/index.js @@ -0,0 +1,34 @@ +const ansi = require('ansi-styles') + +const colors = { + removed: ansi.red, + added: ansi.green, + header: ansi.yellow, + section: ansi.magenta +} + +function colorize (str, opts) { + let headerLength = (opts || {}).headerLength + if (typeof headerLength !== 'number' || Number.isNaN(headerLength)) { + headerLength = 2 + } + + const color = (str, colorId) => { + const { open, close } = colors[colorId] + // avoid highlighting the "\n" (would highlight till the end of the line) + return str.replace(/[^\n\r]+/g, open + '$&' + close) + } + + // this RegExp will include all the `\n` chars into the lines, easier to join + const lines = ((typeof str === 'string' && str) || '').split(/^/m) + + const start = color(lines.slice(0, headerLength).join(''), 'header') + const end = lines.slice(headerLength).join('') + .replace(/^-.*/gm, color('$&', 'removed')) + .replace(/^\+.*/gm, color('$&', 'added')) + .replace(/^@@.+@@/gm, color('$&', 'section')) + + return start + end +} + +module.exports = colorize diff --git a/node_modules/@npmcli/disparity-colors/package.json b/node_modules/@npmcli/disparity-colors/package.json new file mode 100644 index 000000000..3f389a42c --- /dev/null +++ b/node_modules/@npmcli/disparity-colors/package.json @@ -0,0 +1,60 @@ +{ + "name": "@npmcli/disparity-colors", + "version": "1.0.1", + "files": [ + "index.js" + ], + "engines": { + "node": ">=10" + }, + "description": "Colorizes unified diff output", + "repository": "https://github.com/npm/disparity-colors", + "keywords": [ + "disparity", + "npm", + "npmcli", + "diff", + "char", + "unified", + "multiline", + "string", + "color", + "ansi", + "terminal", + "cli", + "tty" + ], + "author": "npm Inc. <support@npmjs.com>", + "contributors": [ + { + "name": "Ruy Adorno", + "url": "https://ruyadorno.com", + "twitter": "ruyadorno" + } + ], + "license": "ISC", + "scripts": { + "lint": "standard index.js", + "pretest": "npm run lint", + "test": "tap", + "snap": "tap", + "preversion": "npm test", + "postversion": "npm publish", + "prepublishOnly": "git push origin --follow-tags" + }, + "tap": { + "check-coverage": true + }, + "standard": { + "ignore": [ + "/tap-snapshots/" + ] + }, + "devDependencies": { + "standard": "^16.0.3", + "tap": "^14.11.0" + }, + "dependencies": { + "ansi-styles": "^4.3.0" + } +} |