@kiln-js/cli 0.1.4 → 0.1.5

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 (2) hide show
  1. package/dist/index.js +104 -126
  2. package/package.json +27 -27
package/dist/index.js CHANGED
@@ -7,11 +7,6 @@ import { cac } from "cac";
7
7
  // src/dev.ts
8
8
  import http from "http";
9
9
  import { createServer as createViteServer } from "vite";
10
-
11
- // src/config.ts
12
- import fs from "fs";
13
- import path2 from "path";
14
- import { pathToFileURL } from "url";
15
10
  import { kiln } from "@kiln-js/vite-plugin";
16
11
 
17
12
  // src/paths.ts
@@ -26,6 +21,70 @@ var paths = {
26
21
  serverOut: path.resolve(root, "dist/server")
27
22
  };
28
23
 
24
+ // src/entries.ts
25
+ import fs from "fs";
26
+ import path2 from "path";
27
+ var EXTENSIONS = ["tsx", "ts", "jsx", "js"];
28
+ var ENV_TYPES = `// Generated by Kiln. Do not edit.
29
+ export {};
30
+
31
+ declare global {
32
+ namespace JSX {
33
+ interface IntrinsicElements {
34
+ "kiln:title": { children?: string | number | (string | number)[] };
35
+ }
36
+ }
37
+ }
38
+ `;
39
+ var DEFAULT_CLIENT = `import { createClient } from "@kiln-js/client";
40
+
41
+ createClient().then((client) => client.start());
42
+ `;
43
+ var DEFAULT_SERVER = `import { createAppHandler, type KilnAssets } from "@kiln-js/server";
44
+
45
+ export function createHandler(assets: KilnAssets) {
46
+ return createAppHandler({ assets });
47
+ }
48
+ `;
49
+ function ensureKilnDir() {
50
+ fs.mkdirSync(paths.kilnDir, { recursive: true });
51
+ }
52
+ function findUserEntry(name) {
53
+ for (const ext of EXTENSIONS) {
54
+ const file = path2.join(paths.root, "app", `${name}.${ext}`);
55
+ if (fs.existsSync(file))
56
+ return file;
57
+ }
58
+ return null;
59
+ }
60
+ function writeGenerated(name, source) {
61
+ ensureKilnDir();
62
+ const target = path2.join(paths.kilnDir, name);
63
+ fs.writeFileSync(target, source);
64
+ return target;
65
+ }
66
+ function resolveEntries() {
67
+ writeGenerated("env.d.ts", ENV_TYPES);
68
+ const client = findUserEntry("client") ?? writeGenerated("client.tsx", DEFAULT_CLIENT);
69
+ const server = findUserEntry("server") ?? writeGenerated("server.ts", DEFAULT_SERVER);
70
+ return {
71
+ client,
72
+ server,
73
+ clientRel: path2.relative(paths.root, client).split(path2.sep).join("/")
74
+ };
75
+ }
76
+
77
+ // src/env.ts
78
+ import { loadEnv } from "vite";
79
+ function loadEnvFiles(mode) {
80
+ const env = loadEnv(mode, paths.root, "");
81
+ for (const [key, value] of Object.entries(env)) {
82
+ if (process.env[key] === undefined)
83
+ process.env[key] = value;
84
+ }
85
+ return env;
86
+ }
87
+
29
88
  // src/log.ts
30
89
  var useColor = Boolean(process.stdout.isTTY) && !process.env.NO_COLOR;
31
90
  var sgr = (open, close) => (s) => useColor ? `\x1B[${open}m${s}\x1B[${close}m` : s;
