Skip to content

Commit 75e4afb

Browse files
committed
feat(@sirutils/{schema,builder}): init definition generation
1 parent faf0c6e commit 75e4afb

33 files changed

+439
-95
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,5 @@ dist
136136
lib/
137137

138138
# others
139-
experiments
140139
.tmp
141140
.idea

biome.jsonc

+14-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@
3232
},
3333
"files": {
3434
"ignoreUnknown": true,
35-
"ignore": ["**/dist/**", "**/node_modules/**"]
35+
"ignore": [
36+
"**/dist/**",
37+
"**/node_modules/**",
38+
"**/_/**"
39+
]
3640
},
3741
"formatter": {
3842
"enabled": true,
@@ -59,7 +63,15 @@
5963
"indentWidth": 2,
6064
"attributePosition": "auto"
6165
},
62-
"globals": ["$", "_", "Deno", "Bun", "JSX", "Sirutils", "React"],
66+
"globals": [
67+
"$",
68+
"_",
69+
"Deno",
70+
"Bun",
71+
"JSX",
72+
"Sirutils",
73+
"React"
74+
],
6375
"parser": {
6476
"unsafeParameterDecoratorsEnabled": true
6577
},

bun.lockb

10.9 KB
Binary file not shown.

experiments/db/moon.yml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
language: "typescript"
2+
type: "library"
3+
platform: "bun"
4+
5+
tasks:
6+
build:
7+
command: "bun x builder build src/index.ts -a --schema"
8+
deps:
9+
- "schema:build"
10+
- "builder:build"
11+
- "core:build"
12+
- "seql:build"
13+
inputs:
14+
- "src/**/*"
15+
- "schemas/**/*"
16+
outputs:
17+
- "dist/**"
18+
watch:
19+
command: "bun x builder build src/index.ts -a -w --schema"
20+
deps:
21+
- "schema:build"
22+
- "builder:build"
23+
- "core:build"
24+
- "seql:build"
25+
local: true
26+
inputs:
27+
- "src/**/*"
28+
- "schemas/**/*"

experiments/db/package.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "@sirutils/db",
3+
"version": "0.0.1",
4+
5+
"type": "module",
6+
"files": ["dist"],
7+
"exports": {
8+
".": {
9+
"default": "./dist/index.js",
10+
"types": "./dist/index.d.ts"
11+
}
12+
},
13+
14+
"devDependencies": {
15+
"@sirutils/builder": "workspace:*"
16+
},
17+
"dependencies": {
18+
"@sirutils/core": "workspace:*",
19+
"@sirutils/schema": "workspace:*"
20+
}
21+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// 65b5738909d5db68ab76896e2cc2b2d8227ddb247733f66cba196992bc9ede4d859ed828c6c4943a9c54b60f6ba269cafaeb5bc90f277c39cb199576d5d107d4
2+
declare global {
3+
namespace Sirutils {
4+
namespace Schema {
5+
namespace Generated {
6+
type Tables = {}
7+
8+
type Blogs = {}
9+
}
10+
}
11+
}
12+
}

experiments/db/schemas/_/users.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// 75d5308c722963f36d60b268556267f2a744e39e55124c4f73f50a35aef9eac1ae79b2c02fc1922b0e1c3e82638f2ea69ce3bd5584b339d2d3239349f3258984
2+
declare global {
3+
namespace Sirutils {
4+
namespace Schema {
5+
namespace Generated {
6+
type Tables = {}
7+
8+
type Users = {}
9+
}
10+
}
11+
}
12+
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "blogs",
3+
"importMaps": {
4+
"users": "./users.json"
5+
},
6+
7+
"fields": [
8+
{
9+
"name": "id",
10+
"type": "ulid"
11+
},
12+
{
13+
"name": "title",
14+
"type": "string",
15+
"max-length": 255
16+
},
17+
{
18+
"name": "author",
19+
"type": "reference",
20+
"mode": "single",
21+
"to": "users"
22+
}
23+
],
24+
25+
"indexes": [
26+
{
27+
"name": "idx",
28+
"fields": ["id"]
29+
}
30+
]
31+
}

