1
0

command_runner.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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, deploy_script: true}, 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.deploy_script && bundle.project.commands ) {
  50. bundle.deploy_script = false;
  51. bundle.script = bundle.project.commands.split('\n');
  52. bundle.counter = 0;
  53. CommandRunner.commands(bundle, callback);
  54. } else if( callback ) {
  55. callback();
  56. }
  57. } else {
  58. CommandRunner.run(bundle, callback);
  59. }
  60. });
  61. },
  62. commands: function(bundle, callback = undefined) {
  63. var command = bundle.script[bundle.counter],
  64. customs = {'%CWD%': bundle.project._id},
  65. options = {
  66. cwd: replace('%ROOT_CWD%/%CWD%', customs)
  67. };
  68. execSync(command, options, bundle.stdout, bundle.stderr, function(errors) {
  69. if( errors ) {
  70. if( callback ) {
  71. return callback();
  72. } else {
  73. return;
  74. }
  75. }
  76. bundle.counter++;
  77. if( bundle.counter >= bundle.script.length ) {
  78. callback();
  79. } else {
  80. CommandRunner.commands(bundle, callback);
  81. }
  82. });
  83. }
  84. }