-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbundle.js
More file actions
170 lines (143 loc) · 4.7 KB
/
Copy pathbundle.js
File metadata and controls
170 lines (143 loc) · 4.7 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
const esbuild = require('esbuild');
const fs = require('fs-extra');
const path = require('path');
const DIST_MAIN_PATH = './dist/main.js';
const BUNDLE_DIR = './dist/bundle-min';
async function bundleMinified() {
try {
const options = parseBundleOptions();
ensureBuilt();
console.log('Building minified bundle...');
if (options.defaultGenerateArchiveCloudCache) {
console.log('Archive cloud cache generation defaults to enabled in this bundle.');
}
await fs.emptyDir(BUNDLE_DIR);
await bundleJavaScript(BUNDLE_DIR);
await copyNinja(BUNDLE_DIR);
await createLaunchScript(BUNDLE_DIR, options);
await createPackageJson(BUNDLE_DIR, options);
const bundleStats = await getBundleStats(BUNDLE_DIR);
console.log('Minified bundle created successfully.');
console.log(`Output directory: ${BUNDLE_DIR}`);
console.log(`Total bundle size: ${bundleStats.totalSizeFormatted}`);
console.log(`Files included: ${bundleStats.fileCount}`);
} catch (error) {
console.error('Minified bundle creation failed:', error);
process.exit(1);
}
}
function parseBundleOptions() {
const args = new Set(process.argv.slice(2));
const envValue = process.env.AILY_BUILDER_BUNDLE_GENERATE_ARCHIVE_CLOUD_CACHE?.toLowerCase();
return {
defaultGenerateArchiveCloudCache:
args.has('--generate-archive-cloud-cache-default') ||
args.has('--default-generate-archive-cloud-cache') ||
envValue === '1' ||
envValue === 'true',
};
}
function ensureBuilt() {
if (!fs.existsSync(DIST_MAIN_PATH)) {
console.log('dist/main.js not found. Please run "npm run build" first.');
process.exit(1);
}
}
async function bundleJavaScript(bundleDir) {
console.log('Bundling JavaScript code...');
await esbuild.build({
entryPoints: [DIST_MAIN_PATH],
bundle: true,
platform: 'node',
target: 'node16',
format: 'cjs',
outfile: path.join(bundleDir, 'aily-builder.js'),
minify: true,
sourcemap: false,
logLevel: 'info',
});
}
async function copyNinja(bundleDir) {
console.log('Copying Ninja build tools for Windows and macOS...');
const ninjaDir = './ninja';
const ninjaDest = path.join(bundleDir, 'ninja');
await fs.ensureDir(ninjaDest);
const ninjaFiles = [
{ name: 'ninja.exe', executable: false },
{ name: 'ninja', executable: true },
];
for (const ninjaFile of ninjaFiles) {
const sourcePath = path.join(ninjaDir, ninjaFile.name);
const destinationPath = path.join(ninjaDest, ninjaFile.name);
if (!await fs.pathExists(sourcePath)) {
throw new Error(`${ninjaFile.name} not found in ${ninjaDir} directory.`);
}
await fs.copy(sourcePath, destinationPath);
if (ninjaFile.executable) {
await fs.chmod(destinationPath, '755');
}
}
}
async function createLaunchScript(bundleDir, options) {
const defaultEnv = options.defaultGenerateArchiveCloudCache
? `if (process.env.AILY_BUILDER_GENERATE_ARCHIVE_CLOUD_CACHE === undefined) {
process.env.AILY_BUILDER_GENERATE_ARCHIVE_CLOUD_CACHE = '1';
}
`
: '';
const launchScript = `#!/usr/bin/env node
${defaultEnv}
require('./aily-builder.js');
`;
const launchScriptPath = path.join(bundleDir, 'index.js');
await fs.writeFile(launchScriptPath, launchScript);
if (process.platform !== 'win32') {
await fs.chmod(launchScriptPath, '755');
await fs.chmod(path.join(bundleDir, 'aily-builder.js'), '755');
}
}
async function createPackageJson(bundleDir, options) {
const projectPackageJson = await fs.readJson('./package.json');
const bundlePackageJson = {
name: projectPackageJson.name,
version: projectPackageJson.version,
description: projectPackageJson.description,
main: 'index.js',
bin: {
'aily-builder': 'index.js',
},
engines: {
node: projectPackageJson.engines?.node || '>=18',
},
ailyBuilder: {
defaultGenerateArchiveCloudCache: options.defaultGenerateArchiveCloudCache,
},
};
await fs.writeFile(
path.join(bundleDir, 'package.json'),
`${JSON.stringify(bundlePackageJson, null, 2)}\n`,
);
}
async function getBundleStats(bundleDir) {
let totalSize = 0;
let fileCount = 0;
async function walkDir(dir) {
const items = await fs.readdir(dir);
for (const item of items) {
const itemPath = path.join(dir, item);
const stat = await fs.stat(itemPath);
if (stat.isDirectory()) {
await walkDir(itemPath);
} else {
totalSize += stat.size;
fileCount++;
}
}
}
await walkDir(bundleDir);
const totalSizeFormatted = totalSize > 1024 * 1024
? `${(totalSize / 1024 / 1024).toFixed(2)} MB`
: `${(totalSize / 1024).toFixed(1)} KB`;
return { totalSize, totalSizeFormatted, fileCount };
}
bundleMinified();