-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
75 lines (66 loc) · 1.93 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// NPM
const yargs = require('yargs').argv
const ProgressBar = require('progress')
const fs = require('fs')
const chalk = require('chalk')
// Local Tools
const init = require('./tools/init')
const install = require('./tools/install')
if (yargs.init) {
init()
} else {
// Make sure we have the mpm.json file locally
try {
require.resolve(`${process.cwd()}/mpm.json`)
} catch (e) {
console.error('No mpm.json found, are you in the right place?')
process.exit();
}
// Load up our options
let opts = {
install: (yargs.i) ? true : false,
init: (yargs.init) ? true : false,
verbose: (yargs.v) ? true : false,
shortLocation: (yargs.p) ?
`${yargs.p}` : `plugins`,
location: (yargs.p) ?
`${process.cwd()}/${yargs.p}` : `${process.cwd()}/plugins`,
package: require(`${process.cwd()}/mpm.json`),
ticks: 0
}
// Ticks
opts.ticks = Object.keys(opts.package.plugins).length
if (yargs.j) opts.ticks = opts.ticks + 1;
if (opts.package.configs) {
opts.ticks = opts.ticks + Object.keys(opts.package.configs).length
}
// Yargs args
if (yargs.a) {
let newPackage = opts.package
let name = yargs.a.split('@')[0]
let resource = yargs.a.split('@')[1]
newPackage.plugins[name] = resource
opts.package = newPackage
fs.writeFile(`${process.cwd()}/mpm.json`, JSON.stringify(newPackage, null, 2), () => {
console.log(
`${chalk.green('INSTALLED:')} ${name} at ${resource}`
);
});
}
// Progress Bar
opts.bar = new ProgressBar(
`${chalk.blue(':plugin → :to')} ${chalk.green(':bar :elapsed')}`,
{
total: opts.ticks,
incomplete: '░',
complete: '█',
width: 50
})
// If plugins location directory doesn't exist, create it.
if (!fs.existsSync(opts.location)){
fs.mkdirSync(opts.location);
}
if (opts.install) {
install(opts, yargs)
}
}