snyk-resolve-deps
Version:
Resolves a node package tree with combined support for both npm@2 and npm@3.
96 lines • 3.8 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.tryRequirePackageJson = exports.cache = void 0;
// Imported from abandoned repo https://github.com/snyk/try-require.
const fs = require("then-fs");
const path = require("path");
const debugModule = require("debug");
const lodash_1 = require("lodash");
const LRU = require("lru-cache");
const debug = debugModule('snyk:resolve:try-require');
const options = { max: 100, maxAge: 1000 * 60 * 60 };
exports.cache = LRU(options);
function tryRequirePackageJson(filename) {
return __awaiter(this, void 0, void 0, function* () {
const cached = exports.cache.get(filename);
if (cached) {
const res = (0, lodash_1.cloneDeep)(cached);
return Promise.resolve(res);
}
return fs
.readFile(filename, 'utf8')
.then(function (pkgStr) {
let leadingBOM = '';
if (pkgStr && pkgStr[0] === '\ufeff') {
// String starts with UTF BOM. Remove it so that JSON.parse doesn't
// stumble, but remember it for later use.
pkgStr = pkgStr.slice(1);
leadingBOM = '\ufeff';
}
const pkg = JSON.parse(pkgStr);
pkg.leading = leadingBOM + pkgStr.match(/^(\s*){/)[1];
pkg.trailing = pkgStr.match(/}(\s*)$/)[1];
return pkg;
})
.catch(function (e) {
debug('tryRequire silently failing on %s', e.message);
return null;
})
.then(function (pkg) {
if (!pkg) {
return pkg;
}
// fixes potential issues later on
if (!pkg.devDependencies) {
pkg.devDependencies = {};
}
if (!pkg.dependencies) {
pkg.dependencies = {};
}
if (!pkg.name) {
pkg.name = path.basename(path.dirname(filename));
}
pkg.__filename = filename;
// test for npm-shrinkwrap and find a .snyk policy file whilst we're at it
const dir = path.dirname(filename);
const promises = [
fs.stat(path.resolve(dir, '.snyk')).catch(pass),
fs.stat(path.resolve(dir, 'npm-shrinkwrap.json')).catch(pass),
];
return Promise.all(promises).then(function (res) {
if (!pkg.snyk) {
pkg.snyk = res[0].isFile();
}
if (pkg.snyk) {
pkg.snyk = dir;
}
if (res[1].isFile()) {
pkg.shrinkwrap = true;
}
return pkg;
});
})
.then(function (pkg) {
exports.cache.set(filename, pkg);
return (0, lodash_1.cloneDeep)(pkg);
});
});
}
exports.tryRequirePackageJson = tryRequirePackageJson;
const pass = function () {
return {
isFile: function () {
return false;
},
};
};
//# sourceMappingURL=try-require.js.map
;