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.3KB

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