JezK
Edit File: fix.js
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _typeof3 = require("@babel/runtime/helpers/typeof"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; function _regenerator() { var data = _interopRequireDefault(require("@babel/runtime/regenerator")); _regenerator = function _regenerator() { return data; }; return data; } function _defineProperty2() { var data = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); _defineProperty2 = function _defineProperty2() { return data; }; return data; } function _toArray2() { var data = _interopRequireDefault(require("@babel/runtime/helpers/toArray")); _toArray2 = function _toArray2() { return data; }; return data; } function _typeof2() { var data = _interopRequireDefault(require("@babel/runtime/helpers/typeof")); _typeof2 = function _typeof2() { return data; }; return data; } function _asyncToGenerator2() { var data = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); _asyncToGenerator2 = function _asyncToGenerator2() { return data; }; return data; } function _constants() { var data = require("../../constants"); _constants = function _constants() { return data; }; return data; } function _util() { var data = require("./util.js"); _util = function _util() { return data; }; return data; } function _index() { var data = require("../../resolvers/index.js"); _index = function _index() { return data; }; return data; } function _inferLicense() { var data = _interopRequireDefault(require("./infer-license.js")); _inferLicense = function _inferLicense() { return data; }; return data; } function fs() { var data = _interopRequireWildcard(require("../fs.js")); fs = function fs() { return data; }; return data; } 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 || _typeof3(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; } function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } var semver = require('semver'); var path = require('path'); var url = require('url'); var VALID_BIN_KEYS = /^(?!\.{0,2}$)[a-z0-9._-]+$/i; var LICENSE_RENAMES = { 'MIT/X11': 'MIT', X11: 'MIT' }; var _default = /*#__PURE__*/function () { var _ref = (0, _asyncToGenerator2()["default"])( /*#__PURE__*/_regenerator()["default"].mark(function _callee(info, moduleLoc, reporter, warn, looseSemver) { var files, authorsFilepath, authorsFilestats, authors, readmeCandidates, _iterator, _step, readmeFilename, readmeFilepath, readmeFileStats, desc, engines, _iterator2, _step2, str, _str$trim$split, _str$trim$split2, name, patternParts, repo, parts, _name, bin, _i, _Object$keys, _key, target, scripts, dirs, binDir, _bin, fullBinDir, _iterator3, _step3, scriptName, manDir, man, fullManDir, _iterator4, _step4, filename, licenses, licenseTypes, _iterator5, _step5, _license, license, licenseFile, licenseFilepath, licenseFileStats, licenseContent, inferredLicense, _license2, basicLicense, expandedLicense, _inferredLicense, noticeFile, noticeFilepath, noticeFileStats, _iterator6, _step6, dependencyType, dependencyList, _name2; return _regenerator()["default"].wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: _context.next = 2; return fs().readdir(moduleLoc); case 2: files = _context.sent; // clean info.version if (typeof info.version === 'string') { info.version = semver.clean(info.version, looseSemver) || info.version; } // if name or version aren't set then set them to empty strings info.name = info.name || ''; info.version = info.version || ''; // if the man field is a string then coerce it to an array if (typeof info.man === 'string') { info.man = [info.man]; } // if the keywords field is a string then split it on any whitespace if (typeof info.keywords === 'string') { info.keywords = info.keywords.split(/\s+/g); } // if there's no contributors field but an authors field then expand it if (!(!info.contributors && files.indexOf('AUTHORS') >= 0)) { _context.next = 19; break; } authorsFilepath = path.join(moduleLoc, 'AUTHORS'); _context.next = 12; return fs().stat(authorsFilepath); case 12: authorsFilestats = _context.sent; if (!authorsFilestats.isFile()) { _context.next = 19; break; } _context.next = 16; return fs().readFile(authorsFilepath); case 16: authors = _context.sent; authors = authors.split(/\r?\n/g) // split on lines .map(function (line) { return line.replace(/^\s*#.*$/, '').trim(); }) // remove comments .filter(function (line) { return !!line; }); // remove empty lines info.contributors = authors; case 19: // expand people fields to objects if (typeof info.author === 'string' || (0, _typeof2()["default"])(info.author) === 'object') { info.author = (0, _util().normalizePerson)(info.author); } if (Array.isArray(info.contributors)) { info.contributors = info.contributors.map(_util().normalizePerson); } if (Array.isArray(info.maintainers)) { info.maintainers = info.maintainers.map(_util().normalizePerson); } // if there's no readme field then load the README file from the cwd if (info.readme) { _context.next = 49; break; } readmeCandidates = files.filter(function (filename) { var lower = filename.toLowerCase(); return lower === 'readme' || lower.indexOf('readme.') === 0; }).sort(function (filename1, filename2) { // favor files with extensions return filename2.indexOf('.') - filename1.indexOf('.'); }); _iterator = _createForOfIteratorHelper(readmeCandidates); _context.prev = 25; _iterator.s(); case 27: if ((_step = _iterator.n()).done) { _context.next = 41; break; } readmeFilename = _step.value; readmeFilepath = path.join(moduleLoc, readmeFilename); _context.next = 32; return fs().stat(readmeFilepath); case 32: readmeFileStats = _context.sent; if (!readmeFileStats.isFile()) { _context.next = 39; break; } info.readmeFilename = readmeFilename; _context.next = 37; return fs().readFile(readmeFilepath); case 37: info.readme = _context.sent; return _context.abrupt("break", 41); case 39: _context.next = 27; break; case 41: _context.next = 46; break; case 43: _context.prev = 43; _context.t0 = _context["catch"](25); _iterator.e(_context.t0); case 46: _context.prev = 46; _iterator.f(); return _context.finish(46); case 49: // if there's no description then take the first paragraph from the readme if (!info.description && info.readme) { desc = (0, _util().extractDescription)(info.readme); if (desc) { info.description = desc; } } // support array of engine keys if (Array.isArray(info.engines)) { engines = {}; _iterator2 = _createForOfIteratorHelper(info.engines); try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { str = _step2.value; if (typeof str === 'string') { _str$trim$split = str.trim().split(/ +/g), _str$trim$split2 = (0, _toArray2()["default"])(_str$trim$split), name = _str$trim$split2[0], patternParts = _str$trim$split2.slice(1); engines[name] = patternParts.join(' '); } } } catch (err) { _iterator2.e(err); } finally { _iterator2.f(); } info.engines = engines; } // if the repository field is a string then assume it's a git repo and expand it if (typeof info.repository === 'string') { info.repository = { type: 'git', url: info.repository }; } repo = info.repository; // explode info.repository.url if it's a hosted git shorthand if (repo && (0, _typeof2()["default"])(repo) === 'object' && typeof repo.url === 'string') { repo.url = (0, _index().hostedGitFragmentToGitUrl)(repo.url, reporter); } // allow bugs to be specified as a string, expand it to an object with a single url prop if (typeof info.bugs === 'string') { info.bugs = { url: info.bugs }; } // normalize homepage url to http if (typeof info.homepage === 'string') { parts = url.parse(info.homepage); parts.protocol = parts.protocol || 'http:'; if (parts.pathname && !parts.hostname) { parts.hostname = parts.pathname; parts.pathname = ''; } info.homepage = url.format(parts); } // if the `bin` field is as string then expand it to an object with a single property // based on the original `bin` field and `name field` // { name: "foo", bin: "cli.js" } -> { name: "foo", bin: { foo: "cli.js" } } if (typeof info.name === 'string' && typeof info.bin === 'string' && info.bin.length > 0) { // Remove scoped package name for consistency with NPM's bin field fixing behaviour _name = info.name.replace(/^@[^\/]+\//, ''); info.bin = (0, _defineProperty2()["default"])({}, _name, info.bin); } // Validate that the bin entries reference only files within their package, and that // their name is a valid file name if ((0, _typeof2()["default"])(info.bin) === 'object' && info.bin !== null) { bin = info.bin; for (_i = 0, _Object$keys = Object.keys(bin); _i < _Object$keys.length; _i++) { _key = _Object$keys[_i]; target = bin[_key]; if (!VALID_BIN_KEYS.test(_key) || !(0, _util().isValidBin)(target)) { delete bin[_key]; warn(reporter.lang('invalidBinEntry', info.name, _key)); } else { bin[_key] = path.normalize(target); } } } else if (typeof info.bin !== 'undefined') { delete info.bin; warn(reporter.lang('invalidBinField', info.name)); } // bundleDependencies is an alias for bundledDependencies if (info.bundledDependencies) { info.bundleDependencies = info.bundledDependencies; delete info.bundledDependencies; } // dummy script object to shove file inferred scripts onto if (info.scripts && (0, _typeof2()["default"])(info.scripts) === 'object') { scripts = info.scripts; } else { scripts = {}; } // if there's a server.js file and no start script then set it to `node server.js` if (!scripts.start && files.indexOf('server.js') >= 0) { scripts.start = 'node server.js'; } // if there's a binding.gyp file and no install script then set it to `node-gyp rebuild` if (!scripts.install && files.indexOf('binding.gyp') >= 0) { scripts.install = 'node-gyp rebuild'; } // set scripts if we've polluted the empty object if (Object.keys(scripts).length) { info.scripts = scripts; } dirs = info.directories; if (!(dirs && (0, _typeof2()["default"])(dirs) === 'object')) { _context.next = 113; break; } binDir = dirs.bin; if (!(!info.bin && binDir && typeof binDir === 'string')) { _context.next = 97; break; } _bin = info.bin = {}; fullBinDir = path.join(moduleLoc, binDir); _context.next = 71; return fs().exists(fullBinDir); case 71: if (!_context.sent) { _context.next = 96; break; } _context.t1 = _createForOfIteratorHelper; _context.next = 75; return fs().readdir(fullBinDir); case 75: _context.t2 = _context.sent; _iterator3 = (0, _context.t1)(_context.t2); _context.prev = 77; _iterator3.s(); case 79: if ((_step3 = _iterator3.n()).done) { _context.next = 86; break; } scriptName = _step3.value; if (!(scriptName[0] === '.')) { _context.next = 83; break; } return _context.abrupt("continue", 84); case 83: _bin[scriptName] = path.join('.', binDir, scriptName); case 84: _context.next = 79; break; case 86: _context.next = 91; break; case 88: _context.prev = 88; _context.t3 = _context["catch"](77); _iterator3.e(_context.t3); case 91: _context.prev = 91; _iterator3.f(); return _context.finish(91); case 94: _context.next = 97; break; case 96: warn(reporter.lang('manifestDirectoryNotFound', binDir, info.name)); case 97: manDir = dirs.man; if (!(!info.man && typeof manDir === 'string')) { _context.next = 113; break; } man = info.man = []; fullManDir = path.join(moduleLoc, manDir); _context.next = 103; return fs().exists(fullManDir); case 103: if (!_context.sent) { _context.next = 112; break; } _context.t4 = _createForOfIteratorHelper; _context.next = 107; return fs().readdir(fullManDir); case 107: _context.t5 = _context.sent; _iterator4 = (0, _context.t4)(_context.t5); try { for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) { filename = _step4.value; if (/^(.*?)\.[0-9]$/.test(filename)) { man.push(path.join('.', manDir, filename)); } } } catch (err) { _iterator4.e(err); } finally { _iterator4.f(); } _context.next = 113; break; case 112: warn(reporter.lang('manifestDirectoryNotFound', manDir, info.name)); case 113: delete info.directories; // normalize licenses field licenses = info.licenses; if (Array.isArray(licenses) && !info.license) { licenseTypes = []; _iterator5 = _createForOfIteratorHelper(licenses); try { for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) { _license = _step5.value; if (_license && (0, _typeof2()["default"])(_license) === 'object') { _license = _license.type; } if (typeof _license === 'string') { licenseTypes.push(_license); } } } catch (err) { _iterator5.e(err); } finally { _iterator5.f(); } licenseTypes = licenseTypes.filter(_util().isValidLicense); if (licenseTypes.length === 1) { info.license = licenseTypes[0]; } else if (licenseTypes.length) { info.license = "(".concat(licenseTypes.join(' OR '), ")"); } } license = info.license; // normalize license if (license && (0, _typeof2()["default"])(license) === 'object') { info.license = license.type; } // get license file licenseFile = files.find(function (filename) { var lower = filename.toLowerCase(); return lower === 'license' || lower.startsWith('license.') || lower === 'unlicense' || lower.startsWith('unlicense.'); }); if (!licenseFile) { _context.next = 132; break; } licenseFilepath = path.join(moduleLoc, licenseFile); _context.next = 123; return fs().stat(licenseFilepath); case 123: licenseFileStats = _context.sent; if (!licenseFileStats.isFile()) { _context.next = 132; break; } _context.next = 127; return fs().readFile(licenseFilepath); case 127: licenseContent = _context.sent; inferredLicense = (0, _inferLicense()["default"])(licenseContent); info.licenseText = licenseContent; _license2 = info.license; if (typeof _license2 === 'string') { if (inferredLicense && (0, _util().isValidLicense)(inferredLicense) && !(0, _util().isValidLicense)(_license2)) { // some packages don't specify their license version but we can infer it based on their license file basicLicense = _license2.toLowerCase().replace(/(-like|\*)$/g, ''); expandedLicense = inferredLicense.toLowerCase(); if (expandedLicense.startsWith(basicLicense)) { // TODO consider doing something to notify the user info.license = inferredLicense; } } } else if (inferredLicense) { // if there's no license then infer it based on the license file info.license = inferredLicense; } else { // valid expression to refer to a license in a file info.license = "SEE LICENSE IN ".concat(licenseFile); } case 132: if (typeof info.license === 'string') { // sometimes licenses are known by different names, reduce them info.license = LICENSE_RENAMES[info.license] || info.license; } else if (typeof info.readme === 'string') { // the license might be at the bottom of the README _inferredLicense = (0, _inferLicense()["default"])(info.readme); if (_inferredLicense) { info.license = _inferredLicense; } } // get notice file noticeFile = files.find(function (filename) { var lower = filename.toLowerCase(); return lower === 'notice' || lower.startsWith('notice.'); }); if (!noticeFile) { _context.next = 143; break; } noticeFilepath = path.join(moduleLoc, noticeFile); _context.next = 138; return fs().stat(noticeFilepath); case 138: noticeFileStats = _context.sent; if (!noticeFileStats.isFile()) { _context.next = 143; break; } _context.next = 142; return fs().readFile(noticeFilepath); case 142: info.noticeText = _context.sent; case 143: _iterator6 = _createForOfIteratorHelper(_constants().MANIFEST_FIELDS); try { for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) { dependencyType = _step6.value; dependencyList = info[dependencyType]; if (dependencyList && (0, _typeof2()["default"])(dependencyList) === 'object') { delete dependencyList['//']; for (_name2 in dependencyList) { dependencyList[_name2] = dependencyList[_name2] || ''; } } } } catch (err) { _iterator6.e(err); } finally { _iterator6.f(); } case 145: case "end": return _context.stop(); } }, _callee, null, [[25, 43, 46, 49], [77, 88, 91, 94]]); })); return function (_x, _x2, _x3, _x4, _x5) { return _ref.apply(this, arguments); }; }(); exports["default"] = _default; //# sourceMappingURL=fix.js.map