cli-engine
Version:
Generic CLI Framework
152 lines (122 loc) • 3.5 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
require('cli-engine-command');
require('cli-engine-config');
var _plugin = require('./plugin');
var _plugin2 = _interopRequireDefault(_plugin);
var _manager = require('./manager');
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _fsExtra = require('fs-extra');
var _fsExtra2 = _interopRequireDefault(_fsExtra);
var _lock = require('../lock');
var _lock2 = _interopRequireDefault(_lock);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
class Cache {
constructor(output) {
this.out = output;
this.config = output.config;
}
initialize() {
this._cache = {
version: this.config.version,
plugins: {},
node_version: null
};
}
clear() {
this._cache = {
version: this.config.version,
plugins: {},
node_version: this._cache.node_version
};
}
get file() {
return _path2.default.join(this.config.cacheDir, 'plugins.json');
}
get cache() {
if (this._cache) return this._cache;
try {
this._cache = _fsExtra2.default.readJSONSync(this.file);
} catch (err) {
if (err.code !== 'ENOENT') this.out.debug(err);
this.initialize();
}
if (this._cache.version !== this.config.version) {
this.clear();
}
return this._cache;
}
plugin(path) {
return this.cache.plugins[path];
}
updatePlugin(path, plugin) {
this.constructor.updated = true;
this.cache.plugins[path] = plugin;
}
deletePlugin(...paths) {
for (let path of paths) {
this.out.debug(`clearing cache for ${path}`);
this.constructor.updated = true;
delete this.cache.plugins[path];
}
this.save();
}
async fetch(pluginPath) {
let c = this.plugin(pluginPath.path);
if (c) return c;
try {
this.out.debug('updating cache for ' + pluginPath.path);
let cachedPlugin = await pluginPath.convertToCached();
this.updatePlugin(pluginPath.path, cachedPlugin);
return cachedPlugin;
} catch (err) {
if (pluginPath.type === 'builtin') throw err;
if (await pluginPath.repair(err)) return this.fetch(pluginPath);
this.out.warn(`Error parsing plugin ${pluginPath.path}`);
this.out.warn(err);
return {
name: pluginPath.path,
path: pluginPath.path,
version: '',
commands: [],
topics: []
};
}
}
async fetchManagers(...managers) {
let plugins = [];
if (this.cache.node_version !== process.version) {
let lock = new _lock2.default(this.out);
let downgrade = await lock.upgrade();
for (let manager of managers) {
await manager.handleNodeVersionChange();
}
await downgrade();
this.cache.node_version = process.version;
this.constructor.updated = true;
}
for (let manager of managers) {
let paths = await manager.list();
for (let path of paths) {
let plugin = await this.fetch(path);
if (plugins.find(p => p.name === plugin.name)) continue;
plugins.push(new _plugin2.default(this.out, path, plugin));
}
}
this.save();
return plugins;
}
save() {
if (!this.constructor.updated) return;
try {
_fsExtra2.default.writeJSONSync(this.file, this.cache);
} catch (err) {
this.out.warn(err);
}
}
}
exports.default = Cache;
Cache.updated = false;