@forinda/kickjs 0.1.0 → 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.
- package/README.md +197 -23
- package/dist/cli.js +765 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.d.ts +133 -3
- package/dist/index.js +829 -100
- package/dist/index.js.map +1 -1
- package/package.json +5 -1
package/dist/cli.js
ADDED
|
@@ -0,0 +1,765 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
#!/usr/bin/env node
|
|
3
|
+
"use strict";
|
|
4
|
+
var __create = Object.create;
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
7
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
9
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
|
|
28
|
+
// src/cli/cli.ts
|
|
29
|
+
var import_commander = require("commander");
|
|
30
|
+
|
|
31
|
+
// package.json
|
|
32
|
+
var package_default = {
|
|
33
|
+
name: "@forinda/kickjs",
|
|
34
|
+
version: "0.1.4",
|
|
35
|
+
description: "Express + TypeScript + Inversify starter with reactive state, request telemetry, and Spring Boot-style decorators.",
|
|
36
|
+
main: "dist/index.js",
|
|
37
|
+
types: "dist/index.d.ts",
|
|
38
|
+
exports: {
|
|
39
|
+
".": {
|
|
40
|
+
require: "./dist/index.js",
|
|
41
|
+
types: "./dist/index.d.ts"
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
files: [
|
|
45
|
+
"dist"
|
|
46
|
+
],
|
|
47
|
+
bin: {
|
|
48
|
+
kick: "dist/cli.js"
|
|
49
|
+
},
|
|
50
|
+
scripts: {
|
|
51
|
+
clean: "rm -rf dist",
|
|
52
|
+
dev: "ts-node-dev --respawn --transpile-only src/main.ts",
|
|
53
|
+
check: "tsc --noEmit",
|
|
54
|
+
build: "tsup",
|
|
55
|
+
start: "node dist/index.js",
|
|
56
|
+
prepublishOnly: "npm run build && npm run check && npm test",
|
|
57
|
+
test: "vitest run",
|
|
58
|
+
"test:watch": "vitest",
|
|
59
|
+
prepare: "husky"
|
|
60
|
+
},
|
|
61
|
+
keywords: [
|
|
62
|
+
"express",
|
|
63
|
+
"typescript",
|
|
64
|
+
"inversify",
|
|
65
|
+
"decorators",
|
|
66
|
+
"starter"
|
|
67
|
+
],
|
|
68
|
+
author: "",
|
|
69
|
+
license: "MIT",
|
|
70
|
+
engines: {
|
|
71
|
+
node: ">=18.17"
|
|
72
|
+
},
|
|
73
|
+
publishConfig: {
|
|
74
|
+
access: "public"
|
|
75
|
+
},
|
|
76
|
+
dependencies: {
|
|
77
|
+
commander: "^11.1.0",
|
|
78
|
+
express: "^5.1.0",
|
|
79
|
+
inversify: "^7.10.0",
|
|
80
|
+
"reflect-metadata": "^0.2.2"
|
|
81
|
+
},
|
|
82
|
+
devDependencies: {
|
|
83
|
+
"@swc/core": "^1.7.28",
|
|
84
|
+
"@types/express": "^5.0.3",
|
|
85
|
+
"@types/node": "^24.5.2",
|
|
86
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
87
|
+
husky: "^9.1.7",
|
|
88
|
+
joi: "^18.0.1",
|
|
89
|
+
"node-mocks-http": "^1.17.2",
|
|
90
|
+
"ts-node": "^10.9.2",
|
|
91
|
+
"ts-node-dev": "^2.0.0",
|
|
92
|
+
tsup: "^8.5.0",
|
|
93
|
+
typescript: "^5.9.2",
|
|
94
|
+
vitest: "^3.2.4",
|
|
95
|
+
zod: "^4.1.9"
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
// src/cli/tasks/init-project.ts
|
|
100
|
+
var import_node_path2 = __toESM(require("path"));
|
|
101
|
+
|
|
102
|
+
// src/cli/utils/fs.ts
|
|
103
|
+
var import_node_fs = require("fs");
|
|
104
|
+
var import_node_path = __toESM(require("path"));
|
|
105
|
+
async function ensureDirectory(target) {
|
|
106
|
+
await import_node_fs.promises.mkdir(target, {
|
|
107
|
+
recursive: true
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
__name(ensureDirectory, "ensureDirectory");
|
|
111
|
+
async function writeFileSafe(filePath, content, options = {}) {
|
|
112
|
+
const { force = false } = options;
|
|
113
|
+
try {
|
|
114
|
+
if (!force) {
|
|
115
|
+
await import_node_fs.promises.access(filePath);
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
} catch (error) {
|
|
119
|
+
}
|
|
120
|
+
await ensureDirectory(import_node_path.default.dirname(filePath));
|
|
121
|
+
await import_node_fs.promises.writeFile(filePath, content, "utf8");
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
__name(writeFileSafe, "writeFileSafe");
|
|
125
|
+
async function fileExists(filePath) {
|
|
126
|
+
try {
|
|
127
|
+
await import_node_fs.promises.access(filePath);
|
|
128
|
+
return true;
|
|
129
|
+
} catch (error) {
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
__name(fileExists, "fileExists");
|
|
134
|
+
async function readJsonFile(filePath) {
|
|
135
|
+
try {
|
|
136
|
+
const raw = await import_node_fs.promises.readFile(filePath, "utf8");
|
|
137
|
+
return JSON.parse(raw);
|
|
138
|
+
} catch (error) {
|
|
139
|
+
return void 0;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
__name(readJsonFile, "readJsonFile");
|
|
143
|
+
async function writeJsonFile(filePath, data, options = {}) {
|
|
144
|
+
await writeFileSafe(filePath, `${JSON.stringify(data, null, 2)}
|
|
145
|
+
`, options);
|
|
146
|
+
}
|
|
147
|
+
__name(writeJsonFile, "writeJsonFile");
|
|
148
|
+
|
|
149
|
+
// src/cli/tasks/init-project.ts
|
|
150
|
+
async function initProject(options) {
|
|
151
|
+
const targetDir = import_node_path2.default.resolve(process.cwd(), options.targetDirectory || ".");
|
|
152
|
+
const force = options.force ?? false;
|
|
153
|
+
const structure = normalizeStructure(options.structure);
|
|
154
|
+
const createdFiles = [];
|
|
155
|
+
const skippedFiles = [];
|
|
156
|
+
await ensureDirectory(targetDir);
|
|
157
|
+
const packageJsonPath = import_node_path2.default.join(targetDir, "package.json");
|
|
158
|
+
const packageJson = await readJsonFile(packageJsonPath) ?? {};
|
|
159
|
+
if (Object.keys(packageJson).length === 0) {
|
|
160
|
+
const name = options.packageName ?? (import_node_path2.default.basename(targetDir).replace(/[^a-zA-Z0-9-_]/g, "-") || "kick-app");
|
|
161
|
+
await writeJsonFile(packageJsonPath, {
|
|
162
|
+
name,
|
|
163
|
+
version: "0.1.0",
|
|
164
|
+
private: true,
|
|
165
|
+
type: "commonjs",
|
|
166
|
+
scripts: {
|
|
167
|
+
dev: "tsx watch src/main.ts",
|
|
168
|
+
build: "tsup",
|
|
169
|
+
start: "node dist/main.js",
|
|
170
|
+
test: "vitest"
|
|
171
|
+
},
|
|
172
|
+
dependencies: {
|
|
173
|
+
"@forinda/kickjs": "latest"
|
|
174
|
+
},
|
|
175
|
+
devDependencies: {
|
|
176
|
+
typescript: "^5.4.0",
|
|
177
|
+
tsx: "^4.7.0",
|
|
178
|
+
tsup: "^8.0.0",
|
|
179
|
+
vitest: "^3.0.0"
|
|
180
|
+
}
|
|
181
|
+
}, {
|
|
182
|
+
force: true
|
|
183
|
+
});
|
|
184
|
+
createdFiles.push("package.json");
|
|
185
|
+
} else {
|
|
186
|
+
skippedFiles.push("package.json");
|
|
187
|
+
}
|
|
188
|
+
const tsconfigPath = import_node_path2.default.join(targetDir, "tsconfig.json");
|
|
189
|
+
const tsconfigExists = await fileExists(tsconfigPath);
|
|
190
|
+
if (!tsconfigExists || force) {
|
|
191
|
+
await writeJsonFile(tsconfigPath, {
|
|
192
|
+
compilerOptions: {
|
|
193
|
+
target: "es2021",
|
|
194
|
+
module: "commonjs",
|
|
195
|
+
moduleResolution: "node",
|
|
196
|
+
lib: [
|
|
197
|
+
"es2021"
|
|
198
|
+
],
|
|
199
|
+
rootDir: "src",
|
|
200
|
+
outDir: "dist",
|
|
201
|
+
strict: true,
|
|
202
|
+
esModuleInterop: true,
|
|
203
|
+
skipLibCheck: true,
|
|
204
|
+
resolveJsonModule: true,
|
|
205
|
+
experimentalDecorators: true,
|
|
206
|
+
emitDecoratorMetadata: true
|
|
207
|
+
},
|
|
208
|
+
include: [
|
|
209
|
+
"src"
|
|
210
|
+
]
|
|
211
|
+
}, {
|
|
212
|
+
force: true
|
|
213
|
+
});
|
|
214
|
+
createdFiles.push("tsconfig.json");
|
|
215
|
+
} else {
|
|
216
|
+
skippedFiles.push("tsconfig.json");
|
|
217
|
+
}
|
|
218
|
+
const mainFilePath = import_node_path2.default.join(targetDir, "src", "main.ts");
|
|
219
|
+
const mainCreated = await writeFileSafe(mainFilePath, `import 'reflect-metadata';
|
|
220
|
+
import { bootstrap, configureApp } from '@forinda/kickjs';
|
|
221
|
+
|
|
222
|
+
configureApp({ prefix: '/api' });
|
|
223
|
+
|
|
224
|
+
async function main() {
|
|
225
|
+
const { shutdown } = await bootstrap({ port: process.env.PORT ?? 3000 });
|
|
226
|
+
process.on('SIGINT', shutdown);
|
|
227
|
+
process.on('SIGTERM', shutdown);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
main().catch((error) => {
|
|
231
|
+
console.error('Failed to bootstrap Kick app', error);
|
|
232
|
+
process.exitCode = 1;
|
|
233
|
+
});
|
|
234
|
+
`, {
|
|
235
|
+
force
|
|
236
|
+
});
|
|
237
|
+
mainCreated ? createdFiles.push("src/main.ts") : skippedFiles.push("src/main.ts");
|
|
238
|
+
const domainBase = import_node_path2.default.join(targetDir, structure.domainRoot, structure.defaultDomain);
|
|
239
|
+
for (const folder of structure.domainFolders) {
|
|
240
|
+
const folderPath = import_node_path2.default.join(domainBase, folder);
|
|
241
|
+
const exists = await fileExists(folderPath);
|
|
242
|
+
await ensureDirectory(folderPath);
|
|
243
|
+
const relativePath = import_node_path2.default.relative(targetDir, folderPath);
|
|
244
|
+
if (exists) {
|
|
245
|
+
skippedFiles.push(relativePath);
|
|
246
|
+
} else {
|
|
247
|
+
createdFiles.push(relativePath);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
const controllerFolder = structure.domainFolders.includes("controllers") ? "controllers" : structure.domainFolders[0];
|
|
251
|
+
const controllerPath = import_node_path2.default.join(domainBase, controllerFolder, "hello.get.controller.ts");
|
|
252
|
+
const controllerCreated = await writeFileSafe(controllerPath, `import type { Request, Response } from 'express';
|
|
253
|
+
import { GetController } from '@forinda/kickjs';
|
|
254
|
+
|
|
255
|
+
export default class HelloGetController extends GetController {
|
|
256
|
+
handle(_req: Request, res: Response) {
|
|
257
|
+
return this.ok(res, { message: 'Hello from Kick!' });
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
`, {
|
|
261
|
+
force
|
|
262
|
+
});
|
|
263
|
+
const relativeControllerPath = import_node_path2.default.relative(targetDir, controllerPath);
|
|
264
|
+
controllerCreated ? createdFiles.push(relativeControllerPath) : skippedFiles.push(relativeControllerPath);
|
|
265
|
+
const appConfigDir = import_node_path2.default.join(targetDir, "src", "config");
|
|
266
|
+
await ensureDirectory(appConfigDir);
|
|
267
|
+
const appConfigPath = import_node_path2.default.join(appConfigDir, "kick.config.ts");
|
|
268
|
+
const appConfigCreated = await writeFileSafe(appConfigPath, buildAppConfigFile(structure), {
|
|
269
|
+
force
|
|
270
|
+
});
|
|
271
|
+
const relativeAppConfig = import_node_path2.default.relative(targetDir, appConfigPath);
|
|
272
|
+
appConfigCreated ? createdFiles.push(relativeAppConfig) : skippedFiles.push(relativeAppConfig);
|
|
273
|
+
const configPath = import_node_path2.default.join(targetDir, "kick.config.ts");
|
|
274
|
+
const configCreated = await writeFileSafe(configPath, buildConfigFile(structure), {
|
|
275
|
+
force
|
|
276
|
+
});
|
|
277
|
+
configCreated ? createdFiles.push("kick.config.ts") : skippedFiles.push("kick.config.ts");
|
|
278
|
+
return {
|
|
279
|
+
createdFiles,
|
|
280
|
+
skippedFiles
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
__name(initProject, "initProject");
|
|
284
|
+
function normalizeStructure(structure) {
|
|
285
|
+
return {
|
|
286
|
+
domainRoot: structure?.domainRoot ?? "src/domains",
|
|
287
|
+
domainFolders: structure?.domainFolders && structure.domainFolders.length > 0 ? [
|
|
288
|
+
...structure.domainFolders
|
|
289
|
+
] : [
|
|
290
|
+
"controllers",
|
|
291
|
+
"services",
|
|
292
|
+
"domain"
|
|
293
|
+
],
|
|
294
|
+
defaultDomain: structure?.defaultDomain ?? "app"
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
__name(normalizeStructure, "normalizeStructure");
|
|
298
|
+
function buildConfigFile(structure) {
|
|
299
|
+
const controllerRoot = `${structure.domainRoot}/${structure.defaultDomain}/controllers`;
|
|
300
|
+
const structureSnippet = JSON.stringify(structure, null, 2).split("\n").map((line, index) => index === 0 ? line : ` ${line}`).join("\n");
|
|
301
|
+
return `import type { KickConfig } from '@forinda/kickjs';
|
|
302
|
+
|
|
303
|
+
const config: KickConfig = {
|
|
304
|
+
structure: ${structureSnippet},
|
|
305
|
+
generators: {
|
|
306
|
+
controllerRoot: '${controllerRoot}'
|
|
307
|
+
},
|
|
308
|
+
commands: [
|
|
309
|
+
{
|
|
310
|
+
name: 'dev',
|
|
311
|
+
description: 'Run the Kick dev server',
|
|
312
|
+
steps: 'npm run dev'
|
|
313
|
+
}
|
|
314
|
+
]
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
export default config;
|
|
318
|
+
`;
|
|
319
|
+
}
|
|
320
|
+
__name(buildConfigFile, "buildConfigFile");
|
|
321
|
+
function buildAppConfigFile(structure) {
|
|
322
|
+
const controllerRoot = `${structure.domainRoot}/${structure.defaultDomain}/controllers`;
|
|
323
|
+
const roots = [
|
|
324
|
+
controllerRoot,
|
|
325
|
+
"src/http"
|
|
326
|
+
].map((root) => `'${root}'`).join(", ");
|
|
327
|
+
return `import { createKickConfig } from '@forinda/kickjs';
|
|
328
|
+
|
|
329
|
+
export default createKickConfig({
|
|
330
|
+
defaults: {
|
|
331
|
+
prefix: '/api',
|
|
332
|
+
api: {
|
|
333
|
+
discovery: {
|
|
334
|
+
roots: [${roots}]
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
},
|
|
338
|
+
env: {
|
|
339
|
+
KICK_PREFIX: 'prefix',
|
|
340
|
+
KICK_HEALTH: 'healthEndpoint',
|
|
341
|
+
KICK_LOG_LEVEL: { path: 'logging.level' }
|
|
342
|
+
}
|
|
343
|
+
});
|
|
344
|
+
`;
|
|
345
|
+
}
|
|
346
|
+
__name(buildAppConfigFile, "buildAppConfigFile");
|
|
347
|
+
|
|
348
|
+
// src/cli/commands/init.ts
|
|
349
|
+
function registerInitCommand(program, defaults = {}) {
|
|
350
|
+
program.command("init [directory]").description("Scaffold a new Kick project in the target directory").option("-f, --force", "Overwrite existing files when scaffolding", false).option("-n, --name <packageName>", "Package name to use in package.json").action(async (directory, options) => {
|
|
351
|
+
const targetDirectory = directory ?? ".";
|
|
352
|
+
const result = await initProject({
|
|
353
|
+
targetDirectory,
|
|
354
|
+
force: options.force,
|
|
355
|
+
packageName: options.name,
|
|
356
|
+
structure: defaults.structure
|
|
357
|
+
});
|
|
358
|
+
if (result.createdFiles.length > 0) {
|
|
359
|
+
console.log(`Created files:
|
|
360
|
+
- ${result.createdFiles.join("\n - ")}`);
|
|
361
|
+
}
|
|
362
|
+
if (result.skippedFiles.length > 0) {
|
|
363
|
+
console.log(`Skipped existing files:
|
|
364
|
+
- ${result.skippedFiles.join("\n - ")}`);
|
|
365
|
+
}
|
|
366
|
+
console.log('Project scaffold complete. Run "npm install" to install dependencies.');
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
__name(registerInitCommand, "registerInitCommand");
|
|
370
|
+
|
|
371
|
+
// src/cli/tasks/generate-controller.ts
|
|
372
|
+
var import_node_path3 = __toESM(require("path"));
|
|
373
|
+
var SUPPORTED_METHODS = [
|
|
374
|
+
"get",
|
|
375
|
+
"post",
|
|
376
|
+
"put",
|
|
377
|
+
"patch",
|
|
378
|
+
"delete"
|
|
379
|
+
];
|
|
380
|
+
async function generateController(options) {
|
|
381
|
+
const method = normalizeMethod(options.method);
|
|
382
|
+
const root = options.root ?? "src/http";
|
|
383
|
+
const rootPath = import_node_path3.default.isAbsolute(root) ? root : import_node_path3.default.join(process.cwd(), root);
|
|
384
|
+
const normalizedName = options.name.replace(/\\/g, "/");
|
|
385
|
+
const segments = normalizedName.split("/").filter(Boolean);
|
|
386
|
+
const fileSegment = segments.pop() ?? "index";
|
|
387
|
+
const relativeDir = segments.join("/");
|
|
388
|
+
const fileName = `${fileSegment}.${method}.controller.ts`;
|
|
389
|
+
const targetFile = import_node_path3.default.join(rootPath, relativeDir, fileName);
|
|
390
|
+
const className = buildClassName([
|
|
391
|
+
...segments,
|
|
392
|
+
fileSegment
|
|
393
|
+
], method);
|
|
394
|
+
const methodController = `${capitalize(method)}Controller`;
|
|
395
|
+
const template = buildTemplate({
|
|
396
|
+
className,
|
|
397
|
+
methodController,
|
|
398
|
+
method,
|
|
399
|
+
tags: options.tags
|
|
400
|
+
});
|
|
401
|
+
const created = await writeFileSafe(targetFile, template, {
|
|
402
|
+
force: options.force
|
|
403
|
+
});
|
|
404
|
+
return {
|
|
405
|
+
filePath: targetFile,
|
|
406
|
+
created
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
__name(generateController, "generateController");
|
|
410
|
+
function normalizeMethod(raw) {
|
|
411
|
+
const method = (raw ?? "get").toLowerCase();
|
|
412
|
+
if (!SUPPORTED_METHODS.includes(method)) {
|
|
413
|
+
throw new Error(`Unsupported method "${raw}". Supported methods: ${SUPPORTED_METHODS.join(", ")}`);
|
|
414
|
+
}
|
|
415
|
+
return method;
|
|
416
|
+
}
|
|
417
|
+
__name(normalizeMethod, "normalizeMethod");
|
|
418
|
+
function buildTemplate(params) {
|
|
419
|
+
const tagsBlock = params.tags && params.tags.length > 0 ? ` static tags = ${JSON.stringify(params.tags)};
|
|
420
|
+
|
|
421
|
+
` : "";
|
|
422
|
+
const requestType = params.method === "get" || params.method === "delete" ? "Request" : "Request";
|
|
423
|
+
return `import type { Request, Response } from 'express';
|
|
424
|
+
import { ${params.methodController} } from '@forinda/kickjs';
|
|
425
|
+
|
|
426
|
+
export default class ${params.className} extends ${params.methodController} {
|
|
427
|
+
${tagsBlock} handle(_req: ${requestType}, res: Response) {
|
|
428
|
+
return this.ok(res, { message: '${humanize(params.method)} ${params.className}' });
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
`;
|
|
432
|
+
}
|
|
433
|
+
__name(buildTemplate, "buildTemplate");
|
|
434
|
+
function humanize(method) {
|
|
435
|
+
return method.toUpperCase();
|
|
436
|
+
}
|
|
437
|
+
__name(humanize, "humanize");
|
|
438
|
+
function buildClassName(segments, method) {
|
|
439
|
+
const normalizedSegments = segments.map((segment) => segment.replace(/\.\.\./g, "Rest")).map((segment) => segment.replace(/\[(.+?)]/g, "$1")).flatMap((segment) => segment.split(/[^a-zA-Z0-9]+/g)).filter(Boolean).map(capitalize);
|
|
440
|
+
if (normalizedSegments.length === 0) {
|
|
441
|
+
normalizedSegments.push("Root");
|
|
442
|
+
}
|
|
443
|
+
normalizedSegments.push(capitalize(method), "Controller");
|
|
444
|
+
return normalizedSegments.join("");
|
|
445
|
+
}
|
|
446
|
+
__name(buildClassName, "buildClassName");
|
|
447
|
+
function capitalize(value) {
|
|
448
|
+
return value.charAt(0).toUpperCase() + value.slice(1);
|
|
449
|
+
}
|
|
450
|
+
__name(capitalize, "capitalize");
|
|
451
|
+
|
|
452
|
+
// src/cli/tasks/generate-domain.ts
|
|
453
|
+
var import_node_path4 = __toESM(require("path"));
|
|
454
|
+
async function generateDomain(options) {
|
|
455
|
+
const structure = options.structure ?? {
|
|
456
|
+
domainRoot: "src/domains",
|
|
457
|
+
domainFolders: [
|
|
458
|
+
"controllers",
|
|
459
|
+
"services",
|
|
460
|
+
"domain"
|
|
461
|
+
],
|
|
462
|
+
defaultDomain: "app"
|
|
463
|
+
};
|
|
464
|
+
const domainRoot = structure.domainRoot ?? "src/domains";
|
|
465
|
+
const domainRootBase = import_node_path4.default.isAbsolute(domainRoot) ? domainRoot : import_node_path4.default.join(process.cwd(), domainRoot);
|
|
466
|
+
const folders = structure.domainFolders && structure.domainFolders.length > 0 ? structure.domainFolders : [
|
|
467
|
+
"controllers"
|
|
468
|
+
];
|
|
469
|
+
const targetRoot = import_node_path4.default.join(domainRootBase, options.name);
|
|
470
|
+
const createdFolders = [];
|
|
471
|
+
const existingFolders = [];
|
|
472
|
+
for (const folder of folders) {
|
|
473
|
+
const folderPath = import_node_path4.default.join(targetRoot, folder);
|
|
474
|
+
const alreadyExists = await fileExists(folderPath);
|
|
475
|
+
await ensureDirectory(folderPath);
|
|
476
|
+
if (alreadyExists) {
|
|
477
|
+
existingFolders.push(import_node_path4.default.relative(process.cwd(), folderPath));
|
|
478
|
+
} else {
|
|
479
|
+
createdFolders.push(import_node_path4.default.relative(process.cwd(), folderPath));
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
let controllerPath;
|
|
483
|
+
let controllerCreated;
|
|
484
|
+
if (options.withController !== false && folders.includes("controllers")) {
|
|
485
|
+
controllerPath = import_node_path4.default.join(targetRoot, "controllers", "index.get.controller.ts");
|
|
486
|
+
controllerCreated = await writeFileSafe(controllerPath, `import type { Request, Response } from 'express';
|
|
487
|
+
import { GetController } from '@forinda/kickjs';
|
|
488
|
+
|
|
489
|
+
export default class ${capitalize2(options.name)}IndexGetController extends GetController {
|
|
490
|
+
handle(_req: Request, res: Response) {
|
|
491
|
+
return this.ok(res, { domain: '${options.name}', message: 'Hello from ${options.name}' });
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
`, {
|
|
495
|
+
force: options.force
|
|
496
|
+
});
|
|
497
|
+
if (controllerCreated) {
|
|
498
|
+
createdFolders.push(import_node_path4.default.relative(process.cwd(), controllerPath));
|
|
499
|
+
} else if (controllerPath) {
|
|
500
|
+
existingFolders.push(import_node_path4.default.relative(process.cwd(), controllerPath));
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
return {
|
|
504
|
+
root: targetRoot,
|
|
505
|
+
createdFolders,
|
|
506
|
+
existingFolders,
|
|
507
|
+
controllerPath,
|
|
508
|
+
controllerCreated
|
|
509
|
+
};
|
|
510
|
+
}
|
|
511
|
+
__name(generateDomain, "generateDomain");
|
|
512
|
+
function capitalize2(value) {
|
|
513
|
+
if (!value) {
|
|
514
|
+
return "Domain";
|
|
515
|
+
}
|
|
516
|
+
return value.charAt(0).toUpperCase() + value.slice(1);
|
|
517
|
+
}
|
|
518
|
+
__name(capitalize2, "capitalize");
|
|
519
|
+
|
|
520
|
+
// src/cli/commands/generate-controller.ts
|
|
521
|
+
function registerGeneratorCommands(program, defaults = {}) {
|
|
522
|
+
const generate = program.command("generate").description("Generate Kick resources");
|
|
523
|
+
generate.command("domain <name>").description("Scaffold a domain folder with optional default files").option("-f, --force", "Overwrite existing generated files", false).option("--no-controller", "Do not create a default controller").action(async (name, options) => {
|
|
524
|
+
const result = await generateDomain({
|
|
525
|
+
name,
|
|
526
|
+
structure: defaults.structure,
|
|
527
|
+
force: options.force,
|
|
528
|
+
withController: options.controller
|
|
529
|
+
});
|
|
530
|
+
console.log(`Domain root: ${result.root}`);
|
|
531
|
+
if (result.createdFolders.length > 0) {
|
|
532
|
+
console.log(`Created:
|
|
533
|
+
- ${result.createdFolders.join("\n - ")}`);
|
|
534
|
+
}
|
|
535
|
+
if (result.existingFolders.length > 0) {
|
|
536
|
+
console.log(`Existing:
|
|
537
|
+
- ${result.existingFolders.join("\n - ")}`);
|
|
538
|
+
}
|
|
539
|
+
});
|
|
540
|
+
generate.command("controller <name>").description("Generate a file-system discovered controller").option("-m, --method <verb>", "HTTP verb to use (get, post, put, patch, delete)", "get").option("-r, --root <path>", "Root directory for generated controllers", defaults.controllerRoot ?? "src/http").option("-f, --force", "Overwrite the controller if it already exists", false).option("--tags <tags>", "Comma separated tags to assign to the controller").action(async (name, options) => {
|
|
541
|
+
const tags = options.tags ? options.tags.split(",").map((tag) => tag.trim()).filter(Boolean) : void 0;
|
|
542
|
+
const defaultRoot = computeDefaultControllerRoot(defaults, name);
|
|
543
|
+
const result = await generateController({
|
|
544
|
+
name,
|
|
545
|
+
method: options.method,
|
|
546
|
+
root: options.root ?? defaultRoot,
|
|
547
|
+
force: options.force,
|
|
548
|
+
tags
|
|
549
|
+
});
|
|
550
|
+
if (result.created) {
|
|
551
|
+
console.log(`Created controller at ${result.filePath}`);
|
|
552
|
+
} else {
|
|
553
|
+
console.log(`Controller already exists at ${result.filePath}`);
|
|
554
|
+
}
|
|
555
|
+
});
|
|
556
|
+
}
|
|
557
|
+
__name(registerGeneratorCommands, "registerGeneratorCommands");
|
|
558
|
+
function computeDefaultControllerRoot(defaults, name) {
|
|
559
|
+
if (defaults.controllerRoot) {
|
|
560
|
+
return defaults.controllerRoot;
|
|
561
|
+
}
|
|
562
|
+
const structure = defaults.structure;
|
|
563
|
+
if (!structure) {
|
|
564
|
+
return "src/http";
|
|
565
|
+
}
|
|
566
|
+
const domainRoot = structure.domainRoot ?? "src/domains";
|
|
567
|
+
const defaultDomain = structure.defaultDomain ?? "app";
|
|
568
|
+
if (name.includes("/")) {
|
|
569
|
+
const [maybeDomain] = name.split("/");
|
|
570
|
+
return `${domainRoot}/${maybeDomain}/controllers`;
|
|
571
|
+
}
|
|
572
|
+
return `${domainRoot}/${defaultDomain}/controllers`;
|
|
573
|
+
}
|
|
574
|
+
__name(computeDefaultControllerRoot, "computeDefaultControllerRoot");
|
|
575
|
+
|
|
576
|
+
// src/cli/config.ts
|
|
577
|
+
var import_node_fs2 = require("fs");
|
|
578
|
+
var import_node_path5 = __toESM(require("path"));
|
|
579
|
+
|
|
580
|
+
// src/utils/errors.ts
|
|
581
|
+
var AppError = class extends Error {
|
|
582
|
+
static {
|
|
583
|
+
__name(this, "AppError");
|
|
584
|
+
}
|
|
585
|
+
code;
|
|
586
|
+
status;
|
|
587
|
+
details;
|
|
588
|
+
constructor(code, message, options = {}) {
|
|
589
|
+
super(message);
|
|
590
|
+
this.code = code;
|
|
591
|
+
this.status = options.status;
|
|
592
|
+
this.details = options.details;
|
|
593
|
+
if (options.cause) {
|
|
594
|
+
this.cause = options.cause;
|
|
595
|
+
}
|
|
596
|
+
this.name = "AppError";
|
|
597
|
+
}
|
|
598
|
+
};
|
|
599
|
+
function createError(code, message, options = {}) {
|
|
600
|
+
return new AppError(code, message, options);
|
|
601
|
+
}
|
|
602
|
+
__name(createError, "createError");
|
|
603
|
+
|
|
604
|
+
// src/cli/config.ts
|
|
605
|
+
var CONFIG_FILE_CANDIDATES = [
|
|
606
|
+
"kick.config.ts",
|
|
607
|
+
"kick.config.js",
|
|
608
|
+
"kick.config.mjs",
|
|
609
|
+
"kick.config.cjs",
|
|
610
|
+
"kick.config.json"
|
|
611
|
+
];
|
|
612
|
+
var tsNodeRegistered = false;
|
|
613
|
+
async function loadKickConfig(cwd = process.cwd()) {
|
|
614
|
+
for (const file of CONFIG_FILE_CANDIDATES) {
|
|
615
|
+
const absolute = import_node_path5.default.join(cwd, file);
|
|
616
|
+
if (!(0, import_node_fs2.existsSync)(absolute)) {
|
|
617
|
+
continue;
|
|
618
|
+
}
|
|
619
|
+
if (file.endsWith(".json")) {
|
|
620
|
+
const raw = await import_node_fs2.promises.readFile(absolute, "utf8");
|
|
621
|
+
return normalizeConfig(JSON.parse(raw));
|
|
622
|
+
}
|
|
623
|
+
if (file.endsWith(".ts")) {
|
|
624
|
+
ensureTsNode();
|
|
625
|
+
}
|
|
626
|
+
const loaded = require(absolute);
|
|
627
|
+
const config = loaded?.default ?? loaded;
|
|
628
|
+
return normalizeConfig(config);
|
|
629
|
+
}
|
|
630
|
+
return void 0;
|
|
631
|
+
}
|
|
632
|
+
__name(loadKickConfig, "loadKickConfig");
|
|
633
|
+
function ensureTsNode() {
|
|
634
|
+
if (tsNodeRegistered) {
|
|
635
|
+
return;
|
|
636
|
+
}
|
|
637
|
+
try {
|
|
638
|
+
require("ts-node/register");
|
|
639
|
+
tsNodeRegistered = true;
|
|
640
|
+
} catch (error) {
|
|
641
|
+
throw createError("TS_NODE_REQUIRED", "Custom CLI config requires ts-node for TypeScript configuration files.");
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
__name(ensureTsNode, "ensureTsNode");
|
|
645
|
+
function normalizeConfig(raw) {
|
|
646
|
+
if (!raw || typeof raw !== "object") {
|
|
647
|
+
throw createError("INVALID_CLI_CONFIG", "kick.config must export an object");
|
|
648
|
+
}
|
|
649
|
+
const config = raw;
|
|
650
|
+
const normalized = {};
|
|
651
|
+
if (Array.isArray(config.commands)) {
|
|
652
|
+
normalized.commands = config.commands.map((command) => {
|
|
653
|
+
if (!command || typeof command !== "object") {
|
|
654
|
+
throw createError("INVALID_CLI_COMMAND", "Command entries must be objects");
|
|
655
|
+
}
|
|
656
|
+
if (!command.name) {
|
|
657
|
+
throw createError("INVALID_CLI_COMMAND", "Command entries must include a name");
|
|
658
|
+
}
|
|
659
|
+
return {
|
|
660
|
+
name: command.name,
|
|
661
|
+
description: command.description,
|
|
662
|
+
steps: Array.isArray(command.steps) ? [
|
|
663
|
+
...command.steps
|
|
664
|
+
] : command.steps ? [
|
|
665
|
+
command.steps
|
|
666
|
+
] : []
|
|
667
|
+
};
|
|
668
|
+
});
|
|
669
|
+
}
|
|
670
|
+
if (config.generators) {
|
|
671
|
+
normalized.generators = {
|
|
672
|
+
controllerRoot: config.generators.controllerRoot ?? void 0
|
|
673
|
+
};
|
|
674
|
+
}
|
|
675
|
+
if (config.structure) {
|
|
676
|
+
normalized.structure = normalizeStructureConfig(config.structure);
|
|
677
|
+
}
|
|
678
|
+
return normalized;
|
|
679
|
+
}
|
|
680
|
+
__name(normalizeConfig, "normalizeConfig");
|
|
681
|
+
function normalizeStructureConfig(raw) {
|
|
682
|
+
return {
|
|
683
|
+
domainRoot: raw.domainRoot ?? "src/domains",
|
|
684
|
+
domainFolders: Array.isArray(raw.domainFolders) && raw.domainFolders.length > 0 ? [
|
|
685
|
+
...raw.domainFolders
|
|
686
|
+
] : [
|
|
687
|
+
"controllers",
|
|
688
|
+
"services",
|
|
689
|
+
"domain"
|
|
690
|
+
],
|
|
691
|
+
defaultDomain: raw.defaultDomain ?? "app"
|
|
692
|
+
};
|
|
693
|
+
}
|
|
694
|
+
__name(normalizeStructureConfig, "normalizeStructureConfig");
|
|
695
|
+
|
|
696
|
+
// src/cli/utils/shell.ts
|
|
697
|
+
var import_node_child_process = require("child_process");
|
|
698
|
+
function runShellCommand(command, options = {}) {
|
|
699
|
+
return new Promise((resolve, reject) => {
|
|
700
|
+
const child = (0, import_node_child_process.spawn)(command, {
|
|
701
|
+
cwd: options.cwd,
|
|
702
|
+
stdio: "inherit",
|
|
703
|
+
shell: true
|
|
704
|
+
});
|
|
705
|
+
child.on("exit", (code) => {
|
|
706
|
+
if (code === 0) {
|
|
707
|
+
resolve();
|
|
708
|
+
} else {
|
|
709
|
+
reject(new Error(`Command failed with exit code ${code}: ${command}`));
|
|
710
|
+
}
|
|
711
|
+
});
|
|
712
|
+
child.on("error", (error) => {
|
|
713
|
+
reject(error);
|
|
714
|
+
});
|
|
715
|
+
});
|
|
716
|
+
}
|
|
717
|
+
__name(runShellCommand, "runShellCommand");
|
|
718
|
+
|
|
719
|
+
// src/cli/commands/custom.ts
|
|
720
|
+
function registerCustomCommands(program, config) {
|
|
721
|
+
if (!config?.commands) {
|
|
722
|
+
return;
|
|
723
|
+
}
|
|
724
|
+
config.commands.forEach((command) => {
|
|
725
|
+
const cmd = program.command(command.name).description(command.description ?? "Custom command defined in kick.config");
|
|
726
|
+
cmd.action(async () => {
|
|
727
|
+
if (!command.steps || command.steps.length === 0) {
|
|
728
|
+
console.warn(`No steps defined for command "${command.name}".`);
|
|
729
|
+
return;
|
|
730
|
+
}
|
|
731
|
+
for (const step of command.steps) {
|
|
732
|
+
await runShellCommand(step);
|
|
733
|
+
}
|
|
734
|
+
});
|
|
735
|
+
});
|
|
736
|
+
}
|
|
737
|
+
__name(registerCustomCommands, "registerCustomCommands");
|
|
738
|
+
|
|
739
|
+
// src/cli/cli.ts
|
|
740
|
+
async function main() {
|
|
741
|
+
const program = new import_commander.Command();
|
|
742
|
+
program.name("kick").description("Kick project CLI").version(package_default.version ?? "0.0.0");
|
|
743
|
+
const config = await loadKickConfig(process.cwd());
|
|
744
|
+
registerInitCommand(program, {
|
|
745
|
+
structure: config?.structure
|
|
746
|
+
});
|
|
747
|
+
registerGeneratorCommands(program, {
|
|
748
|
+
controllerRoot: config?.generators?.controllerRoot,
|
|
749
|
+
structure: config?.structure
|
|
750
|
+
});
|
|
751
|
+
registerCustomCommands(program, config);
|
|
752
|
+
program.showHelpAfterError();
|
|
753
|
+
try {
|
|
754
|
+
await program.parseAsync(process.argv);
|
|
755
|
+
} catch (error) {
|
|
756
|
+
console.error(error instanceof Error ? error.message : error);
|
|
757
|
+
process.exitCode = 1;
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
__name(main, "main");
|
|
761
|
+
main().catch((error) => {
|
|
762
|
+
console.error(error instanceof Error ? error.message : error);
|
|
763
|
+
process.exitCode = 1;
|
|
764
|
+
});
|
|
765
|
+
//# sourceMappingURL=cli.js.map
|