LOCAL.js 3.9 KB

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