1
0

amberc-cli.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 '-m':
  37. defaults.main = optionsArray.shift();
  38. break;
  39. case '-M':
  40. defaults.mainfile = optionsArray.shift();
  41. break;
  42. case '-n':
  43. defaults.amd_namespace = optionsArray.shift();
  44. break;
  45. case '-D':
  46. defaults.output_dir = optionsArray.shift();
  47. break;
  48. case '-d':
  49. amber_dir = path.normalize(optionsArray.shift());
  50. break;
  51. case '-s':
  52. defaults.suffix = optionsArray.shift();
  53. defaults.suffix_used = defaults.suffix;
  54. break;
  55. case '-S':
  56. defaults.loadsuffix = optionsArray.shift();
  57. defaults.suffix_used = defaults.suffix;
  58. break;
  59. case '-v':
  60. defaults.verbose = true;
  61. break;
  62. case '-h':
  63. case '--help':
  64. case '?':
  65. print_usage_and_exit();
  66. break;
  67. default:
  68. var fileSuffix = path.extname(currentItem);
  69. switch (fileSuffix) {
  70. case '.st':
  71. defaults.stFiles.push(currentItem);
  72. break;
  73. case '.js':
  74. defaults.jsFiles.push(currentItem);
  75. break;
  76. default:
  77. // Will end up being the last non js/st argument
  78. programName.push(currentItem);
  79. break;
  80. }
  81. }
  82. currentItem = optionsArray.shift();
  83. }
  84. if(1 < programName.length) {
  85. throw new Error('More than one name for ProgramName given: ' + programName);
  86. } else {
  87. defaults.program = programName[0];
  88. }
  89. return defaults;
  90. }
  91. // print available flags
  92. function print_usage_and_exit() {
  93. var usage = [
  94. 'Usage: amberc [-l lib1,lib2...] [-g jsGlobal1,jsGlobla2] [-m main_class] [-M main_file]',
  95. ' [-n namespace] [-D output_dir] [-v] [-s suffix] [-S suffix] [file1 [file2 ...]] [Program]',
  96. '',
  97. ' amberc compiles Amber files - either separately or into a complete runnable',
  98. ' program. If no .st files are listed only a linking stage is performed.',
  99. ' Files listed will be handled using the following rules:',
  100. '',
  101. ' *.js',
  102. ' Files are linked (concatenated) in listed order.',
  103. ' If not found we look in $AMBER/src/',
  104. '',
  105. ' *.st',
  106. ' Files are compiled into .js files before concatenation.',
  107. ' If not found we look in $AMBER/src/',
  108. '',
  109. ' NOTE: Each .st file is currently considered to be a fileout of a single class',
  110. ' category of the same name as the file!',
  111. '',
  112. ' If no <Program> is specified each given .st file will be compiled into',
  113. ' a matching .js file. Otherwise a <Program>.js file is linked together based on',
  114. ' the given options:',
  115. '',
  116. ' -l library1,library2',
  117. ' Add listed JavaScript libraries in listed order.',
  118. ' Libraries are not separated by spaces or end with .js.',
  119. '',
  120. ' -L directory1,directory2',
  121. ' Add listed directories to the library search path.',
  122. ' The order of processing is:',
  123. ' 1. current directory',
  124. ' 2. directories specified by -L',
  125. ' 3. $AMBER',
  126. '',
  127. ' -g jsGlobal1,jsGlobal2',
  128. ' Comma separated list of JS global variable names.',
  129. ' The names are added to a list containing "window", "document" and others.',
  130. '',
  131. ' -m main_class',
  132. ' Add a call to the class method main_class>>main at the end of <Program>.',
  133. '',
  134. ' -M main_file',
  135. ' Add <main_file> at the end of <Program.js> acting as #main.',
  136. '',
  137. ' -n amd_namespace',
  138. ' Export packages with <amd_namespace> as the require.js namespace.',
  139. ' Default value is "amber_core".',
  140. '',
  141. ' -v',
  142. ' Produce a more verbose output.',
  143. '',
  144. ' -D',
  145. ' Specifies the output directory for all generated .js files.',
  146. ' The hierarchy of the input files is not maintaned.',
  147. ' If this option is omitted all generated .js files are placed next to their input files',
  148. '',
  149. ' -d',
  150. ' Specifies the alternate directory to look for Amber files.',
  151. ' If not specified, the version embedded in CLI is used..',
  152. '',
  153. ' -s suffix',
  154. ' Add <suffix> to compiled .js files. File.st is then compiled into',
  155. ' File.<suffix>.js.',
  156. '',
  157. ' -S suffix',
  158. ' Use <suffix> for all libraries accessed using -l. This makes it possible',
  159. ' to have multiple flavors of Amber and libraries in the same place.',
  160. '',
  161. '',
  162. ' Example invocations:',
  163. '',
  164. ' Just compile Kernel-Objects.st to Kernel-Objects.js:',
  165. '',
  166. ' amberc Kernel-Objects.st',
  167. '',
  168. ' Compile Hello.st to Hello.js and create complete program called Program.js.',
  169. ' Additionally add a call to the class method Hello>>main:',
  170. '',
  171. ' amberc -m Hello Hello.st Program',
  172. '',
  173. ' Compile Cat1.st and Cat2.st files into corresponding .js files.',
  174. ' Link them with myboot.js and myKernel.js',
  175. ' and merge everything into a complete program named Program.js:',
  176. '',
  177. ' amberc -M main.js myboot.js myKernel.js Cat1.st Cat2.st Program',
  178. ];
  179. usage.forEach(function (line) {
  180. console.log(line);
  181. });
  182. process.exit();
  183. }