buncargo 1.0.29 → 3.2.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 (246) hide show
  1. package/dist/bin.d.ts +1 -12
  2. package/dist/bin.js +261 -253
  3. package/dist/cli/bin.d.ts +13 -0
  4. package/dist/cli/bin.js +317 -0
  5. package/dist/cli/commands/help.d.ts +1 -0
  6. package/dist/cli/commands/runtime.d.ts +5 -0
  7. package/dist/cli/commands/version.d.ts +1 -0
  8. package/dist/cli/index.d.ts +1 -0
  9. package/dist/cli/index.js +14 -0
  10. package/dist/cli/run-cli.d.ts +30 -0
  11. package/dist/cli.d.ts +1 -22
  12. package/dist/cli.js +5 -13
  13. package/dist/config/config.d.ts +1 -0
  14. package/dist/config/define-config.d.ts +13 -0
  15. package/dist/config/index.d.ts +3 -0
  16. package/dist/config/index.js +15 -0
  17. package/dist/config/merge-configs.d.ts +3 -0
  18. package/dist/config/validate-config.d.ts +3 -0
  19. package/dist/config.d.ts +1 -72
  20. package/dist/config.js +12 -12
  21. package/dist/core/docker.d.ts +1 -83
  22. package/dist/core/docker.js +35 -32
  23. package/dist/core/index.d.ts +1 -1
  24. package/dist/core/index.js +123 -118
  25. package/dist/core/network.js +2 -2
  26. package/dist/core/ports.js +1 -1
  27. package/dist/core/process.js +1 -1
  28. package/dist/core/quick-tunnel/cloudflared-process.d.ts +10 -0
  29. package/dist/core/quick-tunnel/constants.d.ts +9 -0
  30. package/dist/core/quick-tunnel/index.d.ts +17 -0
  31. package/dist/core/quick-tunnel/install.d.ts +1 -0
  32. package/dist/core/tunnel.d.ts +34 -0
  33. package/dist/core/utils.js +2 -2
  34. package/dist/core/watchdog-runner.js +45 -42
  35. package/dist/core/watchdog.d.ts +1 -0
  36. package/dist/core/watchdog.js +4 -2
  37. package/dist/docker/index.d.ts +1 -0
  38. package/dist/docker/index.js +38 -0
  39. package/dist/docker/runtime.d.ts +87 -0
  40. package/dist/docker/runtime.js +37 -0
  41. package/dist/docker-compose/compose.d.ts +1 -0
  42. package/dist/docker-compose/generated-file.d.ts +7 -0
  43. package/dist/docker-compose/index.d.ts +3 -0
  44. package/dist/docker-compose/index.js +15 -0
  45. package/dist/docker-compose/model.d.ts +6 -0
  46. package/dist/docker-compose/services/clickhouse.d.ts +16 -0
  47. package/dist/docker-compose/services/define-docker-service.d.ts +41 -0
  48. package/dist/docker-compose/services/index.d.ts +23 -0
  49. package/dist/docker-compose/services/index.js +17 -0
  50. package/dist/docker-compose/services/postgres.d.ts +12 -0
  51. package/dist/docker-compose/services/redis.d.ts +12 -0
  52. package/dist/docker-compose/services/shared.d.ts +7 -0
  53. package/dist/docker-compose/yaml.d.ts +2 -0
  54. package/dist/environment/create-dev-environment.d.ts +23 -0
  55. package/dist/environment/index.d.ts +1 -0
  56. package/dist/environment/index.js +15 -0
  57. package/dist/environment/logging.d.ts +17 -0
  58. package/dist/environment/only-apps.d.ts +10 -0
  59. package/dist/environment/seeding.d.ts +9 -0
  60. package/dist/environment.d.ts +1 -23
  61. package/dist/environment.js +12 -14
  62. package/dist/index-045jksh5.js +147 -0
  63. package/dist/index-08wa79cs.js +125 -117
  64. package/dist/index-0kxnae3z.js +335 -0
  65. package/dist/index-1mdrf7nz.js +51 -43
  66. package/dist/index-1yvbwj4k.js +262 -242
  67. package/dist/index-23ev345g.js +475 -0
  68. package/dist/index-2ckr49sf.js +228 -0
  69. package/dist/index-2f47khe5.js +376 -369
  70. package/dist/index-2fr3g85b.js +220 -183
  71. package/dist/index-38xnzpa6.js +450 -0
  72. package/dist/index-3eyrdxw9.js +577 -0
  73. package/dist/index-3h3dhtf2.js +51 -43
  74. package/dist/index-42x95209.js +51 -43
  75. package/dist/index-4gp0az1g.js +145 -0
  76. package/dist/index-4xrxh8yv.js +72 -0
  77. package/dist/index-5aq985p4.js +250 -0
  78. package/dist/index-5gmws6ah.js +181 -0
  79. package/dist/index-5hka0tff.js +78 -76
  80. package/dist/index-5rfqps4b.js +3 -0
  81. package/dist/index-5t9jxqm0.js +428 -0
  82. package/dist/index-6c1w1xk5.js +101 -0
  83. package/dist/index-6cmex7m5.js +72 -0
  84. package/dist/index-6d6x175r.js +572 -0
  85. package/dist/index-6fm7mvwj.js +118 -97
  86. package/dist/index-6srpc523.js +127 -128
  87. package/dist/index-731rzzfp.js +157 -142
  88. package/dist/index-75y4cg2z.js +51 -43
  89. package/dist/index-7ja4ywyj.js +126 -127
  90. package/dist/index-7v19es2e.js +666 -0
  91. package/dist/index-8bw1cmz4.js +531 -0
  92. package/dist/index-8hbbj1mp.js +120 -121
  93. package/dist/index-8xj2p5n5.js +118 -97
  94. package/dist/index-9wyhzw0h.js +574 -0
  95. package/dist/index-ag90ry8t.js +576 -0
  96. package/dist/index-bj79tw5w.js +0 -0
  97. package/dist/index-bnk6nr0g.js +73 -0
  98. package/dist/index-brbbzyks.js +72 -0
  99. package/dist/index-byeqyjrz.js +72 -0
  100. package/dist/index-c0dr6mcv.js +123 -0
  101. package/dist/index-cty0bcry.js +235 -218
  102. package/dist/index-d8tyv5se.js +228 -0
  103. package/dist/index-d9efy0n4.js +176 -150
  104. package/dist/index-enj4zdma.js +574 -0
  105. package/dist/index-etfmqjjf.js +427 -0
  106. package/dist/index-fb29934k.js +172 -0
  107. package/dist/index-g50jw1yf.js +72 -0
  108. package/dist/index-g6eb5wdw.js +118 -117
  109. package/dist/index-ggq3yryx.js +99 -95
  110. package/dist/index-h70tce00.js +177 -0
  111. package/dist/index-hkxtfqtc.js +333 -0
  112. package/dist/index-k370bech.js +72 -0
  113. package/dist/index-kf3dhser.js +146 -143
  114. package/dist/index-ma6tgdb2.js +500 -0
  115. package/dist/index-mam0bcyz.js +123 -0
  116. package/dist/index-mm412dkp.js +274 -0
  117. package/dist/index-n8v18aeb.js +0 -0
  118. package/dist/index-ndnmnsej.js +378 -371
  119. package/dist/index-p8wty0e2.js +389 -379
  120. package/dist/index-qa8akv6y.js +666 -0
  121. package/dist/index-qfphr2fd.js +78 -76
  122. package/dist/index-qqmms8rs.js +51 -43
  123. package/dist/index-qw4093g2.js +51 -43
  124. package/dist/index-qzwpzjbx.js +121 -122
  125. package/dist/index-segbnm0h.js +146 -143
  126. package/dist/index-t0fj6gg1.js +112 -0
  127. package/dist/index-thdkwnv7.js +122 -0
  128. package/dist/index-tjbx2r2t.js +270 -0
  129. package/dist/index-tjqw9vtj.js +62 -54
  130. package/dist/index-vbpb89jy.js +248 -0
  131. package/dist/index-vg55rq0y.js +250 -0
  132. package/dist/index-vhs88xhe.js +99 -95
  133. package/dist/index-vs81yaks.js +244 -0
  134. package/dist/index-w8zxnjka.js +249 -0
  135. package/dist/index-wk2na3t9.js +385 -375
  136. package/dist/index-wz9x8g7z.js +383 -373
  137. package/dist/index-x249gyde.js +388 -378
  138. package/dist/index-x54nbgs7.js +355 -0
  139. package/dist/index-xkvd0nsd.js +187 -0
  140. package/dist/index-yedqxm1z.js +80 -0
  141. package/dist/index-yz4jfz7z.js +338 -0
  142. package/dist/index-zfjzzjkf.js +240 -199
  143. package/dist/index.d.ts +12 -8
  144. package/dist/index.js +56 -34
  145. package/dist/lint.d.ts +1 -46
  146. package/dist/lint.js +3 -7
  147. package/dist/loader/cache.d.ts +4 -0
  148. package/dist/loader/find-config-file.d.ts +2 -0
  149. package/dist/loader/index.d.ts +5 -0
  150. package/dist/loader/index.js +24 -0
  151. package/dist/loader/load-dev-env.d.ts +5 -0
  152. package/dist/loader/loader.d.ts +1 -0
  153. package/dist/loader.d.ts +1 -45
  154. package/dist/loader.js +22 -20
  155. package/dist/prisma/index.d.ts +1 -0
  156. package/dist/prisma/prisma.d.ts +29 -0
  157. package/dist/prisma.d.ts +1 -29
  158. package/dist/prisma.js +6 -10
  159. package/dist/src/bin.js +309 -0
  160. package/dist/src/cli.js +5 -0
  161. package/dist/src/config.js +15 -0
  162. package/dist/src/core/docker.js +38 -0
  163. package/dist/src/core/index.js +130 -0
  164. package/dist/src/core/network.js +9 -0
  165. package/dist/src/core/ports.js +23 -0
  166. package/dist/src/core/process.js +31 -0
  167. package/dist/src/core/utils.js +11 -0
  168. package/dist/src/core/watchdog-runner.js +69 -0
  169. package/dist/src/core/watchdog.js +28 -0
  170. package/dist/src/docker/runtime.js +37 -0
  171. package/dist/src/docker-compose/index.js +16 -0
  172. package/dist/src/docker-compose/services/index.js +17 -0
  173. package/dist/src/environment.js +12 -0
  174. package/dist/src/index.js +122 -0
  175. package/dist/src/lint.js +3 -0
  176. package/dist/src/loader.js +25 -0
  177. package/dist/src/prisma.js +6 -0
  178. package/dist/src/types.js +0 -0
  179. package/dist/typecheck/index.d.ts +1 -0
  180. package/dist/typecheck/index.js +7 -0
  181. package/dist/typecheck/typecheck.d.ts +46 -0
  182. package/dist/types/all-types.d.ts +544 -0
  183. package/dist/types/cli.d.ts +1 -0
  184. package/dist/types/config.d.ts +6 -0
  185. package/dist/types/docker.d.ts +15 -0
  186. package/dist/types/environment.d.ts +8 -0
  187. package/dist/types/hooks.d.ts +9 -0
  188. package/dist/types/index.d.ts +1 -0
  189. package/dist/types/index.js +0 -0
  190. package/dist/types/prisma.d.ts +1 -0
  191. package/dist/types.d.ts +1 -399
  192. package/package.json +55 -48
  193. package/readme.md +365 -109
  194. package/src/cli/bin.ts +77 -0
  195. package/src/cli/commands/help.ts +39 -0
  196. package/src/cli/commands/runtime.ts +72 -0
  197. package/src/cli/commands/version.ts +4 -0
  198. package/src/cli/index.ts +1 -0
  199. package/{cli.ts → src/cli/run-cli.ts} +114 -10
  200. package/src/config/define-config.ts +30 -0
  201. package/src/config/index.ts +3 -0
  202. package/src/config/merge-configs.ts +33 -0
  203. package/src/config/validate-config.ts +136 -0
  204. package/{core → src/core}/index.ts +2 -2
  205. package/{core → src/core}/ports.ts +5 -2
  206. package/{core → src/core}/process.ts +6 -2
  207. package/src/core/quick-tunnel/cloudflared-process.ts +83 -0
  208. package/src/core/quick-tunnel/constants.ts +31 -0
  209. package/src/core/quick-tunnel/index.ts +96 -0
  210. package/src/core/quick-tunnel/install.ts +160 -0
  211. package/src/core/tunnel.ts +165 -0
  212. package/{core → src/core}/utils.ts +1 -0
  213. package/{core → src/core}/watchdog.ts +5 -1
  214. package/src/docker/index.ts +1 -0
  215. package/{core/docker.ts → src/docker/runtime.ts} +11 -4
  216. package/src/docker-compose/generated-file.ts +45 -0
  217. package/src/docker-compose/index.ts +7 -0
  218. package/src/docker-compose/model.ts +197 -0
  219. package/src/docker-compose/services/clickhouse.ts +79 -0
  220. package/src/docker-compose/services/define-docker-service.ts +109 -0
  221. package/src/docker-compose/services/index.ts +67 -0
  222. package/src/docker-compose/services/postgres.ts +60 -0
  223. package/src/docker-compose/services/redis.ts +48 -0
  224. package/src/docker-compose/services/shared.ts +79 -0
  225. package/src/docker-compose/yaml.ts +88 -0
  226. package/{environment.ts → src/environment/create-dev-environment.ts} +214 -141
  227. package/src/environment/index.ts +1 -0
  228. package/src/environment/logging.ts +115 -0
  229. package/src/environment/only-apps.ts +34 -0
  230. package/src/environment/seeding.ts +57 -0
  231. package/{index.ts → src/index.ts} +52 -20
  232. package/src/loader/cache.ts +23 -0
  233. package/src/loader/find-config-file.ts +29 -0
  234. package/src/loader/index.ts +17 -0
  235. package/src/loader/load-dev-env.ts +38 -0
  236. package/src/prisma/index.ts +1 -0
  237. package/{prisma.ts → src/prisma/prisma.ts} +4 -2
  238. package/src/typecheck/index.ts +1 -0
  239. package/{types.ts → src/types/all-types.ts} +186 -8
  240. package/src/types/index.ts +1 -0
  241. package/bin.ts +0 -192
  242. package/config.ts +0 -194
  243. package/loader.ts +0 -126
  244. /package/{core → src/core}/network.ts +0 -0
  245. /package/{core → src/core}/watchdog-runner.ts +0 -0
  246. /package/{lint.ts → src/typecheck/typecheck.ts} +0 -0
