diff --git a/app/command/add.ts b/app/command/add.ts index 988792c..8fa94b8 100644 --- a/app/command/add.ts +++ b/app/command/add.ts @@ -8,25 +8,20 @@ const fs = require('fs-extra'); const _ = require('lodash'); const inquirer = require('inquirer'); const prompt = inquirer.createPromptModule(); -const observatory = require('observatory'); const which = require('which'); const uniqueString = require('unique-string'); const clipboardy = require('clipboardy'); import chalk from 'chalk'; const __ = require('i18n').__; -const cwd = process.cwd(); import { isExistPath, isLink, normalizePath, runShell } from '../utils'; import config from '../config'; -import plugin from '../plugin'; import registry from '../registry'; import globalConfig from '../global-config'; import Gpmrc from '../gpmrc'; import { info, warn } from '../logger'; -const ACTION = 'add'; - interface Argv$ { repo: string; } @@ -36,7 +31,6 @@ interface Options$ { unixify?: boolean; force?: boolean; name?: string; - plugin?: string; ignoreRc?: boolean; } @@ -160,46 +154,8 @@ async function add(repo: string, options: Options$) { warn(__('commands.add.log.warn_copy_clipboard')); } } - - // 获取add的相关插件 - const plugins = plugin.get(ACTION); - - process.chdir(entity.path); - - while (plugins.length) { - const plugin: any = plugins.shift(); - const task = observatory.add( - __('commands.add.log.info_run_plugin', { - name: chalk.green('gpm-plugin-' + plugin.name) - }) - ); - await new Promise((resolve, reject) => { - if (!_.isFunction(plugin.add)) { - reject( - new Error( - __('commands.add.log.err_run_plugin', { - name: plugin.name.red - }) - ) - ); - } else { - plugin.add.call(entity, err => { - if (err) { - task.fail(__('global.tips.fail')); - reject(err); - } else { - task.done('global.tips.success'); - resolve(); - } - }); - } - }); - } - - process.chdir(cwd); } export default async function(argv: Argv$, options: Options$) { - plugin.load(ACTION, options.plugin || ''); return await add(argv.repo, options); } diff --git a/app/command/plugin.ts b/app/command/plugin.ts deleted file mode 100644 index 185ee62..0000000 --- a/app/command/plugin.ts +++ /dev/null @@ -1,59 +0,0 @@ -/** - * Created by axetroy on 2017/3/18. - */ - -import chalk from 'chalk'; -const __ = require('i18n').__; - -import plugin from '../plugin'; -import config from '../config'; -import { error, info } from '../logger'; - -type action$ = 'LIST' | 'REMOVE'; - -interface Argv$ { - action: action$; - key: string; -} - -interface Options$ { - nolog?: boolean; - unixify?: boolean; - force?: boolean; -} - -export default async function configHandler( - argv: Argv$, - options: Options$ -): Promise { - const { action, key } = argv; - - const upperCaseAction: action$ = action.toUpperCase(); - - switch (upperCaseAction) { - case 'LIST': - plugin.list(); - break; - case 'REMOVE': - if (!key) - if (!options.nolog) { - return error( - __('commands.plugin.log.require_key', { - cmd: chalk.green(config.name + ` config set ${key} [key]`) - }) - ); - } - await plugin.remove(key); - break; - default: - !options.nolog && - info( - __('commands.plugin.log.info_help', { - cmd: `${config.name} plugin ${chalk.yellow.underline( - 'list|delete' - )} ${chalk.green(' [key]')}` - }) - ); - break; - } -} diff --git a/app/gpm.ts b/app/gpm.ts index 215a552..823982c 100644 --- a/app/gpm.ts +++ b/app/gpm.ts @@ -19,7 +19,6 @@ import runtimeHandler from './command/runtime'; import configHandler from './command/config'; import cleanHandler from './command/clean'; import findHandler from './command/find'; -import pluginHandler from './command/plugin'; import removeHandler from './command/remove'; inquirer.registerPrompt( @@ -253,24 +252,6 @@ class Gpm extends EventEmitter { .action(function(argv, options) { return runtimeHandler(argv, options).catch(errorHandler); }); - - program - .command('plugin', __('commands.plugin.desc')) - .alias('pl') - .argument('', __('commands.plugin.argv.action.desc')) - .argument('[key]', __('commands.plugin.argv.key.desc')) - .option(FLAGS.unixify.flag, FLAGS.unixify.desc) - .option(FLAGS.force.flag, FLAGS.force.desc) - .option(FLAGS.nolog.flag, FLAGS.nolog.desc) - .action(function( - argv: { - action: any; - key: string; - }, - options - ) { - return pluginHandler(argv, options).catch(errorHandler); - }); } /** diff --git a/app/plugin.ts b/app/plugin.ts deleted file mode 100644 index 2334c8e..0000000 --- a/app/plugin.ts +++ /dev/null @@ -1,160 +0,0 @@ -/** - * Created by axetroy on 17-3-13. - */ -import { EventEmitter } from 'events'; -import * as path from 'path'; -import * as spawn from 'cross-spawn'; -import * as fs from 'fs-extra'; -import * as globalPackageLoader from 'global-modules-path'; -import * as _ from 'lodash'; -import 'console.table'; -import chalk from 'chalk'; - -import { camelcase } from './utils'; - -const GLOBAL_NODE_MODULES_PATH = globalPackageLoader.getPath('./'); - -export interface Plugin$ { - name?: string; - add?: Function; - foreach?: Function; - __gpm__path__?: string; -} - -export interface Plugins$ { - [actionName: string]: Plugin$[]; -} - -export interface PluginLoader$ { - [pluginName: string]: Plugin$; -} - -export interface PluginObj$ { - name: string; - description: string; - homepage: string; -} - -export interface LoaderOutput$ { - [pluginName: string]: Plugin$; -} - -// 支持的插件action -type ACTION$ = 'add' | 'foreach'; - -class Plugin extends EventEmitter { - private plugins: Plugins$ = {}; - private $: PluginLoader$ = this.loader(); - constructor() { - super(); - } - - /** - * get a plugin - * @param {ACTION$} action - * @returns {Plugin$[]} - */ - get(action: ACTION$): Plugin$[] { - return (this.plugins[action] || []).slice(); - } - - /** - * remove a plugin - * @param {string} pluginName - * @returns {Promise} - */ - remove(pluginName: string): Promise { - return new Promise((resolve, reject) => { - const uninstall = spawn( - 'npm', - ['uninstall', `gpm-plugin-${pluginName}`], - { stdio: 'inherit' } - ); - uninstall.on('close', (code: number, signal: string) => { - if (code !== 0) { - reject(code); - throw new Error(`Error Code: ${code}, Exist Signal: ${signal}`); - } else { - resolve(code); - } - }); - }); - } - - /** - * print the list of plugins - * @returns {PluginObj$[]} - */ - list(): PluginObj$[] { - let table: PluginObj$[] = []; - _.each(this.$, (v: any, name) => { - const pkg = fs.readJsonSync(path.join(v.__gpm__path__, 'package.json')); - table.push({ - name: chalk.yellow(pkg.name || ''), - description: chalk.gray(pkg.description || ''), - homepage: pkg.homepage || '' - }); - }); - console.table(table); - return table; - } - - /** - * get one plugin - * @param {string} action - * @param {string} pluginName - * @returns {Plugin$} - */ - load(action: ACTION$, pluginName: string): Plugin$ { - const container: Plugin$[] = (this.plugins[action] = - this.plugins[action] || []); - let plugin: Plugin$ = {}; - if (pluginName) { - const pluginFullName: string = `gpm-plugin-${pluginName}`; - const pluginPath: string = globalPackageLoader.getPath(pluginFullName); - if (!pluginFullName) { - throw new Error( - `Can not found ${pluginFullName}, Please make sure you have install it in global` - ); - } - try { - plugin = require(pluginPath); - } catch (err) { - throw err; - } - plugin.name = plugin.name || pluginName; - if (_.isEmpty(plugin)) - throw new Error( - `Can not found ${chalk.green( - pluginFullName - )}, Please make sure you have install it in global` - ); - container.push(plugin); - } - return plugin; - } - - /** - * load all plugins - * @returns {LoaderOutput$} - */ - loader(): LoaderOutput$ { - const output: LoaderOutput$ = {}; - const modules: string[] = fs.readdirSync(GLOBAL_NODE_MODULES_PATH) || []; - modules - .filter(module => /gpm-plugin-[\w\-\_]+/.test(module)) - .forEach((moduleName: string) => { - const shortName: string = moduleName.replace(/^gpm-plugin-/, ''); - const modulePath: string = path.join( - GLOBAL_NODE_MODULES_PATH, - moduleName - ); - const module: Plugin$ = require(modulePath); - module.__gpm__path__ = modulePath; - output[camelcase(shortName)] = module; - }); - return output; - } -} - -export default new Plugin(); diff --git a/locales/en_US.json b/locales/en_US.json index 6ea91d7..dd71376 100644 --- a/locales/en_US.json +++ b/locales/en_US.json @@ -113,21 +113,6 @@ }, "runtime": { "desc": "Print the program runtime, useful for submit issue." - }, - "plugin": { - "desc": "A series handler of plugin.", - "argv": { - "action": { - "desc": "Plugin action." - }, - "key": { - "desc": "Plugin key." - } - }, - "log": { - "require_key": "The [key] must be required, please try again: {{{cmd}}}", - "info_help": "Flow the command line: {{{cmd}}}" - } } } } diff --git a/locales/zh_CN.json b/locales/zh_CN.json index 6e63998..45dec01 100644 --- a/locales/zh_CN.json +++ b/locales/zh_CN.json @@ -115,21 +115,6 @@ }, "runtime": { "desc": "输出程序的运行环境," - }, - "plugin": { - "desc": "插件的一系列操作", - "argv": { - "action": { - "desc": "插件的action" - }, - "key": { - "desc": "插件的key" - } - }, - "log": { - "require_key": "缺少参数 [key], 请再尝试一次命令: {{{cmd}}}", - "info_help": "请遵循这条命令: {{{cmd}}}" - } } } }