1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| const fs = require('fs') const rimraf = require('rimraf') const stat = fs.stat
const localConfig = { input: './dist', output: '../CN201704A1/src/main/webapp/html', pages: '/index.html', ignore: ['index.html'] }
inserFile(parseFile('index.html'))
function inserFile(fileData) { let path = localConfig.output + localConfig.pages
let data = fs.readFileSync(path, 'utf-8') let esData = escape(data) esData = esData.replace(new RegExp( 'manifest\.[a-z0-9]{20}\.js'), 'manifest.' + fileData.manifest + '.js') esData = esData.replace(new RegExp( 'vendor\.[a-z0-9]{20}\.js'), 'vendor.' + fileData.vender + '.js') esData = esData.replace(new RegExp( 'app' + '\.[a-z0-9]{20}\.js'), 'app' + '.' + fileData.fileNameJs + '.js') esData = esData.replace(new RegExp( 'app' + '\.[a-z0-9]{32}\.css'), 'app' + '.' + fileData.fileNameCss + '.css')
let result = unescape(esData) fs.unlinkSync(path) fs.writeFileSync(path, result) rimraf(localConfig.output + '/static', err => { if(err) throw new Erorr(err) exists( localConfig.input, localConfig.output, copy ); }) }
function parseFile(fileName) { let fileData let path = localConfig.input + '/' + fileName; let data = fs.readFileSync(path, 'utf-8');
let esData = escape(data); let shortFileName = fileName.slice(0, -5)
let tempObj = { fileNameCss: esData.match(new RegExp( 'app' + '\.[a-z0-9]{32}\.css'))[0].slice(4, 36), fileNameJs: esData.match(new RegExp( 'app' + '\.[a-z0-9]{20}\.js'))[0].slice(4, 24), vender: esData.match(new RegExp( 'vendor' + '\.[a-z0-9]{20}\.js'))[0].slice(7, 27), manifest: esData.match(new RegExp('manifest\.[a-z0-9]{20}\.js'))[0].slice(9, 29) } console.log(tempObj)
return tempObj
}
function copy( src, dst ){ fs.readdir( src, function( err, paths ){ if( err ){ throw err; }
paths.forEach(function( path ){ var _src = src + '/' + path, _dst = dst + '/' + path, readable, writable;
if(localConfig.ignore.indexOf(path) != -1) return;
stat( _src, function( err, st ){ if( err ){ throw err; } if( st.isFile() ){ readable = fs.createReadStream( _src ); writable = fs.createWriteStream( _dst ); readable.pipe( writable ); } else if( st.isDirectory() ){ exists( _src, _dst, copy ); } }); }); }); };
function exists( src, dst, callback ){ fs.exists( dst, function( exists ){ if( exists ){ callback( src, dst ); } else{ fs.mkdir( dst, function(){ callback( src, dst ); }); } }); };
|