UNPKG

pertain

Version:

Automated pub/sub across project dependencies. Run code from any installed package based on declarative rules in package.json

80 lines 5.51 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const debug_1 = __importDefault(require("debug")); const path_1 = __importDefault(require("path")); const PackageJson_1 = __importDefault(require("./PackageJson")); const PertainError_1 = __importDefault(require("./PertainError")); const debug = (0, debug_1.default)('pertain:ExplicitDependency'); /** * A Node module that the consuming project (the one calling `pertain()`) has * declared in `dependencies` or `devDependencies`. These are the modules that * we check for the package property we're looking for. */ class ExplicitDependency { /** * Name of this package; for use in dependency detection. */ get name() { return this.pkg.name; } constructor(modulePath) { this.modulePath = modulePath; this.pkg = new PackageJson_1.default(modulePath); } /** * Returns true if this module has declared an explicit peer dependency on * the provided module name. Used to sort all pertaining modules into * dependency order. * * Why only a peer dependency? Because the consuming project doesn't want * `pertain()` to scan infinitely into the module tree; only first-level * dependencies can pertain. So the dependency order is only relevant to * the dependencies that the root project has directly declared. * * TL;DR if you're building an extension framework with `pertain()`, you * should require extensions to use `peerDependencies` when using other * extensions. Otherwise they're not guaranteed to run in the right order. */ dependsOn(name) { return Reflect.has(this.pkg.peerDependencies, name); } /** * Returns a resolvable path to a JS module declared in this `package.json`, * at the JSON path supplied as `subject`. If no path exists, returns false. * * Example: If the subject is `"foo"`, AND `package.json` has a top-level * property `"foo": "./bar.js"`, then `this.pertains("foo")` will be * `"/path/to/this/module/bar.js"`. * * Dot notation lookup works: if the subject is `pwa.build`, then * `package.json`must have a top level `pwa` object with a `build` property. */ pertains(subject) { const pertaining = this.pkg.lookup(subject); // Only strings can be file paths, so anything else does not pertain if (!pertaining || typeof pertaining !== 'string') { debug('%s: Subject %s resolved to %s', this.pkg.name, subject, pertaining); return false; } debug('found declaration of "%s" as "%s" in "%s"', subject, pertaining, this.pkg.name); // This is the indicated path which would pertain; is it requireable? const subscriber = path_1.default.join(this.modulePath, pertaining); try { const pertainingModule = require.resolve(subscriber); debug('found runnable module at %s', pertainingModule); return pertainingModule; } catch (e) { throw new PertainError_1.default(`"${this.pkg.name}" declares a "${subject}" module, but Node could not find a valid JS module to load from "${subscriber}"`); } } /* istanbul ignore next */ toString() { return `${this.pkg.name} @ ${this.modulePath}`; } } exports.default = ExplicitDependency; //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiRXhwbGljaXREZXBlbmRlbmN5LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL0V4cGxpY2l0RGVwZW5kZW5jeS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OztBQUFBLGtEQUE4QjtBQUM5QixnREFBd0I7QUFDeEIsZ0VBQXdDO0FBQ3hDLGtFQUEwQztBQUUxQyxNQUFNLEtBQUssR0FBRyxJQUFBLGVBQVMsRUFBQyw0QkFBNEIsQ0FBQyxDQUFDO0FBRXREOzs7O0dBSUc7QUFDSCxNQUFxQixrQkFBa0I7SUFVckM7O09BRUc7SUFDSCxJQUFXLElBQUk7UUFDYixPQUFPLElBQUksQ0FBQyxHQUFHLENBQUMsSUFBSSxDQUFDO0lBQ3ZCLENBQUM7SUFFRCxZQUFZLFVBQWtCO1FBQzVCLElBQUksQ0FBQyxVQUFVLEdBQUcsVUFBVSxDQUFDO1FBQzdCLElBQUksQ0FBQyxHQUFHLEdBQUcsSUFBSSxxQkFBVyxDQUFDLFVBQVUsQ0FBQyxDQUFDO0lBQ3pDLENBQUM7SUFFRDs7Ozs7Ozs7Ozs7OztPQWFHO0lBQ0ksU0FBUyxDQUFDLElBQVk7UUFDM0IsT0FBTyxPQUFPLENBQUMsR0FBRyxDQUFDLElBQUksQ0FBQyxHQUFHLENBQUMsZ0JBQWdCLEVBQUMsSUFBSSxDQUFDLENBQUM7SUFDckQsQ0FBQztJQUVEOzs7Ozs7Ozs7O09BVUc7SUFDSSxRQUFRLENBQUMsT0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