-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsitedrift.mjs
More file actions
executable file
·111 lines (104 loc) · 3.87 KB
/
Copy pathsitedrift.mjs
File metadata and controls
executable file
·111 lines (104 loc) · 3.87 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
#!/usr/bin/env node
import { parseCommand, resolveConfig, printHelp, readVersion } from './src/cli.mjs';
import { createServer } from './src/server.mjs';
import { resolveTls, setupHttps } from './src/tls.mjs';
import { openBrowser } from './src/browser.mjs';
import { runAgentCommand } from './src/agent.mjs';
import { createSession, removeSession, writeSession } from './src/session.mjs';
import { runMcpServer } from './src/mcp.mjs';
import { installCloudflarePreview, scaffoldCloudflarePreview } from './src/cloudflare.mjs';
let command;
let config;
try {
const parsed = parseCommand();
command = parsed?.command;
} catch (error) {
console.error(`sitedrift: ${error.message}`);
process.exit(2);
}
if (command?.name === 'mcp') {
runMcpServer();
} else if (command?.name === 'cloudflare') {
try {
if (command.action === 'init') {
const result = scaffoldCloudflarePreview(command);
console.log(result.created
? `sitedrift: created ${result.functionFile}`
: `sitedrift: ${result.functionFile} already exists — leaving it as is`);
console.log('Next, add this to your package.json "build" script, after the framework build:');
console.log(` ${result.buildLine}`);
console.log('Then commit both changes and push a preview branch.');
} else {
const result = installCloudflarePreview(command);
console.log(result.installed
? `sitedrift: wrapped ${result.files} HTML files for Cloudflare preview ${result.branch}`
: `sitedrift: unchanged (${result.reason})`);
}
} catch (error) {
console.error(`sitedrift: ${error.message}`);
process.exit(1);
}
} else {
try {
const parsed = parseCommand();
config = resolveConfig(parsed?.argv ?? process.argv.slice(2), { requireLive: !command });
} catch (error) {
console.error(`sitedrift: ${error.message}`);
process.exit(2);
}
if (config.help) { printHelp(); process.exit(0); }
if (config.version) { console.log(readVersion()); process.exit(0); }
if (config.setupHttps) { process.exit(setupHttps()); }
if (command) {
try {
process.exit(await runAgentCommand(command, config));
} catch (error) {
console.error(JSON.stringify({ error: error.message }));
process.exit(1);
}
}
let tls;
try {
tls = resolveTls(config);
} catch (error) {
console.error(error.message);
process.exit(1);
}
const scheme = tls ? 'https' : 'http';
const session = createSession(config, scheme);
const server = createServer(config, tls, session);
const devFrameServer = createServer(config, tls, session, { control: false, side: 'dev' });
const liveFrameServer = createServer(config, tls, session, { control: false, side: 'live' });
const servers = [server, devFrameServer, liveFrameServer];
for (const signal of ['SIGINT', 'SIGTERM']) {
process.once(signal, () => {
removeSession(config);
devFrameServer.close();
liveFrameServer.close();
server.close(() => process.exit(0));
});
}
process.once('exit', () => removeSession(config));
devFrameServer.listen(config.port + 1, config.host, () => {
liveFrameServer.listen(config.port + 2, config.host, () => {
server.listen(config.port, config.host, () => {
writeSession(config, session);
const startUrl = `${session.url}/`
+ (config.initialPath ? `?path=${encodeURIComponent(config.initialPath)}` : '');
console.log(`sitedrift: ${startUrl}`);
console.log(` DEV ${config.devBase.href}`);
console.log(` LIVE ${config.liveBase.href}`);
console.log(` API sitedrift context`);
if (config.open) openBrowser(startUrl);
});
});
});
for (const current of servers) {
current.on('error', (error) => {
removeSession(config);
for (const other of servers) other.close();
console.error(`sitedrift: ${error.message}`);
process.exit(1);
});
}
}