-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.mjs
executable file
·61 lines (50 loc) · 1.71 KB
/
build.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
import { createWriteStream } from 'fs';
import { dirname } from 'path';
import { fileURLToPath } from 'url';
import { readFile } from 'fs/promises';
import { execSync } from 'child_process';
import archiver from 'archiver';
const manifest = await getManifest();
console.log('Building app...');
execSync('npm run build', { stdio: 'inherit' });
execSync('cp ./manifest.webapp ./build/manifest.webapp');
console.log('Packaging app...');
const result = await createZip();
console.log(`Created ${result.fileName} (${result.totalBytes} bytes)`);
if (process.argv[2] && process.argv[2] === '--deploy') {
console.log('Deploying app...');
execSync(`kosqi install --id ${manifest.id} --path ./${result.fileName}`, { stdio: 'inherit' });
}
async function getManifest() {
const file = await readFile('./manifest.webapp', 'utf8');
return JSON.parse(file);
}
function createZip() {
const dir = dirname(fileURLToPath(import.meta.url));
const fileName = `${manifest.id}_${manifest.version}.zip`;
const output = createWriteStream(`${dir}/${fileName}`);
const archive = archiver('zip', {
zlib: { level: 9 }
});
return new Promise((resolve, reject) => {
output.on('close', function() {
resolve({
fileName,
totalBytes: archive.pointer()
});
});
archive.on('warning', function(err) {
if (err.code === 'ENOENT') {
console.warn(err);
} else {
reject(err);
}
});
archive.on('error', function(err) {
reject(err);
});
archive.pipe(output);
archive.directory('build/', false);
archive.finalize();
});
}