shjs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env node
  2. if (require.main !== module) {
  3. throw new Error('Executable-only module should not be required');
  4. }
  5. // we must import global ShellJS methods after the require.main check to prevent the global
  6. // namespace from being polluted if the error is caught
  7. require('../global');
  8. function exitWithErrorMessage(msg) {
  9. console.log(msg);
  10. console.log();
  11. process.exit(1);
  12. }
  13. if (process.argv.length < 3) {
  14. exitWithErrorMessage('ShellJS: missing argument (script name)');
  15. }
  16. var args,
  17. scriptName = process.argv[2];
  18. env['NODE_PATH'] = __dirname + '/../..';
  19. if (!scriptName.match(/\.js/) && !scriptName.match(/\.coffee/)) {
  20. if (test('-f', scriptName + '.js'))
  21. scriptName += '.js';
  22. if (test('-f', scriptName + '.coffee'))
  23. scriptName += '.coffee';
  24. }
  25. if (!test('-f', scriptName)) {
  26. exitWithErrorMessage('ShellJS: script not found ('+scriptName+')');
  27. }
  28. args = process.argv.slice(3);
  29. for (var i = 0, l = args.length; i < l; i++) {
  30. if (args[i][0] !== "-"){
  31. args[i] = '"' + args[i] + '"'; // fixes arguments with multiple words
  32. }
  33. }
  34. var path = require('path');
  35. var extensions = require('interpret').extensions;
  36. var rechoir = require('rechoir');
  37. rechoir.prepare(extensions, scriptName);
  38. require(require.resolve(path.resolve(process.cwd(), scriptName)));