-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzip.mjs
78 lines (66 loc) · 2.11 KB
/
zip.mjs
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
import path from 'node:path';
import fs from 'fs-extra';
import JSZip from 'jszip';
import { glob } from 'glob';
async function zipProject() {
try {
// Read package.json and manifest.json for name and version
const packageJson = await fs.readJson('./package.json');
const manifestJson = await fs.readJson('./manifest.json');
const outputDir = path.join(process.cwd(), '.output');
const zipFileName = `${packageJson.name}-${manifestJson.version}.zip`;
const zipFilePath = path.join(outputDir, zipFileName);
// Ensure output directory exists
await fs.ensureDir(outputDir);
// Define exclusion rules
const excludePatterns = [
'**/node_modules/**',
'**/scss/**',
'zip.mjs',
'.ext-template.mjs',
'.prettierignore',
'.gitignore',
'jsconfig.json',
'package.json',
'package-lock.json',
'prettier.config.mjs',
'ensure-ext-config.mjs',
];
// Read .gitignore to get additional exclusions
let gitignorePatterns = [];
try {
const gitignore = await fs.readFile('.gitignore', 'utf-8');
gitignorePatterns = gitignore
.split('\n')
.map((f) => f.trim())
.filter(Boolean);
} catch (err) {
console.log('.gitignore not found or unreadable, skipping...');
}
// Combine exclusion patterns
const allExcludePatterns = [...excludePatterns, ...gitignorePatterns];
// Find all files that are not excluded
const files = await glob(['**/*'], {
ignore: allExcludePatterns,
nodir: true,
});
if (files.length === 0) {
console.log('No files to zip, exiting...');
return;
}
// Create the zip file
const zip = new JSZip();
for (const file of files) {
const content = await fs.readFile(file);
zip.file(file, content);
}
// Write the zip file
const zipContent = await zip.generateAsync({ type: 'nodebuffer' });
await fs.writeFile(zipFilePath, zipContent);
console.log(`Zip file created: ${zipFilePath}`);
} catch (error) {
console.error('An error occurred:', error);
}
}
// Run the zip function
zipProject();