|
1 | 1 | 'use strict'; |
2 | 2 |
|
| 3 | +const shelljs = require('shelljs'); |
| 4 | +const path = require('path'); |
| 5 | +const chalk = require('chalk'); |
3 | 6 |
|
4 | | -// if (program.init) { |
5 | | -// const mainPath = require.resolve('@micro-app/core'); |
6 | | -// const configPath = path.resolve(mainPath, '../../', microApp.CONSTANT.CONFIG_NAME); |
7 | | -// const dest = path.resolve(process.cwd(), microApp.CONSTANT.CONFIG_NAME); |
8 | | -// shelljs.cp('-R', configPath, dest); |
| 7 | +module.exports = function initCommand(api, opts) { |
9 | 8 |
|
10 | | -// logger.success(`Init Fnished, Create: ${chalk.yellow(microApp.CONSTANT.CONFIG_NAME)}`); |
11 | | -// } |
| 9 | + api.registerMethod('beforeCommandInit', { |
| 10 | + type: api.API_TYPE.EVENT, |
| 11 | + description: '初始化前事件', |
| 12 | + }); |
| 13 | + api.registerMethod('afterCommandInit', { |
| 14 | + type: api.API_TYPE.EVENT, |
| 15 | + description: '初始化完毕后事件', |
| 16 | + }); |
| 17 | + |
| 18 | + // start |
| 19 | + api.registerCommand('init', { |
| 20 | + description: 'init micro app project.', |
| 21 | + usage: 'micro-app init [options]', |
| 22 | + options: { |
| 23 | + '-': 'init default.', |
| 24 | + // '-n <name>': 'only init <name>.', |
| 25 | + }, |
| 26 | + details: ` |
| 27 | +Examples: |
| 28 | + ${chalk.gray('# init')} |
| 29 | + micro-app init |
| 30 | + `.trim(), |
| 31 | + }, args => { |
| 32 | + return initMicro(api, args); |
| 33 | + }); |
| 34 | +}; |
| 35 | + |
| 36 | +function initMicro(api, args) { |
| 37 | + const logger = api.logger; |
| 38 | + const config = api.self; |
| 39 | + const micros = api.micros; |
| 40 | + const microsConfig = api.microsConfig; |
| 41 | + |
| 42 | + api.applyPluginHooks('beforeCommandInit', { args, logger, microsConfig, micros, config }); |
| 43 | + |
| 44 | + const microApp = require('@micro-app/core'); |
| 45 | + const mainPath = require.resolve('@micro-app/core'); |
| 46 | + const from = path.resolve(mainPath, '../../', microApp.CONSTANTS.CONFIG_NAME); |
| 47 | + const to = path.resolve(microApp.CONSTANTS.ROOT, microApp.CONSTANTS.CONFIG_NAME); |
| 48 | + shelljs.cp('-R', from, to); |
| 49 | + |
| 50 | + api.applyPluginHooks('afterCommandInit', { args, logger, microsConfig, micros, config, from, to }); |
| 51 | + |
| 52 | + logger.success(`Init Fnished, Create: ${chalk.yellow(microApp.CONSTANTS.CONFIG_NAME)}`); |
| 53 | +} |
0 commit comments