amberc-cli.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/usr/bin/env node
  2. var path = require('path');
  3. var amberc = require('amber-dev/lib/amberc');
  4. // get parameters passed to the command line script
  5. // discard the first two parameters which are the node binary and the script name
  6. var parameters = process.argv.slice(2);
  7. // check if at least one parameter was passed to the script
  8. if (1 > parameters.length) {
  9. print_usage_and_exit();
  10. }
  11. // Get Amber root directory from the location of this script so that
  12. // we can find the st and js directories etc.
  13. var amber_dir = path.normalize(path.join(__dirname, '..', 'node_modules', 'amber'));
  14. var configuration = handle_options(parameters);
  15. var compiler = new amberc.Compiler(amber_dir);
  16. compiler.main(configuration);
  17. /**
  18. * Process given program options and update defaults values.
  19. * Followed by check_for_closure_compiler() and then collect_files().
  20. */
  21. function handle_options(optionsArray) {
  22. var programName = [];
  23. var currentItem = optionsArray.shift();
  24. var defaults = amberc.createDefaultConfiguration();
  25. while (currentItem != null) {
  26. switch (currentItem) {
  27. case '-m':
  28. optionsArray.shift.split(',').forEach(function (pairString) {
  29. var mapping = pairString.split(':');
  30. defaults.mappings[mapping[0]] = mapping[1];
  31. });
  32. break;
  33. case '-l':
  34. defaults.load.push.apply(defaults.load, optionsArray.shift().split(','));
  35. break;
  36. case '-g':
  37. defaults.jsGlobals.push.apply(defaults.jsGlobals, optionsArray.shift().split(','));
  38. break;
  39. case '-n':
  40. defaults.amdNamespace = optionsArray.shift();
  41. break;
  42. case '-D':
  43. defaults.outputDir = optionsArray.shift();
  44. break;
  45. case '-d':
  46. amber_dir = path.normalize(optionsArray.shift());
  47. break;
  48. case '-v':
  49. defaults.verbose = true;
  50. break;
  51. case '-h':
  52. case '--help':
  53. case '?':
  54. case '-?':
  55. print_usage_and_exit();
  56. break;
  57. default:
  58. defaults.stFiles.push(currentItem);
  59. break;
  60. }
  61. currentItem = optionsArray.shift();
  62. }
  63. if (1 < programName.length) {
  64. throw new Error('More than one name for ProgramName given: ' + programName);
  65. } else {
  66. defaults.program = programName[0];
  67. }
  68. return defaults;
  69. }
  70. // print available flags
  71. function print_usage_and_exit() {
  72. var usage = [
  73. 'Usage: amberc [-m mapping1,mapping2...] [-l lib1,lib2...] [-g jsGlobal1,jsGlobal2]',
  74. ' [-n namespace] [-D output_dir] [-v] file1 file2 ...',
  75. '',
  76. ' amberc compiles Amber files.',
  77. ' Files are compiled into .js files before concatenation.',
  78. ' If not found we look in $AMBER/src/',
  79. '',
  80. ' NOTE: Each .st file is currently considered to be a fileout of a single class',
  81. ' category of the same name as the file!',
  82. '',
  83. ' -m amdpath1:realpath1,amdpath2:realpath2',
  84. ' Set the amd mappings as comma-separate amd:realpath pairs.',
  85. ' Mappings are not separated by spaces or end with .js.',
  86. '',
  87. ' -l library1,library2',
  88. ' Load the libraries specified as comma-separate AMD module names.',
  89. ' Module names are not separated by spaces or end with .js.',
  90. '',
  91. ' -g jsGlobal1,jsGlobal2',
  92. ' Comma separated list of JS global variable names.',
  93. ' The names are added to a list containing "window", "document" and others.',
  94. '',
  95. ' -n amd_namespace',
  96. ' Export packages with <amd_namespace> as the require.js namespace.',
  97. ' Default value is "amber_core".',
  98. '',
  99. ' -v',
  100. ' Produce a more verbose output.',
  101. '',
  102. ' -D',
  103. ' Specifies the output directory for all generated .js files.',
  104. ' The hierarchy of the input files is not maintaned.',
  105. ' If this option is omitted all generated .js files are placed next to their input files',
  106. '',
  107. ' -d',
  108. ' Specifies the alternate directory to look for Amber files.',
  109. ' If not specified, the version embedded in CLI is used.',
  110. '',
  111. ' Example invocations:',
  112. '',
  113. ' Just compile Kernel-Objects.st to Kernel-Objects.js:',
  114. '',
  115. ' amberc Kernel-Objects.st',
  116. ];
  117. usage.forEach(function (line) {
  118. console.log(line);
  119. });
  120. process.exit();
  121. }