2
0

amberc-cli.js 4.1 KB

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