|
| 1 | +const fs = require("fs"); |
| 2 | +const logSymbols = require("log-symbols"); |
| 3 | + |
| 4 | +Reset = "\x1b[0m"; |
| 5 | +Bright = "\x1b[1m"; |
| 6 | +Dim = "\x1b[2m"; |
| 7 | +Underscore = "\x1b[4m"; |
| 8 | +Blink = "\x1b[5m"; |
| 9 | +Reverse = "\x1b[7m"; |
| 10 | +Hidden = "\x1b[8m"; |
| 11 | + |
| 12 | +FgBlack = "\x1b[30m"; |
| 13 | +FgRed = "\x1b[31m"; |
| 14 | +FgGreen = "\x1b[32m"; |
| 15 | +FgYellow = "\x1b[33m"; |
| 16 | +FgBlue = "\x1b[34m"; |
| 17 | +FgMagenta = "\x1b[35m"; |
| 18 | +FgCyan = "\x1b[36m"; |
| 19 | +FgWhite = "\x1b[37m"; |
| 20 | + |
| 21 | +/** |
| 22 | + * @module WebpackOnBuildPlugin |
| 23 | + */ |
| 24 | + |
| 25 | +/** |
| 26 | + * @constructor |
| 27 | + * @param {onBuildCallback} callback - will be called right after build. |
| 28 | + */ |
| 29 | +function WebpackOnBuildPlugin({ platform }) { |
| 30 | + this.platform = platform; |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * @callback onBuildCallback |
| 35 | + * @param {object} stats - webpack stats object |
| 36 | + */ |
| 37 | + |
| 38 | +/** |
| 39 | + * @param {object} compiler |
| 40 | + */ |
| 41 | +WebpackOnBuildPlugin.prototype.apply = function(compiler) { |
| 42 | + const { platform } = this; |
| 43 | + const platformFolderName = platform.split('/').pop(); |
| 44 | + compiler.hooks.done.tap("WebpackOnBuildPlugin", ({ compilation, ...params }) => { |
| 45 | + const testFolder = `${platform}/widget`; |
| 46 | + const outputFiles = Array.from(compiler._assetEmittingWrittenFiles.keys()); |
| 47 | + |
| 48 | + const leadWidgetConfig = async (widgetName) => require(`${platform}/.ccc/widget/${widgetName}/widget.json`) |
| 49 | + |
| 50 | + fs.readdir(testFolder, async (err, files) => { |
| 51 | + console.log("Preparing your " + FgCyan + platformFolderName + Reset + " folder so that it is ready to be deployed...\n"); |
| 52 | + await Promise.allSettled( |
| 53 | + files.map(async (file) => { |
| 54 | + const targetDir = `${testFolder}/${file}/js`; |
| 55 | + const widgetName = file.toLowerCase(); |
| 56 | + const widgetConfig = await leadWidgetConfig(widgetName).catch((e) => ({})); |
| 57 | + let output = outputFiles.find((fileName) => fileName.includes(`${widgetConfig.javascript}.widget`)); |
| 58 | + fs.rmdirSync(targetDir, { recursive: true }); |
| 59 | + if (!output) { |
| 60 | + console.log(' ', logSymbols.error, `/widget${FgGreen}/${file}/${Reset}js/${widgetConfig.javascript}.js`); |
| 61 | + return; |
| 62 | + } |
| 63 | + console.log(' ', logSymbols.success, `/widget${FgGreen}/${file}/${Reset}js/${widgetConfig.javascript}.js`); |
| 64 | + fs.mkdirSync(targetDir, { recursive: true }); |
| 65 | + fs.createReadStream(output).pipe(fs.createWriteStream(`${targetDir}/${widgetConfig.javascript}.js`)); |
| 66 | + }), |
| 67 | + ); |
| 68 | + console.log("\nThe " + FgCyan + platformFolderName + Reset + " folder is ready to be deployed.\n"); |
| 69 | + console.log("Find out more about deployment here:\n"); |
| 70 | + }); |
| 71 | + }); |
| 72 | +}; |
| 73 | + |
| 74 | +module.exports = WebpackOnBuildPlugin; |
0 commit comments