doc_format.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #!/usr/bin/env node
  2. /**
  3. * @license
  4. * Copyright Google LLC All Rights Reserved.
  5. *
  6. * npm install --save-dev colors
  7. * npm install --save-dev iconv-lite
  8. */
  9. 'use strict';
  10. require('colors');
  11. const readline = require('readline');
  12. const { exec } = require("child_process");
  13. const fs = require('fs-extra');
  14. const rl = readline.createInterface({
  15. input: process.stdin,
  16. output: process.stdout,
  17. });
  18. function showValidString(errorLogs, title, pathName) {
  19. const stderrLines = [];
  20. let successCnt = 0;
  21. let warnCnt = 0;
  22. errorLogs.split("\n").forEach(function(line) {
  23. if (line.length > 0 && !line.startsWith('npx: ')) {
  24. if (line.startsWith('[error]')) {
  25. stderrLines.push('| ' + line.red);
  26. } else if (line.startsWith('[warn] ' + pathName)) {
  27. warnCnt++;
  28. stderrLines.push('| ' + line.blue);
  29. } else {
  30. stderrLines.push('| ' + line);
  31. }
  32. if (line.startsWith(pathName)) {
  33. successCnt++;
  34. }
  35. }
  36. });
  37. if (stderrLines.length > 0) {
  38. console.log("--------------------------------------------------------");
  39. console.log("| " + title);
  40. console.log("--------------------------------------------------------");
  41. console.log(stderrLines.join("\n"));
  42. console.log("========================================================");
  43. if (warnCnt > 0) {
  44. console.log("| Warn : " + (warnCnt + " cnt").blue);
  45. console.log("========================================================\n");
  46. }
  47. if (successCnt > 0) {
  48. console.log("| Success : " + (successCnt + " cnt").green);
  49. console.log("========================================================\n");
  50. }
  51. }
  52. return successCnt;
  53. }
  54. function runFormat(dirName, ext, checkType, callBack ) {
  55. // --check --write
  56. exec("npx prettier --"+checkType+" "+dirName+"/**/*." +ext, function(error, stdout, stderr) {
  57. if (stderr) {
  58. showValidString(stderr, 'Standard Error was occurred in ' + dirName.blue + '/**/*.' + ext, dirName );
  59. }
  60. let success = 0;
  61. if (stdout) {
  62. success = showValidString(stdout, 'Format success in ' + dirName.green + '/**/*.' + ext, dirName);
  63. }
  64. callBack(success);
  65. });
  66. }
  67. let totalSuccess = 0 ;
  68. function runFormatProcess(dirName, extList, checkType, callBack ) {
  69. if (extList.length > 0) {
  70. const ext = extList.pop();
  71. runFormat(dirName, ext, checkType, function(success) {
  72. totalSuccess += success;
  73. runFormatProcess(dirName, extList, checkType, callBack);
  74. });
  75. } else {
  76. callBack(totalSuccess);
  77. }
  78. }
  79. function showLogo(callBack) {
  80. fs.readFile('./banner.txt', 'utf8', (error, jsonFile) => {
  81. const logo = jsonFile;
  82. fs.readFile('./package.json', 'utf8', (error, jsonFile) => {
  83. const packageInfo = JSON.parse(jsonFile);
  84. console.log(logo.blue + "\n" + (packageInfo.description +" " + packageInfo.version).red);
  85. callBack();
  86. });
  87. });
  88. }
  89. var myArgs = process.argv.slice(2);
  90. showLogo(() => {
  91. if (myArgs.length > 0) {
  92. const folderName = myArgs[0];
  93. const question = [];
  94. question.push("Select Type of Format!!");
  95. question.push("html - "+folderName.red+"/**/*.html");
  96. question.push("ts - "+folderName.red+"/**/*.ts");
  97. question.push("scss - "+folderName.red+"/**/*.scss");
  98. question.push("css - "+folderName.red+"/**/*.css");
  99. question.push("json - "+folderName.red+"/**/*.json");
  100. question.push("java - "+folderName.red+"/**/*.java");
  101. question.push("vue - "+folderName.red+"/**/*.vue");
  102. question.push("xml - "+folderName.red+"/**/*.xml");
  103. question.push("check - just check");
  104. question.push("Format Types (html ts scss json java vue all check) - ? ");
  105. rl.question(question.join("\n"), function (formatType) {
  106. rl.close();
  107. const formatList = [];
  108. let checkType = 'write';
  109. if (formatType.indexOf('check') > -1) {
  110. checkType = 'check';
  111. }
  112. if (formatType === '') {
  113. formatType = 'all';
  114. }
  115. if (formatType.indexOf('all') > -1) {
  116. formatList.push('html');
  117. formatList.push('ts');
  118. formatList.push('js');
  119. formatList.push('vue');
  120. formatList.push('scss');
  121. formatList.push('css');
  122. formatList.push('json');
  123. } else {
  124. const formatTypeList = formatType.split(' ');
  125. formatTypeList.forEach(function(typeStr) {
  126. switch(typeStr) {
  127. case 'html' :
  128. case 'ts' :
  129. case 'scss' :
  130. case 'css' :
  131. case 'json' :
  132. case 'vue' :
  133. case 'xml' :
  134. case 'java' :
  135. if (formatList.indexOf(typeStr) === -1) {
  136. formatList.push(typeStr);
  137. }
  138. break;
  139. }
  140. });
  141. }
  142. if (formatList.length > 0) {
  143. runFormatProcess(folderName,formatList, checkType, function(cnt) {
  144. console.log('Success Format '.green + ' - ' + (cnt + " cnt").red);
  145. });
  146. } else {
  147. console.log('missing argument!!'.red);
  148. }
  149. });
  150. } else {
  151. console.log('missing argument!!'.red);
  152. }
  153. });