UNPKG

3.98 kBJavaScriptView Raw
1
2var path = require('path');
3var fs = require('fs');
4var os = require('os');
5var spawn = require('child_process').spawn;
6var chalk = require('chalk');
7var parallel = require('async/parallel');
8
9var Configuration = require('../../Configuration.js');
10var cst = require('../../../constants.js');
11var Common = require('../../Common');
12var Utility = require('../../Utility.js');
13var readline = require('readline')
14
15var INTERNAL_MODULES = {
16 'deep-monitoring': {
17 dependencies: [{name: 'v8-profiler-node8'}, {name: 'gc-stats'}, {name: 'event-loop-inspector'}]
18 },
19 'gc-stats': {name: 'gc-stats'},
20 'event-loop-inspector': {name: 'event-loop-inspector'},
21 'v8-profiler': {name: 'v8-profiler-node8'},
22 'profiler': {name: 'v8-profiler-node8'},
23 'typescript': {dependencies: [{name: 'typescript'}, {name: 'ts-node@latest'}]},
24 'livescript': {name: 'livescript'},
25 'coffee-script': {name: 'coffee-script', message: 'Coffeescript v1 support'},
26 'coffeescript': {name: 'coffeescript', message: 'Coffeescript v2 support'}
27};
28
29module.exports = {
30 install,
31 INTERNAL_MODULES,
32 installMultipleModules
33}
34
35
36function install(module, cb, verbose) {
37 if (!module || !module.name || module.name.length === 0) {
38 return cb(new Error('No module name !'));
39 }
40
41 if (typeof verbose === 'undefined') {
42 verbose = true;
43 }
44
45 installLangModule(module.name, function (err) {
46 var display = module.message || module.name;
47 if (err) {
48 if (verbose) { Common.printError(cst.PREFIX_MSG_MOD_ERR + chalk.bold.green(display + ' installation has FAILED (checkout previous logs)')); }
49 return cb(err);
50 }
51
52 if (verbose) { Common.printOut(cst.PREFIX_MSG + chalk.bold.green(display + ' ENABLED')); }
53 return cb();
54 });
55}
56
57function installMultipleModules(modules, cb, post_install) {
58 var functionList = [];
59 for (var i = 0; i < modules.length; i++) {
60 functionList.push((function (index) {
61 return function (callback) {
62 var module = modules[index];
63 if (typeof modules[index] === 'string') {
64 module = {name: modules[index]};
65 }
66 install(module, function ($post_install, err, $index, $modules) {
67 try {
68 var install_instance = spawn(post_install[modules[index]], {
69 stdio : 'inherit',
70 env: process.env,
71 shell : true,
72 cwd : process.cwd()
73 });
74 Common.printOut(cst.PREFIX_MSG_MOD + 'Running configuraton script.');
75 }
76 catch(e)
77 {
78 Common.printOut(cst.PREFIX_MSG_MOD + 'No configuraton script found.');
79 }
80 callback(null, { module: module, err: err });
81 }, false);
82 };
83 })(i));
84 }
85
86 parallel(functionList, function (err, results) {
87 for (var i = 0; i < results.length; i++) {
88 var display = results[i].module.message || results[i].module.name;
89 if (results[i].err) {
90 err = results[i].err;
91 Common.printError(cst.PREFIX_MSG_MOD_ERR + chalk.bold.green(display + ' installation has FAILED (checkout previous logs)'));
92 } else {
93 Common.printOut(cst.PREFIX_MSG + chalk.bold.green(display + ' ENABLED'));
94 }
95 }
96
97 if(cb) cb(err);
98 });
99};
100
101function installLangModule(module_name, cb) {
102 var node_module_path = path.resolve(path.join(__dirname, '../../../'));
103 Common.printOut(cst.PREFIX_MSG_MOD + 'Calling ' + chalk.bold.red('[NPM]') + ' to install ' + module_name + ' ...');
104
105 var install_instance = spawn(cst.IS_WINDOWS ? 'npm.cmd' : 'npm', ['install', module_name, '--loglevel=error'], {
106 stdio : 'inherit',
107 env: process.env,
108 shell : true,
109 cwd : node_module_path
110 });
111
112 install_instance.on('close', function(code) {
113 if (code > 0)
114 return cb(new Error('Module install failed'));
115 return cb(null);
116 });
117
118 install_instance.on('error', function (err) {
119 console.error(err.stack || err);
120 });
121};