@kb-labs/devkit 1.0.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 (295) hide show
  1. package/.cursorrules +32 -0
  2. package/.github/CODEOWNERS +2 -0
  3. package/.github/actions/setup-node-pnpm/action.yml +47 -0
  4. package/.github/workflow-templates/ci.yml +13 -0
  5. package/.github/workflow-templates/drift-check.yml +10 -0
  6. package/.github/workflow-templates/profiles-validate.yml +16 -0
  7. package/.github/workflow-templates/release.yml +8 -0
  8. package/.github/workflows/ci-reusable.yml +131 -0
  9. package/.github/workflows/drift-check-reusable.yml +23 -0
  10. package/.github/workflows/fixtures.yml +74 -0
  11. package/.github/workflows/profiles-validate-reusable.yml +67 -0
  12. package/.github/workflows/release-reusable.yml +50 -0
  13. package/.vscode/settings.json +23 -0
  14. package/AGENTS.md +130 -0
  15. package/LICENSE +21 -0
  16. package/README.md +1542 -0
  17. package/agents/devkit-maintainer/context.globs +15 -0
  18. package/agents/devkit-maintainer/permissions.yml +17 -0
  19. package/agents/devkit-maintainer/prompt.md +28 -0
  20. package/agents/devkit-maintainer/runbook.md +31 -0
  21. package/agents/docs-crafter/prompt.md +24 -0
  22. package/agents/docs-crafter/runbook.md +18 -0
  23. package/agents/release-manager/context.globs +7 -0
  24. package/agents/release-manager/prompt.md +27 -0
  25. package/agents/release-manager/runbook.md +17 -0
  26. package/agents/test-generator/context.globs +7 -0
  27. package/agents/test-generator/prompt.md +27 -0
  28. package/agents/test-generator/runbook.md +18 -0
  29. package/bin/devkit-architecture.mjs +1225 -0
  30. package/bin/devkit-build-order.mjs +500 -0
  31. package/bin/devkit-check-build-readiness.mjs +222 -0
  32. package/bin/devkit-check-commands.mjs +394 -0
  33. package/bin/devkit-check-configs.mjs +461 -0
  34. package/bin/devkit-check-deprecated.mjs +532 -0
  35. package/bin/devkit-check-duplicates.mjs +431 -0
  36. package/bin/devkit-check-exports.mjs +561 -0
  37. package/bin/devkit-check-imports.mjs +712 -0
  38. package/bin/devkit-check-paths.mjs +670 -0
  39. package/bin/devkit-check-scripts.mjs +335 -0
  40. package/bin/devkit-check-structure.mjs +495 -0
  41. package/bin/devkit-check-types.mjs +450 -0
  42. package/bin/devkit-ci.mjs +261 -0
  43. package/bin/devkit-core-gate.mjs +225 -0
  44. package/bin/devkit-fix-deps.mjs +1169 -0
  45. package/bin/devkit-freshness.mjs +199 -0
  46. package/bin/devkit-health.mjs +489 -0
  47. package/bin/devkit-migrate-configs.mjs +370 -0
  48. package/bin/devkit-paths.mjs +192 -0
  49. package/bin/devkit-stats.mjs +453 -0
  50. package/bin/devkit-sync.mjs +12 -0
  51. package/bin/devkit-tsup-external.mjs +148 -0
  52. package/bin/devkit-types-audit.mjs +615 -0
  53. package/bin/devkit-types-order.mjs +637 -0
  54. package/bin/devkit-validate-naming.mjs +203 -0
  55. package/bin/devkit-visualize.mjs +452 -0
  56. package/bin/kb-devkit-qa-history.mjs +453 -0
  57. package/bin/kb-devkit-qa.mjs +915 -0
  58. package/eslint/node.js +134 -0
  59. package/eslint/react.js +260 -0
  60. package/package.json +186 -0
  61. package/prettier/index.json +10 -0
  62. package/sync/index.mjs +693 -0
  63. package/templates/configs/README.md +306 -0
  64. package/templates/configs/eslint.config.js +27 -0
  65. package/templates/configs/package.json.bin +25 -0
  66. package/templates/configs/package.json.lib +30 -0
  67. package/templates/configs/tsconfig.build.json +15 -0
  68. package/templates/configs/tsconfig.json +9 -0
  69. package/templates/configs/tsup.config.bin.ts +34 -0
  70. package/templates/configs/tsup.config.cli.ts +41 -0
  71. package/templates/configs/tsup.config.dual.ts +46 -0
  72. package/templates/configs/tsup.config.ts +36 -0
  73. package/templates/package-json/README.md +290 -0
  74. package/templates/package-json/package.json.bin.template +40 -0
  75. package/templates/package-json/package.json.template +45 -0
  76. package/tsconfig/base.json +21 -0
  77. package/tsconfig/cli.json +12 -0
  78. package/tsconfig/dist/__tests__/cache.spec.d.ts +6 -0
  79. package/tsconfig/dist/__tests__/cache.spec.d.ts.map +1 -0
  80. package/tsconfig/dist/__tests__/cache.spec.js +85 -0
  81. package/tsconfig/dist/__tests__/cache.spec.js.map +1 -0
  82. package/tsconfig/dist/__tests__/fs-atomic.spec.d.ts +6 -0
  83. package/tsconfig/dist/__tests__/fs-atomic.spec.d.ts.map +1 -0
  84. package/tsconfig/dist/__tests__/fs-atomic.spec.js +153 -0
  85. package/tsconfig/dist/__tests__/fs-atomic.spec.js.map +1 -0
  86. package/tsconfig/dist/__tests__/init-workspace.spec.d.ts +6 -0
  87. package/tsconfig/dist/__tests__/init-workspace.spec.d.ts.map +1 -0
  88. package/tsconfig/dist/__tests__/init-workspace.spec.js +99 -0
  89. package/tsconfig/dist/__tests__/init-workspace.spec.js.map +1 -0
  90. package/tsconfig/dist/__tests__/kb-error.spec.d.ts +6 -0
  91. package/tsconfig/dist/__tests__/kb-error.spec.d.ts.map +1 -0
  92. package/tsconfig/dist/__tests__/kb-error.spec.js +190 -0
  93. package/tsconfig/dist/__tests__/kb-error.spec.js.map +1 -0
  94. package/tsconfig/dist/__tests__/preset-lockfile.spec.d.ts +6 -0
  95. package/tsconfig/dist/__tests__/preset-lockfile.spec.d.ts.map +1 -0
  96. package/tsconfig/dist/__tests__/preset-lockfile.spec.js +142 -0
  97. package/tsconfig/dist/__tests__/preset-lockfile.spec.js.map +1 -0
  98. package/tsconfig/dist/__tests__/product-config-profiles.spec.d.ts +6 -0
  99. package/tsconfig/dist/__tests__/product-config-profiles.spec.d.ts.map +1 -0
  100. package/tsconfig/dist/__tests__/product-config-profiles.spec.js +100 -0
  101. package/tsconfig/dist/__tests__/product-config-profiles.spec.js.map +1 -0
  102. package/tsconfig/dist/__tests__/product-config.spec.d.ts +6 -0
  103. package/tsconfig/dist/__tests__/product-config.spec.d.ts.map +1 -0
  104. package/tsconfig/dist/__tests__/product-config.spec.js +298 -0
  105. package/tsconfig/dist/__tests__/product-config.spec.js.map +1 -0
  106. package/tsconfig/dist/__tests__/runtime.spec.d.ts +2 -0
  107. package/tsconfig/dist/__tests__/runtime.spec.d.ts.map +1 -0
  108. package/tsconfig/dist/__tests__/runtime.spec.js +127 -0
  109. package/tsconfig/dist/__tests__/runtime.spec.js.map +1 -0
  110. package/tsconfig/dist/__tests__/upsert-lockfile.spec.d.ts +6 -0
  111. package/tsconfig/dist/__tests__/upsert-lockfile.spec.d.ts.map +1 -0
  112. package/tsconfig/dist/__tests__/upsert-lockfile.spec.js +251 -0
  113. package/tsconfig/dist/__tests__/upsert-lockfile.spec.js.map +1 -0
  114. package/tsconfig/dist/__tests__/validate-config.spec.d.ts +2 -0
  115. package/tsconfig/dist/__tests__/validate-config.spec.d.ts.map +1 -0
  116. package/tsconfig/dist/__tests__/validate-config.spec.js +14 -0
  117. package/tsconfig/dist/__tests__/validate-config.spec.js.map +1 -0
  118. package/tsconfig/dist/api/init-workspace.d.ts +10 -0
  119. package/tsconfig/dist/api/init-workspace.d.ts.map +1 -0
  120. package/tsconfig/dist/api/init-workspace.js +191 -0
  121. package/tsconfig/dist/api/init-workspace.js.map +1 -0
  122. package/tsconfig/dist/api/product-config.d.ts +21 -0
  123. package/tsconfig/dist/api/product-config.d.ts.map +1 -0
  124. package/tsconfig/dist/api/product-config.js +192 -0
  125. package/tsconfig/dist/api/product-config.js.map +1 -0
  126. package/tsconfig/dist/api/read-config.d.ts +22 -0
  127. package/tsconfig/dist/api/read-config.d.ts.map +1 -0
  128. package/tsconfig/dist/api/read-config.js +105 -0
  129. package/tsconfig/dist/api/read-config.js.map +1 -0
  130. package/tsconfig/dist/api/upsert-lockfile.d.ts +10 -0
  131. package/tsconfig/dist/api/upsert-lockfile.d.ts.map +1 -0
  132. package/tsconfig/dist/api/upsert-lockfile.js +63 -0
  133. package/tsconfig/dist/api/upsert-lockfile.js.map +1 -0
  134. package/tsconfig/dist/cache/fs-cache.d.ts +38 -0
  135. package/tsconfig/dist/cache/fs-cache.d.ts.map +1 -0
  136. package/tsconfig/dist/cache/fs-cache.js +142 -0
  137. package/tsconfig/dist/cache/fs-cache.js.map +1 -0
  138. package/tsconfig/dist/errors/kb-error.d.ts +32 -0
  139. package/tsconfig/dist/errors/kb-error.d.ts.map +1 -0
  140. package/tsconfig/dist/errors/kb-error.js +54 -0
  141. package/tsconfig/dist/errors/kb-error.js.map +1 -0
  142. package/tsconfig/dist/fs/__tests__/fs.spec.d.ts +2 -0
  143. package/tsconfig/dist/fs/__tests__/fs.spec.d.ts.map +1 -0
  144. package/tsconfig/dist/fs/__tests__/fs.spec.js +22 -0
  145. package/tsconfig/dist/fs/__tests__/fs.spec.js.map +1 -0
  146. package/tsconfig/dist/fs/fs.d.ts +6 -0
  147. package/tsconfig/dist/fs/fs.d.ts.map +1 -0
  148. package/tsconfig/dist/fs/fs.js +12 -0
  149. package/tsconfig/dist/fs/fs.js.map +1 -0
  150. package/tsconfig/dist/fs/index.d.ts +2 -0
  151. package/tsconfig/dist/fs/index.d.ts.map +1 -0
  152. package/tsconfig/dist/fs/index.js +2 -0
  153. package/tsconfig/dist/fs/index.js.map +1 -0
  154. package/tsconfig/dist/hash/config-hash.d.ts +17 -0
  155. package/tsconfig/dist/hash/config-hash.d.ts.map +1 -0
  156. package/tsconfig/dist/hash/config-hash.js +55 -0
  157. package/tsconfig/dist/hash/config-hash.js.map +1 -0
  158. package/tsconfig/dist/index.d.ts +5 -0
  159. package/tsconfig/dist/index.d.ts.map +1 -0
  160. package/tsconfig/dist/index.js +5 -0
  161. package/tsconfig/dist/index.js.map +1 -0
  162. package/tsconfig/dist/lockfile/lockfile.d.ts +54 -0
  163. package/tsconfig/dist/lockfile/lockfile.d.ts.map +1 -0
  164. package/tsconfig/dist/lockfile/lockfile.js +141 -0
  165. package/tsconfig/dist/lockfile/lockfile.js.map +1 -0
  166. package/tsconfig/dist/logging/__tests__/logger.spec.d.ts +2 -0
  167. package/tsconfig/dist/logging/__tests__/logger.spec.d.ts.map +1 -0
  168. package/tsconfig/dist/logging/__tests__/logger.spec.js +65 -0
  169. package/tsconfig/dist/logging/__tests__/logger.spec.js.map +1 -0
  170. package/tsconfig/dist/logging/__tests__/redaction.spec.d.ts +2 -0
  171. package/tsconfig/dist/logging/__tests__/redaction.spec.d.ts.map +1 -0
  172. package/tsconfig/dist/logging/__tests__/redaction.spec.js +34 -0
  173. package/tsconfig/dist/logging/__tests__/redaction.spec.js.map +1 -0
  174. package/tsconfig/dist/logging/__tests__/sinks-and-env.spec.d.ts +2 -0
  175. package/tsconfig/dist/logging/__tests__/sinks-and-env.spec.d.ts.map +1 -0
  176. package/tsconfig/dist/logging/__tests__/sinks-and-env.spec.js +90 -0
  177. package/tsconfig/dist/logging/__tests__/sinks-and-env.spec.js.map +1 -0
  178. package/tsconfig/dist/logging/index.d.ts +6 -0
  179. package/tsconfig/dist/logging/index.d.ts.map +1 -0
  180. package/tsconfig/dist/logging/index.js +6 -0
  181. package/tsconfig/dist/logging/index.js.map +1 -0
  182. package/tsconfig/dist/logging/logger.d.ts +9 -0
  183. package/tsconfig/dist/logging/logger.d.ts.map +1 -0
  184. package/tsconfig/dist/logging/logger.js +101 -0
  185. package/tsconfig/dist/logging/logger.js.map +1 -0
  186. package/tsconfig/dist/logging/redaction.d.ts +7 -0
  187. package/tsconfig/dist/logging/redaction.d.ts.map +1 -0
  188. package/tsconfig/dist/logging/redaction.js +23 -0
  189. package/tsconfig/dist/logging/redaction.js.map +1 -0
  190. package/tsconfig/dist/logging/sinks/json.d.ts +4 -0
  191. package/tsconfig/dist/logging/sinks/json.d.ts.map +1 -0
  192. package/tsconfig/dist/logging/sinks/json.js +23 -0
  193. package/tsconfig/dist/logging/sinks/json.js.map +1 -0
  194. package/tsconfig/dist/logging/sinks/stdout.d.ts +3 -0
  195. package/tsconfig/dist/logging/sinks/stdout.d.ts.map +1 -0
  196. package/tsconfig/dist/logging/sinks/stdout.js +24 -0
  197. package/tsconfig/dist/logging/sinks/stdout.js.map +1 -0
  198. package/tsconfig/dist/logging/types/index.d.ts +2 -0
  199. package/tsconfig/dist/logging/types/index.d.ts.map +1 -0
  200. package/tsconfig/dist/logging/types/index.js +2 -0
  201. package/tsconfig/dist/logging/types/index.js.map +1 -0
  202. package/tsconfig/dist/logging/types/types.d.ts +37 -0
  203. package/tsconfig/dist/logging/types/types.d.ts.map +1 -0
  204. package/tsconfig/dist/logging/types/types.js +2 -0
  205. package/tsconfig/dist/logging/types/types.js.map +1 -0
  206. package/tsconfig/dist/merge/layered-merge.d.ts +16 -0
  207. package/tsconfig/dist/merge/layered-merge.d.ts.map +1 -0
  208. package/tsconfig/dist/merge/layered-merge.js +97 -0
  209. package/tsconfig/dist/merge/layered-merge.js.map +1 -0
  210. package/tsconfig/dist/preset/resolve-preset.d.ts +29 -0
  211. package/tsconfig/dist/preset/resolve-preset.d.ts.map +1 -0
  212. package/tsconfig/dist/preset/resolve-preset.js +104 -0
  213. package/tsconfig/dist/preset/resolve-preset.js.map +1 -0
  214. package/tsconfig/dist/repo/__tests__/repo.spec.d.ts +2 -0
  215. package/tsconfig/dist/repo/__tests__/repo.spec.d.ts.map +1 -0
  216. package/tsconfig/dist/repo/__tests__/repo.spec.js +25 -0
  217. package/tsconfig/dist/repo/__tests__/repo.spec.js.map +1 -0
  218. package/tsconfig/dist/repo/index.d.ts +2 -0
  219. package/tsconfig/dist/repo/index.d.ts.map +1 -0
  220. package/tsconfig/dist/repo/index.js +2 -0
  221. package/tsconfig/dist/repo/index.js.map +1 -0
  222. package/tsconfig/dist/repo/repo.d.ts +6 -0
  223. package/tsconfig/dist/repo/repo.d.ts.map +1 -0
  224. package/tsconfig/dist/repo/repo.js +25 -0
  225. package/tsconfig/dist/repo/repo.js.map +1 -0
  226. package/tsconfig/dist/runtime/index.d.ts +2 -0
  227. package/tsconfig/dist/runtime/index.d.ts.map +1 -0
  228. package/tsconfig/dist/runtime/index.js +2 -0
  229. package/tsconfig/dist/runtime/index.js.map +1 -0
  230. package/tsconfig/dist/runtime/runtime.d.ts +46 -0
  231. package/tsconfig/dist/runtime/runtime.d.ts.map +1 -0
  232. package/tsconfig/dist/runtime/runtime.js +126 -0
  233. package/tsconfig/dist/runtime/runtime.js.map +1 -0
  234. package/tsconfig/dist/tsconfig.tools.tsbuildinfo +1 -0
  235. package/tsconfig/dist/tsconfig.tsbuildinfo +1 -0
  236. package/tsconfig/dist/types/index.d.ts +2 -0
  237. package/tsconfig/dist/types/index.d.ts.map +1 -0
  238. package/tsconfig/dist/types/index.js +2 -0
  239. package/tsconfig/dist/types/index.js.map +1 -0
  240. package/tsconfig/dist/types/init.d.ts +34 -0
  241. package/tsconfig/dist/types/init.d.ts.map +1 -0
  242. package/tsconfig/dist/types/init.js +6 -0
  243. package/tsconfig/dist/types/init.js.map +1 -0
  244. package/tsconfig/dist/types/preset.d.ts +27 -0
  245. package/tsconfig/dist/types/preset.d.ts.map +1 -0
  246. package/tsconfig/dist/types/preset.js +6 -0
  247. package/tsconfig/dist/types/preset.js.map +1 -0
  248. package/tsconfig/dist/types/types.d.ts +6 -0
  249. package/tsconfig/dist/types/types.d.ts.map +1 -0
  250. package/tsconfig/dist/types/types.js +2 -0
  251. package/tsconfig/dist/types/types.js.map +1 -0
  252. package/tsconfig/dist/utils/__tests__/env.spec.d.ts +2 -0
  253. package/tsconfig/dist/utils/__tests__/env.spec.d.ts.map +1 -0
  254. package/tsconfig/dist/utils/__tests__/env.spec.js +33 -0
  255. package/tsconfig/dist/utils/__tests__/env.spec.js.map +1 -0
  256. package/tsconfig/dist/utils/env.d.ts +7 -0
  257. package/tsconfig/dist/utils/env.d.ts.map +1 -0
  258. package/tsconfig/dist/utils/env.js +25 -0
  259. package/tsconfig/dist/utils/env.js.map +1 -0
  260. package/tsconfig/dist/utils/fs-atomic.d.ts +15 -0
  261. package/tsconfig/dist/utils/fs-atomic.d.ts.map +1 -0
  262. package/tsconfig/dist/utils/fs-atomic.js +45 -0
  263. package/tsconfig/dist/utils/fs-atomic.js.map +1 -0
  264. package/tsconfig/dist/utils/index.d.ts +3 -0
  265. package/tsconfig/dist/utils/index.d.ts.map +1 -0
  266. package/tsconfig/dist/utils/index.js +3 -0
  267. package/tsconfig/dist/utils/index.js.map +1 -0
  268. package/tsconfig/dist/utils/paths.d.ts +21 -0
  269. package/tsconfig/dist/utils/paths.d.ts.map +1 -0
  270. package/tsconfig/dist/utils/paths.js +32 -0
  271. package/tsconfig/dist/utils/paths.js.map +1 -0
  272. package/tsconfig/dist/utils/product-normalize.d.ts +27 -0
  273. package/tsconfig/dist/utils/product-normalize.d.ts.map +1 -0
  274. package/tsconfig/dist/utils/product-normalize.js +45 -0
  275. package/tsconfig/dist/utils/product-normalize.js.map +1 -0
  276. package/tsconfig/dist/validation/validate-config.d.ts +7 -0
  277. package/tsconfig/dist/validation/validate-config.d.ts.map +1 -0
  278. package/tsconfig/dist/validation/validate-config.js +22 -0
  279. package/tsconfig/dist/validation/validate-config.js.map +1 -0
  280. package/tsconfig/lib.json +13 -0
  281. package/tsconfig/node.json +12 -0
  282. package/tsconfig/react-app.json +8 -0
  283. package/tsconfig/react-lib.json +8 -0
  284. package/tsconfig/test.json +15 -0
  285. package/tsup/bin.js +172 -0
  286. package/tsup/dual.js +155 -0
  287. package/tsup/external-sync.mjs +41 -0
  288. package/tsup/external.mjs +109 -0
  289. package/tsup/node.js +65 -0
  290. package/tsup/react-lib.js +18 -0
  291. package/tsup/sdk.js +118 -0
  292. package/vite/react-app.js +50 -0
  293. package/vitest/node.js +37 -0
  294. package/vitest/react.js +39 -0
  295. package/vitest/vitest-setup.ts +9 -0
