@cimplify/sdk 0.41.0 → 0.42.0

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 (142) hide show
  1. package/dist/cli/commands/deploy.mjs +561 -0
  2. package/dist/cli/commands/dev.mjs +434 -0
  3. package/dist/cli/commands/domains.mjs +479 -0
  4. package/dist/cli/commands/env.mjs +610 -0
  5. package/dist/cli/commands/link.mjs +325 -0
  6. package/dist/cli/commands/login.mjs +515 -0
  7. package/dist/cli/commands/logout.mjs +54 -0
  8. package/dist/cli/commands/logs.mjs +358 -0
  9. package/dist/cli/commands/projects.mjs +327 -0
  10. package/dist/cli/commands/rollback.mjs +477 -0
  11. package/dist/cli/commands/status.mjs +380 -0
  12. package/dist/cli/commands/unlink.mjs +58 -0
  13. package/dist/cli/commands/whoami.mjs +234 -0
  14. package/dist/dispatcher.mjs +91 -0
  15. package/dist/styles.css +1 -1
  16. package/package.json +1 -1
  17. package/templates/storefront-bakery/app/accessibility/page.tsx +11 -0
  18. package/templates/storefront-bakery/app/account/addresses/page.tsx +21 -0
  19. package/templates/storefront-bakery/app/account/orders/page.tsx +21 -0
  20. package/templates/storefront-bakery/app/account/page.tsx +22 -0
  21. package/templates/storefront-bakery/app/account/settings/page.tsx +21 -0
  22. package/templates/storefront-bakery/app/contact/contact-form.tsx +109 -0
  23. package/templates/storefront-bakery/app/contact/page.tsx +54 -0
  24. package/templates/storefront-bakery/app/login/page.tsx +17 -0
  25. package/templates/storefront-bakery/app/opensearch.xml/route.ts +37 -0
  26. package/templates/storefront-bakery/app/returns/page.tsx +11 -0
  27. package/templates/storefront-bakery/app/search/page.tsx +38 -0
  28. package/templates/storefront-bakery/app/search/search-client.tsx +7 -0
  29. package/templates/storefront-bakery/app/shipping/page.tsx +16 -0
  30. package/templates/storefront-bakery/app/signup/page.tsx +17 -0
  31. package/templates/storefront-bakery/app/sitemap-page/page.tsx +167 -0
  32. package/templates/storefront-bakery/app/track-order/page.tsx +24 -0
  33. package/templates/storefront-bakery/app/track-order/track-order-form.tsx +69 -0
  34. package/templates/storefront-bakery/components/account-iframe.tsx +13 -0
  35. package/templates/storefront-bakery/components/policy-page.tsx +49 -0
  36. package/templates/storefront-bakery/lib/brand.ts +119 -4
  37. package/templates/storefront-fashion/app/accessibility/page.tsx +11 -0
  38. package/templates/storefront-fashion/app/account/addresses/page.tsx +21 -0
  39. package/templates/storefront-fashion/app/account/orders/page.tsx +21 -0
  40. package/templates/storefront-fashion/app/account/page.tsx +22 -0
  41. package/templates/storefront-fashion/app/account/settings/page.tsx +21 -0
  42. package/templates/storefront-fashion/app/contact/contact-form.tsx +109 -0
  43. package/templates/storefront-fashion/app/contact/page.tsx +54 -0
  44. package/templates/storefront-fashion/app/login/page.tsx +17 -0
  45. package/templates/storefront-fashion/app/lookbook/page.tsx +132 -0
  46. package/templates/storefront-fashion/app/opensearch.xml/route.ts +37 -0
  47. package/templates/storefront-fashion/app/returns/page.tsx +11 -0
  48. package/templates/storefront-fashion/app/search/page.tsx +38 -0
  49. package/templates/storefront-fashion/app/search/search-client.tsx +7 -0
  50. package/templates/storefront-fashion/app/shipping/page.tsx +16 -0
  51. package/templates/storefront-fashion/app/signup/page.tsx +17 -0
  52. package/templates/storefront-fashion/app/sitemap-page/page.tsx +167 -0
  53. package/templates/storefront-fashion/app/size-guide/page.tsx +155 -0
  54. package/templates/storefront-fashion/app/track-order/page.tsx +24 -0
  55. package/templates/storefront-fashion/app/track-order/track-order-form.tsx +69 -0
  56. package/templates/storefront-fashion/components/account-iframe.tsx +13 -0
  57. package/templates/storefront-fashion/components/policy-page.tsx +49 -0
  58. package/templates/storefront-fashion/lib/brand.ts +99 -4
  59. package/templates/storefront-grocery/app/accessibility/page.tsx +11 -0
  60. package/templates/storefront-grocery/app/account/addresses/page.tsx +21 -0
  61. package/templates/storefront-grocery/app/account/orders/page.tsx +21 -0
  62. package/templates/storefront-grocery/app/account/page.tsx +22 -0
  63. package/templates/storefront-grocery/app/account/settings/page.tsx +21 -0
  64. package/templates/storefront-grocery/app/contact/contact-form.tsx +109 -0
  65. package/templates/storefront-grocery/app/contact/page.tsx +54 -0
  66. package/templates/storefront-grocery/app/login/page.tsx +17 -0
  67. package/templates/storefront-grocery/app/opensearch.xml/route.ts +37 -0
  68. package/templates/storefront-grocery/app/returns/page.tsx +11 -0
  69. package/templates/storefront-grocery/app/search/page.tsx +38 -0
  70. package/templates/storefront-grocery/app/search/search-client.tsx +7 -0
  71. package/templates/storefront-grocery/app/shipping/page.tsx +16 -0
  72. package/templates/storefront-grocery/app/signup/page.tsx +17 -0
  73. package/templates/storefront-grocery/app/sitemap-page/page.tsx +167 -0
  74. package/templates/storefront-grocery/app/track-order/page.tsx +24 -0
  75. package/templates/storefront-grocery/app/track-order/track-order-form.tsx +69 -0
  76. package/templates/storefront-grocery/components/account-iframe.tsx +13 -0
  77. package/templates/storefront-grocery/components/policy-page.tsx +49 -0
  78. package/templates/storefront-grocery/lib/brand.ts +92 -3
  79. package/templates/storefront-restaurant/app/accessibility/page.tsx +11 -0
  80. package/templates/storefront-restaurant/app/account/addresses/page.tsx +21 -0
  81. package/templates/storefront-restaurant/app/account/orders/page.tsx +21 -0
  82. package/templates/storefront-restaurant/app/account/page.tsx +22 -0
  83. package/templates/storefront-restaurant/app/account/settings/page.tsx +21 -0
  84. package/templates/storefront-restaurant/app/contact/contact-form.tsx +109 -0
  85. package/templates/storefront-restaurant/app/contact/page.tsx +54 -0
  86. package/templates/storefront-restaurant/app/login/page.tsx +17 -0
  87. package/templates/storefront-restaurant/app/opensearch.xml/route.ts +37 -0
  88. package/templates/storefront-restaurant/app/reservations/page.tsx +66 -0
  89. package/templates/storefront-restaurant/app/reservations/reservations-client.tsx +234 -0
  90. package/templates/storefront-restaurant/app/returns/page.tsx +11 -0
  91. package/templates/storefront-restaurant/app/search/page.tsx +38 -0
  92. package/templates/storefront-restaurant/app/search/search-client.tsx +7 -0
  93. package/templates/storefront-restaurant/app/shipping/page.tsx +16 -0
  94. package/templates/storefront-restaurant/app/signup/page.tsx +17 -0
  95. package/templates/storefront-restaurant/app/sitemap-page/page.tsx +167 -0
  96. package/templates/storefront-restaurant/app/track-order/page.tsx +24 -0
  97. package/templates/storefront-restaurant/app/track-order/track-order-form.tsx +69 -0
  98. package/templates/storefront-restaurant/components/account-iframe.tsx +13 -0
  99. package/templates/storefront-restaurant/components/policy-page.tsx +49 -0
  100. package/templates/storefront-restaurant/lib/brand.ts +91 -3
  101. package/templates/storefront-retail/app/accessibility/page.tsx +11 -0
  102. package/templates/storefront-retail/app/account/addresses/page.tsx +21 -0
  103. package/templates/storefront-retail/app/account/orders/page.tsx +21 -0
  104. package/templates/storefront-retail/app/account/page.tsx +22 -0
  105. package/templates/storefront-retail/app/account/settings/page.tsx +21 -0
  106. package/templates/storefront-retail/app/contact/contact-form.tsx +109 -0
  107. package/templates/storefront-retail/app/contact/page.tsx +54 -0
  108. package/templates/storefront-retail/app/login/page.tsx +17 -0
  109. package/templates/storefront-retail/app/opensearch.xml/route.ts +37 -0
  110. package/templates/storefront-retail/app/returns/page.tsx +11 -0
  111. package/templates/storefront-retail/app/search/page.tsx +38 -0
  112. package/templates/storefront-retail/app/search/search-client.tsx +7 -0
  113. package/templates/storefront-retail/app/shipping/page.tsx +16 -0
  114. package/templates/storefront-retail/app/signup/page.tsx +17 -0
  115. package/templates/storefront-retail/app/sitemap-page/page.tsx +167 -0
  116. package/templates/storefront-retail/app/track-order/page.tsx +24 -0
  117. package/templates/storefront-retail/app/track-order/track-order-form.tsx +69 -0
  118. package/templates/storefront-retail/components/account-iframe.tsx +13 -0
  119. package/templates/storefront-retail/components/policy-page.tsx +49 -0
  120. package/templates/storefront-retail/lib/brand.ts +105 -9
  121. package/templates/storefront-services/app/accessibility/page.tsx +11 -0
  122. package/templates/storefront-services/app/account/addresses/page.tsx +21 -0
  123. package/templates/storefront-services/app/account/orders/page.tsx +21 -0
  124. package/templates/storefront-services/app/account/page.tsx +22 -0
  125. package/templates/storefront-services/app/account/settings/page.tsx +21 -0
  126. package/templates/storefront-services/app/book/book-client.tsx +195 -0
  127. package/templates/storefront-services/app/book/page.tsx +65 -0
  128. package/templates/storefront-services/app/contact/contact-form.tsx +109 -0
  129. package/templates/storefront-services/app/contact/page.tsx +54 -0
  130. package/templates/storefront-services/app/login/page.tsx +17 -0
  131. package/templates/storefront-services/app/opensearch.xml/route.ts +37 -0
  132. package/templates/storefront-services/app/returns/page.tsx +11 -0
  133. package/templates/storefront-services/app/search/page.tsx +38 -0
  134. package/templates/storefront-services/app/search/search-client.tsx +7 -0
  135. package/templates/storefront-services/app/shipping/page.tsx +16 -0
  136. package/templates/storefront-services/app/signup/page.tsx +17 -0
  137. package/templates/storefront-services/app/sitemap-page/page.tsx +167 -0
  138. package/templates/storefront-services/app/track-order/page.tsx +24 -0
  139. package/templates/storefront-services/app/track-order/track-order-form.tsx +69 -0
  140. package/templates/storefront-services/components/account-iframe.tsx +13 -0
  141. package/templates/storefront-services/components/policy-page.tsx +49 -0
  142. package/templates/storefront-services/lib/brand.ts +93 -2
