toiljs 0.0.115 → 0.0.117

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 (326) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/README.md +16 -7
  3. package/TYPESCRIPT_LAW.md +1946 -2073
  4. package/build/backend/.tsbuildinfo +1 -1
  5. package/build/cli/.tsbuildinfo +1 -1
  6. package/build/cli/index.js +2375 -258
  7. package/build/client/.tsbuildinfo +1 -1
  8. package/build/client/dev/devtools.js +1 -1
  9. package/build/client/dev/error-overlay.js +1 -1
  10. package/build/client/routing/mount.js +1 -1
  11. package/build/compiler/.tsbuildinfo +1 -1
  12. package/build/compiler/auth-emails.js +7 -3
  13. package/build/compiler/emails.js +1 -1
  14. package/build/compiler/index.d.ts +1 -0
  15. package/build/compiler/index.js +25 -4
  16. package/build/compiler/pages.js +2 -3
  17. package/build/compiler/prerender.d.ts +3 -8
  18. package/build/compiler/prerender.js +88 -127
  19. package/build/compiler/ssg.js +3 -4
  20. package/build/compiler/template-build.js +3 -4
  21. package/build/compiler/toil-docs.generated.js +6 -5
  22. package/build/compiler/vite.js +3 -6
  23. package/build/devserver/.tsbuildinfo +1 -1
  24. package/build/devserver/db/database.js +7 -3
  25. package/build/devserver/db/types.js +1 -1
  26. package/build/devserver/production.js +4 -3
  27. package/build/devserver/runtime/crypto.d.ts +1 -1
  28. package/build/io/.tsbuildinfo +1 -1
  29. package/build/logger/.tsbuildinfo +1 -1
  30. package/build/shared/.tsbuildinfo +1 -1
  31. package/examples/basic/client/routes/analytics.tsx +7 -5
  32. package/examples/basic/server/tsconfig.json +1 -3
  33. package/package.json +45 -44
  34. package/presets/no-uint8array-tostring.js +120 -153
  35. package/presets/oxlint.d.ts +5 -0
  36. package/presets/oxlint.js +224 -0
  37. package/server/runtime/tsconfig.json +1 -3
  38. package/tsconfig.json +2 -11
  39. package/.github/ISSUE_TEMPLATE/bug_report.md +0 -38
  40. package/.github/ISSUE_TEMPLATE/bug_report.yml +0 -90
  41. package/.github/ISSUE_TEMPLATE/config.yml +0 -8
  42. package/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
  43. package/.github/PULL_REQUEST_TEMPLATE.md +0 -43
  44. package/.github/changelog-config.json +0 -45
  45. package/.github/dependabot.yml +0 -27
  46. package/.github/workflows/ci.yml +0 -222
  47. package/.vscode/settings.json +0 -10
  48. package/docs/README.md +0 -107
  49. package/docs/auth/README.md +0 -104
  50. package/docs/auth/configuration.md +0 -105
  51. package/docs/auth/emails.md +0 -92
  52. package/docs/auth/extending.md +0 -202
  53. package/docs/auth/how-it-works.md +0 -138
  54. package/docs/auth/usage.md +0 -188
  55. package/docs/backend/README.md +0 -143
  56. package/docs/backend/data.md +0 -351
  57. package/docs/backend/rest.md +0 -402
  58. package/docs/backend/rpc.md +0 -226
  59. package/docs/background/README.md +0 -114
  60. package/docs/background/daemons.md +0 -276
  61. package/docs/background/derive.md +0 -179
  62. package/docs/cli/README.md +0 -379
  63. package/docs/concepts/ai-guide.md +0 -132
  64. package/docs/concepts/config.md +0 -416
  65. package/docs/concepts/decorators.md +0 -127
  66. package/docs/concepts/security.md +0 -108
  67. package/docs/concepts/tiers.md +0 -166
  68. package/docs/concepts/types.md +0 -216
  69. package/docs/database/README.md +0 -143
  70. package/docs/database/capacity.md +0 -350
  71. package/docs/database/counters.md +0 -174
  72. package/docs/database/documents.md +0 -277
  73. package/docs/database/events.md +0 -307
  74. package/docs/database/membership.md +0 -246
  75. package/docs/database/setup.md +0 -155
  76. package/docs/database/unique.md +0 -216
  77. package/docs/database/views.md +0 -246
  78. package/docs/frontend/README.md +0 -104
  79. package/docs/frontend/components.md +0 -286
  80. package/docs/frontend/data-fetching.md +0 -260
  81. package/docs/frontend/images.md +0 -148
  82. package/docs/frontend/metadata.md +0 -344
  83. package/docs/frontend/navigation.md +0 -296
  84. package/docs/frontend/rendering.md +0 -236
  85. package/docs/frontend/routing.md +0 -346
  86. package/docs/frontend/scripts.md +0 -118
  87. package/docs/frontend/search.md +0 -191
  88. package/docs/frontend/styling.md +0 -147
  89. package/docs/frontend/toil-global.md +0 -216
  90. package/docs/getting-started/README.md +0 -81
  91. package/docs/getting-started/create-project.md +0 -131
  92. package/docs/getting-started/deploy.md +0 -101
  93. package/docs/getting-started/first-app.md +0 -215
  94. package/docs/getting-started/installation.md +0 -124
  95. package/docs/getting-started/migrating.md +0 -125
  96. package/docs/getting-started/project-structure.md +0 -163
  97. package/docs/introduction/README.md +0 -66
  98. package/docs/introduction/design-principles.md +0 -81
  99. package/docs/introduction/distributed.md +0 -91
  100. package/docs/introduction/how-it-works.md +0 -121
  101. package/docs/introduction/hyperscale.md +0 -70
  102. package/docs/introduction/modern-stack.md +0 -85
  103. package/docs/introduction/vs-other-frameworks.md +0 -64
  104. package/docs/introduction/why-toil.md +0 -99
  105. package/docs/llms.txt +0 -95
  106. package/docs/realtime/README.md +0 -102
  107. package/docs/realtime/channels.md +0 -211
  108. package/docs/realtime/streams.md +0 -369
  109. package/docs/services/README.md +0 -60
  110. package/docs/services/analytics.md +0 -279
  111. package/docs/services/caching.md +0 -175
  112. package/docs/services/cookies.md +0 -235
  113. package/docs/services/crypto.md +0 -209
  114. package/docs/services/email.md +0 -289
  115. package/docs/services/environment.md +0 -141
  116. package/docs/services/ratelimit.md +0 -174
  117. package/docs/services/time.md +0 -85
  118. package/eslint.config.js +0 -57
  119. package/examples/basic/client/routes/docs/[...slug].tsx +0 -12
  120. package/presets/eslint.js +0 -88
  121. package/src/backend/index.ts +0 -204
  122. package/src/cli/configure.ts +0 -363
  123. package/src/cli/create.ts +0 -924
  124. package/src/cli/db.ts +0 -158
  125. package/src/cli/diagnostics.ts +0 -665
  126. package/src/cli/doctor.ts +0 -982
  127. package/src/cli/features.ts +0 -170
  128. package/src/cli/index.ts +0 -326
  129. package/src/cli/notify.ts +0 -120
  130. package/src/cli/proc.ts +0 -56
  131. package/src/cli/ui.ts +0 -169
  132. package/src/cli/update.ts +0 -248
  133. package/src/cli/updates.ts +0 -92
  134. package/src/cli/validate.ts +0 -31
  135. package/src/cli/version-check.ts +0 -125
  136. package/src/client/auth.ts +0 -869
  137. package/src/client/channel/channel.ts +0 -146
  138. package/src/client/components/Form.tsx +0 -65
  139. package/src/client/components/Image.tsx +0 -160
  140. package/src/client/components/Script.tsx +0 -113
  141. package/src/client/components/Slot.tsx +0 -21
  142. package/src/client/dev/devtools.tsx +0 -1089
  143. package/src/client/dev/error-overlay.tsx +0 -225
  144. package/src/client/errors.ts +0 -11
  145. package/src/client/head/head.ts +0 -182
  146. package/src/client/head/metadata.ts +0 -110
  147. package/src/client/index.ts +0 -130
  148. package/src/client/navigation/Link.tsx +0 -108
  149. package/src/client/navigation/NavLink.tsx +0 -86
  150. package/src/client/navigation/navigation.ts +0 -235
  151. package/src/client/navigation/prefetch.ts +0 -169
  152. package/src/client/navigation/scroll.ts +0 -53
  153. package/src/client/routing/Router.tsx +0 -216
  154. package/src/client/routing/action.ts +0 -122
  155. package/src/client/routing/error-boundary.tsx +0 -43
  156. package/src/client/routing/hooks.ts +0 -140
  157. package/src/client/routing/lazy.ts +0 -77
  158. package/src/client/routing/loader.ts +0 -381
  159. package/src/client/routing/match.ts +0 -47
  160. package/src/client/routing/mount.tsx +0 -90
  161. package/src/client/routing/params-context.ts +0 -10
  162. package/src/client/routing/slot-context.ts +0 -7
  163. package/src/client/rpc.ts +0 -97
  164. package/src/client/search/search.ts +0 -189
  165. package/src/client/search/use-page-search.ts +0 -73
  166. package/src/client/ssr/markers.tsx +0 -172
  167. package/src/client/stream/client.ts +0 -237
  168. package/src/client/types.ts +0 -73
  169. package/src/compiler/auth-emails.ts +0 -312
  170. package/src/compiler/config.ts +0 -369
  171. package/src/compiler/docs.ts +0 -127
  172. package/src/compiler/email-preview.ts +0 -314
  173. package/src/compiler/emails.ts +0 -404
  174. package/src/compiler/fonts.ts +0 -89
  175. package/src/compiler/generate.ts +0 -471
  176. package/src/compiler/image-blur.ts +0 -42
  177. package/src/compiler/image-report.ts +0 -88
  178. package/src/compiler/index.ts +0 -1362
  179. package/src/compiler/pages.ts +0 -49
  180. package/src/compiler/plugin.ts +0 -391
  181. package/src/compiler/prerender.ts +0 -263
  182. package/src/compiler/routes.ts +0 -170
  183. package/src/compiler/seo.ts +0 -415
  184. package/src/compiler/sri.ts +0 -144
  185. package/src/compiler/ssg.ts +0 -177
  186. package/src/compiler/ssr-codegen.ts +0 -85
  187. package/src/compiler/template-build.ts +0 -676
  188. package/src/compiler/template.ts +0 -262
  189. package/src/compiler/toil-docs.generated.ts +0 -77
  190. package/src/compiler/vite.ts +0 -245
  191. package/src/devserver/analytics/index.ts +0 -317
  192. package/src/devserver/config/dotenv.ts +0 -102
  193. package/src/devserver/config/env.ts +0 -30
  194. package/src/devserver/config/ratelimit.ts +0 -123
  195. package/src/devserver/daemon/catalog.ts +0 -120
  196. package/src/devserver/daemon/cron.ts +0 -87
  197. package/src/devserver/daemon/host.ts +0 -134
  198. package/src/devserver/daemon/index.ts +0 -369
  199. package/src/devserver/daemon/runtime.ts +0 -48
  200. package/src/devserver/db/catalog.ts +0 -109
  201. package/src/devserver/db/database.ts +0 -1834
  202. package/src/devserver/db/derives.ts +0 -121
  203. package/src/devserver/db/index.ts +0 -20
  204. package/src/devserver/db/routeKinds.ts +0 -191
  205. package/src/devserver/db/types.ts +0 -168
  206. package/src/devserver/email/caps.ts +0 -0
  207. package/src/devserver/email/config.ts +0 -141
  208. package/src/devserver/email/console.ts +0 -348
  209. package/src/devserver/email/index.ts +0 -155
  210. package/src/devserver/email/providers.ts +0 -131
  211. package/src/devserver/email/status.ts +0 -23
  212. package/src/devserver/email/validate.ts +0 -37
  213. package/src/devserver/email/wire.ts +0 -55
  214. package/src/devserver/http/cache.ts +0 -0
  215. package/src/devserver/http/envelope.ts +0 -157
  216. package/src/devserver/http/proxy.ts +0 -172
  217. package/src/devserver/http/runtime.ts +0 -387
  218. package/src/devserver/index.ts +0 -43
  219. package/src/devserver/production-ipc.ts +0 -63
  220. package/src/devserver/production-worker.ts +0 -83
  221. package/src/devserver/production.ts +0 -718
  222. package/src/devserver/runtime/crypto.ts +0 -587
  223. package/src/devserver/runtime/host.ts +0 -427
  224. package/src/devserver/runtime/module.ts +0 -442
  225. package/src/devserver/server.ts +0 -251
  226. package/src/devserver/ssr.ts +0 -208
  227. package/src/devserver/stream/catalog.ts +0 -106
  228. package/src/devserver/stream/host.ts +0 -42
  229. package/src/devserver/stream/index.ts +0 -301
  230. package/src/devserver/stream/manager.ts +0 -167
  231. package/src/devserver/stream/router.ts +0 -101
  232. package/src/devserver/stream/wire.ts +0 -58
  233. package/src/devserver/stream/ws.ts +0 -78
  234. package/src/devserver/wasm/sections.ts +0 -59
  235. package/src/devserver/wasm/surface.ts +0 -115
  236. package/src/io/FastMap.ts +0 -151
  237. package/src/io/FastSet.ts +0 -113
  238. package/src/io/codec.ts +0 -362
  239. package/src/io/index.ts +0 -10
  240. package/src/io/lengths.ts +0 -14
  241. package/src/io/types.ts +0 -19
  242. package/src/logger/index.ts +0 -22
  243. package/src/shared/index.ts +0 -46
  244. package/test/analytics-dev.test.ts +0 -113
  245. package/test/assembly/aspect-shim.ts +0 -33
  246. package/test/assembly/cookie.spec.ts +0 -303
  247. package/test/assembly/example.spec.ts +0 -22
  248. package/test/assembly/ssr.spec.ts +0 -95
  249. package/test/assembly.test.ts +0 -59
  250. package/test/auth-emails.test.ts +0 -118
  251. package/test/built-ssr.test.ts +0 -148
  252. package/test/channel.test.ts +0 -21
  253. package/test/config-host-port.test.ts +0 -59
  254. package/test/configure.test.ts +0 -94
  255. package/test/daemon-build.test.ts +0 -278
  256. package/test/daemon-catalog.test.ts +0 -349
  257. package/test/daemon-emulation.test.ts +0 -256
  258. package/test/db.test.ts +0 -0
  259. package/test/devserver-database.test.ts +0 -1102
  260. package/test/devserver-email.test.ts +0 -306
  261. package/test/devserver-pqauth.test.ts +0 -93
  262. package/test/devserver-secrets.test.ts +0 -63
  263. package/test/devserver.test.ts +0 -259
  264. package/test/doctor.test.ts +0 -298
  265. package/test/dom/Image.test.tsx +0 -144
  266. package/test/dom/Link.test.tsx +0 -67
  267. package/test/dom/NavLink.test.tsx +0 -37
  268. package/test/dom/Script.test.tsx +0 -48
  269. package/test/dom/action.test.tsx +0 -146
  270. package/test/dom/error-overlay.test.tsx +0 -44
  271. package/test/dom/loader.test.tsx +0 -121
  272. package/test/dom/navigation.test.ts +0 -59
  273. package/test/dom/revalidate.test.tsx +0 -38
  274. package/test/dom/route-head.test.tsx +0 -56
  275. package/test/dom/router-loading.test.tsx +0 -44
  276. package/test/dom/scroll.test.ts +0 -56
  277. package/test/dom/slot.test.tsx +0 -131
  278. package/test/dom/use-metadata.test.tsx +0 -58
  279. package/test/dom/view-transitions.test.tsx +0 -53
  280. package/test/dynamic-routing.test.ts +0 -132
  281. package/test/email-console.test.ts +0 -86
  282. package/test/email-preview.test.ts +0 -73
  283. package/test/emails.test.ts +0 -58
  284. package/test/errors.test.ts +0 -21
  285. package/test/example-guestbook.test.ts +0 -123
  286. package/test/features.test.ts +0 -149
  287. package/test/fixtures/bignum-wire/spec.ts +0 -25
  288. package/test/fixtures/daemon-app.ts +0 -65
  289. package/test/fixtures/stream-echo.ts +0 -26
  290. package/test/fixtures/stream-gate.ts +0 -28
  291. package/test/fixtures/stream-trap.ts +0 -18
  292. package/test/fixtures/stream-typed.ts +0 -41
  293. package/test/fonts.test.ts +0 -28
  294. package/test/global-setup.ts +0 -17
  295. package/test/head.test.ts +0 -42
  296. package/test/image-blur.test.ts +0 -50
  297. package/test/io.test.ts +0 -117
  298. package/test/metadata.test.ts +0 -40
  299. package/test/navlink.test.ts +0 -28
  300. package/test/pages.test.ts +0 -105
  301. package/test/placeholder.test.ts +0 -9
  302. package/test/pqauth-e2e.test.ts +0 -216
  303. package/test/pqauth-email.test.ts +0 -801
  304. package/test/prerender.test.ts +0 -116
  305. package/test/prettier-plugin.test.ts +0 -46
  306. package/test/routes.test.ts +0 -76
  307. package/test/rpc-bignum-wire.test.ts +0 -164
  308. package/test/rpc-dispatch.test.ts +0 -132
  309. package/test/rpc-kinds.test.ts +0 -18
  310. package/test/rpc.test.ts +0 -66
  311. package/test/search.test.ts +0 -114
  312. package/test/seo.test.ts +0 -175
  313. package/test/slot-layouts.test.ts +0 -69
  314. package/test/sri.test.ts +0 -129
  315. package/test/ssg.test.ts +0 -36
  316. package/test/ssr-hydration.test.tsx +0 -122
  317. package/test/ssr-render.test.ts +0 -197
  318. package/test/ssr-template.test.tsx +0 -482
  319. package/test/stream-emulation.test.ts +0 -479
  320. package/test/ui.test.ts +0 -40
  321. package/test/update.test.ts +0 -58
  322. package/test/validate.test.ts +0 -42
  323. package/test/version-check.test.ts +0 -130
  324. package/tests/data-parity/generated-parity.ts +0 -99
  325. package/tests/data-parity/parity.ts +0 -80
  326. package/tests/data-parity/spec.ts +0 -46
