-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
84 lines (74 loc) · 2.83 KB
/
index.js
File metadata and controls
84 lines (74 loc) · 2.83 KB
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
const fs = require("fs");
const logSymbols = require("log-symbols");
Reset = "\x1b[0m";
Bright = "\x1b[1m";
Dim = "\x1b[2m";
Underscore = "\x1b[4m";
Blink = "\x1b[5m";
Reverse = "\x1b[7m";
Hidden = "\x1b[8m";
FgBlack = "\x1b[30m";
FgRed = "\x1b[31m";
FgGreen = "\x1b[32m";
FgYellow = "\x1b[33m";
FgBlue = "\x1b[34m";
FgMagenta = "\x1b[35m";
FgCyan = "\x1b[36m";
FgWhite = "\x1b[37m";
/**
* @module WebpackOnBuildPlugin
*/
/**
* @constructor
* @param {onBuildCallback} callback - will be called right after build.
*/
function WebpackOnBuildPlugin ({ platform }) {
this.platform = platform;
}
/**
* @callback onBuildCallback
* @param {object} stats - webpack stats object
*/
/**
* @param {object} compiler
*/
WebpackOnBuildPlugin.prototype.apply = function(compiler) {
const { platform } = this;
const platformFolderName = platform.replace(`${process.env.PWD}/`, "");
compiler.hooks.done.tap("WebpackOnBuildPlugin", ({ compilation, ...params }) => {
try {
const testFolder = `${platform}/widget`;
const outputFiles = Array.from(compiler._assetEmittingWrittenFiles.keys());
const leadWidgetConfig = async (widgetName) => require(`${platform}/.ccc/widget/${widgetName}/widget.json`);
fs.readdir(testFolder, async (err, files) => {
console.log("Preparing your " + FgCyan + platformFolderName + Reset + " folder so that it is ready to be deployed...", "\n");
if (err) {
console.log("\n" + "The " + FgCyan + platformFolderName + Reset + " folder not found.", "\n");
return
}
await Promise.all(
(files || []).map(async (widgetName) => {
const targetDir = `${testFolder}/${widgetName}/js`;
const widgetConfig = await leadWidgetConfig(widgetName).catch((e) => ({}));
let output = outputFiles.find((fileName) => fileName.includes(`${widgetConfig.javascript}`));
if (!output) {
console.log(" ", logSymbols.error, `/widget${FgGreen}/${widgetName}/${Reset}js/${widgetConfig.javascript}.js`);
return;
}
console.log(" ", logSymbols.success, `/widget${FgGreen}/${widgetName}/${Reset}js/${widgetConfig.javascript}.js`);
fs.mkdirSync(targetDir, { recursive: true });
fs.createReadStream(output).pipe(fs.createWriteStream(`${targetDir}/${widgetConfig.javascript}.js`));
}),
);
console.log("\n" + "The", FgCyan + platformFolderName + Reset, "folder is ready to be deployed.");
console.log("Find out more about deployment here:", "\n");
console.log(FgYellow, "https://bit.ly/2YSc5vH", Reset, "\n");
});
} catch (e) {
console.error(e);
console.log("\n" + "please create an issue: ", "\n");
console.log(FgYellow, "https://bit.ly/2YSc5vH", Reset, "\n");
}
});
};
module.exports = WebpackOnBuildPlugin;