-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathserveForPlatform.js
More file actions
117 lines (95 loc) · 4.69 KB
/
serveForPlatform.js
File metadata and controls
117 lines (95 loc) · 4.69 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
import {getPlatformDetails} from "./utils.js";
import {execa} from "execa";
import chalk from "chalk";
import {resolve} from "path";
import {copyFileSync, readFileSync, writeFileSync} from "fs";
const {platform} = getPlatformDetails();
function createTauriDevConfig() {
const tauriDir = resolve("src-tauri");
const tauriConfigPath = resolve(tauriDir, "tauri.conf.json");
const tauriLocalConfigPath = resolve(tauriDir, "tauri-local.conf.json");
console.log('Creating Tauri dev config...');
const configJson = JSON.parse(readFileSync(tauriConfigPath, 'utf8'));
// Override window URLs for dev mode
const devUrl = configJson.build.devPath; // "http://localhost:8000/src/"
// Main window - remove hardcoded URL so devPath is used
if (configJson.tauri.windows[0]) {
delete configJson.tauri.windows[0].url;
}
// fileDrop window - point to dev server
if (configJson.tauri.windows[2]) {
configJson.tauri.windows[2].url = devUrl + "drop-files.html";
}
writeFileSync(tauriLocalConfigPath, JSON.stringify(configJson, null, 4));
console.log('Dev config written to:', tauriLocalConfigPath);
}
// Get target from CLI arg, or detect from platform
const cliArgs = process.argv.slice(2).filter(arg => !arg.startsWith('--'));
const cliArg = cliArgs[0];
let target;
if (cliArg === 'tauri' || cliArg === 'electron') {
target = cliArg;
} else if (cliArg) {
console.error(`Unknown target: ${cliArg}`);
console.error('Usage: npm run serve [tauri|electron] [--dist]');
process.exit(1);
} else {
// Auto-detect: Linux uses Electron, Windows/Mac use Tauri
target = (platform === "linux") ? "electron" : "tauri";
}
// Warn about non-standard platform/target combinations
const recommendedTarget = (platform === "linux") ? "electron" : "tauri";
if (target !== recommendedTarget) {
const y = chalk.yellow;
const b = chalk.bold.yellow;
const line1 = ` Running ${target} on ${platform} is not officially supported.`;
const line2 = ` Recommended: npm run serve (auto-detects ${recommendedTarget} for ${platform})`;
const width = Math.max(50, line1.length, line2.length) + 2;
const border = '═'.repeat(width);
const pad = (str) => str + ' '.repeat(width - str.length);
console.warn(y(`\n╔${border}╗`));
console.warn(y('║') + b(pad(' ⚠️ NON-STANDARD PLATFORM CONFIGURATION')) + y('║'));
console.warn(y(`╠${border}╣`));
console.warn(y('║') + y(pad(line1)) + y('║'));
console.warn(y('║') + y(pad(line2)) + y('║'));
console.warn(y(`╚${border}╝\n`));
}
const serveDist = process.argv.includes('--dist');
if (serveDist && target === "tauri") {
console.error('Error: --dist flag is only supported with Electron, not Tauri.');
process.exit(1);
}
console.log(`Platform: ${platform}, target: ${target}`);
// Run platform-specific command
if (target === "tauri") {
console.log('\nEnsure to start phoenix server at http://localhost:8000 for development.');
console.log('Follow https://github.com/phcode-dev/phoenix#running-phoenix for instructions.\n');
console.log('Setting up src-node...');
await execa("npm", ["run", "_make_src-node"], {stdio: "inherit"});
createTauriDevConfig();
console.log('Starting Tauri dev server...');
await execa("npx", ["tauri", "dev", "--config", "./src-tauri/tauri-local.conf.json"], {stdio: "inherit"});
} else {
const srcNodePath = resolve("../phoenix/src-node");
console.log(`Running "npm install" in ${srcNodePath}`);
await execa("npm", ["install"], {cwd: srcNodePath, stdio: "inherit"});
// Copy config.json to config-effective.json (dev config for serve)
const electronDir = resolve("src-electron");
const configSrc = resolve(electronDir, "config.json");
const configDest = resolve(electronDir, "config-effective.json");
console.log('Copying config.json to config-effective.json...');
copyFileSync(configSrc, configDest);
// Inject version from package.json
const packageJsonPath = resolve(electronDir, "package.json");
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8'));
const effectiveConfig = JSON.parse(readFileSync(configDest, 'utf8'));
effectiveConfig.version = packageJson.version;
// When --dist flag is passed, serve from ../phoenix/dist instead of ../phoenix/src
if (serveDist) {
console.log('Serving from ../phoenix/dist (--dist mode)');
effectiveConfig.phoenixLoadURL = effectiveConfig.phoenixLoadURL.replace('/src/', '/dist/');
}
writeFileSync(configDest, JSON.stringify(effectiveConfig, null, 2));
console.log('Starting Electron...');
await execa("./src-electron/node_modules/.bin/electron", ["src-electron/main.js"], {stdio: "inherit"});
}