@@ -0,0 +1,358 @@
1
+ import { promises } from 'fs';
2
+ import os from 'os';
3
+ import path from 'path';
4
+
5
+ // src/cli/errors.ts
6
+ var CLI_ERROR_CODE = {
7
+ NOT_LOGGED_IN: "NOT_LOGGED_IN",
8
+ NOT_LINKED: "NOT_LINKED",
9
+ NETWORK_ERROR: "NETWORK_ERROR",
10
+ AUTH_FAILED: "AUTH_FAILED",
11
+ PROJECT_NOT_FOUND: "PROJECT_NOT_FOUND",
12
+ INVALID_INPUT: "INVALID_INPUT",
13
+ ALREADY_LINKED: "ALREADY_LINKED",
14
+ SERVER_ERROR: "SERVER_ERROR"};
15
+ var EXIT_CODE = {
16
+ ERROR: 1};
17
+ var CliError = class extends Error {
18
+ constructor(code, message, exitCode = EXIT_CODE.ERROR) {
19
+ super(message);
20
+ this.name = "CliError";
21
+ this.code = code;
22
+ this.exitCode = exitCode;
23
+ }
24
+ };
25
+
26
+ // src/cli/api-client.ts
27
+ var DEFAULT_API_BASE_URL = "https://api.cimplify.io";
28
+ var ENV_API_BASE_URL = "CIMPLIFY_API_URL";
29
+ var DEFAULT_TIMEOUT_MS = 3e4;
30
+ var HEADER_AUTHORIZATION = "Authorization";
31
+ var HEADER_CONTENT_TYPE = "Content-Type";
32
+ var HEADER_ACCEPT = "Accept";
33
+ var HEADER_USER_AGENT = "User-Agent";
34
+ var BEARER_PREFIX = "Bearer ";
35
+ var CONTENT_TYPE_JSON = "application/json";
36
+ var USER_AGENT = "cimplify-cli";
37
+ var METHOD_GET = "GET";
38
+ var METHOD_POST = "POST";
39
+ var METHOD_PUT = "PUT";
40
+ var METHOD_DELETE = "DELETE";
41
+ var STATUS_UNAUTHORIZED = 401;
42
+ var STATUS_FORBIDDEN = 403;
43
+ var STATUS_NOT_FOUND = 404;
44
+ var STATUS_CONFLICT = 409;
45
+ var STATUS_BAD_REQUEST_MIN = 400;
46
+ var STATUS_SERVER_ERROR_MIN = 500;
47
+ var ApiClient = class _ApiClient {
48
+ constructor(baseUrl, apiKey) {
49
+ this.baseUrl = baseUrl.replace(/\/+$/, "");
50
+ this.apiKey = apiKey;
51
+ }
52
+ static fromAuth(auth) {
53
+ return new _ApiClient(auth.apiBaseUrl, auth.apiKey);
54
+ }
55
+ static unauthenticated(baseUrl = resolveBaseUrl()) {
56
+ return new _ApiClient(baseUrl, "");
57
+ }
58
+ static withKey(apiKey, baseUrl = resolveBaseUrl()) {
59
+ return new _ApiClient(baseUrl, apiKey);
60
+ }
61
+ get(pathname, options) {
62
+ return this.request(METHOD_GET, pathname, void 0, options);
63
+ }
64
+ post(pathname, body, options) {
65
+ return this.request(METHOD_POST, pathname, body, options);
66
+ }
67
+ put(pathname, body, options) {
68
+ return this.request(METHOD_PUT, pathname, body, options);
69
+ }
70
+ delete(pathname, options) {
71
+ return this.request(METHOD_DELETE, pathname, void 0, options);
72
+ }
73
+ async request(method, pathname, body, options) {
74
+ const url = this.buildUrl(pathname, options?.query);
75
+ const headers = {
76
+ [HEADER_ACCEPT]: CONTENT_TYPE_JSON,
77
+ [HEADER_USER_AGENT]: USER_AGENT
78
+ };
79
+ if (this.apiKey) {
80
+ headers[HEADER_AUTHORIZATION] = `${BEARER_PREFIX}${this.apiKey}`;
81
+ }
82
+ let payload;
83
+ if (body !== void 0) {
84
+ headers[HEADER_CONTENT_TYPE] = CONTENT_TYPE_JSON;
85
+ payload = JSON.stringify(body);
86
+ }
87
+ const controller = new AbortController();
88
+ const timeoutMs = options?.timeoutMs ?? DEFAULT_TIMEOUT_MS;
89
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
90
+ let response;
91
+ try {
92
+ response = await fetch(url, {
93
+ method,
94
+ headers,
95
+ body: payload,
96
+ signal: controller.signal
97
+ });
98
+ } catch (err) {
99
+ clearTimeout(timer);
100
+ throw toNetworkError(err);
101
+ }
102
+ clearTimeout(timer);
103
+ return parseResponse(response);
104
+ }
105
+ buildUrl(pathname, query) {
106
+ const base = pathname.startsWith("/") ? pathname : `/${pathname}`;
107
+ const url = new URL(`${this.baseUrl}${base}`);
108
+ if (query) {
109
+ for (const [k, v] of Object.entries(query)) {
110
+ if (v === void 0 || v === null) continue;
111
+ url.searchParams.set(k, String(v));
112
+ }
113
+ }
114
+ return url.toString();
115
+ }
116
+ };
117
+ function resolveBaseUrl(override) {
118
+ const fromEnv = process.env[ENV_API_BASE_URL];
119
+ return fromEnv && fromEnv.length > 0 ? fromEnv : DEFAULT_API_BASE_URL;
120
+ }
121
+ async function parseResponse(response) {
122
+ const text = await response.text();
123
+ let body = void 0;
124
+ if (text.length > 0) {
125
+ try {
126
+ body = JSON.parse(text);
127
+ } catch {
128
+ throw new CliError(
129
+ CLI_ERROR_CODE.SERVER_ERROR,
130
+ `Server returned non-JSON response (status ${response.status}): ${truncate(text)}`
131
+ );
132
+ }
133
+ }
134
+ if (response.ok) {
135
+ if (body && typeof body === "object" && "data" in body) {
136
+ return body.data;
137
+ }
138
+ return body;
139
+ }
140
+ const code = mapStatusToCode(response.status);
141
+ const message = extractErrorMessage(body, response.status);
142
+ throw new CliError(code, message);
143
+ }
144
+ function mapStatusToCode(status) {
145
+ if (status === STATUS_UNAUTHORIZED) return CLI_ERROR_CODE.AUTH_FAILED;
146
+ if (status === STATUS_FORBIDDEN) return CLI_ERROR_CODE.AUTH_FAILED;
147
+ if (status === STATUS_NOT_FOUND) return CLI_ERROR_CODE.PROJECT_NOT_FOUND;
148
+ if (status === STATUS_CONFLICT) return CLI_ERROR_CODE.ALREADY_LINKED;
149
+ if (status >= STATUS_SERVER_ERROR_MIN) return CLI_ERROR_CODE.SERVER_ERROR;
150
+ if (status >= STATUS_BAD_REQUEST_MIN) return CLI_ERROR_CODE.INVALID_INPUT;
151
+ return CLI_ERROR_CODE.SERVER_ERROR;
152
+ }
153
+ function extractErrorMessage(body, status) {
154
+ if (body && typeof body === "object") {
155
+ const envelope = body;
156
+ if (envelope.error && typeof envelope.error.error_message === "string") {
157
+ return envelope.error.error_code ? `${envelope.error.error_code}: ${envelope.error.error_message}` : envelope.error.error_message;
158
+ }
159
+ if (typeof envelope.message === "string") return envelope.message;
160
+ }
161
+ return `HTTP ${status}`;
162
+ }
163
+ function toNetworkError(err) {
164
+ if (err instanceof DOMException && err.name === "AbortError") {
165
+ return new CliError(CLI_ERROR_CODE.NETWORK_ERROR, "Request timed out.");
166
+ }
167
+ const message = err instanceof Error ? err.message : String(err);
168
+ return new CliError(CLI_ERROR_CODE.NETWORK_ERROR, `Network error: ${message}`);
169
+ }
170
+ function truncate(value, max = 200) {
171
+ return value.length > max ? `${value.slice(0, max)}\u2026` : value;
172
+ }
173
+
174
+ // src/cli/args.ts
175
+ function parseArgs(argv) {
176
+ const positional = [];
177
+ const flags = /* @__PURE__ */ new Map();
178
+ for (let i = 0; i < argv.length; i++) {
179
+ const arg = argv[i];
180
+ if (!arg.startsWith("--")) {
181
+ positional.push(arg);
182
+ continue;
183
+ }
184
+ const eq = arg.indexOf("=");
185
+ if (eq !== -1) {
186
+ flags.set(arg.slice(2, eq), arg.slice(eq + 1));
187
+ continue;
188
+ }
189
+ const name = arg.slice(2);
190
+ const next = argv[i + 1];
191
+ if (next !== void 0 && !next.startsWith("--")) {
192
+ flags.set(name, next);
193
+ i++;
194
+ } else {
195
+ flags.set(name, true);
196
+ }
197
+ }
198
+ return { positional, flags };
199
+ }
200
+ function flagString(args, name) {
201
+ const v = args.flags.get(name);
202
+ return typeof v === "string" ? v : void 0;
203
+ }
204
+ function flagBool(args, name) {
205
+ return args.flags.has(name);
206
+ }
207
+ var AUTH_FILE_NAME = "auth.json";
208
+ var PROJECT_FILE_NAME = "project.json";
209
+ var STATE_FILE_NAME = "state.json";
210
+ var PROJECT_DIR_NAME = ".cimplify";
211
+ var ENV_XDG_CONFIG_HOME = "XDG_CONFIG_HOME";
212
+ var APP_CONFIG_DIR_NAME = "cimplify";
213
+ var HOME_CONFIG_DIR_NAME = ".config";
214
+ var ENCODING_UTF8 = "utf8";
215
+ function authConfigDir() {
216
+ const xdg = process.env[ENV_XDG_CONFIG_HOME];
217
+ return xdg ? path.join(xdg, APP_CONFIG_DIR_NAME) : path.join(os.homedir(), HOME_CONFIG_DIR_NAME, APP_CONFIG_DIR_NAME);
218
+ }
219
+ function authConfigPath() {
220
+ return path.join(authConfigDir(), AUTH_FILE_NAME);
221
+ }
222
+ function projectConfigDir(cwd = process.cwd()) {
223
+ return path.join(cwd, PROJECT_DIR_NAME);
224
+ }
225
+ function projectConfigPath(cwd = process.cwd()) {
226
+ return path.join(projectConfigDir(cwd), PROJECT_FILE_NAME);
227
+ }
228
+ function projectStatePath(cwd = process.cwd()) {
229
+ return path.join(projectConfigDir(cwd), STATE_FILE_NAME);
230
+ }
231
+ async function readJsonFile(filePath) {
232
+ try {
233
+ const raw = await promises.readFile(filePath, ENCODING_UTF8);
234
+ return JSON.parse(raw);
235
+ } catch (err) {
236
+ if (err.code === "ENOENT") return null;
237
+ throw err;
238
+ }
239
+ }
240
+ async function readAuth() {
241
+ const data = await readJsonFile(authConfigPath());
242
+ if (!data) {
243
+ throw new CliError(
244
+ CLI_ERROR_CODE.NOT_LOGGED_IN,
245
+ "Not logged in. Run `cimplify login` first."
246
+ );
247
+ }
248
+ return data;
249
+ }
250
+ async function readProjectLink(cwd = process.cwd()) {
251
+ const data = await readJsonFile(projectConfigPath(cwd));
252
+ if (!data) {
253
+ throw new CliError(
254
+ CLI_ERROR_CODE.NOT_LINKED,
255
+ "No project linked. Run `cimplify link <project-id>` first."
256
+ );
257
+ }
258
+ return data;
259
+ }
260
+ async function readProjectState(cwd = process.cwd()) {
261
+ return readJsonFile(projectStatePath(cwd));
262
+ }
263
+
264
+ // src/cli/output.ts
265
+ var RESET = "\x1B[0m";
266
+ var RED_OPEN = "\x1B[31m";
267
+ var PREFIX_FAILURE = "\u2717";
268
+ function wrap(open, value) {
269
+ return `${open}${value}${RESET}`;
270
+ }
271
+ function red(value) {
272
+ return wrap(RED_OPEN, value);
273
+ }
274
+ function failure(message) {
275
+ console.error(`${red(PREFIX_FAILURE)} ${message}`);
276
+ }
277
+
278
+ // src/cli/types.ts
279
+ var DEPLOYMENT_STATUS = {
280
+ ACTIVE: "active",
281
+ FAILED: "failed",
282
+ CANCELLED: "cancelled",
283
+ SUPERSEDED: "superseded"
284
+ };
285
+ var TERMINAL_DEPLOYMENT_STATUSES = /* @__PURE__ */ new Set([
286
+ DEPLOYMENT_STATUS.ACTIVE,
287
+ DEPLOYMENT_STATUS.FAILED,
288
+ DEPLOYMENT_STATUS.CANCELLED,
289
+ DEPLOYMENT_STATUS.SUPERSEDED
290
+ ]);
291
+
292
+ // src/cli/commands/logs.ts
293
+ var FLAG_DEPLOYMENT = "deployment";
294
+ var FLAG_FOLLOW = "follow";
295
+ var POLL_INTERVAL_MS = 1e3;
296
+ var POLL_MAX_MS = 60 * 60 * 1e3;
297
+ function progressEndpoint(businessId, projectId, deploymentId) {
298
+ return `/v1/businesses/${encodeURIComponent(businessId)}/projects/${encodeURIComponent(projectId)}/deployments/${encodeURIComponent(deploymentId)}/progress`;
299
+ }
300
+ function nextChunk(cursor, full) {
301
+ if (!full) return "";
302
+ if (full.length <= cursor.printed) return "";
303
+ const chunk = full.slice(cursor.printed);
304
+ cursor.printed = full.length;
305
+ return chunk;
306
+ }
307
+ function sleep(ms) {
308
+ return new Promise((resolve) => setTimeout(resolve, ms));
309
+ }
310
+ async function run(argv) {
311
+ const args = parseArgs(argv);
312
+ const auth = await readAuth();
313
+ const link = await readProjectLink();
314
+ const client = ApiClient.fromAuth(auth);
315
+ let deploymentId = flagString(args, FLAG_DEPLOYMENT);
316
+ if (!deploymentId) {
317
+ const state = await readProjectState();
318
+ deploymentId = state?.lastDeploymentId;
319
+ }
320
+ if (!deploymentId) {
321
+ throw new CliError(
322
+ CLI_ERROR_CODE.INVALID_INPUT,
323
+ "No deployment specified and no recent deployment in state.\nPass --deployment <id> or run `cimplify deploy` first."
324
+ );
325
+ }
326
+ const endpoint = progressEndpoint(link.businessId, link.projectId, deploymentId);
327
+ const follow = flagBool(args, FLAG_FOLLOW);
328
+ if (!follow) {
329
+ const progress = await client.get(endpoint);
330
+ if (progress.build_logs) process.stdout.write(progress.build_logs);
331
+ if (progress.error_message) failure(progress.error_message);
332
+ return;
333
+ }
334
+ const cursor = { printed: 0 };
335
+ const startedAt = Date.now();
336
+ let lastErrorMessage;
337
+ while (true) {
338
+ const progress = await client.get(endpoint);
339
+ const chunk = nextChunk(cursor, progress.build_logs);
340
+ if (chunk.length > 0) process.stdout.write(chunk);
341
+ if (progress.error_message && progress.error_message !== lastErrorMessage) {
342
+ process.stdout.write(`
343
+ ${red(progress.error_message)}
344
+ `);
345
+ lastErrorMessage = progress.error_message;
346
+ }
347
+ if (TERMINAL_DEPLOYMENT_STATUSES.has(progress.status)) return;
348
+ if (Date.now() - startedAt > POLL_MAX_MS) {
349
+ throw new CliError(
350
+ CLI_ERROR_CODE.NETWORK_ERROR,
351
+ "Logs polling timed out (1 hour). Re-run `cimplify logs --follow` to resume."
352
+ );
353
+ }
354
+ await sleep(POLL_INTERVAL_MS);
355
+ }
356
+ }
357
+
358
+ export { run as default, nextChunk };
@@ -0,0 +1,327 @@
1
+ import { promises } from 'fs';
2
+ import os from 'os';
3
+ import path from 'path';
4
+
5
+ // src/cli/errors.ts
6
+ var CLI_ERROR_CODE = {
7
+ NOT_LOGGED_IN: "NOT_LOGGED_IN",
8
+ NETWORK_ERROR: "NETWORK_ERROR",
9
+ AUTH_FAILED: "AUTH_FAILED",
10
+ PROJECT_NOT_FOUND: "PROJECT_NOT_FOUND",
11
+ INVALID_INPUT: "INVALID_INPUT",
12
+ ALREADY_LINKED: "ALREADY_LINKED",
13
+ SERVER_ERROR: "SERVER_ERROR"};
14
+ var EXIT_CODE = {
15
+ ERROR: 1};
16
+ var CliError = class extends Error {
17
+ constructor(code, message, exitCode = EXIT_CODE.ERROR) {
18
+ super(message);
19
+ this.name = "CliError";
20
+ this.code = code;
21
+ this.exitCode = exitCode;
22
+ }
23
+ };
24
+
25
+ // src/cli/api-client.ts
26
+ var DEFAULT_API_BASE_URL = "https://api.cimplify.io";
27
+ var ENV_API_BASE_URL = "CIMPLIFY_API_URL";
28
+ var DEFAULT_TIMEOUT_MS = 3e4;
29
+ var HEADER_AUTHORIZATION = "Authorization";
30
+ var HEADER_CONTENT_TYPE = "Content-Type";
31
+ var HEADER_ACCEPT = "Accept";
32
+ var HEADER_USER_AGENT = "User-Agent";
33
+ var BEARER_PREFIX = "Bearer ";
34
+ var CONTENT_TYPE_JSON = "application/json";
35
+ var USER_AGENT = "cimplify-cli";
36
+ var METHOD_GET = "GET";
37
+ var METHOD_POST = "POST";
38
+ var METHOD_PUT = "PUT";
39
+ var METHOD_DELETE = "DELETE";
40
+ var STATUS_UNAUTHORIZED = 401;
41
+ var STATUS_FORBIDDEN = 403;
42
+ var STATUS_NOT_FOUND = 404;
43
+ var STATUS_CONFLICT = 409;
44
+ var STATUS_BAD_REQUEST_MIN = 400;
45
+ var STATUS_SERVER_ERROR_MIN = 500;
46
+ var ApiClient = class _ApiClient {
47
+ constructor(baseUrl, apiKey) {
48
+ this.baseUrl = baseUrl.replace(/\/+$/, "");
49
+ this.apiKey = apiKey;
50
+ }
51
+ static fromAuth(auth) {
52
+ return new _ApiClient(auth.apiBaseUrl, auth.apiKey);
53
+ }
54
+ static unauthenticated(baseUrl = resolveBaseUrl()) {
55
+ return new _ApiClient(baseUrl, "");
56
+ }
57
+ static withKey(apiKey, baseUrl = resolveBaseUrl()) {
58
+ return new _ApiClient(baseUrl, apiKey);
59
+ }
60
+ get(pathname, options) {
61
+ return this.request(METHOD_GET, pathname, void 0, options);
62
+ }
63
+ post(pathname, body, options) {
64
+ return this.request(METHOD_POST, pathname, body, options);
65
+ }
66
+ put(pathname, body, options) {
67
+ return this.request(METHOD_PUT, pathname, body, options);
68
+ }
69
+ delete(pathname, options) {
70
+ return this.request(METHOD_DELETE, pathname, void 0, options);
71
+ }
72
+ async request(method, pathname, body, options) {
73
+ const url = this.buildUrl(pathname, options?.query);
74
+ const headers = {
75
+ [HEADER_ACCEPT]: CONTENT_TYPE_JSON,
76
+ [HEADER_USER_AGENT]: USER_AGENT
77
+ };
78
+ if (this.apiKey) {
79
+ headers[HEADER_AUTHORIZATION] = `${BEARER_PREFIX}${this.apiKey}`;
80
+ }
81
+ let payload;
82
+ if (body !== void 0) {
83
+ headers[HEADER_CONTENT_TYPE] = CONTENT_TYPE_JSON;
84
+ payload = JSON.stringify(body);
85
+ }
86
+ const controller = new AbortController();
87
+ const timeoutMs = options?.timeoutMs ?? DEFAULT_TIMEOUT_MS;
88
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
89
+ let response;
90
+ try {
91
+ response = await fetch(url, {
92
+ method,
93
+ headers,
94
+ body: payload,
95
+ signal: controller.signal
96
+ });
97
+ } catch (err) {
98
+ clearTimeout(timer);
99
+ throw toNetworkError(err);
100
+ }
101
+ clearTimeout(timer);
102
+ return parseResponse(response);
103
+ }
104
+ buildUrl(pathname, query) {
105
+ const base = pathname.startsWith("/") ? pathname : `/${pathname}`;
106
+ const url = new URL(`${this.baseUrl}${base}`);
107
+ if (query) {
108
+ for (const [k, v] of Object.entries(query)) {
109
+ if (v === void 0 || v === null) continue;
110
+ url.searchParams.set(k, String(v));
111
+ }
112
+ }
113
+ return url.toString();
114
+ }
115
+ };
116
+ function resolveBaseUrl(override) {
117
+ const fromEnv = process.env[ENV_API_BASE_URL];
118
+ return fromEnv && fromEnv.length > 0 ? fromEnv : DEFAULT_API_BASE_URL;
119
+ }
120
+ async function parseResponse(response) {
121
+ const text = await response.text();
122
+ let body = void 0;
123
+ if (text.length > 0) {
124
+ try {
125
+ body = JSON.parse(text);
126
+ } catch {
127
+ throw new CliError(
128
+ CLI_ERROR_CODE.SERVER_ERROR,
129
+ `Server returned non-JSON response (status ${response.status}): ${truncate(text)}`
130
+ );
131
+ }
132
+ }
133
+ if (response.ok) {
134
+ if (body && typeof body === "object" && "data" in body) {
135
+ return body.data;
136
+ }
137
+ return body;
138
+ }
139
+ const code = mapStatusToCode(response.status);
140
+ const message = extractErrorMessage(body, response.status);
141
+ throw new CliError(code, message);
142
+ }
143
+ function mapStatusToCode(status) {
144
+ if (status === STATUS_UNAUTHORIZED) return CLI_ERROR_CODE.AUTH_FAILED;
145
+ if (status === STATUS_FORBIDDEN) return CLI_ERROR_CODE.AUTH_FAILED;
146
+ if (status === STATUS_NOT_FOUND) return CLI_ERROR_CODE.PROJECT_NOT_FOUND;
147
+ if (status === STATUS_CONFLICT) return CLI_ERROR_CODE.ALREADY_LINKED;
148
+ if (status >= STATUS_SERVER_ERROR_MIN) return CLI_ERROR_CODE.SERVER_ERROR;
149
+ if (status >= STATUS_BAD_REQUEST_MIN) return CLI_ERROR_CODE.INVALID_INPUT;
150
+ return CLI_ERROR_CODE.SERVER_ERROR;
151
+ }
152
+ function extractErrorMessage(body, status) {
153
+ if (body && typeof body === "object") {
154
+ const envelope = body;
155
+ if (envelope.error && typeof envelope.error.error_message === "string") {
156
+ return envelope.error.error_code ? `${envelope.error.error_code}: ${envelope.error.error_message}` : envelope.error.error_message;
157
+ }
158
+ if (typeof envelope.message === "string") return envelope.message;
159
+ }
160
+ return `HTTP ${status}`;
161
+ }
162
+ function toNetworkError(err) {
163
+ if (err instanceof DOMException && err.name === "AbortError") {
164
+ return new CliError(CLI_ERROR_CODE.NETWORK_ERROR, "Request timed out.");
165
+ }
166
+ const message = err instanceof Error ? err.message : String(err);
167
+ return new CliError(CLI_ERROR_CODE.NETWORK_ERROR, `Network error: ${message}`);
168
+ }
169
+ function truncate(value, max = 200) {
170
+ return value.length > max ? `${value.slice(0, max)}\u2026` : value;
171
+ }
172
+
173
+ // src/cli/args.ts
174
+ function parseArgs(argv) {
175
+ const positional = [];
176
+ const flags = /* @__PURE__ */ new Map();
177
+ for (let i = 0; i < argv.length; i++) {
178
+ const arg = argv[i];
179
+ if (!arg.startsWith("--")) {
180
+ positional.push(arg);
181
+ continue;
182
+ }
183
+ const eq = arg.indexOf("=");
184
+ if (eq !== -1) {
185
+ flags.set(arg.slice(2, eq), arg.slice(eq + 1));
186
+ continue;
187
+ }
188
+ const name = arg.slice(2);
189
+ const next = argv[i + 1];
190
+ if (next !== void 0 && !next.startsWith("--")) {
191
+ flags.set(name, next);
192
+ i++;
193
+ } else {
194
+ flags.set(name, true);
195
+ }
196
+ }
197
+ return { positional, flags };
198
+ }
199
+ var AUTH_FILE_NAME = "auth.json";
200
+ var ENV_XDG_CONFIG_HOME = "XDG_CONFIG_HOME";
201
+ var APP_CONFIG_DIR_NAME = "cimplify";
202
+ var HOME_CONFIG_DIR_NAME = ".config";
203
+ var ENCODING_UTF8 = "utf8";
204
+ function authConfigDir() {
205
+ const xdg = process.env[ENV_XDG_CONFIG_HOME];
206
+ return xdg ? path.join(xdg, APP_CONFIG_DIR_NAME) : path.join(os.homedir(), HOME_CONFIG_DIR_NAME, APP_CONFIG_DIR_NAME);
207
+ }
208
+ function authConfigPath() {
209
+ return path.join(authConfigDir(), AUTH_FILE_NAME);
210
+ }
211
+ async function readJsonFile(filePath) {
212
+ try {
213
+ const raw = await promises.readFile(filePath, ENCODING_UTF8);
214
+ return JSON.parse(raw);
215
+ } catch (err) {
216
+ if (err.code === "ENOENT") return null;
217
+ throw err;
218
+ }
219
+ }
220
+ async function readAuth() {
221
+ const data = await readJsonFile(authConfigPath());
222
+ if (!data) {
223
+ throw new CliError(
224
+ CLI_ERROR_CODE.NOT_LOGGED_IN,
225
+ "Not logged in. Run `cimplify login` first."
226
+ );
227
+ }
228
+ return data;
229
+ }
230
+
231
+ // src/cli/output.ts
232
+ var RESET = "\x1B[0m";
233
+ var BOLD_OPEN = "\x1B[1m";
234
+ var DIM_OPEN = "\x1B[2m";
235
+ var GREEN_OPEN = "\x1B[32m";
236
+ var PREFIX_SUCCESS = "\u2713";
237
+ function wrap(open, value) {
238
+ return `${open}${value}${RESET}`;
239
+ }
240
+ function bold(value) {
241
+ return wrap(BOLD_OPEN, value);
242
+ }
243
+ function dim(value) {
244
+ return wrap(DIM_OPEN, value);
245
+ }
246
+ function green(value) {
247
+ return wrap(GREEN_OPEN, value);
248
+ }
249
+ function success(message) {
250
+ console.log(`${green(PREFIX_SUCCESS)} ${message}`);
251
+ }
252
+ function info(message) {
253
+ console.log(message);
254
+ }
255
+
256
+ // src/cli/commands/projects.ts
257
+ var SUB_LS = "ls";
258
+ var SUB_LIST = "list";
259
+ var SUB_CREATE = "create";
260
+ var PROJECT_TYPE_DEFAULT = "nextjs-app";
261
+ var COL_GAP = " ";
262
+ function projectsEndpoint(businessId) {
263
+ return `/v1/businesses/${encodeURIComponent(businessId)}/projects`;
264
+ }
265
+ async function run(argv) {
266
+ const args = parseArgs(argv);
267
+ const sub = args.positional[0];
268
+ if (!sub) {
269
+ throw new CliError(
270
+ CLI_ERROR_CODE.INVALID_INPUT,
271
+ `Usage: cimplify projects <${SUB_LS}|${SUB_CREATE}>`
272
+ );
273
+ }
274
+ const auth = await readAuth();
275
+ const client = ApiClient.fromAuth(auth);
276
+ if (sub === SUB_LS || sub === SUB_LIST) {
277
+ await listProjects(client, auth.businessId);
278
+ return;
279
+ }
280
+ if (sub === SUB_CREATE) {
281
+ const name = args.positional[1];
282
+ if (!name) {
283
+ throw new CliError(
284
+ CLI_ERROR_CODE.INVALID_INPUT,
285
+ `Usage: cimplify projects ${SUB_CREATE} <name>`
286
+ );
287
+ }
288
+ await createProject(client, auth.businessId, name);
289
+ return;
290
+ }
291
+ throw new CliError(CLI_ERROR_CODE.INVALID_INPUT, `Unknown subcommand "${sub}"`);
292
+ }
293
+ async function listProjects(client, businessId) {
294
+ const projects = await client.get(projectsEndpoint(businessId));
295
+ if (projects.length === 0) {
296
+ info(dim("No projects yet. Create one: `cimplify projects create <name>`"));
297
+ return;
298
+ }
299
+ const idWidth = Math.max(...projects.map((p) => p.id.length));
300
+ const nameWidth = Math.max(...projects.map((p) => p.name.length));
301
+ info(
302
+ bold(
303
+ [
304
+ "ID".padEnd(idWidth),
305
+ "NAME".padEnd(nameWidth),
306
+ "TYPE"
307
+ ].join(COL_GAP)
308
+ )
309
+ );
310
+ for (const p of projects) {
311
+ info(
312
+ [
313
+ p.id.padEnd(idWidth),
314
+ p.name.padEnd(nameWidth),
315
+ p.project_type ?? ""
316
+ ].join(COL_GAP)
317
+ );
318
+ }
319
+ }
320
+ async function createProject(client, businessId, name) {
321
+ const body = { name, project_type: PROJECT_TYPE_DEFAULT };
322
+ const project = await client.post(projectsEndpoint(businessId), body);
323
+ success(`Created project ${project.name} (${project.id})`);
324
+ info(dim(`Link it: cimplify link ${project.id}`));
325
+ }
326
+
327
+ export { run as default };