Skip to content

Commit 267ddad

Browse files
authored
Update pack-zip.js
1 parent 5e2bd9f commit 267ddad

1 file changed

Lines changed: 51 additions & 36 deletions

File tree

.acode/pack-zip.js

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,62 @@
11
const path = require('path');
22
const fs = require('fs');
3-
const jszip = require('jszip');
4-
5-
const iconFile = path.join(__dirname, '../icon.png');
6-
const pluginJSON = path.join(__dirname, '../plugin.json');
7-
const distFolder = path.join(__dirname, '../dist');
8-
let readmeDotMd = path.join(__dirname, '../readme.md');
9-
10-
if (!fs.existsSync(readmeDotMd)) {
11-
readmeDotMd = path.join(__dirname, '../README.md');
3+
const JSZip = require('jszip');
4+
5+
const rootDir = path.join(__dirname, '..');
6+
const distFolder = path.join(rootDir, 'dist');
7+
const zip = new JSZip();
8+
9+
function safeAddFile(zipObj, name, filePath) {
10+
if (fs.existsSync(filePath)) {
11+
zipObj.file(name, fs.readFileSync(filePath));
12+
console.log(`Added: ${name}`);
13+
} else {
14+
console.warn(`Missing: ${filePath}`);
15+
}
1216
}
1317

14-
// create zip file of dist folder
15-
16-
const zip = new jszip();
18+
// tambahin file wajib di root zip
19+
safeAddFile(zip, 'icon.png', path.join(rootDir, 'icon.png'));
20+
safeAddFile(zip, 'plugin.json', path.join(rootDir, 'plugin.json'));
1721

18-
zip.file('icon.png', fs.readFileSync(iconFile));
19-
zip.file('plugin.json', fs.readFileSync(pluginJSON));
20-
zip.file('readme.md', fs.readFileSync(readmeDotMd));
22+
// readme case-insensitive
23+
let readmePath = path.join(rootDir, 'readme.md');
24+
if (!fs.existsSync(readmePath)) {
25+
readmePath = path.join(rootDir, 'README.md');
26+
}
27+
safeAddFile(zip, 'readme.md', readmePath);
28+
29+
// ambil main.js dari dist langsung di root zip
30+
safeAddFile(zip, 'main.js', path.join(distFolder, 'main.js'));
31+
32+
// copy folder assets (kalau ada)
33+
const assetsFolder = path.join(distFolder, 'assets');
34+
if (fs.existsSync(assetsFolder)) {
35+
function addFolder(zipObj, folderPath, relativePath = '') {
36+
const entries = fs.readdirSync(folderPath);
37+
entries.forEach((file) => {
38+
const fullPath = path.join(folderPath, file);
39+
const stat = fs.statSync(fullPath);
40+
if (stat.isDirectory()) {
41+
const subFolder = zipObj.folder(path.join(relativePath, file));
42+
addFolder(subFolder, fullPath, path.join(relativePath, file));
43+
} else {
44+
zipObj.file(path.join(relativePath, file), fs.readFileSync(fullPath));
45+
}
46+
});
47+
}
48+
addFolder(zip.folder('assets'), assetsFolder, '');
49+
}
2150

22-
loadFile('', distFolder);
51+
// generate fresh zip
52+
const outPath = path.join(rootDir, 'AI.zip');
53+
if (fs.existsSync(outPath)) {
54+
fs.unlinkSync(outPath);
55+
}
2356

2457
zip
2558
.generateNodeStream({ type: 'nodebuffer', streamFiles: true })
26-
.pipe(fs.createWriteStream(path.join(__dirname, '../AI.zip')))
59+
.pipe(fs.createWriteStream(outPath))
2760
.on('finish', () => {
28-
console.log('dist.zip written.');
29-
});
30-
31-
function loadFile(root, folder) {
32-
const distFiles = fs.readdirSync(folder);
33-
distFiles.forEach((file) => {
34-
35-
const stat = fs.statSync(path.join(folder, file));
36-
37-
if (stat.isDirectory()) {
38-
zip.folder(file);
39-
loadFile(path.join(root, file), path.join(folder, file));
40-
return;
41-
}
42-
43-
if (!/LICENSE.txt/.test(file)) {
44-
zip.file(path.join(root, file), fs.readFileSync(path.join(folder, file)));
45-
}
61+
console.log('✅ AI.zip created cleanly');
4662
});
47-
}

0 commit comments

Comments
 (0)