@carlos-tzin/tzin 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.
- package/dist/cli.js +117 -22
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -1,30 +1,125 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process';
|
|
2
2
|
import { fileURLToPath } from 'node:url';
|
|
3
|
+
import { resolve } from 'node:path';
|
|
4
|
+
import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
|
|
5
|
+
const [, , cmd, ...rest] = process.argv;
|
|
3
6
|
function usage() {
|
|
4
7
|
console.error(`tzin CLI
|
|
5
8
|
|
|
6
|
-
|
|
7
|
-
|
|
9
|
+
Commands:
|
|
10
|
+
tzin dev [entry] [--port N] start dev server with hot reload
|
|
11
|
+
tzin generate route <name> generate a route stub
|
|
12
|
+
tzin generate middleware <name> generate a middleware stub
|
|
13
|
+
tzin generate test <name> generate a test stub`);
|
|
8
14
|
process.exit(1);
|
|
9
15
|
}
|
|
10
|
-
|
|
11
|
-
if (cmd !== 'dev')
|
|
16
|
+
if (!cmd || cmd === '--help' || cmd === '-h')
|
|
12
17
|
usage();
|
|
13
|
-
//
|
|
14
|
-
|
|
15
|
-
let
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const entryArg = rest.find((a) => !a.startsWith('-')
|
|
21
|
-
if (entryArg)
|
|
22
|
-
|
|
23
|
-
const devServer = fileURLToPath(new URL('./dev-server.ts', import.meta.url));
|
|
24
|
-
const args = ['tsx', 'watch', '--clear-screen=false', devServer];
|
|
25
|
-
if (entry)
|
|
26
|
-
|
|
27
|
-
args.push('--port', port);
|
|
28
|
-
const child = spawn('npx', args, { stdio: 'inherit' });
|
|
29
|
-
process.on('SIGINT', () => child.kill('SIGINT'));
|
|
30
|
-
child.on('exit', (code) => process.exit(code ?? 0));
|
|
18
|
+
// ── dev ──────────────────────────────────────────────────────────────
|
|
19
|
+
if (cmd === 'dev') {
|
|
20
|
+
let entry = '';
|
|
21
|
+
let port = '3000';
|
|
22
|
+
const portFlag = rest.indexOf('--port');
|
|
23
|
+
if (portFlag !== -1 && rest[portFlag + 1])
|
|
24
|
+
port = rest[portFlag + 1];
|
|
25
|
+
const entryArg = rest.find((a) => !a.startsWith('-'));
|
|
26
|
+
if (entryArg)
|
|
27
|
+
entry = entryArg;
|
|
28
|
+
const devServer = fileURLToPath(new URL('./dev-server.ts', import.meta.url));
|
|
29
|
+
const args = ['tsx', 'watch', '--clear-screen=false', devServer];
|
|
30
|
+
if (entry)
|
|
31
|
+
args.push(entry);
|
|
32
|
+
args.push('--port', port);
|
|
33
|
+
const child = spawn('npx', args, { stdio: 'inherit' });
|
|
34
|
+
process.on('SIGINT', () => child.kill('SIGINT'));
|
|
35
|
+
child.on('exit', (code) => process.exit(code ?? 0));
|
|
36
|
+
process.exit(0);
|
|
37
|
+
}
|
|
38
|
+
// ── generate ─────────────────────────────────────────────────────────
|
|
39
|
+
if (cmd === 'generate' || cmd === 'g') {
|
|
40
|
+
const [sub, ...args] = rest;
|
|
41
|
+
const name = args[0];
|
|
42
|
+
if (!sub || !name) {
|
|
43
|
+
console.error('Usage: tzin generate <route|middleware|test> <name>');
|
|
44
|
+
process.exit(1);
|
|
45
|
+
}
|
|
46
|
+
const cwd = process.cwd();
|
|
47
|
+
if (sub === 'route' || sub === 'r') {
|
|
48
|
+
const dir = resolve(cwd, 'src/routes');
|
|
49
|
+
mkdirSync(dir, { recursive: true });
|
|
50
|
+
const file = resolve(dir, `${name}.ts`);
|
|
51
|
+
if (existsSync(file)) {
|
|
52
|
+
console.error(`File already exists: ${file}`);
|
|
53
|
+
process.exit(1);
|
|
54
|
+
}
|
|
55
|
+
const slug = name.replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '');
|
|
56
|
+
writeFileSync(file, `import { t } from '@carlos-tzin/tzin'
|
|
57
|
+
import { contract, impl } from '@carlos-tzin/tzin'
|
|
58
|
+
|
|
59
|
+
const ${slug} = contract({
|
|
60
|
+
method: 'GET',
|
|
61
|
+
path: '/${slug}',
|
|
62
|
+
responses: {
|
|
63
|
+
200: t.Object({ ok: t.Boolean() }),
|
|
64
|
+
},
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
export const ${slug}Route = impl(${slug}, async () => ({
|
|
68
|
+
status: 200 as const,
|
|
69
|
+
body: { ok: true },
|
|
70
|
+
}))
|
|
71
|
+
`);
|
|
72
|
+
console.log(`Created ${file}`);
|
|
73
|
+
process.exit(0);
|
|
74
|
+
}
|
|
75
|
+
if (sub === 'middleware' || sub === 'm') {
|
|
76
|
+
const dir = resolve(cwd, 'src/middleware');
|
|
77
|
+
mkdirSync(dir, { recursive: true });
|
|
78
|
+
const file = resolve(dir, `${name}.ts`);
|
|
79
|
+
if (existsSync(file)) {
|
|
80
|
+
console.error(`File already exists: ${file}`);
|
|
81
|
+
process.exit(1);
|
|
82
|
+
}
|
|
83
|
+
const slug = name.replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '');
|
|
84
|
+
writeFileSync(file, `import { middleware } from '@carlos-tzin/tzin'
|
|
85
|
+
|
|
86
|
+
export const ${slug} = middleware(async (ctx, next) => {
|
|
87
|
+
// TODO: add logic here
|
|
88
|
+
return next()
|
|
89
|
+
})
|
|
90
|
+
`);
|
|
91
|
+
console.log(`Created ${file}`);
|
|
92
|
+
process.exit(0);
|
|
93
|
+
}
|
|
94
|
+
if (sub === 'test' || sub === 't') {
|
|
95
|
+
const dir = resolve(cwd, 'tests');
|
|
96
|
+
mkdirSync(dir, { recursive: true });
|
|
97
|
+
const file = resolve(dir, `${name}.test.ts`);
|
|
98
|
+
if (existsSync(file)) {
|
|
99
|
+
console.error(`File already exists: ${file}`);
|
|
100
|
+
process.exit(1);
|
|
101
|
+
}
|
|
102
|
+
writeFileSync(file, `import { describe, it, expect } from 'vitest'
|
|
103
|
+
import { createTestClient } from '@carlos-tzin/tzin/test'
|
|
104
|
+
import { app } from '../src/app.js'
|
|
105
|
+
|
|
106
|
+
describe('${name}', () => {
|
|
107
|
+
it('returns 200', async () => {
|
|
108
|
+
const api = await createTestClient(app)
|
|
109
|
+
try {
|
|
110
|
+
const res = await api.get('/${name}')
|
|
111
|
+
expect(res.status).toBe(200)
|
|
112
|
+
} finally {
|
|
113
|
+
await api.close()
|
|
114
|
+
}
|
|
115
|
+
})
|
|
116
|
+
})
|
|
117
|
+
`);
|
|
118
|
+
console.log(`Created ${file}`);
|
|
119
|
+
process.exit(0);
|
|
120
|
+
}
|
|
121
|
+
console.error(`Unknown generate type: ${sub}`);
|
|
122
|
+
console.error('Available: route, middleware, test');
|
|
123
|
+
process.exit(1);
|
|
124
|
+
}
|
|
125
|
+
usage();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@carlos-tzin/tzin",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "Contract-first TypeScript framework. Types that scale, realtime channels with presence, and an MCP server for every API.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "The tzin authors",
|