@@ -1,363 +0,0 @@
1
- /**
2
- * `toiljs configure`, toggle a project's client styling features (CSS preprocessor + Tailwind) on
3
- * an existing app. Detects the current setup, prompts for the desired one, then rewrites the
4
- * stylesheet(s) + the `client/toil.tsx` imports, edits `package.json`, and syncs node_modules with
5
- * the project's package manager (so removed features are fully cleaned, not just disabled).
6
- */
7
- import fs from 'node:fs/promises';
8
- import path from 'node:path';
9
-
10
- import { cancel, confirm, intro, isCancel, note, outro, select, spinner } from '@clack/prompts';
11
- import { loadConfig } from 'toiljs/compiler';
12
- import pc from 'picocolors';
13
-
14
- import {
15
- defaultConfigSource,
16
- detectPreprocessor,
17
- detectTailwind,
18
- packageDiff,
19
- PKG_VERSION,
20
- type Preprocessor,
21
- preprocessorForExt,
22
- PREPROCESSORS,
23
- setConfigImages,
24
- setStyleImports,
25
- styleEntry,
26
- type StyleFeatures,
27
- TAILWIND_CSS,
28
- TAILWIND_ENTRY,
29
- } from './features.js';
30
- import { run } from './proc.js';
31
- import { accent, dim } from './ui.js';
32
-
33
- export interface ConfigureOptions {
34
- readonly root?: string;
35
- readonly cwd: string;
36
- /** When set, the corresponding prompt is skipped (non-interactive). */
37
- readonly preprocessor?: Preprocessor;
38
- readonly tailwind?: boolean;
39
- /** Toggle build-time image optimization. When set, the prompt is skipped. */
40
- readonly images?: boolean;
41
- /** Run the package manager to sync deps. Default `true`; `false` edits files only. */
42
- readonly install?: boolean;
43
- }
44
-
45
- const CONFIG_FILES = [
46
- 'toil.config.ts',
47
- 'toil.config.mts',
48
- 'toil.config.js',
49
- 'toil.config.mjs',
50
- 'toiljs.config.ts',
51
- 'toiljs.config.mts',
52
- 'toiljs.config.js',
53
- 'toiljs.config.mjs',
54
- ];
55
-
56
- /** Reads the project's `toil.config.*` (path + source), or null if none exists. */
57
- async function readConfigFile(root: string): Promise<{ path: string; source: string } | null> {
58
- for (const name of CONFIG_FILES) {
59
- const p = path.join(root, name);
60
- try {
61
- return { path: p, source: await fs.readFile(p, 'utf8') };
62
- } catch {}
63
- }
64
- return null;
65
- }
66
-
67
- /**
68
- * Persists `client.images` to the project's `toil.config`. Edits an existing config in place (or
69
- * creates `toil.config.ts` if none); returns `false` if the existing file's shape couldn't be
70
- * edited, so the caller can tell the user to set it by hand.
71
- */
72
- async function writeImagesFlag(root: string, enabled: boolean): Promise<boolean> {
73
- const existing = await readConfigFile(root);
74
- if (!existing) {
75
- await fs.writeFile(path.join(root, 'toil.config.ts'), defaultConfigSource(enabled), 'utf8');
76
- return true;
77
- }
78
- const next = setConfigImages(existing.source, enabled);
79
- if (next === null) return false;
80
- await fs.writeFile(existing.path, next, 'utf8');
81
- return true;
82
- }
83
-
84
- /** Current `client.images` setting (defaults to `true` when the config can't be loaded). */
85
- async function resolveImages(root: string): Promise<boolean> {
86
- try {
87
- return (await loadConfig({ root })).images;
88
- } catch {
89
- return true;
90
- }
91
- }
92
-
93
- /** Resolves the client source dir, falling back to `<root>/client` if the config can't be loaded. */
94
- async function resolveClientDir(root: string): Promise<string> {
95
- try {
96
- const cfg = await loadConfig({ root });
97
- return cfg.clientAbsDir;
98
- } catch {
99
- return path.join(root, 'client');
100
- }
101
- }
102
-
103
- const PREPROCESSOR_LABEL: Record<Preprocessor, string> = {
104
- css: 'Plain CSS',
105
- sass: 'Sass (SCSS)',
106
- less: 'Less',
107
- stylus: 'Stylus',
108
- };
109
-
110
- interface PackageJson {
111
- dependencies?: Record<string, string>;
112
- devDependencies?: Record<string, string>;
113
- }
114
-
115
- function bail<T>(value: T | symbol): asserts value is T {
116
- if (isCancel(value)) {
117
- cancel('Configuration cancelled.');
118
- process.exit(0);
119
- }
120
- }
121
-
122
- /** Finds the existing main stylesheet's preprocessor by extension, or null if none is present. */
123
- async function detectStylesheet(clientDir: string): Promise<Preprocessor | null> {
124
- for (const p of PREPROCESSORS) {
125
- try {
126
- await fs.access(path.join(clientDir, styleEntry(p)));
127
- return p;
128
- } catch {}
129
- }
130
- try {
131
- await fs.access(path.join(clientDir, 'styles/main.sass'));
132
- return preprocessorForExt('sass');
133
- } catch {
134
- return null;
135
- }
136
- }
137
-
138
- /** Returns the path of the existing `styles/main.*` stylesheet, or null. */
139
- async function findMainStylesheet(clientDir: string): Promise<string | null> {
140
- for (const ext of ['css', 'scss', 'sass', 'less', 'styl']) {
141
- const p = path.join(clientDir, 'styles', `main.${ext}`);
142
- try {
143
- await fs.access(p);
144
- return p;
145
- } catch {}
146
- }
147
- return null;
148
- }
149
-
150
- /** Picks the project's package manager from its lockfile (defaults to npm). */
151
- async function detectPackageManager(root: string): Promise<string> {
152
- const lock: [string, string][] = [
153
- ['pnpm-lock.yaml', 'pnpm'],
154
- ['yarn.lock', 'yarn'],
155
- ['bun.lockb', 'bun'],
156
- ];
157
- for (const [file, pm] of lock) {
158
- try {
159
- await fs.access(path.join(root, file));
160
- return pm;
161
- } catch {}
162
- }
163
- return 'npm';
164
- }
165
-
166
- /** Applies the stylesheet renames, Tailwind entry, and entry imports for the new feature set. */
167
- async function applyStyleFiles(
168
- clientDir: string,
169
- from: StyleFeatures,
170
- to: StyleFeatures,
171
- ): Promise<void> {
172
- if (from.preprocessor !== to.preprocessor) {
173
- const newPath = path.join(clientDir, styleEntry(to.preprocessor));
174
- await fs.mkdir(path.dirname(newPath), { recursive: true });
175
- // Rename whatever main stylesheet actually exists (preserving its content), not an assumed
176
- // name, so we never blow away the user's styles when the on-disk extension differs.
177
- const existing = await findMainStylesheet(clientDir);
178
- if (existing && path.resolve(existing) !== path.resolve(newPath)) {
179
- await fs.rename(existing, newPath);
180
- } else if (!existing) {
181
- await fs.writeFile(newPath, '', 'utf8');
182
- }
183
- }
184
-
185
- const tailwindPath = path.join(clientDir, TAILWIND_ENTRY);
186
- if (to.tailwind && !from.tailwind) {
187
- await fs.mkdir(path.dirname(tailwindPath), { recursive: true });
188
- await fs.writeFile(tailwindPath, TAILWIND_CSS, 'utf8');
189
- } else if (!to.tailwind && from.tailwind) {
190
- await fs.rm(tailwindPath, { force: true });
191
- }
192
-
193
- for (const entry of ['toil.tsx', 'toil.jsx']) {
194
- const entryPath = path.join(clientDir, entry);
195
- try {
196
- const source = await fs.readFile(entryPath, 'utf8');
197
- await fs.writeFile(entryPath, setStyleImports(source, to), 'utf8');
198
- return;
199
- } catch {}
200
- }
201
- }
202
-
203
- /**
204
- * Applies a styling change to a project on disk (no prompts): rewrites stylesheets + the app
205
- * entry's imports and edits `package.json`. Exposed for testing and reuse; the package manager is
206
- * run separately by {@link runConfigure}.
207
- */
208
- export async function applyConfigure(
209
- clientDir: string,
210
- pkgPath: string,
211
- pkg: PackageJson,
212
- from: StyleFeatures,
213
- to: StyleFeatures,
214
- ): Promise<void> {
215
- await applyStyleFiles(clientDir, from, to);
216
- await applyPackages(pkgPath, pkg, from, to);
217
- }
218
-
219
- /** Adds/removes the managed styling packages in `package.json` (sorted devDependencies). */
220
- async function applyPackages(
221
- pkgPath: string,
222
- pkg: PackageJson,
223
- from: StyleFeatures,
224
- to: StyleFeatures,
225
- ): Promise<void> {
226
- const { add, remove } = packageDiff(from, to);
227
- const dev: Record<string, string> = { ...pkg.devDependencies };
228
- const deps: Record<string, string> = { ...pkg.dependencies };
229
- for (const name of add) dev[name] = PKG_VERSION[name] ?? 'latest';
230
- for (const name of remove) {
231
- delete dev[name];
232
- delete deps[name];
233
- }
234
- const sortedDev = Object.fromEntries(
235
- Object.entries(dev).sort(([a], [b]) => a.localeCompare(b)),
236
- );
237
- const next: PackageJson = { ...pkg, devDependencies: sortedDev };
238
- // Always reflect the pruned dependencies (or drop the key entirely if now empty), so a removed
239
- // package can't survive via the original `...pkg` spread.
240
- if (Object.keys(deps).length) next.dependencies = deps;
241
- else delete next.dependencies;
242
- await fs.writeFile(pkgPath, JSON.stringify(next, null, 4) + '\n', 'utf8');
243
- }
244
-
245
- /** Human-readable summary of what changed. */
246
- function describe(from: StyleFeatures, to: StyleFeatures): string {
247
- const lines: string[] = [];
248
- if (from.preprocessor !== to.preprocessor) {
249
- lines.push(
250
- `preprocessor: ${PREPROCESSOR_LABEL[from.preprocessor]} → ${PREPROCESSOR_LABEL[to.preprocessor]}`,
251
- );
252
- }
253
- if (from.tailwind !== to.tailwind) {
254
- lines.push(`Tailwind: ${from.tailwind ? 'on' : 'off'} → ${to.tailwind ? 'on' : 'off'}`);
255
- }
256
- const { add, remove } = packageDiff(from, to);
257
- if (add.length) lines.push(`+ ${add.join(', ')}`);
258
- if (remove.length) lines.push(`- ${remove.join(', ')}`);
259
- return lines.map((l) => dim(' ') + l).join('\n');
260
- }
261
-
262
- /** Runs the interactive configure flow. */
263
- export async function runConfigure(opts: ConfigureOptions): Promise<void> {
264
- intro(accent(' toiljs configure '));
265
- const root = path.resolve(opts.root ?? opts.cwd);
266
-
267
- const pkgPath = path.join(root, 'package.json');
268
- let pkg: PackageJson;
269
- try {
270
- pkg = JSON.parse(await fs.readFile(pkgPath, 'utf8')) as PackageJson;
271
- } catch {
272
- cancel(`No package.json in ${pc.cyan(root)}, run this inside a toiljs project.`);
273
- process.exit(1);
274
- }
275
-
276
- const clientAbsDir = await resolveClientDir(root);
277
- const deps = { ...pkg.dependencies, ...pkg.devDependencies };
278
- const current: StyleFeatures = {
279
- preprocessor: (await detectStylesheet(clientAbsDir)) ?? detectPreprocessor(deps),
280
- tailwind: detectTailwind(deps),
281
- };
282
-
283
- const currentImages = await resolveImages(root);
284
-
285
- const nonInteractive =
286
- opts.preprocessor !== undefined || opts.tailwind !== undefined || opts.images !== undefined;
287
- let target: StyleFeatures;
288
- let targetImages: boolean;
289
- if (nonInteractive) {
290
- target = {
291
- preprocessor: opts.preprocessor ?? current.preprocessor,
292
- tailwind: opts.tailwind ?? current.tailwind,
293
- };
294
- targetImages = opts.images ?? currentImages;
295
- } else {
296
- const ppChoice = await select<Preprocessor>({
297
- message: 'CSS preprocessor',
298
- options: PREPROCESSORS.map((value) => ({ value, label: PREPROCESSOR_LABEL[value] })),
299
- initialValue: current.preprocessor,
300
- });
301
- bail(ppChoice);
302
- const twChoice = await confirm({
303
- message: 'Use Tailwind CSS?',
304
- initialValue: current.tailwind,
305
- });
306
- bail(twChoice);
307
- const imChoice = await confirm({
308
- message: 'Optimize images at build time?',
309
- initialValue: currentImages,
310
- });
311
- bail(imChoice);
312
- target = { preprocessor: ppChoice, tailwind: twChoice };
313
- targetImages = imChoice;
314
- }
315
-
316
- const styleChanged =
317
- target.preprocessor !== current.preprocessor || target.tailwind !== current.tailwind;
318
- const imagesChanged = targetImages !== currentImages;
319
- if (!styleChanged && !imagesChanged) {
320
- outro('No changes, your setup is already up to date.');
321
- return;
322
- }
323
-
324
- const s = spinner();
325
- s.start('Updating project files');
326
- if (styleChanged) await applyConfigure(clientAbsDir, pkgPath, pkg, current, target);
327
- let imagesWarning = '';
328
- if (imagesChanged && !(await writeImagesFlag(root, targetImages))) {
329
- imagesWarning = pc.yellow(
330
- ' Could not edit toil.config automatically, set `client.images` by hand.',
331
- );
332
- }
333
- s.stop('Updated project files');
334
-
335
- if (styleChanged) {
336
- const pm = await detectPackageManager(root);
337
- if (opts.install === false) {
338
- note(`${pc.cyan(`${pm} install`)} to sync the dependency changes.`, 'Next step');
339
- } else {
340
- const i = spinner();
341
- i.start(`Syncing dependencies with ${pm}`);
342
- try {
343
- await run(pm, ['install'], root);
344
- i.stop('Dependencies synced');
345
- } catch {
346
- i.stop(pc.yellow(`Could not run \`${pm} install\`, run it yourself to finish`));
347
- }
348
- }
349
- }
350
-
351
- const summary = [
352
- styleChanged ? describe(current, target) : '',
353
- imagesChanged
354
- ? dim(' ') +
355
- `image optimization: ${currentImages ? 'on' : 'off'} → ${targetImages ? 'on' : 'off'}`
356
- : '',
357
- imagesWarning,
358
- ]
359
- .filter(Boolean)
360
- .join('\n');
361
- note(summary, 'Updated');
362
- outro(`Reconfigured, restart \`${accent('toiljs dev')}\` to pick up the changes.`);
363
- }