UNPKG

@dojo/cli

Version:

Dojo CLI utility

92 lines 3.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const search = require("libnpmsearch"); const path_1 = require("path"); const spawn = require('cross-spawn'); const Configstore = require("configstore"); const loadCommands_1 = require("./loadCommands"); const chalk_1 = require("chalk"); const ONE_DAY = 1000 * 60 * 60 * 24; function default_1(name) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const conf = new Configstore(name); let commands = conf.get('commands') || []; if (commands.length) { const lastUpdated = conf.get('lastUpdated'); if (Date.now() - lastUpdated >= ONE_DAY) { spawn(process.execPath, [path_1.join(__dirname, 'detachedCheckForNewCommands.js'), JSON.stringify({ name })], { detached: true, stdio: 'ignore' }).unref(); } } else { commands = yield getLatestCommands(name); } return commands; }); } exports.default = default_1; function getLatestCommands(name) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const conf = new Configstore(name); const commands = yield searchNpmForCommands(); if (commands && commands.length) { conf.set('commands', commands); conf.set('lastUpdated', Date.now()); } return commands || []; }); } exports.getLatestCommands = getLatestCommands; function searchNpmForCommands() { return tslib_1.__awaiter(this, void 0, void 0, function* () { try { const results = yield search('@dojo/cli-'); return results .filter((result) => { return result.scope === 'dojo' && result.name !== '@dojo/cli'; }) .map(({ name, version, description }) => { return { name, version, description }; }); } catch (error) { console.error('There was an error searching npm: ', error.message || error); } }); } function mergeInstalledCommandsWithAvailableCommands(groupMap, availableCommands) { const regEx = /@dojo\/cli-([^-]+)-(.+)/; availableCommands.forEach((command) => { const [, group, name] = regEx.exec(command.name); const installCommand = `npm i ${command.name}`; const commandWrapper = { name, group, path: installCommand, description: command.description, global: false, installed: false, register: () => { }, run: () => { console.log(`\nTo install this command run ${chalk_1.default.green(installCommand)}\n`); return Promise.resolve(); } }; if (!loadCommands_1.isEjected(group, name)) { if (!groupMap.has(group)) { commandWrapper.default = true; groupMap.set(group, new Map()); } const subCommandsMap = groupMap.get(group); if (!subCommandsMap.has(name)) { subCommandsMap.set(name, commandWrapper); } } }); return groupMap; } exports.mergeInstalledCommandsWithAvailableCommands = mergeInstalledCommandsWithAvailableCommands; //# sourceMappingURL=installableCommands.js.map