UNPKG

cli-engine

Version:
68 lines (53 loc) 2 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _fsExtra = require('fs-extra'); var _fsExtra2 = _interopRequireDefault(_fsExtra); var _path = require('path'); var _path2 = _interopRequireDefault(_path); require('cli-engine-config'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } exports.default = class { static get notPermittedError() { return new Error('Plugin\'s namespace not included in permitted namespaces'); } static throwErrorIfNotPermitted(pluginPath, config) { if (this._permitted(pluginPath, config)) return; throw this.notPermittedError; } static _permitted(pluginPath, config) { let namespace = this._readNamespace(pluginPath); return ['root', 'namespace'].includes(this._installLevel(namespace, config)); } static _installLevel(namespace, config) { let cliBin = config.bin; let namespaces = config.namespaces; if (!namespace && !namespaces) namespace = namespaces = null; if (cliBin === namespace || !namespaces && !namespace) { return 'root'; } else if (namespaces && namespaces.includes(namespace)) { return 'namespace'; } return 'not-permitted-for-install'; } static _readNamespace(pluginPath) { try { let pjson = _fsExtra2.default.readJSONSync(_path2.default.join(pluginPath, 'package.json')); return pjson['cli-engine'] ? pjson['cli-engine'].namespace : undefined; } catch (err) {} } static metaData(pluginPath, config) { let pjsonNamespace = this._readNamespace(pluginPath); let permitted = this._permitted(pluginPath, config); let installLevel = this._installLevel(pjsonNamespace, config); if (installLevel === 'not-permitted-for-install') installLevel = undefined; let namespace = permitted && installLevel === 'namespace' ? pjsonNamespace : undefined; return { permitted, installLevel, namespace, pjsonNamespace }; } };