@tinybirdco/sdk 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +518 -0
- package/bin/tinybird.js +7 -0
- package/dist/api/branches.d.ts +98 -0
- package/dist/api/branches.d.ts.map +1 -0
- package/dist/api/branches.js +203 -0
- package/dist/api/branches.js.map +1 -0
- package/dist/api/branches.test.d.ts +2 -0
- package/dist/api/branches.test.d.ts.map +1 -0
- package/dist/api/branches.test.js +286 -0
- package/dist/api/branches.test.js.map +1 -0
- package/dist/api/build.d.ts +130 -0
- package/dist/api/build.d.ts.map +1 -0
- package/dist/api/build.js +143 -0
- package/dist/api/build.js.map +1 -0
- package/dist/api/build.test.d.ts +2 -0
- package/dist/api/build.test.d.ts.map +1 -0
- package/dist/api/build.test.js +138 -0
- package/dist/api/build.test.js.map +1 -0
- package/dist/api/deploy.d.ts +39 -0
- package/dist/api/deploy.d.ts.map +1 -0
- package/dist/api/deploy.js +135 -0
- package/dist/api/deploy.js.map +1 -0
- package/dist/api/deploy.test.d.ts +2 -0
- package/dist/api/deploy.test.d.ts.map +1 -0
- package/dist/api/deploy.test.js +118 -0
- package/dist/api/deploy.test.js.map +1 -0
- package/dist/api/workspaces.d.ts +46 -0
- package/dist/api/workspaces.d.ts.map +1 -0
- package/dist/api/workspaces.js +39 -0
- package/dist/api/workspaces.js.map +1 -0
- package/dist/api/workspaces.test.d.ts +2 -0
- package/dist/api/workspaces.test.d.ts.map +1 -0
- package/dist/api/workspaces.test.js +65 -0
- package/dist/api/workspaces.test.js.map +1 -0
- package/dist/cli/auth.d.ts +86 -0
- package/dist/cli/auth.d.ts.map +1 -0
- package/dist/cli/auth.js +284 -0
- package/dist/cli/auth.js.map +1 -0
- package/dist/cli/branch-store.d.ts +53 -0
- package/dist/cli/branch-store.d.ts.map +1 -0
- package/dist/cli/branch-store.js +91 -0
- package/dist/cli/branch-store.js.map +1 -0
- package/dist/cli/branch-store.test.d.ts +2 -0
- package/dist/cli/branch-store.test.d.ts.map +1 -0
- package/dist/cli/branch-store.test.js +115 -0
- package/dist/cli/branch-store.test.js.map +1 -0
- package/dist/cli/commands/branch.d.ts +82 -0
- package/dist/cli/commands/branch.d.ts.map +1 -0
- package/dist/cli/commands/branch.js +215 -0
- package/dist/cli/commands/branch.js.map +1 -0
- package/dist/cli/commands/build.d.ts +43 -0
- package/dist/cli/commands/build.d.ts.map +1 -0
- package/dist/cli/commands/build.js +138 -0
- package/dist/cli/commands/build.js.map +1 -0
- package/dist/cli/commands/dev.d.ts +78 -0
- package/dist/cli/commands/dev.d.ts.map +1 -0
- package/dist/cli/commands/dev.js +226 -0
- package/dist/cli/commands/dev.js.map +1 -0
- package/dist/cli/commands/init.d.ts +45 -0
- package/dist/cli/commands/init.d.ts.map +1 -0
- package/dist/cli/commands/init.js +277 -0
- package/dist/cli/commands/init.js.map +1 -0
- package/dist/cli/commands/init.test.d.ts +2 -0
- package/dist/cli/commands/init.test.d.ts.map +1 -0
- package/dist/cli/commands/init.test.js +158 -0
- package/dist/cli/commands/init.test.js.map +1 -0
- package/dist/cli/commands/login.d.ts +37 -0
- package/dist/cli/commands/login.d.ts.map +1 -0
- package/dist/cli/commands/login.js +64 -0
- package/dist/cli/commands/login.js.map +1 -0
- package/dist/cli/config.d.ts +114 -0
- package/dist/cli/config.d.ts.map +1 -0
- package/dist/cli/config.js +258 -0
- package/dist/cli/config.js.map +1 -0
- package/dist/cli/config.test.d.ts +2 -0
- package/dist/cli/config.test.d.ts.map +1 -0
- package/dist/cli/config.test.js +243 -0
- package/dist/cli/config.test.js.map +1 -0
- package/dist/cli/env.d.ts +29 -0
- package/dist/cli/env.d.ts.map +1 -0
- package/dist/cli/env.js +66 -0
- package/dist/cli/env.js.map +1 -0
- package/dist/cli/git.d.ts +29 -0
- package/dist/cli/git.d.ts.map +1 -0
- package/dist/cli/git.js +114 -0
- package/dist/cli/git.js.map +1 -0
- package/dist/cli/git.test.d.ts +2 -0
- package/dist/cli/git.test.d.ts.map +1 -0
- package/dist/cli/git.test.js +125 -0
- package/dist/cli/git.test.js.map +1 -0
- package/dist/cli/index.d.ts +7 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +337 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/cli/utils/schema-validation.d.ts +95 -0
- package/dist/cli/utils/schema-validation.d.ts.map +1 -0
- package/dist/cli/utils/schema-validation.js +175 -0
- package/dist/cli/utils/schema-validation.js.map +1 -0
- package/dist/cli/utils/schema-validation.test.d.ts +5 -0
- package/dist/cli/utils/schema-validation.test.d.ts.map +1 -0
- package/dist/cli/utils/schema-validation.test.js +173 -0
- package/dist/cli/utils/schema-validation.test.js.map +1 -0
- package/dist/client/base.d.ts +116 -0
- package/dist/client/base.d.ts.map +1 -0
- package/dist/client/base.js +328 -0
- package/dist/client/base.js.map +1 -0
- package/dist/client/types.d.ts +137 -0
- package/dist/client/types.d.ts.map +1 -0
- package/dist/client/types.js +43 -0
- package/dist/client/types.js.map +1 -0
- package/dist/generator/client.d.ts +44 -0
- package/dist/generator/client.d.ts.map +1 -0
- package/dist/generator/client.js +144 -0
- package/dist/generator/client.js.map +1 -0
- package/dist/generator/datasource.d.ts +57 -0
- package/dist/generator/datasource.d.ts.map +1 -0
- package/dist/generator/datasource.js +169 -0
- package/dist/generator/datasource.js.map +1 -0
- package/dist/generator/datasource.test.d.ts +2 -0
- package/dist/generator/datasource.test.d.ts.map +1 -0
- package/dist/generator/datasource.test.js +254 -0
- package/dist/generator/datasource.test.js.map +1 -0
- package/dist/generator/index.d.ts +131 -0
- package/dist/generator/index.d.ts.map +1 -0
- package/dist/generator/index.js +121 -0
- package/dist/generator/index.js.map +1 -0
- package/dist/generator/index.test.d.ts +2 -0
- package/dist/generator/index.test.d.ts.map +1 -0
- package/dist/generator/index.test.js +175 -0
- package/dist/generator/index.test.js.map +1 -0
- package/dist/generator/loader.d.ts +156 -0
- package/dist/generator/loader.d.ts.map +1 -0
- package/dist/generator/loader.js +295 -0
- package/dist/generator/loader.js.map +1 -0
- package/dist/generator/pipe.d.ts +72 -0
- package/dist/generator/pipe.d.ts.map +1 -0
- package/dist/generator/pipe.js +174 -0
- package/dist/generator/pipe.js.map +1 -0
- package/dist/generator/pipe.test.d.ts +2 -0
- package/dist/generator/pipe.test.d.ts.map +1 -0
- package/dist/generator/pipe.test.js +393 -0
- package/dist/generator/pipe.test.js.map +1 -0
- package/dist/index.d.ts +74 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +73 -0
- package/dist/index.js.map +1 -0
- package/dist/infer/index.d.ts +202 -0
- package/dist/infer/index.d.ts.map +1 -0
- package/dist/infer/index.js +5 -0
- package/dist/infer/index.js.map +1 -0
- package/dist/schema/datasource.d.ts +135 -0
- package/dist/schema/datasource.d.ts.map +1 -0
- package/dist/schema/datasource.js +105 -0
- package/dist/schema/datasource.js.map +1 -0
- package/dist/schema/datasource.test.d.ts +2 -0
- package/dist/schema/datasource.test.d.ts.map +1 -0
- package/dist/schema/datasource.test.js +142 -0
- package/dist/schema/datasource.test.js.map +1 -0
- package/dist/schema/engines.d.ts +157 -0
- package/dist/schema/engines.d.ts.map +1 -0
- package/dist/schema/engines.js +155 -0
- package/dist/schema/engines.js.map +1 -0
- package/dist/schema/engines.test.d.ts +2 -0
- package/dist/schema/engines.test.d.ts.map +1 -0
- package/dist/schema/engines.test.js +221 -0
- package/dist/schema/engines.test.js.map +1 -0
- package/dist/schema/params.d.ts +106 -0
- package/dist/schema/params.d.ts.map +1 -0
- package/dist/schema/params.js +138 -0
- package/dist/schema/params.js.map +1 -0
- package/dist/schema/params.test.d.ts +2 -0
- package/dist/schema/params.test.d.ts.map +1 -0
- package/dist/schema/params.test.js +175 -0
- package/dist/schema/params.test.js.map +1 -0
- package/dist/schema/pipe.d.ts +436 -0
- package/dist/schema/pipe.d.ts.map +1 -0
- package/dist/schema/pipe.js +484 -0
- package/dist/schema/pipe.js.map +1 -0
- package/dist/schema/pipe.test.d.ts +2 -0
- package/dist/schema/pipe.test.d.ts.map +1 -0
- package/dist/schema/pipe.test.js +488 -0
- package/dist/schema/pipe.test.js.map +1 -0
- package/dist/schema/project.d.ts +202 -0
- package/dist/schema/project.d.ts.map +1 -0
- package/dist/schema/project.js +188 -0
- package/dist/schema/project.js.map +1 -0
- package/dist/schema/project.test.d.ts +2 -0
- package/dist/schema/project.test.d.ts.map +1 -0
- package/dist/schema/project.test.js +180 -0
- package/dist/schema/project.test.js.map +1 -0
- package/dist/schema/types.d.ts +140 -0
- package/dist/schema/types.d.ts.map +1 -0
- package/dist/schema/types.js +174 -0
- package/dist/schema/types.js.map +1 -0
- package/dist/schema/types.test.d.ts +2 -0
- package/dist/schema/types.test.d.ts.map +1 -0
- package/dist/schema/types.test.js +176 -0
- package/dist/schema/types.test.js.map +1 -0
- package/dist/test/handlers.d.ts +58 -0
- package/dist/test/handlers.d.ts.map +1 -0
- package/dist/test/handlers.js +62 -0
- package/dist/test/handlers.js.map +1 -0
- package/dist/test/setup.d.ts +5 -0
- package/dist/test/setup.d.ts.map +1 -0
- package/dist/test/setup.js +11 -0
- package/dist/test/setup.js.map +1 -0
- package/package.json +57 -0
- package/src/api/branches.test.ts +377 -0
- package/src/api/branches.ts +334 -0
- package/src/api/build.test.ts +216 -0
- package/src/api/build.ts +266 -0
- package/src/api/deploy.test.ts +193 -0
- package/src/api/deploy.ts +163 -0
- package/src/api/workspaces.test.ts +81 -0
- package/src/api/workspaces.ts +77 -0
- package/src/cli/auth.ts +358 -0
- package/src/cli/branch-store.test.ts +139 -0
- package/src/cli/branch-store.ts +137 -0
- package/src/cli/commands/branch.ts +306 -0
- package/src/cli/commands/build.ts +183 -0
- package/src/cli/commands/dev.ts +334 -0
- package/src/cli/commands/init.test.ts +249 -0
- package/src/cli/commands/init.ts +323 -0
- package/src/cli/commands/login.ts +98 -0
- package/src/cli/config.test.ts +359 -0
- package/src/cli/config.ts +335 -0
- package/src/cli/env.ts +86 -0
- package/src/cli/git.test.ts +147 -0
- package/src/cli/git.ts +125 -0
- package/src/cli/index.ts +382 -0
- package/src/cli/utils/schema-validation.test.ts +222 -0
- package/src/cli/utils/schema-validation.ts +272 -0
- package/src/client/base.ts +414 -0
- package/src/client/types.ts +165 -0
- package/src/generator/client.ts +194 -0
- package/src/generator/datasource.test.ts +297 -0
- package/src/generator/datasource.ts +217 -0
- package/src/generator/index.test.ts +209 -0
- package/src/generator/index.ts +203 -0
- package/src/generator/loader.ts +406 -0
- package/src/generator/pipe.test.ts +441 -0
- package/src/generator/pipe.ts +220 -0
- package/src/index.ts +191 -0
- package/src/infer/index.ts +247 -0
- package/src/schema/datasource.test.ts +187 -0
- package/src/schema/datasource.ts +195 -0
- package/src/schema/engines.test.ts +247 -0
- package/src/schema/engines.ts +271 -0
- package/src/schema/params.test.ts +208 -0
- package/src/schema/params.ts +249 -0
- package/src/schema/pipe.test.ts +588 -0
- package/src/schema/pipe.ts +832 -0
- package/src/schema/project.test.ts +236 -0
- package/src/schema/project.ts +394 -0
- package/src/schema/types.test.ts +212 -0
- package/src/schema/types.ts +366 -0
- package/src/test/handlers.ts +79 -0
- package/src/test/setup.ts +13 -0
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Branch management commands
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { loadConfig, type ResolvedConfig } from "../config.js";
|
|
6
|
+
import {
|
|
7
|
+
listBranches,
|
|
8
|
+
getBranch,
|
|
9
|
+
deleteBranch,
|
|
10
|
+
type TinybirdBranch,
|
|
11
|
+
BranchApiError,
|
|
12
|
+
} from "../../api/branches.js";
|
|
13
|
+
import { getWorkspace } from "../../api/workspaces.js";
|
|
14
|
+
import {
|
|
15
|
+
getBranchToken,
|
|
16
|
+
removeBranch as removeCachedBranch,
|
|
17
|
+
listCachedBranches,
|
|
18
|
+
} from "../branch-store.js";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Branch command options
|
|
22
|
+
*/
|
|
23
|
+
export interface BranchCommandOptions {
|
|
24
|
+
/** Working directory (defaults to cwd) */
|
|
25
|
+
cwd?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Result of listing branches
|
|
30
|
+
*/
|
|
31
|
+
export interface BranchListResult {
|
|
32
|
+
/** Whether the operation was successful */
|
|
33
|
+
success: boolean;
|
|
34
|
+
/** List of branches */
|
|
35
|
+
branches?: TinybirdBranch[];
|
|
36
|
+
/** Error message if failed */
|
|
37
|
+
error?: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Result of getting branch status
|
|
42
|
+
*/
|
|
43
|
+
export interface BranchStatusResult {
|
|
44
|
+
/** Whether the operation was successful */
|
|
45
|
+
success: boolean;
|
|
46
|
+
/** Current git branch */
|
|
47
|
+
gitBranch: string | null;
|
|
48
|
+
/** Sanitized Tinybird branch name */
|
|
49
|
+
tinybirdBranchName: string | null;
|
|
50
|
+
/** Whether we're on the main branch */
|
|
51
|
+
isMainBranch: boolean;
|
|
52
|
+
/** Tinybird branch info (if exists) */
|
|
53
|
+
tinybirdBranch?: TinybirdBranch;
|
|
54
|
+
/** Whether a cached token exists */
|
|
55
|
+
hasCachedToken: boolean;
|
|
56
|
+
/** Error message if failed */
|
|
57
|
+
error?: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Result of deleting a branch
|
|
62
|
+
*/
|
|
63
|
+
export interface BranchDeleteResult {
|
|
64
|
+
/** Whether the operation was successful */
|
|
65
|
+
success: boolean;
|
|
66
|
+
/** Error message if failed */
|
|
67
|
+
error?: string;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* List all Tinybird branches
|
|
72
|
+
*
|
|
73
|
+
* @param options - Command options
|
|
74
|
+
* @returns List result
|
|
75
|
+
*/
|
|
76
|
+
export async function runBranchList(
|
|
77
|
+
options: BranchCommandOptions = {}
|
|
78
|
+
): Promise<BranchListResult> {
|
|
79
|
+
const cwd = options.cwd ?? process.cwd();
|
|
80
|
+
|
|
81
|
+
let config: ResolvedConfig;
|
|
82
|
+
try {
|
|
83
|
+
config = loadConfig(cwd);
|
|
84
|
+
} catch (error) {
|
|
85
|
+
return {
|
|
86
|
+
success: false,
|
|
87
|
+
error: (error as Error).message,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
try {
|
|
92
|
+
const branches = await listBranches({
|
|
93
|
+
baseUrl: config.baseUrl,
|
|
94
|
+
token: config.token,
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
return {
|
|
98
|
+
success: true,
|
|
99
|
+
branches,
|
|
100
|
+
};
|
|
101
|
+
} catch (error) {
|
|
102
|
+
return {
|
|
103
|
+
success: false,
|
|
104
|
+
error: (error as Error).message,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Get current branch status
|
|
111
|
+
*
|
|
112
|
+
* @param options - Command options
|
|
113
|
+
* @returns Status result
|
|
114
|
+
*/
|
|
115
|
+
export async function runBranchStatus(
|
|
116
|
+
options: BranchCommandOptions = {}
|
|
117
|
+
): Promise<BranchStatusResult> {
|
|
118
|
+
const cwd = options.cwd ?? process.cwd();
|
|
119
|
+
|
|
120
|
+
let config: ResolvedConfig;
|
|
121
|
+
try {
|
|
122
|
+
config = loadConfig(cwd);
|
|
123
|
+
} catch (error) {
|
|
124
|
+
return {
|
|
125
|
+
success: false,
|
|
126
|
+
gitBranch: null,
|
|
127
|
+
tinybirdBranchName: null,
|
|
128
|
+
isMainBranch: false,
|
|
129
|
+
hasCachedToken: false,
|
|
130
|
+
error: (error as Error).message,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const gitBranch = config.gitBranch;
|
|
135
|
+
const tinybirdBranchName = config.tinybirdBranch; // Sanitized name
|
|
136
|
+
const isMainBranch = config.isMainBranch;
|
|
137
|
+
|
|
138
|
+
// Fetch the workspace ID from the API
|
|
139
|
+
let workspaceId: string;
|
|
140
|
+
try {
|
|
141
|
+
const workspace = await getWorkspace({
|
|
142
|
+
baseUrl: config.baseUrl,
|
|
143
|
+
token: config.token,
|
|
144
|
+
});
|
|
145
|
+
workspaceId = workspace.id;
|
|
146
|
+
} catch (error) {
|
|
147
|
+
return {
|
|
148
|
+
success: false,
|
|
149
|
+
gitBranch,
|
|
150
|
+
tinybirdBranchName,
|
|
151
|
+
isMainBranch,
|
|
152
|
+
hasCachedToken: false,
|
|
153
|
+
error: (error as Error).message,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Check for cached token (use sanitized name)
|
|
158
|
+
const cachedBranch = tinybirdBranchName ? getBranchToken(workspaceId, tinybirdBranchName) : null;
|
|
159
|
+
const hasCachedToken = cachedBranch !== null;
|
|
160
|
+
|
|
161
|
+
// If on main branch, just return status
|
|
162
|
+
if (isMainBranch || !tinybirdBranchName) {
|
|
163
|
+
return {
|
|
164
|
+
success: true,
|
|
165
|
+
gitBranch,
|
|
166
|
+
tinybirdBranchName,
|
|
167
|
+
isMainBranch,
|
|
168
|
+
hasCachedToken,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// Try to get the Tinybird branch info (use sanitized name)
|
|
173
|
+
try {
|
|
174
|
+
const tinybirdBranch = await getBranch(
|
|
175
|
+
{
|
|
176
|
+
baseUrl: config.baseUrl,
|
|
177
|
+
token: config.token,
|
|
178
|
+
},
|
|
179
|
+
tinybirdBranchName
|
|
180
|
+
);
|
|
181
|
+
|
|
182
|
+
return {
|
|
183
|
+
success: true,
|
|
184
|
+
gitBranch,
|
|
185
|
+
tinybirdBranchName,
|
|
186
|
+
isMainBranch,
|
|
187
|
+
tinybirdBranch,
|
|
188
|
+
hasCachedToken,
|
|
189
|
+
};
|
|
190
|
+
} catch (error) {
|
|
191
|
+
// If 404, branch doesn't exist yet
|
|
192
|
+
if (error instanceof BranchApiError && error.status === 404) {
|
|
193
|
+
return {
|
|
194
|
+
success: true,
|
|
195
|
+
gitBranch,
|
|
196
|
+
tinybirdBranchName,
|
|
197
|
+
isMainBranch,
|
|
198
|
+
hasCachedToken,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return {
|
|
203
|
+
success: false,
|
|
204
|
+
gitBranch,
|
|
205
|
+
tinybirdBranchName,
|
|
206
|
+
isMainBranch,
|
|
207
|
+
hasCachedToken,
|
|
208
|
+
error: (error as Error).message,
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Delete a Tinybird branch
|
|
215
|
+
*
|
|
216
|
+
* @param name - Branch name to delete
|
|
217
|
+
* @param options - Command options
|
|
218
|
+
* @returns Delete result
|
|
219
|
+
*/
|
|
220
|
+
export async function runBranchDelete(
|
|
221
|
+
name: string,
|
|
222
|
+
options: BranchCommandOptions = {}
|
|
223
|
+
): Promise<BranchDeleteResult> {
|
|
224
|
+
const cwd = options.cwd ?? process.cwd();
|
|
225
|
+
|
|
226
|
+
let config: ResolvedConfig;
|
|
227
|
+
try {
|
|
228
|
+
config = loadConfig(cwd);
|
|
229
|
+
} catch (error) {
|
|
230
|
+
return {
|
|
231
|
+
success: false,
|
|
232
|
+
error: (error as Error).message,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
try {
|
|
237
|
+
// Fetch the workspace ID from the API
|
|
238
|
+
const workspace = await getWorkspace({
|
|
239
|
+
baseUrl: config.baseUrl,
|
|
240
|
+
token: config.token,
|
|
241
|
+
});
|
|
242
|
+
const workspaceId = workspace.id;
|
|
243
|
+
|
|
244
|
+
// Delete from Tinybird API
|
|
245
|
+
await deleteBranch(
|
|
246
|
+
{
|
|
247
|
+
baseUrl: config.baseUrl,
|
|
248
|
+
token: config.token,
|
|
249
|
+
},
|
|
250
|
+
name
|
|
251
|
+
);
|
|
252
|
+
|
|
253
|
+
// Remove from local cache
|
|
254
|
+
removeCachedBranch(workspaceId, name);
|
|
255
|
+
|
|
256
|
+
return {
|
|
257
|
+
success: true,
|
|
258
|
+
};
|
|
259
|
+
} catch (error) {
|
|
260
|
+
return {
|
|
261
|
+
success: false,
|
|
262
|
+
error: (error as Error).message,
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* List cached branches (requires API call to get workspace ID)
|
|
269
|
+
*/
|
|
270
|
+
export async function runBranchListCached(
|
|
271
|
+
options: BranchCommandOptions = {}
|
|
272
|
+
): Promise<{ branches: Record<string, { id: string; createdAt: string }> }> {
|
|
273
|
+
const cwd = options.cwd ?? process.cwd();
|
|
274
|
+
|
|
275
|
+
let config: ResolvedConfig;
|
|
276
|
+
try {
|
|
277
|
+
config = loadConfig(cwd);
|
|
278
|
+
} catch {
|
|
279
|
+
return { branches: {} };
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// Fetch the workspace ID from the API
|
|
283
|
+
let workspaceId: string;
|
|
284
|
+
try {
|
|
285
|
+
const workspace = await getWorkspace({
|
|
286
|
+
baseUrl: config.baseUrl,
|
|
287
|
+
token: config.token,
|
|
288
|
+
});
|
|
289
|
+
workspaceId = workspace.id;
|
|
290
|
+
} catch {
|
|
291
|
+
return { branches: {} };
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
const cached = listCachedBranches(workspaceId);
|
|
295
|
+
|
|
296
|
+
// Return without tokens for security
|
|
297
|
+
const branches: Record<string, { id: string; createdAt: string }> = {};
|
|
298
|
+
for (const [name, info] of Object.entries(cached)) {
|
|
299
|
+
branches[name] = {
|
|
300
|
+
id: info.id,
|
|
301
|
+
createdAt: info.createdAt,
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
return { branches };
|
|
306
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Build command - generates and pushes resources to Tinybird
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { loadConfig, type ResolvedConfig } from "../config.js";
|
|
6
|
+
import { buildFromInclude, type BuildFromIncludeResult } from "../../generator/index.js";
|
|
7
|
+
import { buildToTinybird, type BuildApiResult } from "../../api/build.js";
|
|
8
|
+
import { deployToMain } from "../../api/deploy.js";
|
|
9
|
+
import { getOrCreateBranch } from "../../api/branches.js";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Build command options
|
|
13
|
+
*/
|
|
14
|
+
export interface BuildCommandOptions {
|
|
15
|
+
/** Working directory (defaults to cwd) */
|
|
16
|
+
cwd?: string;
|
|
17
|
+
/** Skip pushing to API (just generate) */
|
|
18
|
+
dryRun?: boolean;
|
|
19
|
+
/** Override the token from config (used for branch tokens) */
|
|
20
|
+
tokenOverride?: string;
|
|
21
|
+
/** Use /v1/deploy instead of /v1/build (for main branch) */
|
|
22
|
+
useDeployEndpoint?: boolean;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Build command result
|
|
27
|
+
*/
|
|
28
|
+
export interface BuildCommandResult {
|
|
29
|
+
/** Whether the build was successful */
|
|
30
|
+
success: boolean;
|
|
31
|
+
/** Build result with generated resources */
|
|
32
|
+
build?: BuildFromIncludeResult;
|
|
33
|
+
/** Build API result (if not dry run) */
|
|
34
|
+
deploy?: BuildApiResult;
|
|
35
|
+
/** Error message if failed */
|
|
36
|
+
error?: string;
|
|
37
|
+
/** Duration in milliseconds */
|
|
38
|
+
durationMs: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Run the build command
|
|
43
|
+
*
|
|
44
|
+
* Loads the schema, generates resources, and pushes to Tinybird API.
|
|
45
|
+
*
|
|
46
|
+
* @param options - Build options
|
|
47
|
+
* @returns Build command result
|
|
48
|
+
*/
|
|
49
|
+
export async function runBuild(options: BuildCommandOptions = {}): Promise<BuildCommandResult> {
|
|
50
|
+
const startTime = Date.now();
|
|
51
|
+
const cwd = options.cwd ?? process.cwd();
|
|
52
|
+
|
|
53
|
+
// Load config
|
|
54
|
+
let config: ResolvedConfig;
|
|
55
|
+
try {
|
|
56
|
+
config = loadConfig(cwd);
|
|
57
|
+
} catch (error) {
|
|
58
|
+
return {
|
|
59
|
+
success: false,
|
|
60
|
+
error: (error as Error).message,
|
|
61
|
+
durationMs: Date.now() - startTime,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Build resources from include paths
|
|
66
|
+
let buildResult: BuildFromIncludeResult;
|
|
67
|
+
try {
|
|
68
|
+
buildResult = await buildFromInclude({
|
|
69
|
+
includePaths: config.include,
|
|
70
|
+
cwd: config.cwd,
|
|
71
|
+
});
|
|
72
|
+
} catch (error) {
|
|
73
|
+
return {
|
|
74
|
+
success: false,
|
|
75
|
+
error: `Build failed: ${(error as Error).message}`,
|
|
76
|
+
durationMs: Date.now() - startTime,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// If dry run, return without pushing
|
|
81
|
+
if (options.dryRun) {
|
|
82
|
+
return {
|
|
83
|
+
success: true,
|
|
84
|
+
build: buildResult,
|
|
85
|
+
durationMs: Date.now() - startTime,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Deploy to Tinybird
|
|
90
|
+
// Determine token and endpoint based on git branch
|
|
91
|
+
let effectiveToken = options.tokenOverride ?? config.token;
|
|
92
|
+
let useDeployEndpoint = options.useDeployEndpoint ?? config.isMainBranch;
|
|
93
|
+
|
|
94
|
+
// For feature branches, get or create the Tinybird branch and use its token
|
|
95
|
+
const debug = !!process.env.TINYBIRD_DEBUG;
|
|
96
|
+
if (debug) {
|
|
97
|
+
console.log(`[debug] isMainBranch: ${config.isMainBranch}`);
|
|
98
|
+
console.log(`[debug] tinybirdBranch: ${config.tinybirdBranch}`);
|
|
99
|
+
console.log(`[debug] tokenOverride: ${!!options.tokenOverride}`);
|
|
100
|
+
}
|
|
101
|
+
if (!config.isMainBranch && config.tinybirdBranch && !options.tokenOverride) {
|
|
102
|
+
if (debug) {
|
|
103
|
+
console.log(`[debug] Getting/creating Tinybird branch: ${config.tinybirdBranch}`);
|
|
104
|
+
}
|
|
105
|
+
try {
|
|
106
|
+
const tinybirdBranch = await getOrCreateBranch(
|
|
107
|
+
{
|
|
108
|
+
baseUrl: config.baseUrl,
|
|
109
|
+
token: config.token,
|
|
110
|
+
},
|
|
111
|
+
config.tinybirdBranch
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
if (!tinybirdBranch.token) {
|
|
115
|
+
return {
|
|
116
|
+
success: false,
|
|
117
|
+
build: buildResult,
|
|
118
|
+
error: `Branch '${config.tinybirdBranch}' was created but no token was returned.`,
|
|
119
|
+
durationMs: Date.now() - startTime,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
effectiveToken = tinybirdBranch.token;
|
|
124
|
+
useDeployEndpoint = false; // Always use /v1/build for branches
|
|
125
|
+
if (debug) {
|
|
126
|
+
console.log(`[debug] Using branch token for branch: ${config.tinybirdBranch}`);
|
|
127
|
+
}
|
|
128
|
+
} catch (error) {
|
|
129
|
+
return {
|
|
130
|
+
success: false,
|
|
131
|
+
build: buildResult,
|
|
132
|
+
error: `Failed to get/create branch: ${(error as Error).message}`,
|
|
133
|
+
durationMs: Date.now() - startTime,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
let deployResult: BuildApiResult;
|
|
139
|
+
try {
|
|
140
|
+
// Use /v1/deploy for main branch, /v1/build for feature branches
|
|
141
|
+
if (useDeployEndpoint) {
|
|
142
|
+
deployResult = await deployToMain(
|
|
143
|
+
{
|
|
144
|
+
baseUrl: config.baseUrl,
|
|
145
|
+
token: effectiveToken,
|
|
146
|
+
},
|
|
147
|
+
buildResult.resources
|
|
148
|
+
);
|
|
149
|
+
} else {
|
|
150
|
+
deployResult = await buildToTinybird(
|
|
151
|
+
{
|
|
152
|
+
baseUrl: config.baseUrl,
|
|
153
|
+
token: effectiveToken,
|
|
154
|
+
},
|
|
155
|
+
buildResult.resources
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
} catch (error) {
|
|
159
|
+
return {
|
|
160
|
+
success: false,
|
|
161
|
+
build: buildResult,
|
|
162
|
+
error: `Deploy failed: ${(error as Error).message}`,
|
|
163
|
+
durationMs: Date.now() - startTime,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (!deployResult.success) {
|
|
168
|
+
return {
|
|
169
|
+
success: false,
|
|
170
|
+
build: buildResult,
|
|
171
|
+
deploy: deployResult,
|
|
172
|
+
error: deployResult.error,
|
|
173
|
+
durationMs: Date.now() - startTime,
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return {
|
|
178
|
+
success: true,
|
|
179
|
+
build: buildResult,
|
|
180
|
+
deploy: deployResult,
|
|
181
|
+
durationMs: Date.now() - startTime,
|
|
182
|
+
};
|
|
183
|
+
}
|