@@ -0,0 +1,355 @@
1
+ // src/core/quick-tunnel/index.ts
2
+ import { existsSync } from "node:fs";
3
+ import { createInterface } from "node:readline";
4
+
5
+ // src/core/quick-tunnel/cloudflared-process.ts
6
+ import { spawn } from "node:child_process";
7
+
8
+ // src/core/quick-tunnel/constants.ts
9
+ import { tmpdir } from "node:os";
10
+ import path from "node:path";
11
+ var CLOUDFLARED_VERSION = process.env.CLOUDFLARED_VERSION || "2023.10.0";
12
+ var RELEASE_BASE = "https://github.com/cloudflare/cloudflared/releases/";
13
+ var cloudflaredBinPath = path.join(tmpdir(), "buncargo-cloudflared", process.platform === "win32" ? `cloudflared.${CLOUDFLARED_VERSION}.exe` : `cloudflared.${CLOUDFLARED_VERSION}`);
14
+ var cloudflaredNotice = `
15
+ \uD83D\uDD25 Your installation of cloudflared software constitutes a symbol of your signature
16
+ indicating that you accept the terms of the Cloudflare License, Terms and Privacy Policy.
17
+
18
+ ❯ License: \`https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/downloads/license/\`
19
+ ❯ Terms: \`https://www.cloudflare.com/terms/\`
20
+ ❯ Privacy Policy: \`https://www.cloudflare.com/privacypolicy/\`
21
+ `;
22
+
23
+ // src/core/quick-tunnel/cloudflared-process.ts
24
+ var urlRegex = /\|\s+(https?:\/\/\S+)/;
25
+ function startCloudflaredTunnel(options) {
26
+ const args = ["tunnel"];
27
+ for (const [key, value] of Object.entries(options)) {
28
+ if (typeof value === "string") {
29
+ args.push(`${key}`, value);
30
+ } else if (typeof value === "number") {
31
+ args.push(`${key}`, value.toString());
32
+ } else if (value === null) {
33
+ args.push(`${key}`);
34
+ }
35
+ }
36
+ if (args.length === 1) {
37
+ args.push("--url", "localhost:8080");
38
+ }
39
+ const child = spawn(cloudflaredBinPath, args, {
40
+ stdio: ["ignore", "pipe", "pipe"]
41
+ });
42
+ if (process.env.DEBUG) {
43
+ child.stdout?.pipe(process.stdout);
44
+ child.stderr?.pipe(process.stderr);
45
+ }
46
+ let settled = false;
47
+ let urlResolver;
48
+ let urlRejector;
49
+ const url = new Promise((resolve, reject) => {
50
+ urlResolver = (v) => {
51
+ if (!settled) {
52
+ settled = true;
53
+ resolve(v);
54
+ }
55
+ };
56
+ urlRejector = (e) => {
57
+ if (!settled) {
58
+ settled = true;
59
+ reject(e);
60
+ }
61
+ };
62
+ });
63
+ const parser = (data) => {
64
+ const str = data.toString();
65
+ const urlMatch = str.match(urlRegex);
66
+ if (urlMatch) {
67
+ urlResolver(urlMatch[1] ?? "");
68
+ }
69
+ };
70
+ child.stdout?.on("data", parser).on("error", urlRejector);
71
+ child.stderr?.on("data", parser).on("error", urlRejector);
72
+ child.on("exit", (code, signal) => {
73
+ if (!settled) {
74
+ urlRejector(new Error(`cloudflared exited before a tunnel URL was parsed (code=${code}, signal=${signal ?? "none"})`));
75
+ }
76
+ });
77
+ child.on("error", urlRejector);
78
+ const stop = () => child.kill("SIGINT");
79
+ return { url, child, stop };
80
+ }
81
+
82
+ // src/core/quick-tunnel/install.ts
83
+ import { execSync } from "node:child_process";
84
+ import fs from "node:fs";
85
+ import https from "node:https";
86
+ import path2 from "node:path";
87
+ var LINUX_URL = {
88
+ arm64: "cloudflared-linux-arm64",
89
+ arm: "cloudflared-linux-arm",
90
+ x64: "cloudflared-linux-amd64",
91
+ ia32: "cloudflared-linux-386"
92
+ };
93
+ var MACOS_URL = {
94
+ arm64: "cloudflared-darwin-amd64.tgz",
95
+ x64: "cloudflared-darwin-amd64.tgz"
96
+ };
97
+ var WINDOWS_URL = {
98
+ x64: "cloudflared-windows-amd64.exe",
99
+ ia32: "cloudflared-windows-386.exe"
100
+ };
101
+ function resolveBase(version) {
102
+ if (version === "latest") {
103
+ return `${RELEASE_BASE}latest/download/`;
104
+ }
105
+ return `${RELEASE_BASE}download/${version}/`;
106
+ }
107
+ function installCloudflared(to = cloudflaredBinPath, version = CLOUDFLARED_VERSION) {
108
+ switch (process.platform) {
109
+ case "linux": {
110
+ return installLinux(to, version);
111
+ }
112
+ case "darwin": {
113
+ return installMacos(to, version);
114
+ }
115
+ case "win32": {
116
+ return installWindows(to, version);
117
+ }
118
+ default: {
119
+ throw new Error(`Unsupported platform: ${process.platform}`);
120
+ }
121
+ }
122
+ }
123
+ async function installLinux(to, version = CLOUDFLARED_VERSION) {
124
+ const file = LINUX_URL[process.arch];
125
+ if (file === undefined) {
126
+ throw new Error(`Unsupported architecture: ${process.arch}`);
127
+ }
128
+ await download(resolveBase(version) + file, to);
129
+ fs.chmodSync(to, 493);
130
+ return to;
131
+ }
132
+ async function installMacos(to, version = CLOUDFLARED_VERSION) {
133
+ const file = MACOS_URL[process.arch];
134
+ if (file === undefined) {
135
+ throw new Error(`Unsupported architecture: ${process.arch}`);
136
+ }
137
+ await download(resolveBase(version) + file, `${to}.tgz`);
138
+ if (process.env.DEBUG) {
139
+ console.log(`Extracting to ${to}`);
140
+ }
141
+ execSync(`tar -xzf ${path2.basename(`${to}.tgz`)}`, {
142
+ cwd: path2.dirname(to)
143
+ });
144
+ fs.unlinkSync(`${to}.tgz`);
145
+ fs.renameSync(`${path2.dirname(to)}/cloudflared`, to);
146
+ return to;
147
+ }
148
+ async function installWindows(to, version = CLOUDFLARED_VERSION) {
149
+ const file = WINDOWS_URL[process.arch];
150
+ if (file === undefined) {
151
+ throw new Error(`Unsupported architecture: ${process.arch}`);
152
+ }
153
+ await download(resolveBase(version) + file, to);
154
+ return to;
155
+ }
156
+ function download(url, to, redirect = 0) {
157
+ if (redirect === 0) {
158
+ if (process.env.DEBUG) {
159
+ console.log(`Downloading ${url} to ${to}`);
160
+ }
161
+ } else if (process.env.DEBUG) {
162
+ console.log(`Redirecting to ${url}`);
163
+ }
164
+ return new Promise((resolve, reject) => {
165
+ if (!fs.existsSync(path2.dirname(to))) {
166
+ fs.mkdirSync(path2.dirname(to), { recursive: true });
167
+ }
168
+ let done = true;
169
+ const file = fs.createWriteStream(to);
170
+ const request = https.get(url, (res) => {
171
+ if (res.statusCode === 302 && res.headers.location !== undefined) {
172
+ const redirection = res.headers.location;
173
+ done = false;
174
+ file.close(() => {
175
+ download(redirection, to, redirect + 1).then(resolve, reject);
176
+ });
177
+ return;
178
+ }
179
+ res.pipe(file);
180
+ });
181
+ file.on("finish", () => {
182
+ if (done) {
183
+ file.close(() => {
184
+ resolve(to);
185
+ });
186
+ }
187
+ });
188
+ request.on("error", (err) => {
189
+ fs.unlink(to, () => {
190
+ reject(err);
191
+ });
192
+ });
193
+ file.on("error", (err) => {
194
+ fs.unlink(to, () => {
195
+ reject(err);
196
+ });
197
+ });
198
+ request.end();
199
+ });
200
+ }
201
+
202
+ // src/core/quick-tunnel/index.ts
203
+ function resolvedLocalUrl(opts) {
204
+ return opts.url ?? `${opts.protocol || "http"}://${opts.hostname ?? "localhost"}:${opts.port ?? 3000}`;
205
+ }
206
+ function envAcceptsCloudflareNotice() {
207
+ const v = process.env.BUNCARGO_ACCEPT_CLOUDFLARE_NOTICE;
208
+ const u = process.env.UNTUN_ACCEPT_CLOUDFLARE_NOTICE;
209
+ return v === "1" || v === "true" || u === "1" || u === "true";
210
+ }
211
+ async function promptInstallCloudflared() {
212
+ if (!process.stdin.isTTY || !process.stdout.isTTY) {
213
+ return false;
214
+ }
215
+ return new Promise((resolve) => {
216
+ const rl = createInterface({
217
+ input: process.stdin,
218
+ output: process.stdout
219
+ });
220
+ rl.question("Do you agree with the above terms and wish to install the binary from GitHub? (y/N) ", (answer) => {
221
+ rl.close();
222
+ resolve(/^y(es)?$/i.test(answer.trim()));
223
+ });
224
+ });
225
+ }
226
+ async function startQuickTunnel(opts) {
227
+ const url = resolvedLocalUrl(opts);
228
+ console.log(`Starting cloudflared tunnel to ${url}`);
229
+ if (!existsSync(cloudflaredBinPath)) {
230
+ console.log(cloudflaredNotice);
231
+ const canInstall = opts.acceptCloudflareNotice || envAcceptsCloudflareNotice() || await promptInstallCloudflared();
232
+ if (!canInstall) {
233
+ console.error("Skipping tunnel setup.");
234
+ return;
235
+ }
236
+ await installCloudflared();
237
+ }
238
+ const cfArgs = { "--url": url };
239
+ if (!opts.verifyTLS) {
240
+ cfArgs["--no-tls-verify"] = null;
241
+ }
242
+ const tunnel = startCloudflaredTunnel(cfArgs);
243
+ const cleanup = async () => {
244
+ tunnel.stop();
245
+ };
246
+ return {
247
+ getURL: async () => await tunnel.url,
248
+ close: cleanup
249
+ };
250
+ }
251
+
252
+ // src/core/tunnel.ts
253
+ function parseExposeNames(exposeValue) {
254
+ if (exposeValue === undefined)
255
+ return null;
256
+ const names = exposeValue.split(",").map((name) => name.trim()).filter(Boolean);
257
+ return new Set(names);
258
+ }
259
+ async function resolvePublicUrl(tunnel) {
260
+ if (typeof tunnel.getURL === "function") {
261
+ return await tunnel.getURL();
262
+ }
263
+ return tunnel.url ?? tunnel.publicUrl ?? tunnel.tunnelUrl ?? null;
264
+ }
265
+ function toCloseFn(tunnel) {
266
+ const close = tunnel.close ?? tunnel.stop ?? tunnel.destroy;
267
+ if (!close)
268
+ return async () => {};
269
+ return async () => {
270
+ await close();
271
+ };
272
+ }
273
+ function resolveExposeTargets(env, exposeValue) {
274
+ const requestedNames = parseExposeNames(exposeValue);
275
+ const knownTargets = new Map;
276
+ const enabledTargets = new Map;
277
+ for (const [name, config] of Object.entries(env.services)) {
278
+ const port = env.ports[name];
279
+ if (port === undefined)
280
+ continue;
281
+ const target = { kind: "service", name, port };
282
+ knownTargets.set(name, target);
283
+ if (config.expose === true) {
284
+ enabledTargets.set(name, target);
285
+ }
286
+ }
287
+ for (const [name, config] of Object.entries(env.apps)) {
288
+ const port = env.ports[name];
289
+ if (port === undefined)
290
+ continue;
291
+ const target = { kind: "app", name, port };
292
+ knownTargets.set(name, target);
293
+ if (config.expose === true) {
294
+ enabledTargets.set(name, target);
295
+ }
296
+ }
297
+ if (requestedNames === null) {
298
+ return {
299
+ targets: Array.from(enabledTargets.values()),
300
+ unknownNames: [],
301
+ notEnabledNames: []
302
+ };
303
+ }
304
+ const unknownNames = [];
305
+ const notEnabledNames = [];
306
+ const targets = [];
307
+ for (const name of requestedNames) {
308
+ if (!knownTargets.has(name)) {
309
+ unknownNames.push(name);
310
+ continue;
311
+ }
312
+ const enabledTarget = enabledTargets.get(name);
313
+ if (!enabledTarget) {
314
+ notEnabledNames.push(name);
315
+ continue;
316
+ }
317
+ targets.push(enabledTarget);
318
+ }
319
+ return { targets, unknownNames, notEnabledNames };
320
+ }
321
+ async function startPublicTunnels(targets, options = {}) {
322
+ const start = options.start ?? ((input) => startQuickTunnel(input));
323
+ const tunnels = [];
324
+ try {
325
+ for (const target of targets) {
326
+ const localUrl = `http://localhost:${target.port}`;
327
+ const tunnel = await start({
328
+ url: localUrl
329
+ });
330
+ if (tunnel === undefined) {
331
+ throw new Error(`Tunnel for "${target.name}" could not be started (cloudflared missing or install declined)`);
332
+ }
333
+ const publicUrl = await resolvePublicUrl(tunnel);
334
+ if (!publicUrl) {
335
+ throw new Error(`Tunnel for "${target.name}" did not provide a public URL`);
336
+ }
337
+ tunnels.push({
338
+ kind: target.kind,
339
+ name: target.name,
340
+ localUrl,
341
+ publicUrl,
342
+ close: toCloseFn(tunnel)
343
+ });
344
+ }
345
+ return tunnels;
346
+ } catch (error) {
347
+ await stopPublicTunnels(tunnels);
348
+ throw error;
349
+ }
350
+ }
351
+ async function stopPublicTunnels(tunnels) {
352
+ await Promise.allSettled(tunnels.map((tunnel) => tunnel.close()));
353
+ }
354
+
355
+ export { resolveExposeTargets, startPublicTunnels, stopPublicTunnels };
@@ -0,0 +1,187 @@
1
+ // src/core/ports.ts
2
+ import { existsSync, readFileSync, statSync } from "node:fs";
3
+ import { basename, dirname, resolve } from "node:path";
4
+
5
+ function findMonorepoRoot(startDir) {
6
+ let dir = startDir ?? process.cwd();
7
+ while (dir !== "/") {
8
+ try {
9
+ const pkgPath = resolve(dir, "package.json");
10
+ if (existsSync(pkgPath)) {
11
+ const content = readFileSync(pkgPath, "utf-8");
12
+ const pkg = JSON.parse(content);
13
+ if (pkg.workspaces) {
14
+ return dir;
15
+ }
16
+ }
17
+ } catch {}
18
+ dir = dirname(dir);
19
+ }
20
+ return process.cwd();
21
+ }
22
+ function getWorktreeName(root) {
23
+ const monorepoRoot = root ?? findMonorepoRoot();
24
+ const gitPath = resolve(monorepoRoot, ".git");
25
+ try {
26
+ if (!existsSync(gitPath) || !statSync(gitPath).isFile()) return null;
27
+ const content = readFileSync(gitPath, "utf-8").trim();
28
+ const match = content.match(/^gitdir:\s*(.+)$/);
29
+ if (!match?.[1]) return null;
30
+ return basename(match[1]);
31
+ } catch {
32
+ return null;
33
+ }
34
+ }
35
+ function isWorktree(root) {
36
+ return getWorktreeName(root) !== null;
37
+ }
38
+ function sanitizeProjectSuffix(value) {
39
+ return value
40
+ .toLowerCase()
41
+ .replace(/[^a-z0-9-]/g, "-")
42
+ .replace(/-+/g, "-")
43
+ .replace(/^-+|-+$/g, "");
44
+ }
45
+ function getWorktreeProjectSuffix(root) {
46
+ const worktreeName = getWorktreeName(root);
47
+ if (!worktreeName) return null;
48
+ const sanitized = sanitizeProjectSuffix(worktreeName);
49
+ return sanitized || "worktree";
50
+ }
51
+ function simpleHash(str) {
52
+ let hash = 0;
53
+ for (let i = 0; i < str.length; i++) {
54
+ const char = str.charCodeAt(i);
55
+ hash = (hash << 5) - hash + char;
56
+ hash = hash & hash;
57
+ }
58
+ return Math.abs(hash);
59
+ }
60
+ function calculatePortOffset(suffix, root) {
61
+ const worktreeName = getWorktreeName(root);
62
+ if (!worktreeName) return 0;
63
+ const hashInput = suffix ? `${worktreeName}-${suffix}` : worktreeName;
64
+ return 10 + (simpleHash(hashInput) % 90);
65
+ }
66
+ function getProjectName(prefix, suffix, root) {
67
+ const monorepoRoot = root ?? findMonorepoRoot();
68
+ const dirName = basename(monorepoRoot);
69
+ const baseName = `${prefix}-${dirName.toLowerCase().replace(/[^a-z0-9-]/g, "-")}`;
70
+ return suffix ? `${baseName}-${suffix}` : baseName;
71
+ }
72
+ function computeDevIdentity(options) {
73
+ const {
74
+ projectPrefix,
75
+ suffix,
76
+ root: providedRoot,
77
+ worktreeIsolation = true,
78
+ } = options;
79
+ const root = providedRoot ?? findMonorepoRoot();
80
+ const worktree = isWorktree(root);
81
+ const worktreeSuffix =
82
+ worktree && worktreeIsolation ? getWorktreeProjectSuffix(root) : null;
83
+ const projectSuffix =
84
+ [suffix, worktreeSuffix].filter(Boolean).join("-") || undefined;
85
+ const projectName = getProjectName(projectPrefix, projectSuffix, root);
86
+ const portOffset = calculatePortOffset(suffix, root);
87
+ return {
88
+ worktree,
89
+ worktreeSuffix,
90
+ projectSuffix,
91
+ projectName,
92
+ portOffset,
93
+ };
94
+ }
95
+ function computePorts(services, apps, offset) {
96
+ const ports = {};
97
+ for (const [name, config] of Object.entries(services)) {
98
+ ports[name] = config.port + offset;
99
+ if (config.secondaryPort) {
100
+ ports[`${name}Secondary`] = config.secondaryPort + offset;
101
+ }
102
+ }
103
+ if (apps) {
104
+ for (const [name, config] of Object.entries(apps)) {
105
+ ports[name] = config.port + offset;
106
+ }
107
+ }
108
+ return ports;
109
+ }
110
+ var SERVICE_DEFAULTS = {
111
+ postgres: { user: "postgres", password: "postgres", database: "postgres" },
112
+ postgresql: { user: "postgres", password: "postgres", database: "postgres" },
113
+ redis: { user: "", password: "", database: "" },
114
+ clickhouse: { user: "default", password: "clickhouse", database: "default" },
115
+ mysql: { user: "root", password: "root", database: "mysql" },
116
+ mongodb: { user: "", password: "", database: "" },
117
+ };
118
+ function buildServiceUrl(serviceName, ctx, config) {
119
+ const defaults = SERVICE_DEFAULTS[serviceName];
120
+ if (!defaults && !config.database) return null;
121
+ const user = config.user ?? defaults?.user ?? "";
122
+ const password = config.password ?? defaults?.password ?? "";
123
+ const database = config.database ?? defaults?.database ?? "";
124
+ switch (serviceName) {
125
+ case "postgres":
126
+ case "postgresql":
127
+ return `postgresql://${user}:${password}@${ctx.host}:${ctx.port}/${database}`;
128
+ case "redis":
129
+ return `redis://${ctx.host}:${ctx.port}`;
130
+ case "clickhouse":
131
+ return `http://${user}:${password}@${ctx.host}:${ctx.port}/${database}`;
132
+ case "mysql":
133
+ return `mysql://${user}:${password}@${ctx.host}:${ctx.port}/${database}`;
134
+ case "mongodb":
135
+ return `mongodb://${ctx.host}:${ctx.port}/${database}`;
136
+ default:
137
+ return null;
138
+ }
139
+ }
140
+ function computeUrls(services, apps, ports, localIp) {
141
+ const urls = {};
142
+ const host = "localhost";
143
+ for (const [name, config] of Object.entries(services)) {
144
+ const port = ports[name];
145
+ const secondaryPort = ports[`${name}Secondary`];
146
+ if (port === undefined) continue;
147
+ const ctx = { port, secondaryPort, host, localIp };
148
+ if (config.urlTemplate) {
149
+ urls[name] = config.urlTemplate(ctx);
150
+ } else {
151
+ const builtUrl = buildServiceUrl(
152
+ name,
153
+ { port, host },
154
+ {
155
+ database: config.database,
156
+ user: config.user,
157
+ password: config.password,
158
+ },
159
+ );
160
+ if (builtUrl) {
161
+ urls[name] = builtUrl;
162
+ } else {
163
+ urls[name] = `http://${host}:${port}`;
164
+ }
165
+ }
166
+ }
167
+ if (apps) {
168
+ for (const [name, _config] of Object.entries(apps)) {
169
+ const port = ports[name];
170
+ urls[name] = `http://${host}:${port}`;
171
+ urls[`${name}Local`] = `http://${localIp}:${port}`;
172
+ }
173
+ }
174
+ return urls;
175
+ }
176
+
177
+ export {
178
+ findMonorepoRoot,
179
+ getWorktreeName,
180
+ isWorktree,
181
+ getWorktreeProjectSuffix,
182
+ calculatePortOffset,
183
+ getProjectName,
184
+ computeDevIdentity,
185
+ computePorts,
186
+ computeUrls,
187
+ };
@@ -0,0 +1,80 @@
1
+ import { createDevEnvironment } from "./index-38xnzpa6.js";
2
+
3
+ // src/loader/cache.ts
4
+ var cachedEnv = null;
5
+ function setCachedDevEnv(env) {
6
+ cachedEnv = env;
7
+ }
8
+ function getCachedDevEnv() {
9
+ return cachedEnv;
10
+ }
11
+ function clearDevEnvCache() {
12
+ cachedEnv = null;
13
+ }
14
+
15
+ // src/loader/find-config-file.ts
16
+ import { existsSync } from "node:fs";
17
+ import { dirname, join } from "node:path";
18
+
19
+ var CONFIG_FILES = [
20
+ "dev.config.ts",
21
+ "dev.config.js",
22
+ "dev-tools.config.ts",
23
+ "dev-tools.config.js",
24
+ ];
25
+ function findConfigFile(startDir) {
26
+ let currentDir = startDir;
27
+ while (true) {
28
+ for (const file of CONFIG_FILES) {
29
+ const configPath = join(currentDir, file);
30
+ if (existsSync(configPath)) {
31
+ return configPath;
32
+ }
33
+ }
34
+ const parentDir = dirname(currentDir);
35
+ if (parentDir === currentDir) {
36
+ return null;
37
+ }
38
+ currentDir = parentDir;
39
+ }
40
+ }
41
+ // src/loader/load-dev-env.ts
42
+ async function loadDevEnv(options) {
43
+ if (!options?.reload) {
44
+ const cached = getCachedDevEnv();
45
+ if (cached) return cached;
46
+ }
47
+ const cwd = options?.cwd ?? process.cwd();
48
+ const configPath = findConfigFile(cwd);
49
+ if (configPath) {
50
+ const mod = await import(configPath);
51
+ const config = mod.default;
52
+ if (!config?.projectPrefix || !config?.services) {
53
+ throw new Error(
54
+ `Invalid config in "${configPath}". Use defineDevConfig() and export as default.`,
55
+ );
56
+ }
57
+ const env = createDevEnvironment(config);
58
+ setCachedDevEnv(env);
59
+ return env;
60
+ }
61
+ throw new Error(
62
+ "No config file found. Create dev.config.ts with: export default defineDevConfig({ ... })",
63
+ );
64
+ }
65
+
66
+ // src/loader/index.ts
67
+ function getDevEnv() {
68
+ const env = getCachedDevEnv();
69
+ if (!env) {
70
+ throw new Error("Dev environment not loaded. Call loadDevEnv() first.");
71
+ }
72
+ return env;
73
+ }
74
+ export {
75
+ clearDevEnvCache,
76
+ CONFIG_FILES,
77
+ findConfigFile,
78
+ loadDevEnv,
79
+ getDevEnv,
80
+ };