|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const chalk = require('chalk'); |
| 4 | +const request = require('request'); |
| 5 | + |
| 6 | +module.exports = class DeployServerWebpackPlugin { |
| 7 | + constructor(config = {}) { |
| 8 | + this.config = config; |
| 9 | + } |
| 10 | + |
| 11 | + apply(compiler) { |
| 12 | + compiler.plugin('after-emit', (compilation, callback) => { |
| 13 | + this.validConfig(compilation); |
| 14 | + this.deployHandler(callback); |
| 15 | + }); |
| 16 | + } |
| 17 | + |
| 18 | + validConfig(compilation) { |
| 19 | + const { receiver, token = '' } = this.config; |
| 20 | + let { mapping } = this.config; |
| 21 | + |
| 22 | + if (!receiver) { |
| 23 | + this.error('Missing param: receiver'); |
| 24 | + } |
| 25 | + if (!mapping) { |
| 26 | + this.error('Missing param: mapping'); |
| 27 | + } |
| 28 | + |
| 29 | + const mappingType = Object.prototype.toString.call(mapping); |
| 30 | + if (mappingType === '[object Object]') { |
| 31 | + mapping = [mapping]; |
| 32 | + } else if (mappingType === '[object Array]') { |
| 33 | + // do nothing |
| 34 | + } else { |
| 35 | + this.error('Invalid param: mapping'); |
| 36 | + } |
| 37 | + |
| 38 | + const assets = compilation.assets; |
| 39 | + const avalAssets = {}; |
| 40 | + mapping.map((item, index) => { |
| 41 | + for (let key in assets) { |
| 42 | + const asset = assets[key]; |
| 43 | + const assetPath = asset.existsAt; |
| 44 | + const from = item.from; |
| 45 | + // limit "src" path within compiled files |
| 46 | + if (assetPath.startsWith(from)) { |
| 47 | + if (!avalAssets[from]) { |
| 48 | + avalAssets[from] = [assetPath]; |
| 49 | + continue; |
| 50 | + } |
| 51 | + avalAssets[from].push(assetPath); |
| 52 | + } |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + const avalKeys = Object.keys(avalAssets); |
| 57 | + if (!avalKeys.length) { |
| 58 | + this.error('No available mapping files'); |
| 59 | + } |
| 60 | + |
| 61 | + this.receiver = receiver; |
| 62 | + this.mapping = mapping; |
| 63 | + this.token = token; |
| 64 | + this.avalKeys = avalKeys; |
| 65 | + this.avalAssets = avalAssets; |
| 66 | + } |
| 67 | + |
| 68 | + deployHandler(callback) { |
| 69 | + const formData = {}; |
| 70 | + this.avalKeys.map((item, index) => { |
| 71 | + this.avalAssets[item].map(from => { |
| 72 | + // compatible with windows |
| 73 | + const dest = (this.mapping[index].to + from.replace(item, '')).replace(/\\/g, '/'); |
| 74 | + this.deploy({ |
| 75 | + dest, |
| 76 | + token: this.token, |
| 77 | + file: fs.createReadStream(from) |
| 78 | + }); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + callback(); |
| 83 | + } |
| 84 | + |
| 85 | + deploy(formData) { |
| 86 | + request.post({ |
| 87 | + url: this.receiver, |
| 88 | + formData |
| 89 | + }, (err, { statusCode } = {}, body) => { |
| 90 | + const time = new Date().toLocaleTimeString(); |
| 91 | + if (!err && 200 === statusCode) { |
| 92 | + console.log(chalk.green(`[${time}] [success] => ${formData.dest}`)); |
| 93 | + return; |
| 94 | + } |
| 95 | + console.log(chalk.yellow(`[${time}] [failed] => ${formData.dest} ${body}`)); |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + error(err) { |
| 100 | + console.log(`\n${chalk.yellow('[deploy-server-webpack-plugin] ' + err)}`); |
| 101 | + process.exit(); |
| 102 | + } |
| 103 | +}; |
0 commit comments