experiments/db/schemas/users.json

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "users",
3+
"importMaps": {},
4+
5+
"fields": [
6+
{
7+
"name": "id",
8+
"type": "ulid"
9+
},
10+
{
11+
"name": "name",
12+
"type": "string",
13+
"max-length": 255
14+
},
15+
{
16+
"name": "surname",
17+
"type": "string",
18+
"max-length": 255
19+
},
20+
{
21+
"name": "age",
22+
"type": "string",
23+
"max-length": 255,
24+
"attributes": ["personal"]
25+
}
26+
],
27+
28+
"indexes": [
29+
{
30+
"name": "idx",
31+
"fields": ["id"]
32+
}
33+
]
34+
}

experiments/db/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// biome-ignore lint/style/useExportType: <explanation>
2+
export {}

experiments/db/tsconfig.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"include": ["src", "test", "schemas/_"],
4+
"references": [
5+
{
6+
"path": "../../packages/core"
7+
},
8+
{
9+
"path": "../../packages/seql"
10+
},
11+
{
12+
"path": "../../tools/builder"
13+
},
14+
{
15+
"path": "../../tools/schema"
16+
},
17+
{
18+
"path": "../core"
19+
}
20+
]
21+
}

package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "sirutils",
33
"private": "true",
44
"type": "module",
5-
"workspaces": ["{packages,plugins,apps,tools}/**"],
5+
"workspaces": ["{packages,plugins,apps,tools,experiments}/**"],
66
"devDependencies": {
77
"@biomejs/biome": "1.7.3",
88
"@commitlint/cli": "^19.3.0",
@@ -16,7 +16,9 @@
1616
"prepare": "husky",
1717
"commit": "commit"
1818
},
19-
"trustedDependencies": [
20-
"@biomejs/biome"
21-
]
19+
"trustedDependencies": ["@biomejs/biome"],
20+
"dependencies": {
21+
"deep-object-diff": "^1.1.9",
22+
"ts-morph": "^22.0.0"
23+
}
2224
}

packages/core/moon.yml

-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ tasks:
66
build:
77
command: "bun x builder build src/index.ts -a"
88
deps:
9-
- "core:clean"
109
- "builder:build"
1110
inputs:
1211
- "src/**/*"
@@ -15,7 +14,6 @@ tasks:
1514
watch:
1615
command: "bun x builder build src/index.ts -a -w"
1716
deps:
18-
- "core:clean"
1917
- "builder:build"
2018
local: true
2119
inputs:

packages/seql/moon.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ tasks:
66
build:
77
command: "bun x builder build src/index.ts -a"
88
deps:
9-
- "seql:clean"
109
- "builder:build"
1110
- "core:build"
1211
inputs:
1312
- "src/**/*"
13+
- "schemas/**/*"
1414
outputs:
1515
- "dist/**"
1616
watch:
1717
command: "bun x builder build src/index.ts -a -w"
1818
deps:
19-
- "seql:clean"
2019
- "builder:build"
2120
- "core:build"
2221
local: true
2322
inputs:
2423
- "src/**/*"
24+
- "schemas/**/*"

tools/builder/moon.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ tasks:
66
build:
77
command: "bun run src/index.ts build src/index.ts -a"
88
deps:
9-
- "builder:clean"
9+
- schema:build
1010
inputs:
1111
- "src/**/*"
1212
outputs:

tools/builder/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@
2424
"get-tsconfig": "^4.7.4"
2525
},
2626
"peerDependencies": {
27-
"@sirutils/schema": "^0.0.0"
27+
"@sirutils/schema": "workspace:*"
2828
}
2929
}

tools/builder/src/definitions.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ export interface CommanderOptions {
22
cwd: string
33
watch: boolean
44
externalAll: boolean
5+
external: string[]
6+
dts: boolean
7+
58
schema: boolean
69
schemaDir: string
7-
external: string[]
810
}

