|
1
|
|
|
'use strict' |
|
2
|
|
|
|
|
3
|
|
|
const fs = require('fs') |
|
4
|
|
|
const path = require('path') |
|
5
|
|
|
const chalk = require('chalk') |
|
6
|
|
|
const jsonfile = require('jsonfile') |
|
7
|
|
|
|
|
8
|
|
|
const output = require('../output') |
|
9
|
|
|
const util = require('../util') |
|
10
|
|
|
const Module = require('../containers/Module') |
|
11
|
|
|
|
|
12
|
|
|
const dir = util.getPluginsDirectory() |
|
13
|
|
|
const pkg = path.join(dir, 'package.json') |
|
14
|
|
|
|
|
15
|
|
|
const Plugins = { |
|
16
|
|
|
directory: dir, |
|
17
|
|
|
plugins: getDirectories(), |
|
18
|
|
|
packageFile: pkg |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
function getDirectories () { |
|
22
|
|
|
try { |
|
23
|
|
|
var deps = jsonfile.readFileSync(pkg) |
|
24
|
|
|
if (!deps || !deps.dependencies || typeof deps.dependencies !== 'object') { |
|
25
|
|
|
return [] |
|
26
|
|
|
} |
|
27
|
|
|
return Object.keys(deps.dependencies).filter(function (file) { |
|
28
|
|
|
return fs.existsSync(path.join(dir, 'node_modules', file, 'package.json')) |
|
29
|
|
|
}) |
|
30
|
|
|
} catch (e) { |
|
31
|
|
|
// Do nothing |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
return [] |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
Plugins.applyPlugins = function (program) { |
|
38
|
|
|
// Patch 'program' |
|
39
|
|
|
program.util = util |
|
40
|
|
|
program.request = util.request |
|
41
|
|
|
program.output = output |
|
42
|
|
|
program.module = Module |
|
43
|
|
|
program.plugins = Plugins |
|
44
|
|
|
|
|
45
|
|
|
// Read directories |
|
46
|
|
|
for (var x in Plugins.plugins) { |
|
47
|
|
|
if (!Plugins.plugins.hasOwnProperty(x)) { |
|
48
|
|
|
continue |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
require(path.join(dir, 'node_modules', Plugins.plugins[x]))(program) |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
Plugins.listPlugins = function () { |
|
56
|
|
|
for (var x in Plugins.plugins) { |
|
57
|
|
|
if (!Plugins.plugins.hasOwnProperty(x)) { |
|
58
|
|
|
continue |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
Plugins.describePlugin(Plugins.plugins[x]) |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
Plugins.describePlugin = function (pluginName) { |
|
66
|
|
|
var info = require(path.join(dir, 'node_modules', pluginName, 'package.json')) |
|
67
|
|
|
var title = chalk.underline(info.name) + ' (' + info.version + ')' |
|
68
|
|
|
if ('description' in info && info.description && info.description.length) { |
|
69
|
|
|
title += '\n ' + info.description |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
output.log(title) |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
module.exports = Plugins |
|
76
|
|
|
|