-
Notifications
You must be signed in to change notification settings - Fork 3
/
postbuild.js
54 lines (45 loc) · 1.78 KB
/
postbuild.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
const fs = require('fs');
const path = require('path');
const archiver = require('archiver');
require('dotenv').config({ path: './build-settings.env' });
const distPath = path.join(__dirname, 'dist', 'custom-module');
const targetPath = path.join(__dirname, 'dist', `${process.env.INST_ID}-${process.env.VIEW_ID}`);
const zipPath = path.join(__dirname, 'dist', `${process.env.INST_ID}-${process.env.VIEW_ID}.zip`);
function removeDirectory(directory, callback) {
fs.rm(directory, { recursive: true, force: true }, callback);
}
function renameAndArchive() {
fs.rename(distPath, targetPath, (err) => {
if (err) throw err;
console.log(`Renamed directory to ${targetPath}`);
const output = fs.createWriteStream(zipPath);
const archive = archiver('zip', { zlib: { level: 9 } });
output.on('close', () => {
console.log(`Archive completed: ${archive.pointer()} total bytes`);
console.log(`Zip file created at: ${zipPath}`);
console.log('Please upload the zip file to Alma BO custom package section to deploy your custom module.');
});
archive.on('warning', (err) => {
if (err.code === 'ENOENT') {
console.log('Warning:', err);
} else {
throw err;
}
});
archive.on('error', (err) => {
throw err;
});
archive.pipe(output);
archive.directory(targetPath, path.basename(targetPath)); // This ensures the directory itself is included
archive.finalize();
});
}
// Check if target directory exists and remove it if it does
if (fs.existsSync(targetPath)) {
removeDirectory(targetPath, (err) => {
if (err) throw err;
renameAndArchive();
});
} else {
renameAndArchive();
}