-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathmobile-web-build.cjs
More file actions
277 lines (237 loc) · 8.4 KB
/
Copy pathmobile-web-build.cjs
File metadata and controls
277 lines (237 loc) · 8.4 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env node
/**
* Shared mobile-web build helpers for desktop dev/build flows.
*
* We must clean copied resources in Tauri target directories before rebuilding,
* otherwise old Vite hashed assets remain in target profile mobile-web folders and get bundled
* or uploaded by remote connect along with the latest files.
*/
const { execSync } = require('child_process');
const path = require('path');
const {
printInfo,
printSuccess,
printError,
} = require('./console-style.cjs');
const ROOT_DIR = path.resolve(__dirname, '..');
function decodeOutput(output) {
if (!output) return '';
if (typeof output === 'string') return output;
const buffer = Buffer.isBuffer(output) ? output : Buffer.from(output);
if (process.platform !== 'win32') return buffer.toString('utf-8');
const utf8 = buffer.toString('utf-8');
if (!utf8.includes('�')) return utf8;
try {
const { TextDecoder } = require('util');
const decoder = new TextDecoder('gbk');
const gbk = decoder.decode(buffer);
if (gbk && !gbk.includes('�')) return gbk;
return gbk || utf8;
} catch (error) {
return utf8;
}
}
function tailOutput(output, maxLines = 12) {
if (!output) return '';
const lines = output
.split(/\r?\n/)
.map((line) => line.trimEnd())
.filter((line) => line.trim() !== '');
if (lines.length <= maxLines) return lines.join('\n');
return lines.slice(-maxLines).join('\n');
}
function runSilent(command, cwd = ROOT_DIR) {
try {
const stdout = execSync(command, {
cwd,
stdio: 'pipe',
encoding: 'buffer',
});
return { ok: true, stdout: decodeOutput(stdout), stderr: '' };
} catch (error) {
const stdout = error.stdout ? decodeOutput(error.stdout) : '';
const stderr = error.stderr ? decodeOutput(error.stderr) : '';
return { ok: false, stdout, stderr, error };
}
}
function runInherit(command, cwd = ROOT_DIR) {
try {
execSync(command, { cwd, stdio: 'inherit' });
return { ok: true, error: null };
} catch (error) {
return { ok: false, error };
}
}
const MOBILE_WEB_INPUT_IGNORED_DIRS = new Set(['node_modules', 'dist']);
/**
* Newest mtime among the mobile-web build inputs (recursively for directories).
* Returns null when nothing relevant exists.
*/
function getNewestInputMtime(entryPath) {
const fs = require('fs');
if (!fs.existsSync(entryPath)) {
return null;
}
const stat = fs.lstatSync(entryPath);
if (stat.isSymbolicLink()) {
return null;
}
if (stat.isFile()) {
return { path: entryPath, mtimeMs: stat.mtimeMs };
}
if (!stat.isDirectory()) {
return null;
}
let newest = null;
for (const entry of fs.readdirSync(entryPath, { withFileTypes: true })) {
if (entry.isSymbolicLink()) {
continue;
}
if (entry.isDirectory() && MOBILE_WEB_INPUT_IGNORED_DIRS.has(entry.name)) {
continue;
}
const candidate = getNewestInputMtime(path.join(entryPath, entry.name));
if (candidate && (!newest || candidate.mtimeMs > newest.mtimeMs)) {
newest = candidate;
}
}
return newest;
}
// Marker lives outside dist/ so it never ships as a bundled Tauri resource.
function getMobileWebBuildMarkerPath(mobileWebDir) {
return path.join(mobileWebDir, 'node_modules', '.cache', 'bitfun-mobile-web-build-marker');
}
/**
* mtime-based short-circuit (same idea as dev.cjs getDesktopPreviewRebuildPlan):
* when dist/ exists and the build marker is newer than every input, the whole
* clean/install/build cycle can be skipped. Escape hatches:
* --force flag / BITFUN_MOBILE_WEB_FORCE_BUILD=1 env.
*/
function getMobileWebRebuildPlan(mobileWebDir, force = false, rootDir = ROOT_DIR) {
const fs = require('fs');
if (force) {
return { shouldBuild: true, reason: 'Force rebuild requested for mobile-web' };
}
const markerPath = getMobileWebBuildMarkerPath(mobileWebDir);
if (!fs.existsSync(path.join(mobileWebDir, 'dist', 'index.html')) || !fs.existsSync(markerPath)) {
return { shouldBuild: true, reason: 'mobile-web dist is missing or has no build marker' };
}
const markerMtimeMs = fs.statSync(markerPath).mtimeMs;
const inputs = [
path.join(mobileWebDir, 'src'),
path.join(mobileWebDir, 'public'),
path.join(mobileWebDir, 'index.html'),
path.join(mobileWebDir, 'package.json'),
path.join(mobileWebDir, 'tsconfig.json'),
path.join(rootDir, 'pnpm-lock.yaml'),
path.join(rootDir, 'pnpm-workspace.yaml'),
path.join(rootDir, 'design-system', 'package.json'),
path.join(rootDir, 'design-system', 'packages', 'design-tokens', 'package.json'),
path.join(rootDir, 'design-system', 'packages', 'design-tokens', 'scripts'),
path.join(rootDir, 'design-system', 'packages', 'design-tokens', 'src'),
path.join(rootDir, 'design-system', 'packages', 'theme-bitfun', 'package.json'),
path.join(rootDir, 'design-system', 'packages', 'theme-bitfun', 'scripts'),
path.join(rootDir, 'design-system', 'packages', 'theme-bitfun', 'src'),
path.join(rootDir, 'design-system', 'tooling', 'token-engine', 'package.json'),
path.join(rootDir, 'design-system', 'tooling', 'token-engine', 'src'),
];
for (const entry of fs.readdirSync(mobileWebDir)) {
if (entry.startsWith('vite.config.')) {
inputs.push(path.join(mobileWebDir, entry));
}
}
let newestInput = null;
for (const input of inputs) {
const candidate = getNewestInputMtime(input);
if (candidate && (!newestInput || candidate.mtimeMs > newestInput.mtimeMs)) {
newestInput = candidate;
}
}
if (newestInput && newestInput.mtimeMs > markerMtimeMs) {
return {
shouldBuild: true,
reason: `mobile-web inputs changed since the last build (${path.relative(rootDir, newestInput.path)})`,
};
}
return {
shouldBuild: false,
reason: 'mobile-web dist is up to date; skipping clean/install/build (use --force or BITFUN_MOBILE_WEB_FORCE_BUILD=1 to rebuild)',
};
}
function writeMobileWebBuildMarker(mobileWebDir) {
const fs = require('fs');
const markerPath = getMobileWebBuildMarkerPath(mobileWebDir);
fs.mkdirSync(path.dirname(markerPath), { recursive: true });
fs.writeFileSync(markerPath, `${new Date().toISOString()}\n`);
}
function cleanStaleMobileWebResources(logInfo = printInfo) {
const fs = require('fs');
const targetDir = path.join(ROOT_DIR, 'target');
if (!fs.existsSync(targetDir)) return 0;
let cleaned = 0;
for (const profile of fs.readdirSync(targetDir)) {
const mobileWebDir = path.join(targetDir, profile, 'mobile-web');
if (fs.existsSync(mobileWebDir) && fs.statSync(mobileWebDir).isDirectory()) {
fs.rmSync(mobileWebDir, { recursive: true, force: true });
cleaned++;
}
}
if (cleaned > 0) {
logInfo(`Cleaned stale mobile-web resources from ${cleaned} target profile(s)`);
}
return cleaned;
}
function buildMobileWeb(options = {}) {
const {
install = false,
force = process.env.BITFUN_MOBILE_WEB_FORCE_BUILD === '1',
logInfo = printInfo,
logSuccess = printSuccess,
logError = printError,
} = options;
const mobileWebDir = path.join(ROOT_DIR, 'src/mobile-web');
const rebuildPlan = getMobileWebRebuildPlan(mobileWebDir, force);
if (!rebuildPlan.shouldBuild) {
logInfo(rebuildPlan.reason);
return { ok: true, skipped: true };
}
logInfo(rebuildPlan.reason);
cleanStaleMobileWebResources(logInfo);
if (install) {
const installResult = runSilent('pnpm install --silent', mobileWebDir);
if (!installResult.ok) {
logError('mobile-web pnpm install failed');
const output = tailOutput(installResult.stderr || installResult.stdout);
if (output) {
logError(output);
} else if (installResult.error?.message) {
logError(installResult.error.message);
}
return { ok: false };
}
}
const buildResult = runInherit('pnpm run build', mobileWebDir);
if (!buildResult.ok) {
logError('mobile-web build failed');
if (buildResult.error?.message) {
logError(buildResult.error.message);
}
return { ok: false };
}
writeMobileWebBuildMarker(mobileWebDir);
logSuccess('mobile-web build complete');
return { ok: true };
}
if (require.main === module) {
const args = process.argv.slice(2);
const result = buildMobileWeb({
install: args.includes('--install'),
force: args.includes('--force') || process.env.BITFUN_MOBILE_WEB_FORCE_BUILD === '1',
});
process.exit(result.ok ? 0 : 1);
}
module.exports = {
buildMobileWeb,
cleanStaleMobileWebResources,
getMobileWebRebuildPlan,
};