-
Notifications
You must be signed in to change notification settings - Fork 1
/
dist.mjs
70 lines (65 loc) · 2.48 KB
/
dist.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
import { copyFile, readFile, writeFile } from 'node:fs/promises';
import path from 'node:path';
import nwbuild from 'nw-builder';
const packageManifest = JSON.parse(await readFile('./package.json'));
const appBaseDir = path.resolve('./dist/app/');
const defaultBuildCfg = {
manifestProps: [
'name',
'version',
'main',
],
osTypes: [
'windows',
'linux',
],
nwVersion: '0.70.1',
};
// Copy main.js to dist/app/ directory for packaging
// NOTE: The predist script should run webpack (or something similar) after `npm run build`, to bundle main.js and any Node.js dependencies into a single file.
// NOTE: If this isn't done, the following will need to be modified to copy all necessary files/dependencies to the dist/app/ directory.
console.log(`Copying Node-context script to ${appBaseDir}`);
try {
await copyFile('main.js', path.resolve(appBaseDir, 'main.js'));
} catch (error) {
console.error('Unable to copy Node-context script to app directory:', error);
}
// Create production package.json
console.log('Generating application manifest (package.json)...');
const manifestProps = packageManifest.build?.manifestProps || defaultBuildCfg.manifestProps;
const prodManifest = {};
for (const propName of manifestProps) {
prodManifest[propName] = packageManifest[propName];
}
try {
await writeFile(path.resolve(appBaseDir, 'package.json'), JSON.stringify(prodManifest, null, 4));
} catch (error) {
console.error('Unable to generate application manifest:', error);
}
// Build package for each OS type
const appOsTypes = packageManifest.build?.osTypes || defaultBuildCfg.osTypes;
const appName = packageManifest.build?.appName || packageManifest.name;
const appVersion = packageManifest.version || '1.0.0';
for (const osType of appOsTypes) {
console.log(`Building package for ${osType}...`);
const platform = osType === 'windows' ? 'win' : osType;
const nwVersion = packageManifest.devDependencies.nw.replace('^', '').split('-')[0] || defaultBuildCfg.nwVersion;
const outDir = path.resolve(`./dist/${appName}-${appVersion}-${osType}/`);
const nwBuildArgs = {
glob: false,
srcDir: appBaseDir,
version: nwVersion,
flavor: 'normal',
platform: platform,
arch: process.arch,
outDir,
run: false,
zip: true
};
try {
await nwbuild(nwBuildArgs);
} catch (error) {
console.error(`Error building package for ${osType}`);
}
console.log(`Finished building package for ${osType}`);
}