UNPKG

astx

Version:

super powerful structural search and replace for JavaScript and TypeScript to automate your refactoring

113 lines (93 loc) 3.44 kB
'use strict' var _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault') var _typeof = require('@babel/runtime/helpers/typeof') Object.defineProperty(exports, '__esModule', { value: true, }) exports['default'] = formatDiff var diff = _interopRequireWildcard(require('diff')) var _chalk = _interopRequireDefault(require('chalk')) function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== 'function') return null var cacheBabelInterop = new WeakMap() var cacheNodeInterop = new WeakMap() return (_getRequireWildcardCache = function _getRequireWildcardCache( nodeInterop ) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop })(nodeInterop) } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj } if ( obj === null || (_typeof(obj) !== 'object' && typeof obj !== 'function') ) { return { default: obj, } } var cache = _getRequireWildcardCache(nodeInterop) if (cache && cache.has(obj)) { return cache.get(obj) } var newObj = {} var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor for (var key in obj) { if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc) } else { newObj[key] = obj[key] } } } newObj['default'] = obj if (cache) { cache.set(obj, newObj) } return newObj } // Adapted from the print-diff package // https://github.com/LinusU/node-print-diff/blob/master/LICENSE // The MIT License (MIT) // Copyright (c) 2015 Linus Unnebäck // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. var originalColor = _chalk['default'].red var modifiedColor = _chalk['default'].green function rework(line) { switch (line[0]) { case '+': return modifiedColor(line) case '-': return originalColor(line) } return line } function formatDiff(actual, expected) { var patch = diff.createPatch('string', actual, expected) var lines = patch.split('\n').slice(4).map(rework) return '' .concat(originalColor('- original'), ' ') .concat(modifiedColor('+ modified'), '\n\n') .concat(lines.join('\n'), '\n') }