|
| 1 | +"use strict"; |
| 2 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 3 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 4 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 5 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 6 | + function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } |
| 7 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 8 | + }); |
| 9 | +}; |
| 10 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 11 | +const path = require("path"); |
| 12 | +const aws = require("aws-sdk"); |
| 13 | +const fs = require("fs"); |
| 14 | +const S3UploaderUtil_1 = require("./S3UploaderUtil"); |
| 15 | +const FileUtil_1 = require("./FileUtil"); |
| 16 | +class S3Uploader { |
| 17 | + constructor($option) { |
| 18 | + this.configS3 = () => { |
| 19 | + aws.config.update({ |
| 20 | + accessKeyId: this.options.accessKeyId, |
| 21 | + secretAccessKey: this.options.secretAccessKey, |
| 22 | + region: this.options.region, |
| 23 | + signatureVersion: "v4", |
| 24 | + }); |
| 25 | + this.s3 = new aws.S3({ apiVersion: "2006-03-01" }); |
| 26 | + }; |
| 27 | + this.apply = (compiler) => { |
| 28 | + compiler.plugin("done", compilation => { |
| 29 | + console.log("[S3Uploader apply] compile finished"); |
| 30 | + fs.readdir(this.staticPath, ($err, $subDir) => { |
| 31 | + if ($err) { |
| 32 | + console.log(`\x1b[31m[S3Uploader apply] static directory read error : ${$err.message} \x1b[0m`); |
| 33 | + } |
| 34 | + else { |
| 35 | + this.uploadList = $subDir.reduce(($result, $dir) => { |
| 36 | + const subDirPath = path.join(this.staticPath, $dir); |
| 37 | + return $result.concat(fs.readdirSync(subDirPath).map($file => path.join(subDirPath, $file))); |
| 38 | + }, []); |
| 39 | + this.upload().then($res => { |
| 40 | + console.log("\x1b[33m%s\x1b[0m", `\n-- UPLOAD COMPLETE --\n`); |
| 41 | + console.log("\x1b[33m%s\x1b[0m", ` JS : ${this.uploadResult.js}`); |
| 42 | + console.log("\x1b[33m%s\x1b[0m", ` CSS : ${this.uploadResult.css}\n`); |
| 43 | + if (this.options.replaceHtml) { |
| 44 | + this.replaceIndexHtml(); |
| 45 | + } |
| 46 | + }); |
| 47 | + } |
| 48 | + }); |
| 49 | + }); |
| 50 | + }; |
| 51 | + this.upload = () => __awaiter(this, void 0, void 0, function* () { |
| 52 | + console.log(`[S3Uploader upload] list count ${this.uploadList.length}`); |
| 53 | + for (const file of this.uploadList) { |
| 54 | + if (path.extname(file) === ".js") { |
| 55 | + this.updateUrlPublicPath(file, false); |
| 56 | + } |
| 57 | + else if (path.extname(file) === ".css") { |
| 58 | + this.updateUrlPublicPath(file, true); |
| 59 | + } |
| 60 | + yield new Promise(resolve => { |
| 61 | + const key = file.replace(this.staticPath, `${(!this.options.key) ? "" : this.options.key + "/"}static`); |
| 62 | + this.s3.putObject({ |
| 63 | + ACL: this.options.acl || "public-read", |
| 64 | + Bucket: this.options.bucket, |
| 65 | + Key: key, |
| 66 | + ContentType: FileUtil_1.getContentType(file), |
| 67 | + Body: fs.createReadStream(file), |
| 68 | + }, ($err, $res) => { |
| 69 | + if ($err) { |
| 70 | + console.log(`\x1b[31m[S3Uploader upload] s3 putObject error : ${$err.message} \x1b[0m`); |
| 71 | + } |
| 72 | + else { |
| 73 | + const url = `https://${this.uploadDomain}/${key}`; |
| 74 | + console.log("\x1b[33m%s\x1b[0m", url); |
| 75 | + if (path.extname(key) === ".js") { |
| 76 | + this.uploadResult.js = url; |
| 77 | + } |
| 78 | + else if (path.extname(key) === ".css") { |
| 79 | + this.uploadResult.css = url; |
| 80 | + } |
| 81 | + resolve(); |
| 82 | + } |
| 83 | + }); |
| 84 | + }); |
| 85 | + } |
| 86 | + return Promise.resolve(); |
| 87 | + }); |
| 88 | + this.updateUrlPublicPath = ($filePath, $isCSS) => { |
| 89 | + const file = fs.readFileSync($filePath, "utf8"); |
| 90 | + const reg = $isCSS ? /\/static\/media\//g : /n.p\+\"static\/media\//g; |
| 91 | + const https = $isCSS ? `https://` : `"https://`; |
| 92 | + fs.writeFileSync($filePath, file.replace(reg, `${https}${this.uploadDomain}/static/media/`), "utf8"); |
| 93 | + }; |
| 94 | + this.replaceIndexHtml = () => { |
| 95 | + fs.readFile(this.htmlPath, "utf8", ($err, $html) => { |
| 96 | + if ($err) { |
| 97 | + console.log(`\x1b[31m[S3Uploader replaceIndexHtml] read index.html error : ${$err.message} \x1b[0m`); |
| 98 | + } |
| 99 | + else { |
| 100 | + const cssTag = `<link href="`; |
| 101 | + const jsTag = `<script type="text/javascript" src="`; |
| 102 | + const cssSrc = this.uploadResult.css.substring(0, this.uploadResult.css.indexOf("static/css") + "static/css".length); |
| 103 | + const jsSrc = this.uploadResult.js.substring(0, this.uploadResult.js.indexOf("static/js") + "static/js".length); |
| 104 | + const updateHtml = $html.replace(`${cssTag}/static/css`, `${cssTag}${cssSrc}`).replace(`${jsTag}/static/js`, `${jsTag}${jsSrc}`); |
| 105 | + fs.writeFile(this.htmlPath, updateHtml, "utf8", ($error) => { |
| 106 | + if ($error) { |
| 107 | + console.log(`\x1b[31m[S3Uploader replaceIndexHtml] replace index.html's css & js error : ${$error.message} \x1b[0m`); |
| 108 | + } |
| 109 | + else { |
| 110 | + console.log("\x1b[33m%s\x1b[0m", "[S3Uploader replaceIndexHtml] replace index.html success"); |
| 111 | + } |
| 112 | + }); |
| 113 | + } |
| 114 | + }); |
| 115 | + }; |
| 116 | + S3UploaderUtil_1.validateParam($option); |
| 117 | + this.options = $option; |
| 118 | + this.staticPath = path.join(this.options.buildPath, "static"); |
| 119 | + this.htmlPath = path.join(this.options.buildPath, "index.html"); |
| 120 | + this.uploadDomain = this.options.cloudfront || `s3.${this.options.region}.amazonaws.com/${this.options.bucket}`; |
| 121 | + this.uploadResult = { css: "", js: "" }; |
| 122 | + this.configS3(); |
| 123 | + } |
| 124 | +} |
| 125 | +exports.default = S3Uploader; |
0 commit comments