This repository has been archived by the owner on Dec 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
67 lines (58 loc) · 2.54 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
const JSZip = require("jszip"), fs = require('fs'), path = require('path');
const chromeFiles = [{origin: 'background.chrome.js', name: 'background.js'}, 'gptsaver.js', {origin: 'manifest.chrome.json', name: 'manifest.json'}, 'filesInfo.json'];
const firefoxFiles = [{origin: 'background.firefox.html', name: 'background.html'}, 'gptsaver.js', {origin: 'manifest.firefox.json', name: 'manifest.json'}, 'filesInfo.json'];
function create(rootFiles, name) {
const zip = new JSZip();
for (const file of rootFiles) {
let name = typeof file == "object" ? file.name : file
let source = typeof file == "object" ? file.origin : file
zip.file(name, fs.readFileSync(path.join(__dirname, source)));
}
const dirsToZip = ['modules', 'assets'];
for (const dir of dirsToZip) {
const dirPath = path.join(__dirname, dir);
if (fs.existsSync(dirPath)) {
const files = getAllFilesFromDir(dirPath);
for (const file of files) {
const relativePath = path.relative(dirPath, file);
zip.file(`${dir}/${relativePath}`, fs.readFileSync(file));
}
}
}
zip
.generateNodeStream({type:'nodebuffer',streamFiles:true})
.pipe(fs.createWriteStream(`${name}.zip`))
.on('finish', function () {
console.log(`${name}.zip written.`);
fs.readFile(`${name}.zip`, function (err, data) {
if (err) throw err;
JSZip.loadAsync(data).then(function (zip) {
zip.forEach(function (relativePath, zipEntry) {
if (!zipEntry.dir) {
zipEntry.async('nodebuffer').then(function (content) {
const extractedFilePath = `ext/${name}/${relativePath}`;
fs.mkdirSync(extractedFilePath.substring(0, extractedFilePath.lastIndexOf('/')), { recursive: true });
fs.writeFileSync(extractedFilePath, content);
});
}
});
console.log('Contenu du zip extrait.');
})
})
});
function getAllFilesFromDir(dirPath, fileTypes = []) {
return fs.readdirSync(dirPath).reduce((files, file) => {
const filePath = path.join(dirPath, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
return files.concat(getAllFilesFromDir(filePath, fileTypes));
} else if (fileTypes.length === 0 || fileTypes.includes(path.extname(filePath))) {
return files.concat(filePath);
} else {
return files;
}
}, []);
}
}
create(chromeFiles, 'chrome')
create(firefoxFiles, 'firefox')