amberc-cli.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/usr/bin/env node
  2. var path = require('path');
  3. var amberc = require('amber-dev').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 '-C':
  28. defaults.configFile = optionsArray.shift();
  29. break;
  30. case '-p':
  31. optionsArray.shift.split(',').forEach(function (pairString) {
  32. var mapping = pairString.split(':');
  33. defaults.paths[mapping[0]] = mapping[1];
  34. });
  35. break;
  36. case '-l':
  37. defaults.load.push.apply(defaults.load, optionsArray.shift().split(','));
  38. break;
  39. case '-g':
  40. defaults.jsGlobals.push.apply(defaults.jsGlobals, optionsArray.shift().split(','));
  41. break;
  42. case '-n':
  43. defaults.amdNamespace = optionsArray.shift();
  44. break;
  45. case '-D':
  46. defaults.outputDir = optionsArray.shift();
  47. break;
  48. case '-d':
  49. amber_dir = path.normalize(optionsArray.shift());
  50. break;
  51. case '-v':
  52. defaults.verbose = true;
  53. break;
  54. case '-h':
  55. case '--help':
  56. case '?':
  57. case '-?':
  58. print_usage_and_exit();
  59. break;
  60. default:
  61. defaults.stFiles.push(currentItem);
  62. break;
  63. }
  64. currentItem = optionsArray.shift();
  65. }
  66. if (1 < programName.length) {
  67. throw new Error('More than one name for ProgramName given: ' + programName);
  68. } else {
  69. defaults.program = programName[0];
  70. }
  71. return defaults;
  72. }
  73. // print available flags
  74. function print_usage_and_exit() {
  75. var usage = [
  76. 'Usage: amberc [-C configFile | -p mapping1,mapping2...] [-l lib1,lib2...] [-g jsGlobal1,jsGlobal2]',
  77. ' [-n namespace] [-D output_dir] [-v] file1 file2 ...',
  78. '',
  79. ' amberc compiles Amber files.',
  80. ' Files are compiled into .js files before concatenation.',
  81. ' If not found we look in $AMBER/src/',
  82. '',
  83. ' NOTE: Each .st file is currently considered to be a fileout of a single class',
  84. ' category of the same name as the file!',
  85. '',
  86. ' -C configFile',
  87. ' Set the file with amd config. Normally, you just use config.js here.',
  88. ' If used, -p options are ignored and -d is only used if configfile does not',
  89. ' contain mappings for amber and/or amber_core.',
  90. '',
  91. ' -p amdpath1:realpath1,amdpath2:realpath2',
  92. ' Set the amd paths mapping as comma-separate amd:realpath pairs.',
  93. ' Mapping elements are not separated by spaces or end with .js.',
  94. '',
  95. ' -l library1,library2',
  96. ' Load the libraries specified as comma-separate AMD module names.',
  97. ' Module names are not separated by spaces or end with .js.',
  98. '',
  99. ' -g jsGlobal1,jsGlobal2',
  100. ' Comma separated list of JS global variable names.',
  101. ' The names are added to a list containing "window", "document" and others.',
  102. '',
  103. ' -n amd_namespace',
  104. ' Export packages with <amd_namespace> as the require.js namespace.',
  105. ' Default value is "amber_core".',
  106. '',
  107. ' -v',
  108. ' Produce a more verbose output.',
  109. '',
  110. ' -D',
  111. ' Specifies the output directory for all generated .js files.',
  112. ' The hierarchy of the input files is not maintaned.',
  113. ' If this option is omitted all generated .js files are placed next to their input files',
  114. '',
  115. ' -d',
  116. ' Specifies the alternate directory to look for Amber files.',
  117. ' If not specified, the version embedded in CLI is used.',
  118. '',
  119. ' Example invocations:',
  120. '',
  121. ' Just compile Kernel-Objects.st to Kernel-Objects.js:',
  122. '',
  123. ' amberc Kernel-Objects.st',
  124. ];
  125. usage.forEach(function (line) {
  126. console.log(line);
  127. });
  128. process.exit();
  129. }