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