-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpublish-reflect.js
85 lines (74 loc) · 1.88 KB
/
publish-reflect.js
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
import { spawn } from "child_process";
import fs from "fs";
run();
async function run() {
// Could do in parallel but output harder to read that way.
await publish(
"loop-orchestrator",
"./reflect/orchestrator/server.ts",
"NEXT_PUBLIC_ORCHESTRATOR_SERVER"
);
await publish(
"loop-share",
"./reflect/share/server.ts",
"NEXT_PUBLIC_SHARE_SERVER"
);
await publish(
"loop-play",
"./reflect/play/server.ts",
"NEXT_PUBLIC_PLAY_SERVER"
);
console.log("Wrote .env");
console.log(fs.readFileSync("./.env").toString());
}
async function publish(appBaseName, serverPath, envVar) {
const refName = getEnv(
process.env.VERCEL_GIT_COMMIT_REF,
"VERCEL_GIT_COMMIT_REF"
);
const appName = `${appBaseName}-${refName}`
.toLowerCase()
.replace(/^[^a-z]/, "")
.replace(/[^a-z0-9-]/g, "-");
const output = JSON.parse(
await runCommand("npx", [
"reflect",
"publish",
`--app=${appName}`,
`--server-path=${serverPath}`,
"--reflect-channel=canary",
"--auth-key-from-env=REFLECT_AUTH_KEY",
"--output=json",
])
);
if (output.success) {
fs.appendFileSync("./.env", `${envVar}=${output.url}` + "\n");
}
}
function runCommand(command, args) {
console.log("running command: " + command + " " + args.join(" "));
return new Promise((resolve, reject) => {
const child = spawn(command, args);
let output = "";
child.stdout.on("data", (data) => {
output += data;
process.stdout.write(data);
});
child.stderr.on("data", (data) => {
process.stderr.write(data);
});
child.on("close", (code) => {
if (code !== 0) {
reject(new Error(`Command failed with exit code ${code}`));
} else {
resolve(output);
}
});
});
}
function getEnv(v, name) {
if (!v) {
throw new Error("Missing required env var: " + name);
}
return v;
}