@@ -0,0 +1,25 @@
1
+ /**
2
+ * @module @kb-labs/core/utils/env
3
+ * Small helpers to convert ENV strings to typed values.
4
+ */
5
+ export function toBool(v) {
6
+ if (v === undefined) {
7
+ return undefined;
8
+ }
9
+ const s = v.trim().toLowerCase();
10
+ if (s === "1" || s === "true" || s === "yes" || s === "on") {
11
+ return true;
12
+ }
13
+ if (s === "0" || s === "false" || s === "no" || s === "off") {
14
+ return false;
15
+ }
16
+ return undefined; // unknown → let caller decide default
17
+ }
18
+ export function toInt(v) {
19
+ if (v === undefined) {
20
+ return undefined;
21
+ }
22
+ const n = Number(v);
23
+ return Number.isFinite(n) ? n : undefined;
24
+ }
25
+ //# sourceMappingURL=env.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"env.js","sourceRoot":"","sources":["../../../../kb-labs-core/packages/config/src/utils/env.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,UAAU,MAAM,CAAC,CAAqB;IACxC,IAAI,CAAC,KAAK,SAAS,EAAE,CAAC;QAAA,OAAO,SAAS,CAAC;IAAA,CAAC;IACxC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;IACjC,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,MAAM,IAAI,CAAC,KAAK,KAAK,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC;QAAA,OAAO,IAAI,CAAC;IAAA,CAAC;IAC1E,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,OAAO,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,KAAK,KAAK,EAAE,CAAC;QAAA,OAAO,KAAK,CAAC;IAAA,CAAC;IAC5E,OAAO,SAAS,CAAC,CAAC,sCAAsC;AAC5D,CAAC;AAED,MAAM,UAAU,KAAK,CAAC,CAAqB;IACvC,IAAI,CAAC,KAAK,SAAS,EAAE,CAAC;QAAA,OAAO,SAAS,CAAC;IAAA,CAAC;IACxC,MAAM,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;IACpB,OAAO,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;AAC9C,CAAC"}
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @module @kb-labs/core/config/utils/fs-atomic
3
+ * Atomic file write operations using tmp+rename pattern
4
+ */
5
+ /**
6
+ * Write file atomically using tmp+rename pattern
7
+ * This ensures that the file is never partially written in case of crashes
8
+ */
9
+ export declare function writeFileAtomic(path: string, data: string | Uint8Array): Promise<void>;
10
+ /**
11
+ * Ensure a path is within the workspace root
12
+ * Throws ERR_PATH_OUTSIDE_WORKSPACE if the path escapes the workspace
13
+ */
14
+ export declare function ensureWithinWorkspace(targetPath: string, workspaceRoot: string): void;
15
+ //# sourceMappingURL=fs-atomic.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fs-atomic.d.ts","sourceRoot":"","sources":["../../../../kb-labs-core/packages/config/src/utils/fs-atomic.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAOH;;;GAGG;AACH,wBAAsB,eAAe,CACnC,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,GAAG,UAAU,GACxB,OAAO,CAAC,IAAI,CAAC,CAqBf;AAED;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,UAAU,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,GAAG,IAAI,CAYrF"}
@@ -0,0 +1,45 @@
1
+ /**
2
+ * @module @kb-labs/core/config/utils/fs-atomic
3
+ * Atomic file write operations using tmp+rename pattern
4
+ */
5
+ import { promises as fs } from 'node:fs';
6
+ import { resolve } from 'node:path';
7
+ import { KbError } from '../errors/kb-error';
8
+ import { dirname } from 'node:path';
9
+ /**
10
+ * Write file atomically using tmp+rename pattern
11
+ * This ensures that the file is never partially written in case of crashes
12
+ */
13
+ export async function writeFileAtomic(path, data) {
14
+ const tmp = `${path}.tmp-${Date.now()}-${Math.random().toString(36).slice(2)}`;
15
+ try {
16
+ // Ensure parent directory exists
17
+ await fs.mkdir(dirname(path), { recursive: true });
18
+ // Write to temp file
19
+ await fs.writeFile(tmp, data, 'utf-8');
20
+ // Atomic rename
21
+ await fs.rename(tmp, path);
22
+ }
23
+ catch (error) {
24
+ // Clean up temp file if it exists
25
+ try {
26
+ await fs.unlink(tmp);
27
+ }
28
+ catch {
29
+ // Ignore cleanup errors
30
+ }
31
+ throw error;
32
+ }
33
+ }
34
+ /**
35
+ * Ensure a path is within the workspace root
36
+ * Throws ERR_PATH_OUTSIDE_WORKSPACE if the path escapes the workspace
37
+ */
38
+ export function ensureWithinWorkspace(targetPath, workspaceRoot) {
39
+ const absTarget = resolve(targetPath);
40
+ const absRoot = resolve(workspaceRoot);
41
+ if (!absTarget.startsWith(absRoot)) {
42
+ throw new KbError('ERR_PATH_OUTSIDE_WORKSPACE', `Refusing to write outside workspace: ${targetPath}`, 'Check cwd or use a relative path within the workspace', { targetPath, workspaceRoot });
43
+ }
44
+ }
45
+ //# sourceMappingURL=fs-atomic.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fs-atomic.js","sourceRoot":"","sources":["../../../../kb-labs-core/packages/config/src/utils/fs-atomic.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,QAAQ,IAAI,EAAE,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CACnC,IAAY,EACZ,IAAyB;IAEzB,MAAM,GAAG,GAAG,GAAG,IAAI,QAAQ,IAAI,CAAC,GAAG,EAAE,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;IAE/E,IAAI,CAAC;QACH,iCAAiC;QACjC,MAAM,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QAEnD,qBAAqB;QACrB,MAAM,EAAE,CAAC,SAAS,CAAC,GAAG,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;QAEvC,gBAAgB;QAChB,MAAM,EAAE,CAAC,MAAM,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;IAC7B,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,kCAAkC;QAClC,IAAI,CAAC;YACH,MAAM,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QACvB,CAAC;QAAC,MAAM,CAAC;YACP,wBAAwB;QAC1B,CAAC;QACD,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,qBAAqB,CAAC,UAAkB,EAAE,aAAqB;IAC7E,MAAM,SAAS,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;IACtC,MAAM,OAAO,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC;IAEvC,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE,CAAC;QACnC,MAAM,IAAI,OAAO,CACf,4BAA4B,EAC5B,wCAAwC,UAAU,EAAE,EACpD,uDAAuD,EACvD,EAAE,UAAU,EAAE,aAAa,EAAE,CAC9B,CAAC;IACJ,CAAC;AACH,CAAC"}
@@ -0,0 +1,3 @@
1
+ export * from './env';
2
+ export * from './product-normalize';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../kb-labs-core/packages/config/src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,OAAO,CAAA;AACrB,cAAc,qBAAqB,CAAA"}
@@ -0,0 +1,3 @@
1
+ export * from './env';
2
+ export * from './product-normalize';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../kb-labs-core/packages/config/src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,OAAO,CAAA;AACrB,cAAc,qBAAqB,CAAA"}
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Get profiles root directory
3
+ */
4
+ export declare function getProfilesRootDir(cwd: string): string;
5
+ /**
6
+ * Get product configuration directory
7
+ */
8
+ export declare function getProductConfigDir(cwd: string, fsProduct: string): string;
9
+ /**
10
+ * Get product configuration file path
11
+ */
12
+ export declare function getProductConfigPath(cwd: string, fsProduct: string): string;
13
+ /**
14
+ * Get .kb root directory
15
+ */
16
+ export declare function getKbRootDir(cwd: string): string;
17
+ /**
18
+ * Get lockfile path
19
+ */
20
+ export declare function getLockfilePath(cwd: string): string;
21
+ //# sourceMappingURL=paths.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"paths.d.ts","sourceRoot":"","sources":["../../../../kb-labs-core/packages/config/src/utils/paths.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEtD;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,CAE1E;AAED;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,CAE3E;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEhD;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEnD"}
@@ -0,0 +1,32 @@
1
+ import path from 'node:path';
2
+ /**
3
+ * Get profiles root directory
4
+ */
5
+ export function getProfilesRootDir(cwd) {
6
+ return path.join(cwd, '.kb', 'profiles');
7
+ }
8
+ /**
9
+ * Get product configuration directory
10
+ */
11
+ export function getProductConfigDir(cwd, fsProduct) {
12
+ return path.join(cwd, '.kb', fsProduct);
13
+ }
14
+ /**
15
+ * Get product configuration file path
16
+ */
17
+ export function getProductConfigPath(cwd, fsProduct) {
18
+ return path.join(getProductConfigDir(cwd, fsProduct), `${fsProduct}.config.json`);
19
+ }
20
+ /**
21
+ * Get .kb root directory
22
+ */
23
+ export function getKbRootDir(cwd) {
24
+ return path.join(cwd, '.kb');
25
+ }
26
+ /**
27
+ * Get lockfile path
28
+ */
29
+ export function getLockfilePath(cwd) {
30
+ return path.join(getKbRootDir(cwd), 'lock.json');
31
+ }
32
+ //# sourceMappingURL=paths.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"paths.js","sourceRoot":"","sources":["../../../../kb-labs-core/packages/config/src/utils/paths.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B;;GAEG;AACH,MAAM,UAAU,kBAAkB,CAAC,GAAW;IAC5C,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,KAAK,EAAE,UAAU,CAAC,CAAC;AAC3C,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,mBAAmB,CAAC,GAAW,EAAE,SAAiB;IAChE,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,KAAK,EAAE,SAAS,CAAC,CAAC;AAC1C,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,oBAAoB,CAAC,GAAW,EAAE,SAAiB;IACjE,OAAO,IAAI,CAAC,IAAI,CAAC,mBAAmB,CAAC,GAAG,EAAE,SAAS,CAAC,EAAE,GAAG,SAAS,cAAc,CAAC,CAAC;AACpF,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,YAAY,CAAC,GAAW;IACtC,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;AAC/B,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,eAAe,CAAC,GAAW;IACzC,OAAO,IAAI,CAAC,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,EAAE,WAAW,CAAC,CAAC;AACnD,CAAC"}
@@ -0,0 +1,27 @@
1
+ /**
2
+ * @module @kb-labs/core/config/utils/product-normalize
3
+ * Product ID normalization between code (camelCase) and filesystem (kebab-case)
4
+ */
5
+ import type { ProductId } from '../types/types';
6
+ export type { ProductId } from '../types/types';
7
+ /**
8
+ * Convert ProductId to filesystem kebab-case format
9
+ * @param id ProductId in camelCase
10
+ * @returns kebab-case string for filesystem paths
11
+ */
12
+ export declare function toFsProduct(id: ProductId): string;
13
+ /**
14
+ * Convert filesystem kebab-case key to ProductId
15
+ * @param fsKey kebab-case string from filesystem
16
+ * @returns ProductId in camelCase
17
+ */
18
+ export declare function toConfigProduct(fsKey: string): ProductId;
19
+ /**
20
+ * Check if a string is a valid ProductId
21
+ */
22
+ export declare function isValidProductId(value: string): value is ProductId;
23
+ /**
24
+ * Get all valid ProductId values
25
+ */
26
+ export declare function getAllProductIds(): ProductId[];
27
+ //# sourceMappingURL=product-normalize.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"product-normalize.d.ts","sourceRoot":"","sources":["../../../../kb-labs-core/packages/config/src/utils/product-normalize.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAGhD,YAAY,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAUhD;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,EAAE,EAAE,SAAS,GAAG,MAAM,CAEjD;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,CAKxD;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,IAAI,SAAS,CAElE;AAED;;GAEG;AACH,wBAAgB,gBAAgB,IAAI,SAAS,EAAE,CAE9C"}
@@ -0,0 +1,45 @@
1
+ /**
2
+ * @module @kb-labs/core/config/utils/product-normalize
3
+ * Product ID normalization between code (camelCase) and filesystem (kebab-case)
4
+ */
5
+ const FS_MAP = new Map([
6
+ ['aiReview', 'ai-review'],
7
+ ['aiDocs', 'ai-docs'],
8
+ ['devlink', 'devlink'],
9
+ ['release', 'release'],
10
+ ['devkit', 'devkit'],
11
+ ]);
12
+ /**
13
+ * Convert ProductId to filesystem kebab-case format
14
+ * @param id ProductId in camelCase
15
+ * @returns kebab-case string for filesystem paths
16
+ */
17
+ export function toFsProduct(id) {
18
+ return FS_MAP.get(id) ?? id;
19
+ }
20
+ /**
21
+ * Convert filesystem kebab-case key to ProductId
22
+ * @param fsKey kebab-case string from filesystem
23
+ * @returns ProductId in camelCase
24
+ */
25
+ export function toConfigProduct(fsKey) {
26
+ for (const [k, v] of FS_MAP) {
27
+ if (v === fsKey) {
28
+ return k;
29
+ }
30
+ }
31
+ return fsKey;
32
+ }
33
+ /**
34
+ * Check if a string is a valid ProductId
35
+ */
36
+ export function isValidProductId(value) {
37
+ return FS_MAP.has(value);
38
+ }
39
+ /**
40
+ * Get all valid ProductId values
41
+ */
42
+ export function getAllProductIds() {
43
+ return Array.from(FS_MAP.keys());
44
+ }
45
+ //# sourceMappingURL=product-normalize.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"product-normalize.js","sourceRoot":"","sources":["../../../../kb-labs-core/packages/config/src/utils/product-normalize.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAOH,MAAM,MAAM,GAAG,IAAI,GAAG,CAAC;IACrB,CAAC,UAAU,EAAE,WAAW,CAAC;IACzB,CAAC,QAAQ,EAAE,SAAS,CAAC;IACrB,CAAC,SAAS,EAAE,SAAS,CAAC;IACtB,CAAC,SAAS,EAAE,SAAS,CAAC;IACtB,CAAC,QAAQ,EAAE,QAAQ,CAAC;CACrB,CAAC,CAAC;AAEH;;;;GAIG;AACH,MAAM,UAAU,WAAW,CAAC,EAAa;IACvC,OAAO,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC;AAC9B,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAAC,KAAa;IAC3C,KAAK,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,MAAM,EAAE,CAAC;QAC5B,IAAI,CAAC,KAAK,KAAK,EAAE,CAAC;YAAA,OAAO,CAAc,CAAC;QAAA,CAAC;IAC3C,CAAC;IACD,OAAO,KAAkB,CAAC;AAC5B,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,gBAAgB,CAAC,KAAa;IAC5C,OAAO,MAAM,CAAC,GAAG,CAAC,KAAkB,CAAC,CAAC;AACxC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,gBAAgB;IAC9B,OAAO,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAgB,CAAC;AAClD,CAAC"}
@@ -0,0 +1,7 @@
1
+ import type { ProductId } from '../types';
2
+ export type ValidationResult = {
3
+ ok: boolean;
4
+ errors: any[] | null;
5
+ };
6
+ export declare function validateProductConfig(product: ProductId, config: unknown): ValidationResult;
7
+ //# sourceMappingURL=validate-config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validate-config.d.ts","sourceRoot":"","sources":["../../../../kb-labs-core/packages/config/src/validation/validate-config.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAU1C,MAAM,MAAM,gBAAgB,GAAG;IAAE,EAAE,EAAE,OAAO,CAAC;IAAC,MAAM,EAAE,GAAG,EAAE,GAAG,IAAI,CAAA;CAAE,CAAC;AAcrE,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,GAAG,gBAAgB,CAO3F"}
@@ -0,0 +1,22 @@
1
+ import Ajv from 'ajv';
2
+ import addFormats from 'ajv-formats';
3
+ import { review as reviewSchema, docs as docsSchema, tests as testsSchema, assistant as assistantSchema, devlink as devlinkSchema, } from '@kb-labs/profile-schemas';
4
+ const ajv = new Ajv({ allErrors: true, strict: false });
5
+ addFormats(ajv);
6
+ const schemaMap = {
7
+ aiReview: reviewSchema,
8
+ aiDocs: docsSchema,
9
+ devlink: devlinkSchema,
10
+ devkit: testsSchema, // placeholder mapping
11
+ release: assistantSchema, // placeholder mapping
12
+ // extend with more products as needed
13
+ };
14
+ export function validateProductConfig(product, config) {
15
+ const schema = schemaMap[product];
16
+ if (!schema)
17
+ return { ok: true, errors: null };
18
+ const validate = ajv.compile(schema);
19
+ const ok = validate(config);
20
+ return { ok: !!ok, errors: validate.errors || null };
21
+ }
22
+ //# sourceMappingURL=validate-config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validate-config.js","sourceRoot":"","sources":["../../../../kb-labs-core/packages/config/src/validation/validate-config.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,KAAK,CAAC;AACtB,OAAO,UAAU,MAAM,aAAa,CAAC;AAErC,OAAO,EACL,MAAM,IAAI,YAAY,EACtB,IAAI,IAAI,UAAU,EAClB,KAAK,IAAI,WAAW,EACpB,SAAS,IAAI,eAAe,EAC5B,OAAO,IAAI,aAAa,GAEzB,MAAM,0BAA0B,CAAC;AAIlC,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC;AACxD,UAAU,CAAC,GAAG,CAAC,CAAC;AAEhB,MAAM,SAAS,GAAoC;IACjD,QAAQ,EAAE,YAAY;IACtB,MAAM,EAAE,UAAU;IAClB,OAAO,EAAE,aAAa;IACtB,MAAM,EAAE,WAAW,EAAE,sBAAsB;IAC3C,OAAO,EAAE,eAAe,EAAE,sBAAsB;IAChD,sCAAsC;CACvC,CAAC;AAEF,MAAM,UAAU,qBAAqB,CAAC,OAAkB,EAAE,MAAe;IACvE,MAAM,MAAM,GAAG,SAAS,CAAC,OAAO,CAAC,CAAC;IAClC,IAAI,CAAC,MAAM;QAAE,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;IAE/C,MAAM,QAAQ,GAAG,GAAG,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IACrC,MAAM,EAAE,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC;IAC5B,OAAO,EAAE,EAAE,EAAE,CAAC,CAAC,EAAE,EAAE,MAAM,EAAE,QAAQ,CAAC,MAAM,IAAI,IAAI,EAAE,CAAC;AACvD,CAAC"}
@@ -0,0 +1,13 @@
1
+ {
2
+ "extends": "./base.json",
3
+ "compilerOptions": {
4
+ "outDir": "dist",
5
+ "declaration": true,
6
+ "declarationMap": true,
7
+ "sourceMap": true,
8
+ "stripInternal": true
9
+ },
10
+ "include": [
11
+ "src"
12
+ ]
13
+ }
@@ -0,0 +1,12 @@
1
+ {
2
+ "extends": "./base.json",
3
+ "compilerOptions": {
4
+ "outDir": "dist",
5
+ "declaration": true,
6
+ "declarationMap": true,
7
+ "skipLibCheck": true
8
+ },
9
+ "include": [
10
+ "src"
11
+ ]
12
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "./base.json",
3
+ "compilerOptions": {
4
+ "jsx": "react-jsx",
5
+ "lib": ["ES2022", "DOM", "DOM.Iterable"]
6
+ }
7
+ }
8
+
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "./base.json",
3
+ "compilerOptions": {
4
+ "jsx": "react-jsx",
5
+ "lib": ["ES2022"]
6
+ }
7
+ }
8
+
@@ -0,0 +1,15 @@
1
+ {
2
+ "extends": "./node.json",
3
+ "compilerOptions": {
4
+ "types": [
5
+ "vitest/globals",
6
+ "node"
7
+ ]
8
+ },
9
+ "include": [
10
+ "**/__tests__/**/*",
11
+ "**/*.{test,spec}.ts",
12
+ "**/*.{test,spec}.mts",
13
+ "**/*.{test,spec}.cts"
14
+ ]
15
+ }
package/tsup/bin.js ADDED
@@ -0,0 +1,172 @@
1
+ import { defineConfig } from 'tsup';
2
+
3
+ /**
4
+ * Preset for bin/executable builds.
5
+ *
6
+ * ## Overview
7
+ *
8
+ * This preset bundles ALL code (workspace packages + node_modules) into a single
9
+ * standalone executable. Only Node.js built-ins remain external.
10
+ *
11
+ * ## Features
12
+ *
13
+ * - **Format**: CommonJS (better compatibility with bundled deps)
14
+ * - **Bundling**: ALL dependencies included (workspace packages, node_modules)
15
+ * - **Externals**: Only Node.js built-ins (fs, path, crypto, etc.)
16
+ * - **Shebang**: Automatically adds `#!/usr/bin/env node`
17
+ * - **Node.js built-ins plugin**: Handles both `node:*` and bare imports (e.g., `readline/promises`)
18
+ * - **No types**: Binaries don't need .d.ts files
19
+ *
20
+ * ## When to use
21
+ *
22
+ * ✅ **Use for:**
23
+ * - CLI binaries (cli-bin)
24
+ * - Daemon binaries (core-state-daemon)
25
+ * - Standalone executables
26
+ * - Any script that needs to run without installing workspace packages
27
+ *
28
+ * ❌ **DO NOT use for:**
29
+ * - Libraries (use tsup/node.js instead)
30
+ * - Packages meant to be imported by other packages
31
+ *
32
+ * ## Important Notes
33
+ *
34
+ * 1. **Remove shebang from source**: Don't include `#!/usr/bin/env node` in src/bin.ts
35
+ * The preset adds it automatically via banner.
36
+ *
37
+ * 2. **Update package.json bin path**: Point to .cjs file:
38
+ * ```json
39
+ * "bin": { "my-cli": "./dist/bin.cjs" }
40
+ * ```
41
+ *
42
+ * 3. **Bundle size**: Expect 500KB-5MB depending on dependencies.
43
+ * This is normal for standalone executables.
44
+ *
45
+ * 4. **Dynamic imports**: Preserved! Plugins can still be loaded dynamically
46
+ * from filesystem. Only the core code is bundled.
47
+ *
48
+ * ## Basic Example
49
+ *
50
+ * ```typescript
51
+ * import { defineConfig } from 'tsup';
52
+ * import binPreset from '@kb-labs/devkit/tsup/bin.js';
53
+ *
54
+ * export default defineConfig({
55
+ * ...binPreset,
56
+ * tsconfig: "tsconfig.build.json",
57
+ * entry: { bin: 'src/bin.ts' },
58
+ * });
59
+ * ```
60
+ *
61
+ * ## Dual Build Example (Library + Bin)
62
+ *
63
+ * For packages that provide both a library and executable:
64
+ *
65
+ * ```typescript
66
+ * // tsup.bin.config.ts
67
+ * import { defineConfig } from 'tsup';
68
+ * import binPreset from '@kb-labs/devkit/tsup/bin.js';
69
+ *
70
+ * export default defineConfig({
71
+ * ...binPreset,
72
+ * tsconfig: "tsconfig.build.json",
73
+ * entry: { bin: 'src/bin.ts' },
74
+ * });
75
+ * ```
76
+ *
77
+ * ```typescript
78
+ * // tsup.lib.config.ts
79
+ * import { defineConfig } from 'tsup';
80
+ * import nodePreset from '@kb-labs/devkit/tsup/node.js';
81
+ *
82
+ * export default defineConfig({
83
+ * ...nodePreset,
84
+ * tsconfig: "tsconfig.build.json",
85
+ * entry: { index: 'src/index.ts' },
86
+ * clean: false, // Already cleaned by bin build
87
+ * });
88
+ * ```
89
+ *
90
+ * ```json
91
+ * // package.json
92
+ * {
93
+ * "scripts": {
94
+ * "build": "pnpm clean && tsup --config tsup.bin.config.ts && tsup --config tsup.lib.config.ts"
95
+ * }
96
+ * }
97
+ * ```
98
+ *
99
+ * ## Troubleshooting
100
+ *
101
+ * ### Error: "Cannot find module 'readline/promises'"
102
+ * This is handled automatically by the esbuild plugin. If you still see this error,
103
+ * it might be a Node.js built-in not in the NODE_BUILTINS list. Report it!
104
+ *
105
+ * ### Error: "Dynamic require is not supported"
106
+ * This happens when bundling CJS packages with dynamic requires. The preset uses
107
+ * CJS format specifically to avoid this. If you see this error, check if you're
108
+ * using format: ['esm'] by mistake.
109
+ *
110
+ * ### Shebang appearing twice
111
+ * Remove `#!/usr/bin/env node` from your src/bin.ts source file. The preset
112
+ * adds it automatically.
113
+ */
114
+ // List of Node.js built-in modules (without node: prefix)
115
+ const NODE_BUILTINS = [
116
+ 'assert', 'async_hooks', 'buffer', 'child_process', 'cluster', 'console',
117
+ 'constants', 'crypto', 'dgram', 'diagnostics_channel', 'dns', 'domain',
118
+ 'events', 'fs', 'http', 'http2', 'https', 'inspector', 'module', 'net',
119
+ 'os', 'path', 'perf_hooks', 'process', 'punycode', 'querystring', 'readline',
120
+ 'repl', 'stream', 'string_decoder', 'sys', 'timers', 'tls', 'trace_events',
121
+ 'tty', 'url', 'util', 'v8', 'vm', 'wasi', 'worker_threads', 'zlib',
122
+ ];
123
+
124
+ export default defineConfig({
125
+ format: ['cjs'], // Use CJS for better compatibility with bundled deps
126
+ target: 'es2022',
127
+ platform: 'node', // Treat as Node.js environment
128
+ sourcemap: true,
129
+ clean: true,
130
+ dts: false, // Binaries don't need type definitions
131
+ treeshake: true,
132
+ minify: false,
133
+ outDir: 'dist',
134
+ splitting: false,
135
+
136
+ // Bundle EVERYTHING (workspace packages, node_modules, etc)
137
+ noExternal: [/.*/],
138
+
139
+ // CRITICAL: Use 'require' condition for dual-format packages
140
+ // When bundling CJS, we want to resolve to .cjs files, not .js (ESM)
141
+ // This prevents "Could not resolve" errors for dual-format workspace packages
142
+ esbuildOptions(options) {
143
+ options.conditions = ['require', 'node'];
144
+ },
145
+
146
+ // esbuild plugin to mark Node.js built-ins as external
147
+ esbuildPlugins: [
148
+ {
149
+ name: 'node-builtins-external',
150
+ setup(build) {
151
+ // Match Node.js built-ins with or without node: prefix, including subpaths
152
+ const builtinRegex = new RegExp(`^(node:)?(${NODE_BUILTINS.join('|')})(\\/.*)?$`);
153
+
154
+ build.onResolve({ filter: builtinRegex }, (args) => {
155
+ return { path: args.path, external: true };
156
+ });
157
+ },
158
+ },
159
+ ],
160
+
161
+ // ONLY externalize Node.js built-ins and native modules
162
+ // Note: The esbuild plugin above handles most cases, but we keep this as a safety net
163
+ external: [
164
+ // Native modules that can't be bundled
165
+ 'fsevents',
166
+ ],
167
+
168
+ // Add shebang for executables
169
+ banner: {
170
+ js: '#!/usr/bin/env node',
171
+ },
172
+ });