-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.js
64 lines (54 loc) · 1.95 KB
/
build.js
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
// build peanut plugin
// (c) 2023 Gleamoe
// License: MIT
const fs = require('fs');
const fsExtra = require('fs-extra');
const path = require('path');
if (fs.existsSync("./out")) {
fs.rmSync("./out", { recursive: true, force: true });
}
fs.mkdirSync("./out");
const packageFile = require("./package.json");
const pluginPath = path.resolve(__dirname,"./out/" + packageFile.name);
fs.mkdirSync(pluginPath);
fsExtra.copySync("./dist", pluginPath + "/dist");
fs.copyFileSync("./preload.js", pluginPath + "/dist/preload.js");
fs.copyFileSync("./README.md", pluginPath + "/README.md");
fs.copyFileSync("./LICENSE", pluginPath + "/LICENSE");
fs.copyFileSync("./ffmpeg-LICENSE.txt", pluginPath + "/dist/ffmpeg-LICENSE.txt");
fs.copyFileSync("./ffmpeg.exe", pluginPath + "/dist/ffmpeg.exe");
delete packageFile.dependencies;
delete packageFile.devDependencies;
delete packageFile.scripts;
packageFile.main = "dist/main.js";
fsExtra.writeJsonSync(pluginPath + "/package.json", packageFile, { spaces: 2 });
const wrapFileInFunction = (file, savePath) => {
fs.readFile(file, 'utf8', (err, data) => {
if (err) {
console.error(`Read ${file} failed: ${err}`);
return;
}
const importStatements = [];
const codeLines = [];
data.split('\n').forEach((line) => {
if (line.startsWith('const') && line.includes('require')) {
importStatements.push(line);
} else {
codeLines.push(line);
}
});
const wrappedFunction = [
...importStatements,
'const f = (context) => {',
...codeLines,
'};',
'module.exports = f;',
].join('\n');
fs.writeFile(path.resolve(savePath,file), wrappedFunction, 'utf8', (err) => {
if (err) {
console.error(`Write file failed: ${err}`);
}
});
});
}
wrapFileInFunction('./main.js', pluginPath + "/dist");