-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzipDist.js
More file actions
59 lines (49 loc) · 1.67 KB
/
zipDist.js
File metadata and controls
59 lines (49 loc) · 1.67 KB
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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const readline = require('readline');
const archiver = require('archiver');
// Create a readline interface for prompting the user.
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Function that zips a folder using archiver.
function zipFolder(folderPath, outputPath) {
return new Promise((resolve, reject) => {
const output = fs.createWriteStream(outputPath);
const archive = archiver('zip', { zlib: { level: 9 } });
output.on('close', () => resolve(archive.pointer()));
archive.on('error', (err) => reject(err));
archive.pipe(output);
archive.directory(folderPath, false);
archive.finalize();
});
}
const distFolder = path.join(__dirname, 'dist');
const zippedDir = path.join(__dirname, 'zipped_prod_builds');
// Verify that the dist folder exists.
if (!fs.existsSync(distFolder)) {
console.error('Error: "dist" folder does not exist in this directory.');
process.exit(1);
}
// Ensure the zipped_prod_builds directory exists, or create it.
if (!fs.existsSync(zippedDir)) {
fs.mkdirSync(zippedDir);
}
// Prompt the user for the zip file title.
rl.question('Enter the title for the zip file: ', async (zipTitle) => {
if (!zipTitle.trim()) {
console.error('No title provided. Exiting.');
rl.close();
return;
}
const outputZipPath = path.join(zippedDir, `${zipTitle.trim()}.zip`);
try {
const bytes = await zipFolder(distFolder, outputZipPath);
console.log(`Successfully zipped ${bytes} total bytes to ${outputZipPath}`);
} catch (err) {
console.error('Error zipping folder:', err);
}
rl.close();
});