t0n 0.20260706.1 → 0.20260804.0

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "t0n",
3
3
  "description": "Collection of elegant typescript resources for web artisans",
4
- "version": "0.20260706.1",
4
+ "version": "0.20260804.0",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
7
7
  "files": ["src"],
@@ -18,12 +18,13 @@
18
18
  },
19
19
  "dependencies": {
20
20
  "chokidar": "^3.5.2",
21
+ "citty": "^0.2.2",
22
+ "consola": "^3.4.2",
21
23
  "hono": "^4.12.8",
22
24
  "pathe": "^2.0"
23
25
  },
24
26
  "devDependencies": {
25
- "bun-types": "^1.3.8",
26
- "tsup": "^8.5.0",
27
+ "bun-types": "^1.3.14",
27
28
  "tsx": "^4.19.4",
28
29
  "typescript": "^5.9.3"
29
30
  },
package/src/cli.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import chokidar from 'chokidar'
2
2
  import { relative } from 'pathe'
3
- import { wait, log } from 't0n/log'
3
+ import { wait, log, logo, error } from './log'
4
4
 
5
5
  export type ChokidarEventName = 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir'
6
6
 
@@ -89,3 +89,66 @@ function getAssetChangeMessage(
89
89
  return `${path} changed`
90
90
  }
91
91
  }
92
+
93
+ export const killProcess = async (proc: Bun.Subprocess | null) => {
94
+ if (!proc || proc.exitCode !== null) return
95
+
96
+ try {
97
+ proc.kill('SIGTERM')
98
+
99
+ const exited = await Promise.race([
100
+ proc.exited.then(() => true),
101
+ new Promise(r => setTimeout(r, 1000)).then(() => false),
102
+ ])
103
+
104
+ if (!exited && proc.exitCode === null) {
105
+ proc.kill('SIGKILL')
106
+ await proc.exited
107
+ }
108
+ } catch (e) {
109
+ error('Error stopping:', e)
110
+ }
111
+ }
112
+
113
+ import { isColorSupported, dim } from './color'
114
+ import { defineCommand, runMain, renderUsage } from 'citty'
115
+ import type { ArgsDef, CommandDef } from 'citty'
116
+ import { createConsola } from 'consola'
117
+
118
+ export { defineCommand as command }
119
+ export function cli({ name, description, version, commands }: {
120
+ name: string,
121
+ description?: string,
122
+ version: string,
123
+ commands: Record<string, string>,
124
+ }) {
125
+ const _cli = description || name.toUpperCase()
126
+ const v = [_cli, isColorSupported ? dim('v'+version) : version].join(' ')
127
+
128
+ const _args = process.argv.slice(2)
129
+ const length = _args.length
130
+ if (!length || (length === 1 && ['-v', '--version', '--v', '-version'].includes(_args[0]?.toLowerCase()))) {
131
+ console.log(v)
132
+ process.exit(0)
133
+ }
134
+
135
+ console.log(`\n${logo} ${v}\n`)
136
+
137
+ const consola = createConsola({ formatOptions: {date: false} })
138
+ async function showUsage<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>, parent?: CommandDef<T>) {
139
+ try {
140
+ consola.log((await renderUsage(cmd, parent)).split('\n').slice(1).join('\n') + '\n')
141
+ } catch (error) {
142
+ consola.error(error)
143
+ }
144
+ }
145
+
146
+ runMain(defineCommand({
147
+ meta: {
148
+ name,
149
+ version: '',
150
+ description: _cli,
151
+ },
152
+ subCommands: Object.fromEntries(Object.entries(commands).map(([key, path]) => [key, () => import(path).then(r => r.default)])),
153
+ }), { rawArgs: length ? undefined : ['-h'], showUsage })
154
+ }
package/src/import.ts CHANGED
@@ -7,11 +7,11 @@ export async function IMPORT<T extends unknown>(path: string, ...name: string[])
7
7
  const source = path +'?v='+ Date.now()
8
8
 
9
9
  if (name.length > 0) {
10
- const module = await import(source)
10
+ const mod = await import(source)
11
11
  const result: Record<string, any> = {}
12
12
  for (const n of name) {
13
- if (n in module)
14
- result[n] = module[n]
13
+ if (n in mod)
14
+ result[n] = mod[n]
15
15
  else
16
16
  throw new Error(`Module ${path} does not export ${n}`)
17
17
  }
package/src/log.ts CHANGED
@@ -31,8 +31,13 @@ export const substep = (...msg: any[]) => {
31
31
  console.log(` ${gray('⁕')} ` + msg[i])
32
32
  }
33
33
 
34
- export const ln = () => console.log('\n')
35
- export const rn = () => console.log('\t')
34
+ export const repeat = (max: number = 1, ...msg: any[]) => {
35
+ if (max < 2) return console.log(...msg)
36
+ for (let i = 0; i < max; i++)
37
+ console.log(...msg)
38
+ }
39
+ export const ln = (max: number = 1) => repeat(max, '\n')
40
+ export const rn = (max: number = 1) => repeat(max, '\t')
36
41
 
37
42
  export const logo = gray(bold('λ'))
38
43
  export const prefixes = {