command_runner.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. var exec = Npm.require('child_process').exec,
  2. execSync = function(cmd, options, stdoutHandler, stderrHandler, callback) {
  3. stdoutHandler('$ ' + cmd);
  4. exec(cmd,
  5. options,
  6. Meteor.bindEnvironment(
  7. function(errors, stdout, stderr) {
  8. if( stdout !== '' ) {
  9. stdoutHandler(stdout);
  10. }
  11. if( stderr != '' ) {
  12. stderrHandler(stderr);
  13. } else if( errors ) {
  14. stderrHandler('Internal error');
  15. }
  16. callback(errors);
  17. }
  18. )
  19. );
  20. },
  21. replace = function(string, customs = {}) {
  22. var globals = {'%ROOT_CWD%': DEPLOYMENT_FOLDER};
  23. for(var key in globals) {
  24. string = string.replace(key, globals[key]);
  25. }
  26. for(var key in customs) {
  27. string = string.replace(key, customs[key]);
  28. }
  29. return string;
  30. };
  31. CommandRunner = {
  32. run: function( data, callback = undefined) {
  33. var bundle = _.extend({deployment: {}, project:{}, stdout: console.log, stderr: console.error, counter: 0, step: 0}, data),
  34. customs = {'%CWD%': bundle.project._id, '%GIT%': bundle.project.git_url};
  35. var line = bundle.script[bundle.counter],
  36. command = replace(line.cmd, customs ),
  37. options = line.options;
  38. options.cwd = replace(options.cwd, customs);
  39. execSync(command, options, bundle.stdout, bundle.stderr, function(errors) {
  40. if( errors ) {
  41. if( callback ) {
  42. return callback();
  43. } else {
  44. return;
  45. }
  46. }
  47. bundle.counter++;
  48. if( bundle.counter >= bundle.script.length ) {
  49. if( bundle.step < 1 ) {
  50. bundle.step++;
  51. bundle.counter = 0;
  52. if( bundle.project.commands ) {
  53. bundle.script = bundle.project.commands.split('\n');
  54. CommandRunner.commands(bundle, callback);
  55. } else if( bundle.project.run ) {
  56. CommandRunner.launch(bundle, callback);
  57. } else if( callback ) {
  58. callback();
  59. }
  60. } else if( callback ) {
  61. callback();
  62. }
  63. } else {
  64. CommandRunner.run(bundle, callback);
  65. }
  66. });
  67. },
  68. commands: function(bundle, callback = undefined) {
  69. var command = bundle.script[bundle.counter],
  70. customs = {'%CWD%': bundle.project._id},
  71. options = {
  72. cwd: replace('%ROOT_CWD%/%CWD%', customs)
  73. };
  74. execSync(command, options, bundle.stdout, bundle.stderr, function(errors) {
  75. if( errors ) {
  76. if( callback ) {
  77. return callback();
  78. } else {
  79. return;
  80. }
  81. }
  82. bundle.counter++;
  83. if( bundle.counter >= bundle.script.length ) {
  84. if( bundle.project.run ) {
  85. CommandRunner.launch(bundle, callback);
  86. } else {
  87. callback();
  88. }
  89. } else {
  90. CommandRunner.commands(bundle, callback);
  91. }
  92. });
  93. },
  94. launch: function(bundle, callback = undefined) {
  95. var command = bundle.project.run,
  96. customs = {'%CWD%': bundle.project._id},
  97. options = {
  98. cwd: replace('%ROOT_CWD%/%CWD%', customs),
  99. env: {}
  100. },
  101. variables = bundle.project.variables;
  102. for( var index in variables ) {
  103. options.env[variables[index].name] = variables[index].value;
  104. }
  105. execSync(command, options, bundle.stdout, bundle.stderr, function(errors) {
  106. if( callback ) {
  107. return callback();
  108. } else {
  109. return;
  110. }
  111. });
  112. }
  113. }