@@ -152,95 +211,13 @@ function banner(version) {
152
211
  `);
153
212
  }
154
213
 
155
- // src/config.ts
156
- var CANDIDATES = ["kiln.config.ts", "kiln.config.mjs", "kiln.config.js"];
157
- async function loadConfig() {
158
- const file = CANDIDATES.map((f) => path2.join(paths.root, f)).find((f) => fs.existsSync(f));
159
- if (!file)
160
- return {};
161
- const mod = await import(pathToFileURL(file).href);
162
- const config = mod.default ?? {};
163
- if (config.plugins?.length) {
164
- log.step(`plugins: ${config.plugins.map((p) => p.name).join(", ")}`);
165
- }
166
- return config;
167
- }
168
- async function kilnPlugins() {
169
- const config = await loadConfig();
170
- return kiln({ pagesDir: paths.pagesDir, apiDir: paths.apiDir, plugins: config.plugins });
171
- }
172
-
173
- // src/entries.ts
214
+ // src/errors.ts
174
215
  import fs2 from "fs";
175
216
  import path3 from "path";
176
- var EXTENSIONS = ["tsx", "ts", "jsx", "js"];
177
- var ENV_TYPES = `// Generated by Kiln. Do not edit.
178
- export {};
179
-
180
- declare global {
181
- namespace JSX {
182
- interface IntrinsicElements {
183
- "kiln:title": { children?: string | number | (string | number)[] };
184
- }
185
- }
186
- }
187
- `;
188
- var DEFAULT_CLIENT = `import { createClient } from "@kiln-js/client";
189
-
190
- createClient().then((client) => client.start());
191
- `;
192
- var DEFAULT_SERVER = `import { createAppHandler, type KilnAssets } from "@kiln-js/server";
193
-
194
- export function createHandler(assets: KilnAssets) {
195
- return createAppHandler({ assets });
196
- }
197
- `;
198
- function ensureKilnDir() {
199
- fs2.mkdirSync(paths.kilnDir, { recursive: true });
200
- }
201
- function findUserEntry(name) {
202
- for (const ext of EXTENSIONS) {
203
- const file = path3.join(paths.root, "app", `${name}.${ext}`);
204
- if (fs2.existsSync(file))
205
- return file;
206
- }
207
- return null;
208
- }
209
- function writeGenerated(name, source) {
210
- ensureKilnDir();
211
- const target = path3.join(paths.kilnDir, name);
212
- fs2.writeFileSync(target, source);
213
- return target;
214
- }
215
- function resolveEntries() {
216
- writeGenerated("env.d.ts", ENV_TYPES);
217
- const client = findUserEntry("client") ?? writeGenerated("client.tsx", DEFAULT_CLIENT);
218
- const server = findUserEntry("server") ?? writeGenerated("server.ts", DEFAULT_SERVER);
219
- return {
220
- client,
221
- server,
222
- clientRel: path3.relative(paths.root, client).split(path3.sep).join("/")
223
- };
224
- }
225
-
226
- // src/env.ts
227
- import { loadEnv } from "vite";
228
- function loadEnvFiles(mode) {
229
- const env = loadEnv(mode, paths.root, "");
230
- for (const [key, value] of Object.entries(env)) {
231
- if (process.env[key] === undefined)
232
- process.env[key] = value;
233
- }
234
- return env;
235
- }
236
-
237
- // src/errors.ts
238
- import fs3 from "fs";
239
- import path4 from "path";
240
217
  var norm = (p) => p.replace(/\\/g, "/");
241
218
  var rootNorm = norm(paths.root);
242
219
  var toError = (err) => err instanceof Error ? err : new Error(String(err));
243
- var rel = (file) => norm(path4.relative(paths.root, file));
220
+ var rel = (file) => norm(path3.relative(paths.root, file));
244
221
  var esc = (s) => s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
245
222
  var editorLink = (f) => `vscode://file/${encodeURI(norm(f.file))}:${f.line}:${f.column}`;
246
223
  function parseFrames(stack) {
@@ -264,11 +241,11 @@ var isUserFile = (file) => {
264
241
  return n.startsWith(rootNorm) && !n.includes("/node_modules/");
265
242
  };
266
243
  function findOrigin(frames) {
267
- return frames.find((f) => isUserFile(f.file) && fs3.existsSync(f.file));
244
+ return frames.find((f) => isUserFile(f.file) && fs2.existsSync(f.file));
268
245
  }
269
246
  function codeLines(f, radius = 2) {
270
247
  try {
271
- const lines = fs3.readFileSync(f.file, "utf8").split(/\r?\n/);
248
+ const lines = fs2.readFileSync(f.file, "utf8").split(/\r?\n/);
272
249
  const from = Math.max(1, f.line - radius);
273
250
  const to = Math.min(lines.length, f.line + radius);
274
251
  const out = [];
@@ -489,7 +466,7 @@ async function startDev(port) {
489
466
  vite = await createViteServer({
490
467
  root: paths.root,
491
468
  appType: "custom",
492
- plugins: await kilnPlugins(),
469
+ plugins: kiln({ pagesDir: paths.pagesDir, apiDir: paths.apiDir }),
493
470
  logLevel: "warn",
494
471
  server: { middlewareMode: true, hmr: { server } }
495
472
  });
@@ -517,17 +494,18 @@ async function startDev(port) {
517
494
  }
518
495
 
519
496
  // src/build.ts
520
- import fs4 from "fs";
521
- import path5 from "path";
497
+ import fs3 from "fs";
498
+ import path4 from "path";
522
499
  import { build } from "vite";
523
500
  import { adaptVercel } from "@kiln-js/adapter-vercel";
501
+ import { kiln as kiln2 } from "@kiln-js/vite-plugin";
524
502
  var TARGETS = ["none", "vercel"];
525
503
  function dirSize(dir) {
526
- if (!fs4.existsSync(dir))
504
+ if (!fs3.existsSync(dir))
527
505
  return 0;
528
- return fs4.readdirSync(dir, { withFileTypes: true }).reduce((sum, entry) => {
529
- const full = path5.join(dir, entry.name);
530
- return sum + (entry.isDirectory() ? dirSize(full) : fs4.statSync(full).size);
506
+ return fs3.readdirSync(dir, { withFileTypes: true }).reduce((sum, entry) => {
507
+ const full = path4.join(dir, entry.name);
508
+ return sum + (entry.isDirectory() ? dirSize(full) : fs3.statSync(full).size);
531
509
  }, 0);
532
510
  }
533
511
  async function runBuild(target = "none") {
@@ -537,7 +515,7 @@ async function runBuild(target = "none") {
537
515
  const total = performance.now();
538
516
  loadEnvFiles("production");
539
517
  const entries = resolveEntries();
540
- const plugins = await kilnPlugins();
518
+ const plugins = kiln2({ pagesDir: paths.pagesDir, apiDir: paths.apiDir });
541
519
  log.info(`Building for production ${colors.gray(`\xB7 target ${target}`)}`);
542
520
  await task("client bundle", () => build({
543
521
  root: paths.root,
@@ -581,8 +559,8 @@ async function runBuild(target = "none") {
581
559
  }
582
560
 
583
561
  // src/start.ts
584
- import path6 from "path";
585
- import { pathToFileURL as pathToFileURL2 } from "url";
562
+ import path5 from "path";
563
+ import { pathToFileURL } from "url";
586
564
 
587
565
  // src/compress.ts
588
566
  import { brotliCompressSync, gzipSync, constants } from "zlib";
@@ -661,7 +639,7 @@ async function runStart(port) {
661
639
  const startedAt = performance.now();
662
640
  loadEnvFiles("production");
663
641
  const { clientRel } = resolveEntries();
664
- const manifest = JSON.parse(await Bun.file(path6.join(paths.clientOut, ".vite/manifest.json")).text());
642
+ const manifest = JSON.parse(await Bun.file(path5.join(paths.clientOut, ".vite/manifest.json")).text());
665
643
  const entry = manifest[clientRel];
666
644
  if (!entry)
667
645
  throw new Error(`No manifest entry for ${clientRel}. Run "kiln build" first.`);
@@ -670,7 +648,7 @@ async function runStart(port) {
670
648
  `),
671
649
  entry: `/${entry.file}`
672
650
  };
673
- const serverEntry = await import(pathToFileURL2(path6.join(paths.serverOut, "server.js")).href);
651
+ const serverEntry = await import(pathToFileURL(path5.join(paths.serverOut, "server.js")).href);
674
652
  const handler = serverEntry.createHandler(assets);
675
653
  const server = Bun.serve({
676
654
  port,
@@ -680,8 +658,8 @@ async function runStart(port) {
680
658
  const requestId = crypto.randomUUID().slice(0, 8);
681
659
  try {
682
660
  if (pathname !== "/") {
683
- const filePath = path6.join(paths.clientOut, decodeURIComponent(pathname));
684
- if (filePath.startsWith(paths.clientOut + path6.sep)) {
661
+ const filePath = path5.join(paths.clientOut, decodeURIComponent(pathname));
662
+ if (filePath.startsWith(paths.clientOut + path5.sep)) {
685
663
  const asset = await serveStatic(req, filePath);
686
664
  if (asset)
687
665
  return asset;
@@ -717,8 +695,8 @@ async function runStart(port) {
717
695
  }
718
696
 
719
697
  // src/routes.ts
720
- import fs5 from "fs";
721
- import path7 from "path";
698
+ import fs4 from "fs";
699
+ import path6 from "path";
722
700
  import { scanRoutes, scanApi } from "@kiln-js/core/scan";
723
701
  var METHOD_RE = /export\s+(?:async\s+)?(?:function|const)\s+(GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS)\b/g;
724
702
  var MODE_COLORS = {
@@ -726,9 +704,9 @@ var MODE_COLORS = {
726
704
  server: colors.sky,
727
705
  client: colors.violet
728
706
  };
729
- var rel2 = (file) => path7.relative(paths.root, file).split(path7.sep).join("/");
707
+ var rel2 = (file) => path6.relative(paths.root, file).split(path6.sep).join("/");
730
708
  function methodsOf(file) {
731
- const src = fs5.readFileSync(file, "utf8");
709
+ const src = fs4.readFileSync(file, "utf8");
732
710
  return [...src.matchAll(METHOD_RE)].map((m) => m[1]);
733
711
  }
734
712
  function printRoutes() {
@@ -756,8 +734,8 @@ function printRoutes() {
756
734
  }
757
735
 
758
736
  // src/create.ts
759
- import fs6 from "fs";
760
- import path8 from "path";
737
+ import fs5 from "fs";
738
+ import path7 from "path";
761
739
  import inquirer from "inquirer";
762
740
 
763
741
  // src/templates.ts
@@ -1017,8 +995,8 @@ async function runCreate(nameArg) {
1017
995
  customEntries: extras.includes("entries"),
1018
996
  linked: answers.linked
1019
997
  };
1020
- const dir = path8.resolve(process.cwd(), options.name);
1021
- if (fs6.existsSync(dir) && fs6.readdirSync(dir).length > 0) {
998
+ const dir = path7.resolve(process.cwd(), options.name);
999
+ if (fs5.existsSync(dir) && fs5.readdirSync(dir).length > 0) {
1022
1000
  const { overwrite } = await inquirer.prompt([
1023
1001
  {
1024
1002
  type: "confirm",
@@ -1034,9 +1012,9 @@ async function runCreate(nameArg) {
1034
1012
  }
1035
1013
  log.step(`Writing project to ${dir}`);
1036
1014
  for (const [rel, content] of Object.entries(projectFiles(options))) {
1037
- const target = path8.join(dir, rel);
1038
- fs6.mkdirSync(path8.dirname(target), { recursive: true });
1039
- fs6.writeFileSync(target, content);
1015
+ const target = path7.join(dir, rel);
1016
+ fs5.mkdirSync(path7.dirname(target), { recursive: true });
1017
+ fs5.writeFileSync(target, content);
1040
1018
  }
1041
1019
  log.success(`Created ${options.name}`);
1042
1020
  if (answers.install) {
@@ -1049,8 +1027,8 @@ async function runCreate(nameArg) {
1049
1027
  }
1050
1028
 
1051
1029
  // src/generate.ts
1052
- import fs7 from "fs";
1053
- import path9 from "path";
1030
+ import fs6 from "fs";
1031
+ import path8 from "path";
1054
1032
  import inquirer2 from "inquirer";
1055
1033
  var SUFFIX2 = { universal: "", server: ".server", client: ".client" };
1056
1034
  var ALL_METHODS = ["GET", "POST", "PUT", "PATCH", "DELETE"];
@@ -1107,10 +1085,10 @@ export default function Layout({ children }: { children: ReactNode }) {
1107
1085
  `;
1108
1086
  function targetFor(kind, name, mode) {
1109
1087
  if (kind === "page")
1110
- return path9.join(paths.pagesDir, `${name}${SUFFIX2[mode]}.tsx`);
1088
+ return path8.join(paths.pagesDir, `${name}${SUFFIX2[mode]}.tsx`);
1111
1089
  if (kind === "api")
1112
- return path9.join(paths.apiDir, `${name}.ts`);
1113
- return path9.join(paths.pagesDir, name, "_layout.tsx");
1090
+ return path8.join(paths.apiDir, `${name}.ts`);
1091
+ return path8.join(paths.pagesDir, name, "_layout.tsx");
1114
1092
  }
1115
1093
  async function runGenerate(kindArg, nameArg, opts = {}) {
1116
1094
  resolveEntries();
@@ -1178,13 +1156,13 @@ async function runGenerate(kindArg, nameArg, opts = {}) {
1178
1156
  throw new Error("Pick at least one HTTP method");
1179
1157
  }
1180
1158
  const file = targetFor(kind, name, mode);
1181
- if (fs7.existsSync(file)) {
1182
- throw new Error(`${path9.relative(paths.root, file)} already exists`);
1159
+ if (fs6.existsSync(file)) {
1160
+ throw new Error(`${path8.relative(paths.root, file)} already exists`);
1183
1161
  }
1184
1162
  const content = kind === "page" ? pageTemplate(name, loader) : kind === "api" ? apiTemplate(methods) : LAYOUT_TEMPLATE;
1185
- fs7.mkdirSync(path9.dirname(file), { recursive: true });
1186
- fs7.writeFileSync(file, content);
1187
- log.success(`Created ${path9.relative(paths.root, file).replace(/\\/g, "/")}`);
1163
+ fs6.mkdirSync(path8.dirname(file), { recursive: true });
1164
+ fs6.writeFileSync(file, content);
1165
+ log.success(`Created ${path8.relative(paths.root, file).replace(/\\/g, "/")}`);
1188
1166
  log.info("Run `kiln routes` to see the new route.");
1189
1167
  }
1190
1168
 
package/package.json CHANGED
@@ -1,28 +1,28 @@
1
- {
2
- "name": "@kiln-js/cli",
3
- "version": "0.1.4",
4
- "type": "module",
5
- "bin": {
6
- "kiln": "./dist/index.js"
7
- },
8
- "files": [
9
- "dist"
10
- ],
11
- "scripts": {
12
- "build": "bun build ./src/index.ts --outdir ./dist --target bun --packages external --banner '#!/usr/bin/env bun' && tsc -p tsconfig.json --emitDeclarationOnly",
13
- "typecheck": "tsc -p tsconfig.json --noEmit"
14
- },
15
- "dependencies": {
16
- "@kiln-js/adapter-vercel": "*",
17
- "@kiln-js/core": "*",
18
- "@kiln-js/server": "*",
19
- "@kiln-js/vite-plugin": "*",
20
- "cac": "^6.7.14",
21
- "inquirer": "^9.3.7",
22
- "vite": "5.4.21"
23
- },
24
- "devDependencies": {
25
- "@types/inquirer": "^9.0.7",
26
- "@types/node": "^22.0.0"
27
- }
1
+ {
2
+ "name": "@kiln-js/cli",
3
+ "version": "0.1.5",
4
+ "type": "module",
5
+ "bin": {
6
+ "kiln": "./dist/index.js"
7
+ },
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "scripts": {
12
+ "build": "bun build ./src/index.ts --outdir ./dist --target bun --packages external --banner \u0027#!/usr/bin/env bun\u0027 \u0026\u0026 tsc -p tsconfig.json --emitDeclarationOnly",
13
+ "typecheck": "tsc -p tsconfig.json --noEmit"
14
+ },
15
+ "dependencies": {
16
+ "@kiln-js/adapter-vercel": "^0.1.3",
17
+ "@kiln-js/core": "^0.1.4",
18
+ "@kiln-js/server": "^0.1.3",
19
+ "@kiln-js/vite-plugin": "^0.1.5",
20
+ "cac": "^6.7.14",
21
+ "inquirer": "^9.3.7",
22
+ "vite": "5.4.21"
23
+ },
24
+ "devDependencies": {
25
+ "@types/inquirer": "^9.0.7",
26
+ "@types/node": "^22.0.0"
27
+ }
28
28
  }