Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

index.js 9.3KB

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