tools/builder/src/index.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { watch } from 'fs'
22
import { Command } from 'commander'
33

44
import pkg from '../package.json'
5+
import type { CommanderOptions } from './definitions'
56
import { build, dependencies } from './utils'
67

78
const program = new Command()
@@ -13,21 +14,25 @@ program
1314
.argument('<string...>', 'paths')
1415
.option('-w --watch', 'watch', false)
1516
.option('--cwd <string>', 'cwd', process.cwd())
17+
.option('-e --external <packages...>', 'externals', [])
1618
.option('-a --external-all', 'externals', false)
17-
.option('-e --external <numbers...>', 'externals', [])
1819
.option('--no-dts', 'bundled .d.ts file', true)
19-
.action(async (paths: string[], options) => {
20+
21+
.option('-s --schema', 'schema', false)
22+
.option('-sd --schema-dir', 'schema directory', 'schemas')
23+
24+
.action(async (paths: string[], options: CommanderOptions) => {
2025
const externals = [
2126
...(options.externalAll ? await dependencies(options.cwd) : []),
2227
...(options.external || []),
2328
]
2429

25-
await build(paths, options.cwd, externals, options.dts)
30+
await build(paths, options, externals)
2631

2732
if (options.watch) {
2833
watch(options.cwd, { recursive: true }, async (event, filename) => {
2934
if (typeof filename === 'string' && !filename.includes('dist')) {
30-
await build(paths, options.cwd, externals, options.dts)
35+
await build(paths, options, externals)
3136
}
3237
})
3338
}

tools/builder/src/utils.ts

+23-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { join } from 'path'
22
import type { BunPlugin } from 'bun'
33

4+
import type { CommanderOptions } from './definitions'
45
import { dts } from './plugins'
56

67
export const dependencies = async (cwd = process.cwd()): Promise<string[]> => {
@@ -19,16 +20,15 @@ export const dependencies = async (cwd = process.cwd()): Promise<string[]> => {
1920

2021
export const build = async (
2122
paths: string[],
22-
cwd: string = process.cwd(),
23+
options: CommanderOptions,
2324
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
24-
external = [] as any[],
25-
buildDts = true
25+
external = [] as any[]
2626
) => {
27-
const entryPoints = paths.map(p => join(cwd, p))
28-
const outDir = join(cwd, './dist')
27+
const entryPoints = paths.map(p => join(options.cwd, p))
28+
const outDir = join(options.cwd, './dist')
2929
const plugins: BunPlugin[] = []
3030

31-
if (buildDts) {
31+
if (options.dts) {
3232
plugins.push(
3333
dts({
3434
output: {
@@ -37,13 +37,28 @@ export const build = async (
3737
exportReferencedTypes: false,
3838
},
3939
compilationOptions: {
40-
preferredConfigPath: join(cwd, 'tsconfig.json'),
40+
preferredConfigPath: join(options.cwd, 'tsconfig.json'),
4141
followSymlinks: false,
4242
},
4343
})
4444
)
4545
}
4646

47+
if (options.schema) {
48+
try {
49+
const schemaGeneratorPlugin = await import('@sirutils/schema')
50+
51+
plugins.push(
52+
schemaGeneratorPlugin.schemaGeneratorPlugin({
53+
dir: options.schemaDir,
54+
})
55+
)
56+
} catch (err) {
57+
// biome-ignore lint/nursery/noConsole: <explanation>
58+
console.warn(`[@sirutils/builder] cannot build schemas: ${err}`)
59+
}
60+
}
61+
4762
await Bun.build({
4863
entrypoints: entryPoints,
4964
outdir: outDir,
@@ -53,5 +68,5 @@ export const build = async (
5368
})
5469

5570
// biome-ignore lint/nursery/noConsole: <explanation>
56-
console.log(`[@sirutils/builder] building: ${cwd.split('/').slice(-2).join('/')}`)
71+
console.log(`[@sirutils/builder] building: ${options.cwd.split('/').slice(-2).join('/')}`)
5772
}

0 commit comments

Comments
 (0)