1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | const path = require('path')
|
7 | const fs = require('fs')
|
8 | const execa = require('execa')
|
9 | const chalk = require('chalk')
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | const pluginREG = /^(mip-|@[\w-]+\/mip-)cli-plugin-/
|
15 |
|
16 |
|
17 | const installedPath = path.join(__dirname, '../../..')
|
18 |
|
19 |
|
20 | exports.resolvePluginName = name => {
|
21 | if (pluginREG.test(name)) {
|
22 | return name
|
23 | }
|
24 |
|
25 | return `mip-cli-plugin-${name}`
|
26 | }
|
27 |
|
28 | exports.installPackage = async (targetDir, command, packageName) => {
|
29 | const args = ['install', '--loglevel', 'error']
|
30 | args.push(packageName)
|
31 | await exports.executeCommand(command, args, targetDir)
|
32 | }
|
33 |
|
34 | exports.isInstalled = pkg => {
|
35 | const packageDir = path.join(installedPath, pkg)
|
36 | return fs.existsSync(packageDir)
|
37 | }
|
38 |
|
39 | exports.executeCommand = (command, args, targetDir) => {
|
40 | return new Promise((resolve, reject) => {
|
41 | const child = execa(command, args, {
|
42 | cwd: targetDir,
|
43 | stdio: 'inherit'
|
44 | })
|
45 |
|
46 | child.on('close', code => {
|
47 | if (code !== 0) {
|
48 | reject(new Error(`command failed: ${command} ${args.join(' ')}`))
|
49 | return
|
50 | }
|
51 | resolve()
|
52 | })
|
53 | })
|
54 | }
|
55 |
|
56 | exports.resolve = pkg => {
|
57 | function fix (dir) {
|
58 | let idx = dir.lastIndexOf(pkg)
|
59 | if (idx === -1) {
|
60 | throw new Error('Invalid package name')
|
61 | }
|
62 | dir = dir.substr(0, idx + pkg.length)
|
63 |
|
64 | return dir
|
65 | }
|
66 |
|
67 |
|
68 | try {
|
69 | return fix(require.resolve(pkg))
|
70 | } catch (e) {
|
71 | return path.resolve(__dirname, '..', '..', '..', pkg)
|
72 | }
|
73 | }
|
74 |
|
75 | exports.resolveCommand = pkg => {
|
76 |
|
77 |
|
78 | return pkg.replace(pluginREG, '')
|
79 | }
|
80 |
|
81 | exports.showPluginCmdHelpInfo = () => {
|
82 | let pluginsPkgs = exports.getPluginPackages()
|
83 |
|
84 | if (!pluginsPkgs.length) {
|
85 | return
|
86 | }
|
87 |
|
88 | console.log()
|
89 | console.log(' User Plugin Commands:')
|
90 | console.log()
|
91 |
|
92 | pluginsPkgs.forEach(pkg => {
|
93 | let cmd
|
94 | let description
|
95 | try {
|
96 | cmd = exports.resolveCommand(pkg)
|
97 | const cmdModule = require(path.join(exports.resolve(pkg), 'cli', cmd))
|
98 | description = cmdModule.cli.config.description
|
99 | } catch (e) {}
|
100 |
|
101 | if (cmd) {
|
102 |
|
103 | const MAGIC = 15
|
104 | console.log(` ${pad(cmd, MAGIC)}${chalk.green(description || '')}`)
|
105 | }
|
106 | })
|
107 |
|
108 | console.log()
|
109 | }
|
110 |
|
111 | exports.getPluginPackages = () => {
|
112 | let pkgs = fs.readdirSync(installedPath)
|
113 | return pkgs.filter(item => pluginREG.test(item))
|
114 | }
|
115 |
|
116 | function pad (str, width) {
|
117 | let len = Math.max(0, width - str.length)
|
118 | return str + Array(len + 1).join(' ')
|
119 | }
|