UNPKG

paqman

Version:

Perform tasks relative to your package.json, such as requiring

54 lines (52 loc) 1.77 kB
var Path = require('path'), LRU = require('lru-cache') module.exports = Object.create({ CWD: process.cwd(), // For overrides pathPrefix: './', // Also for overrides packageFileName: 'package.json', // Also for overrides didCachePackageMembers: false, // For package member caching allowCacheOverwrites: false, cache: LRU(), previousRequireCwd: null, fromCache: function (key, creationLambda) { var retVal if (!(retVal = this.cache.get(key))) { this.cache.set(key, (retVal = creationLambda(key))) } return retVal }, relativePath: function relativePath(pathRelativeToCwd) { return this.fromCache(this.pathPrefix + pathRelativeToCwd, Path.join.bind(Path, this.CWD)) }, relativeRequire: function relativeRequire(pathRelativeToCwd) { var path = this.relativePath(pathRelativeToCwd) var pkg = null try { pkg = require(path) } catch (e) { console.error(e) } finally { return pkg } }, uninstallRelativeRequire: function installRelativeRequire() { global.requireCwd = this.previousRequireCwd this.previousRequireCwd = null }, installRelativeRequire: function installRelativeRequire() { this.previousRequireCwd = global.requireCwd global.requireCwd = this.relativeRequire.bind(this) }, get packageJSON() { var packageJSON = this.fromCache(this.packageFileName, this.relativeRequire.bind(this, this.packageFileName)) if (!this.didCachePackageMembers) { packageJSON && Object.keys(packageJSON).forEach(function (key) { if (!this.cache.has(key) || this.allowCacheOverwrites) { this.cache.set(key, packageJSON[key]) } }.bind(this)) this.didCachePackageMembers = true } return packageJSON } })