You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 9.4KB

6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. 
  2. // --------------
  3. // elxr
  4. // A cli tool for elixr.
  5. const processedArgs = minimist(process.argv.slice(2));
  6. var cli = 'elxr';
  7. var ver = '#unversioned';
  8. var help = '#unkown list of commands... please refer dveloper documentation for ' + cli;
  9. nodeShellExec('echo', ['elxr'], { inherit : true}) //, {stdio: "inherit"}
  10. var __runcmd = function(label){
  11. // var child = nodeShellExec('mkdir', ['-p', label], { inherit : true} );
  12. // console.log('Starting directory: ' + process.cwd());
  13. // try {
  14. // child = child.on('close', () => { process.chdir(label) } );
  15. // console.log('New directory: ' + process.cwd());
  16. // }
  17. // catch (err) {
  18. // console.log('chdir: ' + err);
  19. // }
  20. // child.on('close', function(){
  21. // var options = {
  22. // shell : true
  23. // , inherit : true
  24. // // , cwd : '' + process.cwd
  25. // // , env : process.env
  26. // };
  27. // nodeShellExec('git', ['init'], { inherit : true});
  28. nodeShellExec('pwd', { inherit : true});
  29. //$ "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --profile-directory="Profile 1" http://localhost:4200/tests/index.html?grep=loopback
  30. nodeShellExec("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe", [
  31. "--profile-directory=Profile 1"
  32. , 'http://localhost:4200/tests/index.html?grep=model convert ember to loopback' + '&filter=none' /*+ '&filter=model convert ember to loopback'*/]);
  33. // nodeShellExec('npm', ['init', '-y'], options);
  34. // nodeShellExec('npm', ['init', '-y'], options);
  35. // })
  36. }
  37. __runcmd(processedArgs.label || processedArgs._[0] || (function(){ console.log(cli + ' ' + ver + ' ' + help); return '-h' } )());
  38. // nodeShellExec('git', ['status']);
  39. function nodeShellExec() {
  40. const { spawn } = require('child_process');
  41. const child = spawn(...arguments);
  42. // use child.stdout.setEncoding('utf8'); if you want text chunks
  43. child.stdout.setEncoding('utf8');
  44. console.log('here')
  45. child.stdout.on('data', (chunk) => console.log(chunk));
  46. child.on('error', (chunk) => console.error(chunk));
  47. child.stderr.pipe(process.stderr);
  48. child.on('close', (code) => console.log(`child process ${Array.from(arguments)[0]} exited with code ${code}`));
  49. return child;
  50. }
  51. // -------------------------------
  52. // Command line option parsing
  53. // -------------------------------
  54. function minimist(args, opts) {
  55. if (!opts) opts = {};
  56. var flags = { bools : {}, strings : {}, unknownFn: null };
  57. if (typeof opts['unknown'] === 'function') {
  58. flags.unknownFn = opts['unknown'];
  59. }
  60. if (typeof opts['boolean'] === 'boolean' && opts['boolean']) {
  61. flags.allBools = true;
  62. } else {
  63. [].concat(opts['boolean']).filter(Boolean).forEach(function (key) {
  64. flags.bools[key] = true;
  65. });
  66. }
  67. var aliases = {};
  68. Object.keys(opts.alias || {}).forEach(function (key) {
  69. aliases[key] = [].concat(opts.alias[key]);
  70. aliases[key].forEach(function (x) {
  71. aliases[x] = [key].concat(aliases[key].filter(function (y) {
  72. return x !== y;
  73. }));
  74. });
  75. });
  76. [].concat(opts.string).filter(Boolean).forEach(function (key) {
  77. flags.strings[key] = true;
  78. if (aliases[key]) {
  79. flags.strings[aliases[key]] = true;
  80. }
  81. });
  82. var defaults = opts['default'] || {};
  83. var argv = { _ : [] };
  84. Object.keys(flags.bools).forEach(function (key) {
  85. setArg(key, defaults[key] === undefined ? false : defaults[key]);
  86. });
  87. var notFlags = [];
  88. if (args.indexOf('--') !== -1) {
  89. notFlags = args.slice(args.indexOf('--')+1);
  90. args = args.slice(0, args.indexOf('--'));
  91. }
  92. function argDefined(key, arg) {
  93. return (flags.allBools && /^--[^=]+$/.test(arg)) ||
  94. flags.strings[key] || flags.bools[key] || aliases[key];
  95. }
  96. function setArg (key, val, arg) {
  97. if (arg && flags.unknownFn && !argDefined(key, arg)) {
  98. if (flags.unknownFn(arg) === false) return;
  99. }
  100. var value = !flags.strings[key] && isNumber(val)
  101. ? Number(val) : val
  102. ;
  103. setKey(argv, key.split('.'), value);
  104. (aliases[key] || []).forEach(function (x) {
  105. setKey(argv, x.split('.'), value);
  106. });
  107. }
  108. function setKey (obj, keys, value) {
  109. var o = obj;
  110. keys.slice(0,-1).forEach(function (key) {
  111. if (o[key] === undefined) o[key] = {};
  112. o = o[key];
  113. });
  114. var key = keys[keys.length - 1];
  115. if (o[key] === undefined || flags.bools[key] || typeof o[key] === 'boolean') {
  116. o[key] = value;
  117. }
  118. else if (Array.isArray(o[key])) {
  119. o[key].push(value);
  120. }
  121. else {
  122. o[key] = [ o[key], value ];
  123. }
  124. }
  125. function aliasIsBoolean(key) {
  126. return aliases[key].some(function (x) {
  127. return flags.bools[x];
  128. });
  129. }
  130. for (var i = 0; i < args.length; i++) {
  131. var arg = args[i];
  132. if (/^--.+=/.test(arg)) {
  133. // Using [\s\S] instead of . because js doesn't support the
  134. // 'dotall' regex modifier. See:
  135. // http://stackoverflow.com/a/1068308/13216
  136. var m = arg.match(/^--([^=]+)=([\s\S]*)$/);
  137. var key = m[1];
  138. var value = m[2];
  139. if (flags.bools[key]) {
  140. value = value !== 'false';
  141. }
  142. setArg(key, value, arg);
  143. }
  144. else if (/^--no-.+/.test(arg)) {
  145. var key = arg.match(/^--no-(.+)/)[1];
  146. setArg(key, false, arg);
  147. }
  148. else if (/^--.+/.test(arg)) {
  149. var key = arg.match(/^--(.+)/)[1];
  150. var next = args[i + 1];
  151. if (next !== undefined && !/^-/.test(next)
  152. && !flags.bools[key]
  153. && !flags.allBools
  154. && (aliases[key] ? !aliasIsBoolean(key) : true)) {
  155. setArg(key, next, arg);
  156. i++;
  157. }
  158. else if (/^(true|false)$/.test(next)) {
  159. setArg(key, next === 'true', arg);
  160. i++;
  161. }
  162. else {
  163. setArg(key, flags.strings[key] ? '' : true, arg);
  164. }
  165. }
  166. else if (/^-[^-]+/.test(arg)) {
  167. var letters = arg.slice(1,-1).split('');
  168. var broken = false;
  169. for (var j = 0; j < letters.length; j++) {
  170. var next = arg.slice(j+2);
  171. if (next === '-') {
  172. setArg(letters[j], next, arg)
  173. continue;
  174. }
  175. if (/[A-Za-z]/.test(letters[j]) && /=/.test(next)) {
  176. setArg(letters[j], next.split('=')[1], arg);
  177. broken = true;
  178. break;
  179. }
  180. if (/[A-Za-z]/.test(letters[j])
  181. && /-?\d+(\.\d*)?(e-?\d+)?$/.test(next)) {
  182. setArg(letters[j], next, arg);
  183. broken = true;
  184. break;
  185. }
  186. if (letters[j+1] && letters[j+1].match(/\W/)) {
  187. setArg(letters[j], arg.slice(j+2), arg);
  188. broken = true;
  189. break;
  190. }
  191. else {
  192. setArg(letters[j], flags.strings[letters[j]] ? '' : true, arg);
  193. }
  194. }
  195. var key = arg.slice(-1)[0];
  196. if (!broken && key !== '-') {
  197. if (args[i+1] && !/^(-|--)[^-]/.test(args[i+1])
  198. && !flags.bools[key]
  199. && (aliases[key] ? !aliasIsBoolean(key) : true)) {
  200. setArg(key, args[i+1], arg);
  201. i++;
  202. }
  203. else if (args[i+1] && /true|false/.test(args[i+1])) {
  204. setArg(key, args[i+1] === 'true', arg);
  205. i++;
  206. }
  207. else {
  208. setArg(key, flags.strings[key] ? '' : true, arg);
  209. }
  210. }
  211. }
  212. else {
  213. if (!flags.unknownFn || flags.unknownFn(arg) !== false) {
  214. argv._.push(
  215. flags.strings['_'] || !isNumber(arg) ? arg : Number(arg)
  216. );
  217. }
  218. if (opts.stopEarly) {
  219. argv._.push.apply(argv._, args.slice(i + 1));
  220. break;
  221. }
  222. }
  223. }
  224. Object.keys(defaults).forEach(function (key) {
  225. if (!hasKey(argv, key.split('.'))) {
  226. setKey(argv, key.split('.'), defaults[key]);
  227. (aliases[key] || []).forEach(function (x) {
  228. setKey(argv, x.split('.'), defaults[key]);
  229. });
  230. }
  231. });
  232. if (opts['--']) {
  233. argv['--'] = new Array();
  234. notFlags.forEach(function(key) {
  235. argv['--'].push(key);
  236. });
  237. }
  238. else {
  239. notFlags.forEach(function(key) {
  240. argv._.push(key);
  241. });
  242. }
  243. return argv;
  244. };
  245. function hasKey (obj, keys) {
  246. var o = obj;
  247. keys.slice(0,-1).forEach(function (key) {
  248. o = (o[key] || {});
  249. });
  250. var key = keys[keys.length - 1];
  251. return key in o;
  252. }
  253. function isNumber (x) {
  254. if (typeof x === 'number') return true;
  255. if (/^0x[0-9a-f]+$/i.test(x)) return true;
  256. return /^[-+]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/.test(x);
  257. }