@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.
Files changed (258) hide show
  1. package/README.md +518 -0
  2. package/bin/tinybird.js +7 -0
  3. package/dist/api/branches.d.ts +98 -0
  4. package/dist/api/branches.d.ts.map +1 -0
  5. package/dist/api/branches.js +203 -0
  6. package/dist/api/branches.js.map +1 -0
  7. package/dist/api/branches.test.d.ts +2 -0
  8. package/dist/api/branches.test.d.ts.map +1 -0
  9. package/dist/api/branches.test.js +286 -0
  10. package/dist/api/branches.test.js.map +1 -0
  11. package/dist/api/build.d.ts +130 -0
  12. package/dist/api/build.d.ts.map +1 -0
  13. package/dist/api/build.js +143 -0
  14. package/dist/api/build.js.map +1 -0
  15. package/dist/api/build.test.d.ts +2 -0
  16. package/dist/api/build.test.d.ts.map +1 -0
  17. package/dist/api/build.test.js +138 -0
  18. package/dist/api/build.test.js.map +1 -0
  19. package/dist/api/deploy.d.ts +39 -0
  20. package/dist/api/deploy.d.ts.map +1 -0
  21. package/dist/api/deploy.js +135 -0
  22. package/dist/api/deploy.js.map +1 -0
  23. package/dist/api/deploy.test.d.ts +2 -0
  24. package/dist/api/deploy.test.d.ts.map +1 -0
  25. package/dist/api/deploy.test.js +118 -0
  26. package/dist/api/deploy.test.js.map +1 -0
  27. package/dist/api/workspaces.d.ts +46 -0
  28. package/dist/api/workspaces.d.ts.map +1 -0
  29. package/dist/api/workspaces.js +39 -0
  30. package/dist/api/workspaces.js.map +1 -0
  31. package/dist/api/workspaces.test.d.ts +2 -0
  32. package/dist/api/workspaces.test.d.ts.map +1 -0
  33. package/dist/api/workspaces.test.js +65 -0
  34. package/dist/api/workspaces.test.js.map +1 -0
  35. package/dist/cli/auth.d.ts +86 -0
  36. package/dist/cli/auth.d.ts.map +1 -0
  37. package/dist/cli/auth.js +284 -0
  38. package/dist/cli/auth.js.map +1 -0
  39. package/dist/cli/branch-store.d.ts +53 -0
  40. package/dist/cli/branch-store.d.ts.map +1 -0
  41. package/dist/cli/branch-store.js +91 -0
  42. package/dist/cli/branch-store.js.map +1 -0
  43. package/dist/cli/branch-store.test.d.ts +2 -0
  44. package/dist/cli/branch-store.test.d.ts.map +1 -0
  45. package/dist/cli/branch-store.test.js +115 -0
  46. package/dist/cli/branch-store.test.js.map +1 -0
  47. package/dist/cli/commands/branch.d.ts +82 -0
  48. package/dist/cli/commands/branch.d.ts.map +1 -0
  49. package/dist/cli/commands/branch.js +215 -0
  50. package/dist/cli/commands/branch.js.map +1 -0
  51. package/dist/cli/commands/build.d.ts +43 -0
  52. package/dist/cli/commands/build.d.ts.map +1 -0
  53. package/dist/cli/commands/build.js +138 -0
  54. package/dist/cli/commands/build.js.map +1 -0
  55. package/dist/cli/commands/dev.d.ts +78 -0
  56. package/dist/cli/commands/dev.d.ts.map +1 -0
  57. package/dist/cli/commands/dev.js +226 -0
  58. package/dist/cli/commands/dev.js.map +1 -0
  59. package/dist/cli/commands/init.d.ts +45 -0
  60. package/dist/cli/commands/init.d.ts.map +1 -0
  61. package/dist/cli/commands/init.js +277 -0
  62. package/dist/cli/commands/init.js.map +1 -0
  63. package/dist/cli/commands/init.test.d.ts +2 -0
  64. package/dist/cli/commands/init.test.d.ts.map +1 -0
  65. package/dist/cli/commands/init.test.js +158 -0
  66. package/dist/cli/commands/init.test.js.map +1 -0
  67. package/dist/cli/commands/login.d.ts +37 -0
  68. package/dist/cli/commands/login.d.ts.map +1 -0
  69. package/dist/cli/commands/login.js +64 -0
  70. package/dist/cli/commands/login.js.map +1 -0
  71. package/dist/cli/config.d.ts +114 -0
  72. package/dist/cli/config.d.ts.map +1 -0
  73. package/dist/cli/config.js +258 -0
  74. package/dist/cli/config.js.map +1 -0
  75. package/dist/cli/config.test.d.ts +2 -0
  76. package/dist/cli/config.test.d.ts.map +1 -0
  77. package/dist/cli/config.test.js +243 -0
  78. package/dist/cli/config.test.js.map +1 -0
  79. package/dist/cli/env.d.ts +29 -0
  80. package/dist/cli/env.d.ts.map +1 -0
  81. package/dist/cli/env.js +66 -0
  82. package/dist/cli/env.js.map +1 -0
  83. package/dist/cli/git.d.ts +29 -0
  84. package/dist/cli/git.d.ts.map +1 -0
  85. package/dist/cli/git.js +114 -0
  86. package/dist/cli/git.js.map +1 -0
  87. package/dist/cli/git.test.d.ts +2 -0
  88. package/dist/cli/git.test.d.ts.map +1 -0
  89. package/dist/cli/git.test.js +125 -0
  90. package/dist/cli/git.test.js.map +1 -0
  91. package/dist/cli/index.d.ts +7 -0
  92. package/dist/cli/index.d.ts.map +1 -0
  93. package/dist/cli/index.js +337 -0
  94. package/dist/cli/index.js.map +1 -0
  95. package/dist/cli/utils/schema-validation.d.ts +95 -0
  96. package/dist/cli/utils/schema-validation.d.ts.map +1 -0
  97. package/dist/cli/utils/schema-validation.js +175 -0
  98. package/dist/cli/utils/schema-validation.js.map +1 -0
  99. package/dist/cli/utils/schema-validation.test.d.ts +5 -0
  100. package/dist/cli/utils/schema-validation.test.d.ts.map +1 -0
  101. package/dist/cli/utils/schema-validation.test.js +173 -0
  102. package/dist/cli/utils/schema-validation.test.js.map +1 -0
  103. package/dist/client/base.d.ts +116 -0
  104. package/dist/client/base.d.ts.map +1 -0
  105. package/dist/client/base.js +328 -0
  106. package/dist/client/base.js.map +1 -0
  107. package/dist/client/types.d.ts +137 -0
  108. package/dist/client/types.d.ts.map +1 -0
  109. package/dist/client/types.js +43 -0
  110. package/dist/client/types.js.map +1 -0
  111. package/dist/generator/client.d.ts +44 -0
  112. package/dist/generator/client.d.ts.map +1 -0
  113. package/dist/generator/client.js +144 -0
  114. package/dist/generator/client.js.map +1 -0
  115. package/dist/generator/datasource.d.ts +57 -0
  116. package/dist/generator/datasource.d.ts.map +1 -0
  117. package/dist/generator/datasource.js +169 -0
  118. package/dist/generator/datasource.js.map +1 -0
  119. package/dist/generator/datasource.test.d.ts +2 -0
  120. package/dist/generator/datasource.test.d.ts.map +1 -0
  121. package/dist/generator/datasource.test.js +254 -0
  122. package/dist/generator/datasource.test.js.map +1 -0
  123. package/dist/generator/index.d.ts +131 -0
  124. package/dist/generator/index.d.ts.map +1 -0
  125. package/dist/generator/index.js +121 -0
  126. package/dist/generator/index.js.map +1 -0
  127. package/dist/generator/index.test.d.ts +2 -0
  128. package/dist/generator/index.test.d.ts.map +1 -0
  129. package/dist/generator/index.test.js +175 -0
  130. package/dist/generator/index.test.js.map +1 -0
  131. package/dist/generator/loader.d.ts +156 -0
  132. package/dist/generator/loader.d.ts.map +1 -0
  133. package/dist/generator/loader.js +295 -0
  134. package/dist/generator/loader.js.map +1 -0
  135. package/dist/generator/pipe.d.ts +72 -0
  136. package/dist/generator/pipe.d.ts.map +1 -0
  137. package/dist/generator/pipe.js +174 -0
  138. package/dist/generator/pipe.js.map +1 -0
  139. package/dist/generator/pipe.test.d.ts +2 -0
  140. package/dist/generator/pipe.test.d.ts.map +1 -0
  141. package/dist/generator/pipe.test.js +393 -0
  142. package/dist/generator/pipe.test.js.map +1 -0
  143. package/dist/index.d.ts +74 -0
  144. package/dist/index.d.ts.map +1 -0
  145. package/dist/index.js +73 -0
  146. package/dist/index.js.map +1 -0
  147. package/dist/infer/index.d.ts +202 -0
  148. package/dist/infer/index.d.ts.map +1 -0
  149. package/dist/infer/index.js +5 -0
  150. package/dist/infer/index.js.map +1 -0
  151. package/dist/schema/datasource.d.ts +135 -0
  152. package/dist/schema/datasource.d.ts.map +1 -0
  153. package/dist/schema/datasource.js +105 -0
  154. package/dist/schema/datasource.js.map +1 -0
  155. package/dist/schema/datasource.test.d.ts +2 -0
  156. package/dist/schema/datasource.test.d.ts.map +1 -0
  157. package/dist/schema/datasource.test.js +142 -0
  158. package/dist/schema/datasource.test.js.map +1 -0
  159. package/dist/schema/engines.d.ts +157 -0
  160. package/dist/schema/engines.d.ts.map +1 -0
  161. package/dist/schema/engines.js +155 -0
  162. package/dist/schema/engines.js.map +1 -0
  163. package/dist/schema/engines.test.d.ts +2 -0
  164. package/dist/schema/engines.test.d.ts.map +1 -0
  165. package/dist/schema/engines.test.js +221 -0
  166. package/dist/schema/engines.test.js.map +1 -0
  167. package/dist/schema/params.d.ts +106 -0
  168. package/dist/schema/params.d.ts.map +1 -0
  169. package/dist/schema/params.js +138 -0
  170. package/dist/schema/params.js.map +1 -0
  171. package/dist/schema/params.test.d.ts +2 -0
  172. package/dist/schema/params.test.d.ts.map +1 -0
  173. package/dist/schema/params.test.js +175 -0
  174. package/dist/schema/params.test.js.map +1 -0
  175. package/dist/schema/pipe.d.ts +436 -0
  176. package/dist/schema/pipe.d.ts.map +1 -0
  177. package/dist/schema/pipe.js +484 -0
  178. package/dist/schema/pipe.js.map +1 -0
  179. package/dist/schema/pipe.test.d.ts +2 -0
  180. package/dist/schema/pipe.test.d.ts.map +1 -0
  181. package/dist/schema/pipe.test.js +488 -0
  182. package/dist/schema/pipe.test.js.map +1 -0
  183. package/dist/schema/project.d.ts +202 -0
  184. package/dist/schema/project.d.ts.map +1 -0
  185. package/dist/schema/project.js +188 -0
  186. package/dist/schema/project.js.map +1 -0
  187. package/dist/schema/project.test.d.ts +2 -0
  188. package/dist/schema/project.test.d.ts.map +1 -0
  189. package/dist/schema/project.test.js +180 -0
  190. package/dist/schema/project.test.js.map +1 -0
  191. package/dist/schema/types.d.ts +140 -0
  192. package/dist/schema/types.d.ts.map +1 -0
  193. package/dist/schema/types.js +174 -0
  194. package/dist/schema/types.js.map +1 -0
  195. package/dist/schema/types.test.d.ts +2 -0
  196. package/dist/schema/types.test.d.ts.map +1 -0
  197. package/dist/schema/types.test.js +176 -0
  198. package/dist/schema/types.test.js.map +1 -0
  199. package/dist/test/handlers.d.ts +58 -0
  200. package/dist/test/handlers.d.ts.map +1 -0
  201. package/dist/test/handlers.js +62 -0
  202. package/dist/test/handlers.js.map +1 -0
  203. package/dist/test/setup.d.ts +5 -0
  204. package/dist/test/setup.d.ts.map +1 -0
  205. package/dist/test/setup.js +11 -0
  206. package/dist/test/setup.js.map +1 -0
  207. package/package.json +57 -0
  208. package/src/api/branches.test.ts +377 -0
  209. package/src/api/branches.ts +334 -0
  210. package/src/api/build.test.ts +216 -0
  211. package/src/api/build.ts +266 -0
  212. package/src/api/deploy.test.ts +193 -0
  213. package/src/api/deploy.ts +163 -0
  214. package/src/api/workspaces.test.ts +81 -0
  215. package/src/api/workspaces.ts +77 -0
  216. package/src/cli/auth.ts +358 -0
  217. package/src/cli/branch-store.test.ts +139 -0
  218. package/src/cli/branch-store.ts +137 -0
  219. package/src/cli/commands/branch.ts +306 -0
  220. package/src/cli/commands/build.ts +183 -0
  221. package/src/cli/commands/dev.ts +334 -0
  222. package/src/cli/commands/init.test.ts +249 -0
  223. package/src/cli/commands/init.ts +323 -0
  224. package/src/cli/commands/login.ts +98 -0
  225. package/src/cli/config.test.ts +359 -0
  226. package/src/cli/config.ts +335 -0
  227. package/src/cli/env.ts +86 -0
  228. package/src/cli/git.test.ts +147 -0
  229. package/src/cli/git.ts +125 -0
  230. package/src/cli/index.ts +382 -0
  231. package/src/cli/utils/schema-validation.test.ts +222 -0
  232. package/src/cli/utils/schema-validation.ts +272 -0
  233. package/src/client/base.ts +414 -0
  234. package/src/client/types.ts +165 -0
  235. package/src/generator/client.ts +194 -0
  236. package/src/generator/datasource.test.ts +297 -0
  237. package/src/generator/datasource.ts +217 -0
  238. package/src/generator/index.test.ts +209 -0
  239. package/src/generator/index.ts +203 -0
  240. package/src/generator/loader.ts +406 -0
  241. package/src/generator/pipe.test.ts +441 -0
  242. package/src/generator/pipe.ts +220 -0
  243. package/src/index.ts +191 -0
  244. package/src/infer/index.ts +247 -0
  245. package/src/schema/datasource.test.ts +187 -0
  246. package/src/schema/datasource.ts +195 -0
  247. package/src/schema/engines.test.ts +247 -0
  248. package/src/schema/engines.ts +271 -0
  249. package/src/schema/params.test.ts +208 -0
  250. package/src/schema/params.ts +249 -0
  251. package/src/schema/pipe.test.ts +588 -0
  252. package/src/schema/pipe.ts +832 -0
  253. package/src/schema/project.test.ts +236 -0
  254. package/src/schema/project.ts +394 -0
  255. package/src/schema/types.test.ts +212 -0
  256. package/src/schema/types.ts +366 -0
  257. package/src/test/handlers.ts +79 -0
  258. 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
+ }