deploy.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #!/usr/bin/env node
  2. /**
  3. * Deploy compliled source ( dest -> was )
  4. *
  5. * npm install --save-dev colors
  6. * npm install --save-dev iconv-lite
  7. */
  8. 'use strict';
  9. require('colors');
  10. const iconv = require('iconv-lite');
  11. const fs = require('fs-extra');
  12. const readline = require('readline');
  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 contentsHtml = buf
  69. .toString()
  70. .replace(/\/mobile\/\.\//gi, '/mobile/')
  71. .replace(/sgc\.mobile/g, 'SGC Mobile')
  72. .replace(/SGC Mobile/g, 'SGC Mobile')
  73. .replace(/manifest\.json/g, 'manifest.json?v=' + (new Date().getTime()))
  74. .replace(/\t/g,'').split("\n").join('');
  75. var contentsJsp = buf
  76. .toString()
  77. .replace(/\/mobile\/\.\//gi, '/mobile/')
  78. .replace(/sgc\.mobile/g, '<%=(String)request.getAttribute("title")%>')
  79. .replace(/SGC Mobile/g, '<%=(String)request.getAttribute("title")%>')
  80. .replace(/manifest\.json/g, '<%=(String)request.getAttribute("manifest")%>?v=' + (new Date().getTime()))
  81. // .replace(/manifest\.json/g, '<%=(String)request.getAttribute("manifest")%>')
  82. .replace(/\t/g,'').split("\n").join('');
  83. fs.writeFile(dirName + '/index.html', contentsHtml, 'utf-8', () => {
  84. fs.writeFile(dirName + '/index.jsp', '<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>' +contentsJsp, 'utf-8', () => {
  85. fs.readFile(dirName + '/firebase-messaging-sw.js', 'utf-8', function (err, buf) {
  86. var contents = buf.toString().replace(/\/img\//g, '/mobile/img/');
  87. fs.writeFile(dirName + '/../firebase-messaging-sw.js', contents, 'utf-8', () => {
  88. fs.readFile(dirName + '/manifest.json', 'utf-8', function (err, buf) {
  89. var contents = buf
  90. .toString()
  91. .replace(/\.\/img\//g, '/mobile/img/')
  92. .replace(/\/#/g, '/mobile/#')
  93. // .replace(/\".\/\"/g, '/mobile/');
  94. fs.writeFile(
  95. dirName + '/../../resources/mobile-manifest.json',
  96. contents,
  97. 'utf-8',
  98. () => {
  99. runDepoly(dirName, extList, callBack);
  100. }
  101. );
  102. });
  103. });
  104. });
  105. });
  106. });
  107. });
  108. });
  109. break;
  110. default:
  111. runDepoly(dirName, extList, callBack);
  112. break;
  113. }
  114. } else {
  115. callBack(success);
  116. }
  117. }
  118. function showLogo(callBack) {
  119. fs.readFile('./banner.txt', 'utf8', (error, jsonFile) => {
  120. const logo = jsonFile;
  121. fs.readFile('./package.json', 'utf8', (error, jsonFile) => {
  122. const packageInfo = JSON.parse(jsonFile);
  123. console.log(logo.red + "\n" + (packageInfo.description +" " + packageInfo.version).red);
  124. callBack();
  125. });
  126. });
  127. }
  128. var myArgs = process.argv.slice(2);
  129. showLogo(() => {
  130. if (myArgs.length > 0) {
  131. const folderName = myArgs[0];
  132. const question = [];
  133. question.push('Select Type of Run!!');
  134. question.push('clear - ' + folderName.red + '/** will be remove');
  135. question.push('copy - ' + folderName.red + '/** wll be replaced by dist/**');
  136. question.push(' - ' + folderName + '/../' + 'firebase-messaging-sw.js'.red + ' wll be replaced.');
  137. question.push(' - ' + folderName + '/../../resources/' + 'mobile-manifest.json'.red + ' wll be replaced.');
  138. question.push('Run Type (clear, copy, all) - ? ');
  139. rl.question(question.join('\n'), function (formatType) {
  140. rl.close();
  141. const formatList = [];
  142. if (formatType === '') {
  143. formatList.push('clear');
  144. formatList.push('copy');
  145. } else if (formatType.indexOf('all') > -1) {
  146. formatList.push('clear');
  147. formatList.push('copy');
  148. } else {
  149. const formatTypeList = formatType.split(' ');
  150. formatTypeList.forEach(function (typeStr) {
  151. switch (typeStr) {
  152. case 'clear':
  153. case 'copy':
  154. if (formatList.indexOf(typeStr) === -1) {
  155. formatList.push(typeStr);
  156. }
  157. break;
  158. }
  159. });
  160. }
  161. if (formatList.length > 0) {
  162. formatList.push('end');
  163. runDepoly(folderName, formatList, function (cnt) {
  164. console.log('Success Deploy '.green);
  165. process.exit(1);
  166. });
  167. } else {
  168. console.log('missing argument!!'.red);
  169. process.exit(1);
  170. }
  171. });
  172. } else {
  173. console.log('missing argument!!'.red);
  174. process.exit(1);
  175. }
  176. });