-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathgulpfile.js
133 lines (107 loc) · 3.65 KB
/
gulpfile.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { createRequire } from "module";
import gulp from "gulp";
import gulpZip from "gulp-zip";
import fs from "fs";
import { glob } from "glob";
import * as path from "path";
function removeUnwantedFiles(cb) {
const filesToRemove = [
"dist/**/tailwind*.js",
"dist/**/postcss*.js",
"dist/gulpfile.js",
];
filesToRemove.forEach((pattern) => {
const files = glob.sync(pattern);
files.forEach((file) => {
try {
fs.unlinkSync(file);
} catch (err) {
console.error(`Error deleting ${file}: ${err}`);
}
});
});
cb();
}
function zip() {
const require = createRequire(import.meta.url);
const manifest = require("./package.json");
const zipFileName = `${manifest.name.replaceAll(" ", "-")}-${manifest.version}-${process.env.VITE_TARGET_BROWSER}.zip`;
return gulp
.src("dist/**")
.pipe(gulpZip(zipFileName))
.pipe(gulp.dest("package"));
}
const createPackage = gulp.series(removeUnwantedFiles, zip);
function rootifyOptionsPageHTMLEntries(done) {
const manifestPath = "./dist/manifest.json";
if (!fs.existsSync(manifestPath)) {
console.warn(
"Manifest file not found. Skipping rootifyOptionsPageHTMLEntries.",
);
return done();
}
const htmlFiles = glob.sync("dist/src/**/*.html");
htmlFiles.forEach((file) => {
// Move the file into the root of the dist folder
const fileName = path.basename(file);
const destPath = path.join("dist", fileName);
fs.renameSync(file, destPath);
console.log(`Moved ${file} to ${destPath}`);
// Update paths in the HTML file
const htmlContent = fs.readFileSync(destPath, "utf8");
const updatedHtmlContent = htmlContent.replace(/(\.\.\/)+(\w+)\//g, "$2/");
fs.writeFileSync(destPath, updatedHtmlContent);
// Remove empty directories
let currentDir = path.dirname(file);
while (currentDir !== "dist") {
if (fs.readdirSync(currentDir).length === 0) {
fs.rmdirSync(currentDir);
console.log(`Removed empty directory: ${currentDir}`);
currentDir = path.dirname(currentDir);
} else {
break;
}
}
// Reflect new file locations
try {
const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
if (manifest.options_ui && manifest.options_ui.page) {
manifest.options_ui.page = path.basename(manifest.options_ui.page);
}
if (manifest.options_page) {
manifest.options_page = path.basename(manifest.options_page);
}
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2));
console.log("Updated manifest.json with new file locations");
} catch (error) {
console.error("Error updating manifest.json:", error);
}
});
done();
}
function removeStaticCssFilesFromManifest(done) {
const manifestPath = "./dist/manifest.json";
if (!fs.existsSync(manifestPath)) {
console.warn(
"Manifest file not found. Skipping removeStaticCssFilesFromManifest.",
);
return done();
}
//remove content_scripts[n].css
try {
const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
// Remove CSS entries from content scripts
if (manifest.content_scripts) {
manifest.content_scripts = manifest.content_scripts.map((script) => {
const { css, ...rest } = script;
return rest;
});
}
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2));
console.log("Removed static CSS files from manifest.json content scripts");
} catch (error) {
console.error("Error removing CSS files from manifest.json:", error);
}
done();
}
export { createPackage, rootifyOptionsPageHTMLEntries, removeStaticCssFilesFromManifest };