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.
 
 
 

110 lines
3.2 KiB

  1. var fs = require('fs');
  2. var readline = require('readline');
  3. var os = require('os');
  4. var path = require('path');
  5. // Node.js readline 逐行读取、写入文件内容的示例
  6. // https://www.jb51.net/article/135706.htm
  7. // nodejs读取文件、按行读取
  8. // https://blog.csdn.net/weixin_42171955/article/details/100156212
  9. var strInputFileName = '../dist/index.html';
  10. var strOutputFileName = 'E:\\code\\lou\\public\\view\\order_main\\add.html';
  11. var strOutputFileName2 = 'E:\\code\\lou\\public\\view\\order_main\\edit.html';
  12. var fRead = fs.createReadStream(strInputFileName);
  13. var fWrite = fs.createWriteStream(strOutputFileName);
  14. var fWrite2 = fs.createWriteStream(strOutputFileName2);
  15. fRead.on('end', () => {
  16. console.log('end');
  17. });
  18. var objReadline = readline.createInterface({
  19. input: fRead,
  20. output: fWrite,
  21. terminal: false
  22. });
  23. objReadline.on('line', (strLine) => {
  24. let strTemp = `<script>window.id = '';function close(){window.parent.postMessage('message', window.location.protocol+"//"+window.location.host);}</script>${strLine}`;
  25. let strTemp2 = `<script> window.id = getQueryVariable("id");function getQueryVariable(variable){var query = window.location.search.substring(1);var vars = query.split("&");for (var i=0;i<vars.length;i++) { var pair = vars[i].split("=");if(pair[0] == variable){return pair[1];}}return(false);}function close(){window.parent.postMessage('message', window.location.protocol+"//"+window.location.host);}</script>${strLine}`;
  26. fWrite.write(strTemp);
  27. fWrite2.write(strTemp2);
  28. });
  29. objReadline.on('close', () => {
  30. console.log('readline close...');
  31. });
  32. function removeDir(dir) {
  33. let files = fs.readdirSync(dir)
  34. for(var i=0;i<files.length;i++){
  35. let newPath = path.join(dir,files[i]);
  36. let stat = fs.statSync(newPath)
  37. if(stat.isDirectory()){
  38. //如果是文件夹就递归下去
  39. removeDir(newPath);
  40. }else {
  41. //删除文件
  42. fs.unlinkSync(newPath);
  43. }
  44. }
  45. fs.rmdirSync(dir)//如果文件夹是空的,就将自己删除掉
  46. }
  47. if(fs.statSync("E:\\code\\lou\\public\\static").isDirectory()){
  48. //如果是文件夹就递归下去
  49. removeDir('E:\\code\\lou\\public\\static');
  50. }else {
  51. //删除文件
  52. fs.unlinkSync(newPath);
  53. }
  54. fs.mkdir("E:\\code\\lou\\public\\static",function(){//创建目录
  55. })
  56. var stat=fs.stat;
  57. var copy=function(src,dst){
  58. //读取目录
  59. fs.readdir(src,function(err,paths){
  60. console.log(paths)
  61. if(err){
  62. throw err;
  63. }
  64. paths.forEach(function(path){
  65. var _src=src+'/'+path;
  66. var _dst=dst+'/'+path;
  67. var readable;
  68. var writable;
  69. stat(_src,function(err,st){
  70. if(err){
  71. throw err;
  72. }
  73. if(st.isFile()){
  74. readable=fs.createReadStream(_src);//创建读取流
  75. writable=fs.createWriteStream(_dst);//创建写入流
  76. readable.pipe(writable);
  77. }else if(st.isDirectory()){
  78. exists(_src,_dst,copy);
  79. }
  80. });
  81. });
  82. });
  83. }
  84. var exists=function(src,dst,callback){
  85. //测试某个路径下文件是否存在
  86. fs.exists(dst,function(exists){
  87. if(exists){//不存在
  88. callback(src,dst);
  89. }else{//存在
  90. fs.mkdir(dst,function(){//创建目录
  91. callback(src,dst)
  92. })
  93. }
  94. })
  95. }
  96. exists('../dist/static','E:\\code\\lou\\public\\static',copy)