amberc-cli.js 5.3 KB

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