@kiln-js/cli 0.1.2 → 0.1.4

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.
@@ -0,0 +1,3 @@
1
+ import { type KilnConfig } from "@kiln-js/vite-plugin";
2
+ export declare function loadConfig(): Promise<KilnConfig>;
3
+ export declare function kilnPlugins(): Promise<import("vite").PluginOption[]>;
package/dist/index.js CHANGED
@@ -7,7 +7,12 @@ import { cac } from "cac";
7
7
  // src/dev.ts
8
8
  import http from "http";
9
9
  import { createServer as createViteServer } from "vite";
10
- import { kiln } from "@kiln/vite-plugin";
10
+
11
+ // src/config.ts
12
+ import fs from "fs";
13
+ import path2 from "path";
14
+ import { pathToFileURL } from "url";
15
+ import { kiln } from "@kiln-js/vite-plugin";
11
16
 
12
17
  // src/paths.ts
13
18
  import path from "path";
@@ -21,70 +26,6 @@ var paths = {
21
26
  serverOut: path.resolve(root, "dist/server")
22
27
  };
23
28
 
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/client";
40
-
41
- createClient().then((client) => client.start());
42
- `;
43
- var DEFAULT_SERVER = `import { createAppHandler, type KilnAssets } from "@kiln/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
-
88
29
  // src/log.ts
89
30
  var useColor = Boolean(process.stdout.isTTY) && !process.env.NO_COLOR;
90
31
  var sgr = (open, close) => (s) => useColor ? `\x1B[${open}m${s}\x1B[${close}m` : s;
