make.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. require('./global');
  2. global.config.fatal = true;
  3. global.target = {};
  4. var args = process.argv.slice(2),
  5. targetArgs,
  6. dashesLoc = args.indexOf('--');
  7. // split args, everything after -- if only for targets
  8. if (dashesLoc > -1) {
  9. targetArgs = args.slice(dashesLoc + 1, args.length);
  10. args = args.slice(0, dashesLoc);
  11. }
  12. // This ensures we only execute the script targets after the entire script has
  13. // been evaluated
  14. setTimeout(function() {
  15. var t;
  16. if (args.length === 1 && args[0] === '--help') {
  17. console.log('Available targets:');
  18. for (t in global.target)
  19. console.log(' ' + t);
  20. return;
  21. }
  22. // Wrap targets to prevent duplicate execution
  23. for (t in global.target) {
  24. (function(t, oldTarget){
  25. // Wrap it
  26. global.target[t] = function() {
  27. if (!oldTarget.done){
  28. oldTarget.done = true;
  29. oldTarget.result = oldTarget.apply(oldTarget, arguments);
  30. }
  31. return oldTarget.result;
  32. };
  33. })(t, global.target[t]);
  34. }
  35. // Execute desired targets
  36. if (args.length > 0) {
  37. args.forEach(function(arg) {
  38. if (arg in global.target)
  39. global.target[arg](targetArgs);
  40. else {
  41. console.log('no such target: ' + arg);
  42. }
  43. });
  44. } else if ('all' in global.target) {
  45. global.target.all(targetArgs);
  46. }
  47. }, 0);