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 (undefined !== currentItem) {
  26. switch (currentItem) {
  27. case '-l':
  28. defaults.load.push.apply(defaults.load, optionsArray.shift().split(','));
  29. break;
  30. case '-L':
  31. defaults.jsLibraryDirs.push.apply(defaults.jsLibraryDirs, optionsArray.shift().split(','));
  32. break;
  33. case '-g':
  34. defaults.jsGlobals.push.apply(defaults.jsGlobals, optionsArray.shift().split(','));
  35. break;
  36. case '-n':
  37. defaults.amd_namespace = optionsArray.shift();
  38. break;
  39. case '-D':
  40. defaults.output_dir = optionsArray.shift();
  41. break;
  42. case '-d':
  43. amber_dir = path.normalize(optionsArray.shift());
  44. break;
  45. case '-v':
  46. defaults.verbose = true;
  47. break;
  48. case '-h':
  49. case '--help':
  50. case '?':
  51. case '-?':
  52. print_usage_and_exit();
  53. break;
  54. default:
  55. defaults.stFiles.push(currentItem);
  56. break;
  57. }
  58. currentItem = optionsArray.shift();
  59. }
  60. if (1 < programName.length) {
  61. throw new Error('More than one name for ProgramName given: ' + programName);
  62. } else {
  63. defaults.program = programName[0];
  64. }
  65. return defaults;
  66. }
  67. // print available flags
  68. function print_usage_and_exit() {
  69. var usage = [
  70. 'Usage: amberc [-L libdir1,libdir2...] [-l lib1,lib2...] [-g jsGlobal1,jsGlobal2]',
  71. ' [-n namespace] [-D output_dir] [-v] file1 file2 ...',
  72. '',
  73. ' amberc compiles Amber files.',
  74. ' Files are compiled into .js files before concatenation.',
  75. ' If not found we look in $AMBER/src/',
  76. '',
  77. ' NOTE: Each .st file is currently considered to be a fileout of a single class',
  78. ' category of the same name as the file!',
  79. '',
  80. ' -l library1,library2',
  81. ' Add listed JavaScript libraries in listed order.',
  82. ' Libraries are not separated by spaces or end with .js.',
  83. '',
  84. ' -L directory1,directory2',
  85. ' Add listed directories to the library search path.',
  86. ' The order of processing is:',
  87. ' 1. current directory',
  88. ' 2. directories specified by -L',
  89. ' 3. $AMBER',
  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. }