@@ -211,13 +152,95 @@ function banner(version) {
211
152
  `);
212
153
  }
213
154
 
214
- // src/errors.ts
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
215
174
  import fs2 from "fs";
216
175
  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";
217
240
  var norm = (p) => p.replace(/\\/g, "/");
218
241
  var rootNorm = norm(paths.root);
219
242
  var toError = (err) => err instanceof Error ? err : new Error(String(err));
220
- var rel = (file) => norm(path3.relative(paths.root, file));
243
+ var rel = (file) => norm(path4.relative(paths.root, file));
221
244
  var esc = (s) => s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
222
245
  var editorLink = (f) => `vscode://file/${encodeURI(norm(f.file))}:${f.line}:${f.column}`;
223
246
  function parseFrames(stack) {
@@ -241,11 +264,11 @@ var isUserFile = (file) => {
241
264
  return n.startsWith(rootNorm) && !n.includes("/node_modules/");
242
265
  };
243
266
  function findOrigin(frames) {
244
- return frames.find((f) => isUserFile(f.file) && fs2.existsSync(f.file));
267
+ return frames.find((f) => isUserFile(f.file) && fs3.existsSync(f.file));
245
268
  }
246
269
  function codeLines(f, radius = 2) {
247
270
  try {
248
- const lines = fs2.readFileSync(f.file, "utf8").split(/\r?\n/);
271
+ const lines = fs3.readFileSync(f.file, "utf8").split(/\r?\n/);
249
272
  const from = Math.max(1, f.line - radius);
250
273
  const to = Math.min(lines.length, f.line + radius);
251
274
  const out = [];
@@ -466,7 +489,7 @@ async function startDev(port) {
466
489
  vite = await createViteServer({
467
490
  root: paths.root,
468
491
  appType: "custom",
469
- plugins: kiln({ pagesDir: paths.pagesDir, apiDir: paths.apiDir }),
492
+ plugins: await kilnPlugins(),
470
493
  logLevel: "warn",
471
494
  server: { middlewareMode: true, hmr: { server } }
472
495
  });
@@ -494,18 +517,17 @@ async function startDev(port) {
494
517
  }
495
518
 
496
519
  // src/build.ts
497
- import fs3 from "fs";
498
- import path4 from "path";
520
+ import fs4 from "fs";
521
+ import path5 from "path";
499
522
  import { build } from "vite";
500
- import { kiln as kiln2 } from "@kiln/vite-plugin";
501
- import { adaptVercel } from "@kiln/adapter-vercel";
523
+ import { adaptVercel } from "@kiln-js/adapter-vercel";
502
524
  var TARGETS = ["none", "vercel"];
503
525
  function dirSize(dir) {
504
- if (!fs3.existsSync(dir))
526
+ if (!fs4.existsSync(dir))
505
527
  return 0;
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);
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);
509
531
  }, 0);
510
532
  }
511
533
  async function runBuild(target = "none") {
@@ -515,7 +537,7 @@ async function runBuild(target = "none") {
515
537
  const total = performance.now();
516
538
  loadEnvFiles("production");
517
539
  const entries = resolveEntries();
518
- const plugins = kiln2({ pagesDir: paths.pagesDir, apiDir: paths.apiDir });
540
+ const plugins = await kilnPlugins();
519
541
  log.info(`Building for production ${colors.gray(`\xB7 target ${target}`)}`);
520
542
  await task("client bundle", () => build({
521
543
  root: paths.root,
@@ -559,8 +581,8 @@ async function runBuild(target = "none") {
559
581
  }
560
582
 
561
583
  // src/start.ts
562
- import path5 from "path";
563
- import { pathToFileURL } from "url";
584
+ import path6 from "path";
585
+ import { pathToFileURL as pathToFileURL2 } from "url";
564
586
 
565
587
  // src/compress.ts
566
588
  import { brotliCompressSync, gzipSync, constants } from "zlib";
@@ -639,7 +661,7 @@ async function runStart(port) {
639
661
  const startedAt = performance.now();
640
662
  loadEnvFiles("production");
641
663
  const { clientRel } = resolveEntries();
642
- const manifest = JSON.parse(await Bun.file(path5.join(paths.clientOut, ".vite/manifest.json")).text());
664
+ const manifest = JSON.parse(await Bun.file(path6.join(paths.clientOut, ".vite/manifest.json")).text());
643
665
  const entry = manifest[clientRel];
644
666
  if (!entry)
645
667
  throw new Error(`No manifest entry for ${clientRel}. Run "kiln build" first.`);
@@ -648,7 +670,7 @@ async function runStart(port) {
648
670
  `),
649
671
  entry: `/${entry.file}`
650
672
  };
651
- const serverEntry = await import(pathToFileURL(path5.join(paths.serverOut, "server.js")).href);
673
+ const serverEntry = await import(pathToFileURL2(path6.join(paths.serverOut, "server.js")).href);
652
674
  const handler = serverEntry.createHandler(assets);
653
675
  const server = Bun.serve({
654
676
  port,
@@ -658,8 +680,8 @@ async function runStart(port) {
658
680
  const requestId = crypto.randomUUID().slice(0, 8);
659
681
  try {
660
682
  if (pathname !== "/") {
661
- const filePath = path5.join(paths.clientOut, decodeURIComponent(pathname));
662
- if (filePath.startsWith(paths.clientOut + path5.sep)) {
683
+ const filePath = path6.join(paths.clientOut, decodeURIComponent(pathname));
684
+ if (filePath.startsWith(paths.clientOut + path6.sep)) {
663
685
  const asset = await serveStatic(req, filePath);
664
686
  if (asset)
665
687
  return asset;
@@ -695,18 +717,18 @@ async function runStart(port) {
695
717
  }
696
718
 
697
719
  // src/routes.ts
698
- import fs4 from "fs";
699
- import path6 from "path";
700
- import { scanRoutes, scanApi } from "@kiln/core/scan";
720
+ import fs5 from "fs";
721
+ import path7 from "path";
722
+ import { scanRoutes, scanApi } from "@kiln-js/core/scan";
701
723
  var METHOD_RE = /export\s+(?:async\s+)?(?:function|const)\s+(GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS)\b/g;
702
724
  var MODE_COLORS = {
703
725
  universal: colors.mint,
704
726
  server: colors.sky,
705
727
  client: colors.violet
706
728
  };
707
- var rel2 = (file) => path6.relative(paths.root, file).split(path6.sep).join("/");
729
+ var rel2 = (file) => path7.relative(paths.root, file).split(path7.sep).join("/");
708
730
  function methodsOf(file) {
709
- const src = fs4.readFileSync(file, "utf8");
731
+ const src = fs5.readFileSync(file, "utf8");
710
732
  return [...src.matchAll(METHOD_RE)].map((m) => m[1]);
711
733
  }
712
734
  function printRoutes() {
@@ -734,14 +756,14 @@ function printRoutes() {
734
756
  }
735
757
 
736
758
  // src/create.ts
737
- import fs5 from "fs";
738
- import path7 from "path";
759
+ import fs6 from "fs";
760
+ import path8 from "path";
739
761
  import inquirer from "inquirer";
740
762
 
741
763
  // src/templates.ts
742
- var SCOPE = "@klin-js";
764
+ var SCOPE = "@kiln-js";
743
765
  var CLI_PACKAGE = "kiln";
744
- var KILN_VERSION = "0.1.0";
766
+ var KILN_VERSION = "0.1.3";
745
767
  var VITE_VERSION = "5.4.21";
746
768
  var pkg = (name) => `${SCOPE}/${name}`;
747
769
  var kilnDep = (linked) => linked ? "*" : `^${KILN_VERSION}`;
@@ -881,7 +903,7 @@ function projectFiles(o) {
881
903
  "@types/bun": "latest",
882
904
  "@types/react": "^18.3.0",
883
905
  "@types/react-dom": "^18.3.0",
884
- [CLI_PACKAGE]: kilnDep(o.linked),
906
+ [pkg("cli")]: kilnDep(o.linked),
885
907
  typescript: "^5.6.0",
886
908
  vite: VITE_VERSION
887
909
  }
@@ -995,8 +1017,8 @@ async function runCreate(nameArg) {
995
1017
  customEntries: extras.includes("entries"),
996
1018
  linked: answers.linked
997
1019
  };
998
- const dir = path7.resolve(process.cwd(), options.name);
999
- if (fs5.existsSync(dir) && fs5.readdirSync(dir).length > 0) {
1020
+ const dir = path8.resolve(process.cwd(), options.name);
1021
+ if (fs6.existsSync(dir) && fs6.readdirSync(dir).length > 0) {
1000
1022
  const { overwrite } = await inquirer.prompt([
1001
1023
  {
1002
1024
  type: "confirm",
@@ -1012,9 +1034,9 @@ async function runCreate(nameArg) {
1012
1034
  }
1013
1035
  log.step(`Writing project to ${dir}`);
1014
1036
  for (const [rel, content] of Object.entries(projectFiles(options))) {
1015
- const target = path7.join(dir, rel);
1016
- fs5.mkdirSync(path7.dirname(target), { recursive: true });
1017
- fs5.writeFileSync(target, content);
1037
+ const target = path8.join(dir, rel);
1038
+ fs6.mkdirSync(path8.dirname(target), { recursive: true });
1039
+ fs6.writeFileSync(target, content);
1018
1040
  }
1019
1041
  log.success(`Created ${options.name}`);
1020
1042
  if (answers.install) {
@@ -1027,8 +1049,8 @@ async function runCreate(nameArg) {
1027
1049
  }
1028
1050
 
1029
1051
  // src/generate.ts
1030
- import fs6 from "fs";
1031
- import path8 from "path";
1052
+ import fs7 from "fs";
1053
+ import path9 from "path";
1032
1054
  import inquirer2 from "inquirer";
1033
1055
  var SUFFIX2 = { universal: "", server: ".server", client: ".client" };
1034
1056
  var ALL_METHODS = ["GET", "POST", "PUT", "PATCH", "DELETE"];
@@ -1072,7 +1094,7 @@ function apiTemplate(methods) {
1072
1094
  }
1073
1095
  `;
1074
1096
  });
1075
- return `import type { ApiContext } from "@kiln/server";
1097
+ return `import type { ApiContext } from "@kiln-js/server";
1076
1098
 
1077
1099
  ${handlers.join(`
1078
1100
  `)}`;
@@ -1085,10 +1107,10 @@ export default function Layout({ children }: { children: ReactNode }) {
1085
1107
  `;
1086
1108
  function targetFor(kind, name, mode) {
1087
1109
  if (kind === "page")
1088
- return path8.join(paths.pagesDir, `${name}${SUFFIX2[mode]}.tsx`);
1110
+ return path9.join(paths.pagesDir, `${name}${SUFFIX2[mode]}.tsx`);
1089
1111
  if (kind === "api")
1090
- return path8.join(paths.apiDir, `${name}.ts`);
1091
- return path8.join(paths.pagesDir, name, "_layout.tsx");
1112
+ return path9.join(paths.apiDir, `${name}.ts`);
1113
+ return path9.join(paths.pagesDir, name, "_layout.tsx");
1092
1114
  }
1093
1115
  async function runGenerate(kindArg, nameArg, opts = {}) {
1094
1116
  resolveEntries();
@@ -1156,13 +1178,13 @@ async function runGenerate(kindArg, nameArg, opts = {}) {
1156
1178
  throw new Error("Pick at least one HTTP method");
1157
1179
  }
1158
1180
  const file = targetFor(kind, name, mode);
1159
- if (fs6.existsSync(file)) {
1160
- throw new Error(`${path8.relative(paths.root, file)} already exists`);
1181
+ if (fs7.existsSync(file)) {
1182
+ throw new Error(`${path9.relative(paths.root, file)} already exists`);
1161
1183
  }
1162
1184
  const content = kind === "page" ? pageTemplate(name, loader) : kind === "api" ? apiTemplate(methods) : LAYOUT_TEMPLATE;
1163
- fs6.mkdirSync(path8.dirname(file), { recursive: true });
1164
- fs6.writeFileSync(file, content);
1165
- log.success(`Created ${path8.relative(paths.root, file).replace(/\\/g, "/")}`);
1185
+ fs7.mkdirSync(path9.dirname(file), { recursive: true });
1186
+ fs7.writeFileSync(file, content);
1187
+ log.success(`Created ${path9.relative(paths.root, file).replace(/\\/g, "/")}`);
1166
1188
  log.info("Run `kiln routes` to see the new route.");
1167
1189
  }
1168
1190
 
package/package.json CHANGED
@@ -1,28 +1,28 @@
1
- {
2
- "name": "@kiln-js/cli",
3
- "version": "0.1.2",
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": "*",
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.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
+ }
28
28
  }