generated from imohanvadivel/desk-ext-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
102 lines (87 loc) · 3.16 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import AdmZip from "adm-zip";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
appendAppInURL();
moveFiles();
setTimeout(() => zipFiles(), 100);
function moveFiles() {
// Create the app directory in output
const appDir = path.join(__dirname, "output", "app");
if (!fs.existsSync(appDir)) {
fs.mkdirSync(appDir);
}
// Paths to the files/directories you want to move
const sourceFiles = [
path.join(__dirname, "output", "widget1.html"),
path.join(__dirname, "output", "widget2.html"),
path.join(__dirname, "output", "chunks"),
path.join(__dirname, "output", "assets")
];
const destinationDir = path.join(__dirname, "output", "app");
// Ensure the destination directory exists
if (!fs.existsSync(destinationDir)) {
console.error("Destination directory does not exist");
process.exit(1);
}
// Move each file/directory to the destination directory
sourceFiles.forEach((source) => {
const destPath = path.join(destinationDir, path.basename(source));
fs.rename(source, destPath, (err) => {
if (err) {
console.error(`Error moving ${source}:`, err);
}
});
});
}
function appendAppInURL() {
// Define the path to the JSON file
const filePath = path.join(__dirname, "output/plugin-manifest.json");
// Read the JSON file
fs.readFile(filePath, "utf8", (err, data) => {
if (err) {
console.error("Error reading the file:", err);
return;
}
// Parse the JSON data
let jsonData;
try {
jsonData = JSON.parse(data);
} catch (parseError) {
console.error("Error parsing JSON:", parseError);
return;
}
// Modify the "url" field in the "widgets" array
if (jsonData.modules && jsonData.modules.widgets) {
jsonData.modules.widgets.forEach((widget) => {
if (widget.url && !widget.url.startsWith("/app")) {
widget.url = `/app${widget.url}`;
}
if (widget.logo && !widget.logo.startsWith("/app")) {
widget.logo = `/app${widget.logo}`;
}
if (widget.icon && !widget.icon.startsWith("/app")) {
widget.icon = `/app${widget.icon}`;
}
});
}
// Convert the modified JSON back to a string
const updatedJson = JSON.stringify(jsonData, null, 2);
// Write the updated JSON back to the file
fs.writeFile(filePath, updatedJson, "utf8", (writeErr) => {
if (writeErr) {
console.error("Error writing the manifest file:", writeErr);
}
});
});
}
function zipFiles() {
const zip = new AdmZip();
const filePath = path.join(__dirname, "output");
zip.addLocalFolder(filePath);
const outputZipPath = path.join(__dirname, "dist", "ext.zip");
zip.writeZip(outputZipPath);
console.log(`Zipped file saved to ${outputZipPath}`);
}