deploy.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/usr/bin/env node
  2. /**
  3. * @license
  4. * Copyright Google LLC All Rights Reserved.
  5. */
  6. 'use strict';
  7. const colors = require('colors');
  8. const iconv = require('iconv-lite');
  9. const fs = require('fs-extra');
  10. const readline = require('readline');
  11. const { exec } = require('child_process');
  12. const cwd = process.cwd() + '\\';
  13. const rl = readline.createInterface({
  14. input: process.stdin,
  15. output: process.stdout,
  16. });
  17. function showValidString(errorLogs, title, pathName) {
  18. const stderrLines = [];
  19. let successCnt = 0;
  20. let warnCnt = 0;
  21. errorLogs.split('\n').forEach(function (line) {
  22. if (line.length > 0 && !line.startsWith('npx: ')) {
  23. if (line.startsWith('[error]')) {
  24. stderrLines.push('| ' + line.red);
  25. } else if (line.startsWith('[warn] ' + pathName)) {
  26. warnCnt++;
  27. stderrLines.push('| ' + line.blue);
  28. } else {
  29. stderrLines.push('| ' + line);
  30. }
  31. if (line.startsWith(pathName)) {
  32. successCnt++;
  33. }
  34. }
  35. });
  36. if (stderrLines.length > 0) {
  37. console.log('--------------------------------------------------------');
  38. console.log('| ' + title);
  39. console.log('--------------------------------------------------------');
  40. console.log(iconv.decode(Buffer.from(stderrLines.join('\n'), 'utf-8'), 'euc-kr'));
  41. console.log('========================================================');
  42. if (warnCnt > 0) {
  43. console.log('| Warn : ' + (warnCnt + ' cnt').blue);
  44. console.log('========================================================\n');
  45. }
  46. if (successCnt > 0) {
  47. console.log('| Success : ' + (successCnt + ' cnt').green);
  48. console.log('========================================================\n');
  49. }
  50. }
  51. return successCnt;
  52. }
  53. var success = 0;
  54. function runDepoly(dirName, extList, callBack) {
  55. if (extList.length > 0) {
  56. var ext = extList.shift();
  57. switch (ext) {
  58. case 'clear':
  59. fs.remove(dirName, function () {
  60. fs.mkdir(dirName, function (aaa) {
  61. runDepoly(dirName, extList, callBack);
  62. });
  63. });
  64. break;
  65. case 'copy':
  66. fs.copy('dist/', dirName, function (err, aaa) {
  67. fs.readFile(dirName + '/index.html', 'utf-8', function (err, buf) {
  68. var contents = buf
  69. .toString()
  70. .replace(/\/mobile\/\.\//gi, '/mobile/')
  71. .replace(/sgc\.mobile/g, 'SGC Mobile');
  72. fs.writeFile(dirName + '/index.html', contents, 'utf-8', () => {
  73. fs.readFile(dirName + '/firebase-messaging-sw.js', 'utf-8', function (err, buf) {
  74. var contents = buf.toString();
  75. fs.writeFile(dirName + '/../firebase-messaging-sw.js', contents, 'utf-8', () => {
  76. fs.readFile(dirName + '/manifest.json', 'utf-8', function (err, buf) {
  77. var contents = buf
  78. .toString()
  79. .replace(/\.\/img\//g, '/mobile/img/')
  80. .replace(/\/#/g, '/mobile/#')
  81. // .replace(/\".\/\"/g, '/mobile/');
  82. fs.writeFile(
  83. dirName + '/../../resources/mobile-manifest.json',
  84. contents,
  85. 'utf-8',
  86. () => {
  87. runDepoly(dirName, extList, callBack);
  88. }
  89. );
  90. });
  91. });
  92. });
  93. });
  94. });
  95. });
  96. break;
  97. default:
  98. runDepoly(dirName, extList, callBack);
  99. break;
  100. }
  101. } else {
  102. callBack(success);
  103. }
  104. }
  105. console.log(
  106. '\n\n' +
  107. [
  108. ' ____ ____ ____ ____ _ _',
  109. '/ ___| / ___| / ___| | _ \\ ___ _ __ | |_ __ _ | |',
  110. "\\___ \\ | | _ | | | |_) | / _ \\ | '__| | __| / _` | | |",
  111. ' ___) | | |_| | | |___ | __/ | (_) | | | | |_ | (_| | | |',
  112. '|____/ \\____| \\____| |_| \\___/ |_| \\__| \\__,_| |_|',
  113. 'SGC Mobile v1.0.0'.red,
  114. ].join('\n') +
  115. '\n\n'
  116. );
  117. var myArgs = process.argv.slice(2);
  118. if (myArgs.length > 0) {
  119. const folderName = myArgs[0];
  120. const question = [];
  121. question.push('Select Type of Run!!');
  122. question.push('clear - ' + folderName.red + '/** will be remove');
  123. question.push('copy - ' + folderName.red + '/** wll be replaced by dist/**');
  124. question.push(' - ' + folderName + '/../' + 'firebase-messaging-sw.js'.red + ' wll be replaced.');
  125. question.push(' - ' + folderName + '/../../resources/' + 'mobile-manifest.json'.red + ' wll be replaced.');
  126. question.push('Run Type (clear, copy, all) - ? ');
  127. rl.question(question.join('\n'), function (formatType) {
  128. rl.close();
  129. const formatList = [];
  130. if (formatType === '') {
  131. formatList.push('clear');
  132. formatList.push('copy');
  133. } else if (formatType.indexOf('all') > -1) {
  134. formatList.push('clear');
  135. formatList.push('copy');
  136. } else {
  137. const formatTypeList = formatType.split(' ');
  138. formatTypeList.forEach(function (typeStr) {
  139. switch (typeStr) {
  140. case 'clear':
  141. case 'copy':
  142. if (formatList.indexOf(typeStr) === -1) {
  143. formatList.push(typeStr);
  144. }
  145. break;
  146. }
  147. });
  148. }
  149. if (formatList.length > 0) {
  150. formatList.push('end');
  151. runDepoly(folderName, formatList, function (cnt) {
  152. console.log('Success Deploy '.green);
  153. process.exit(1);
  154. });
  155. } else {
  156. console.log('missing argument!!'.red);
  157. process.exit(1);
  158. }
  159. });
  160. } else {
  161. console.log('missing argument!!'.red);
  162. process.exit(1);
  163. }