forked from CapSoftware/Cap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-analytics.js
More file actions
executable file
·98 lines (88 loc) · 2.8 KB
/
Copy pathsetup-analytics.js
File metadata and controls
executable file
·98 lines (88 loc) · 2.8 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
#!/usr/bin/env node
import { spawnSync } from "node:child_process";
import fs from "node:fs";
import path from "node:path";
import process from "node:process";
import { fileURLToPath } from "node:url";
import { PROJECT_ROOT, resolveTinybirdAuth } from "./shared.js";
const DEFAULT_PYTHON = process.env.PYTHON || "python3";
const TB_BINARY = process.env.TB_BIN || "tb";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const tinybirdProjectDir = path.join(__dirname, "tinybird");
const projectTinybPath = path.join(tinybirdProjectDir, ".tinyb");
const rootTinybPath = path.join(PROJECT_ROOT, ".tinyb");
const run = (command, args, options = {}) => {
const result = spawnSync(command, args, {
stdio: "inherit",
...options,
});
if (result.error || result.status !== 0) {
const message =
result.error?.message || `Command ${command} ${args.join(" ")} failed.`;
throw new Error(message);
}
};
const ensureTinybirdCli = () => {
try {
run(TB_BINARY, ["--version"], { stdio: "ignore" });
} catch {
console.log("Tinybird CLI not found. Installing via pip...");
run(DEFAULT_PYTHON, ["-m", "pip", "install", "--user", "tinybird-cli"], {
stdio: "inherit",
});
console.log("Tinybird CLI installed. Re-running version check...");
run(TB_BINARY, ["--version"], { stdio: "inherit" });
}
};
const ensureTinybirdLogin = () => {
if (fs.existsSync(rootTinybPath)) {
return;
}
console.log("Tinybird credentials not found. Launching `tb login`...");
run(TB_BINARY, ["login"], { cwd: PROJECT_ROOT, stdio: "inherit" });
if (!fs.existsSync(rootTinybPath)) {
throw new Error(
"Tinybird login did not create a .tinyb file. Please rerun `tb login` and try again.",
);
}
};
const syncProjectTinyb = () => {
if (!fs.existsSync(rootTinybPath)) return;
fs.copyFileSync(rootTinybPath, projectTinybPath);
};
const deployTinybirdProject = (auth) => {
console.log(
"⚠️ Running `tb --cloud deploy --allow-destructive-operations --wait` inside scripts/analytics/tinybird. This will synchronize the Tinybird workspace to the resources defined in that folder and remove any other datasources/pipes in the workspace.",
);
run(
TB_BINARY,
["--cloud", "deploy", "--allow-destructive-operations", "--wait"],
{
cwd: tinybirdProjectDir,
env: {
...process.env,
TB_HOST: auth.host,
TB_TOKEN: auth.token,
TB_LOCAL: "0",
...(auth.userToken ? { TB_USER_TOKEN: auth.userToken } : {}),
},
},
);
};
function main() {
try {
ensureTinybirdCli();
ensureTinybirdLogin();
const auth = resolveTinybirdAuth();
syncProjectTinyb();
deployTinybirdProject(auth);
console.log("✅ Tinybird analytics resources are ready.");
} catch (error) {
console.error(
"❌ Failed to set up Tinybird analytics:",
error instanceof Error ? error.message : error,
);
process.exit(1);
}
}
main();