ramdb2-cli 2.0.0-alpha.4 → 2.0.0-alpha.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/CHANGELOG.md +6 -0
- package/README.md +4 -0
- package/package.json +2 -2
- package/src/cli.js +2 -1
- package/src/generate.js +45 -7
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -48,6 +48,10 @@ Several `--schema` files are read as one schema. In a project created by `ramdb2
|
|
|
48
48
|
Never edit a generated file: the next generation would erase the change. Everything you write goes in
|
|
49
49
|
`db/extended`, and in your own schema files (`extend type Query { ... }`).
|
|
50
50
|
|
|
51
|
+
When a type leaves the schema, its file in `db/generated/` is removed (`removed` in the output); only a file with
|
|
52
|
+
the header `// Generated by ramdb2-cli` is ever removed. Its file in `db/extended/`, written by you, is kept and
|
|
53
|
+
named in a warning. `client` does the same in its `generated/` and `extended/` folders.
|
|
54
|
+
|
|
51
55
|
### The schema: the preTypeDefs
|
|
52
56
|
|
|
53
57
|
```graphql
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ramdb2-cli",
|
|
3
|
-
"version": "2.0.0-alpha.
|
|
3
|
+
"version": "2.0.0-alpha.5",
|
|
4
4
|
"description": "Code generator and project scaffolding for ramdb2 and ramdb2-server",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": "./src/index.js",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@vitejs/plugin-vue": "^6.0.10",
|
|
31
|
-
"ramdb2-core": "^2.0.0-alpha.
|
|
31
|
+
"ramdb2-core": "^2.0.0-alpha.5",
|
|
32
32
|
"vite": "^8.3.5",
|
|
33
33
|
"vue": "^3.5.44",
|
|
34
34
|
"vue-router": "^5.4.0"
|
package/src/cli.js
CHANGED
|
@@ -127,9 +127,10 @@ const commands = {
|
|
|
127
127
|
},
|
|
128
128
|
}
|
|
129
129
|
|
|
130
|
-
function report({ written, kept, warnings }) {
|
|
130
|
+
function report({ written, kept, removed = [], warnings }) {
|
|
131
131
|
for (const path of written) console.log(`written ${path}`)
|
|
132
132
|
for (const path of kept) console.log(`kept ${path} (written by hand)`)
|
|
133
|
+
for (const path of removed) console.log(`removed ${path} (type no longer in the schema)`)
|
|
133
134
|
for (const warning of warnings) console.log(`warning ${warning}`)
|
|
134
135
|
}
|
|
135
136
|
|
package/src/generate.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
|
|
2
|
-
import { dirname, join } from 'node:path'
|
|
1
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
|
|
2
|
+
import { dirname, join, posix } from 'node:path'
|
|
3
3
|
import { readSchema } from './model.js'
|
|
4
4
|
import { classFiles } from './generate/classes.js'
|
|
5
5
|
import { clientFiles } from './generate/client.js'
|
|
@@ -9,24 +9,29 @@ import { operationsFile } from './generate/operations.js'
|
|
|
9
9
|
/**
|
|
10
10
|
* Generates the database layer of a project from its preTypeDefs (spec section 12).
|
|
11
11
|
* Generated files are rewritten; files written by hand (extended classes) are created only when missing.
|
|
12
|
+
* The generated classes of a type gone from the schema are removed, its extended class is named in a warning.
|
|
12
13
|
* @param {{schema: string | string[], dest: string}} options schema file(s), destination folder
|
|
13
|
-
* @returns {{written: string[], kept: string[], warnings: string[]}} paths relative to dest
|
|
14
|
+
* @returns {{written: string[], kept: string[], removed: string[], warnings: string[]}} paths relative to dest
|
|
14
15
|
*/
|
|
15
16
|
export function generate({ schema, dest }) {
|
|
16
17
|
const model = readSchemaFiles(schema)
|
|
17
|
-
|
|
18
|
+
const files = [...classFiles(model), typeDefsFile(model), operationsFile(model)]
|
|
19
|
+
const { removed, warnings } = removeOrphans(files, dest)
|
|
20
|
+
return { ...writeFiles(files, dest), removed, warnings: [...model.warnings, ...warnings] }
|
|
18
21
|
}
|
|
19
22
|
|
|
20
23
|
/**
|
|
21
24
|
* Generates the classes of a ramdb2-client application from the preTypeDefs of its server (spec section 12, D49):
|
|
22
|
-
* generated/<table>.js (rewritten), extended/<table>.js (created once) and index.js, in dest.
|
|
25
|
+
* generated/<table>.js (rewritten), extended/<table>.js (created once) and index.js, in dest. Orphans are handled
|
|
26
|
+
* as by generate.
|
|
23
27
|
* @param {{schema: string | string[], dest: string}} options schema file(s), destination folder
|
|
24
|
-
* @returns {{written: string[], kept: string[], warnings: string[]}} paths relative to dest
|
|
28
|
+
* @returns {{written: string[], kept: string[], removed: string[], warnings: string[]}} paths relative to dest
|
|
25
29
|
*/
|
|
26
30
|
export function generateClient({ schema, dest }) {
|
|
27
31
|
const model = readSchemaFiles(schema)
|
|
28
32
|
const { files, warnings } = clientFiles(model)
|
|
29
|
-
|
|
33
|
+
const orphans = removeOrphans(files, dest)
|
|
34
|
+
return { ...writeFiles(files, dest), removed: orphans.removed, warnings: [...model.warnings, ...warnings, ...orphans.warnings] }
|
|
30
35
|
}
|
|
31
36
|
|
|
32
37
|
function readSchemaFiles(schema) {
|
|
@@ -49,3 +54,36 @@ function writeFiles(files, dest) {
|
|
|
49
54
|
}
|
|
50
55
|
return { written, kept }
|
|
51
56
|
}
|
|
57
|
+
|
|
58
|
+
// The marker of a file written by ramdb2-cli: only such a file is ever removed
|
|
59
|
+
const GENERATED = '// Generated by ramdb2-cli'
|
|
60
|
+
|
|
61
|
+
// The classes of a type removed from the schema (D61): in each folder of generated classes, a generated file that
|
|
62
|
+
// this generation does not write is removed; in each folder of extended classes, a file without its generated
|
|
63
|
+
// class is only named in a warning, since it was written by hand.
|
|
64
|
+
function removeOrphans(files, dest) {
|
|
65
|
+
const removed = []
|
|
66
|
+
const warnings = []
|
|
67
|
+
const folders = new Map()
|
|
68
|
+
for (const file of files) {
|
|
69
|
+
const folder = posix.dirname(file.path)
|
|
70
|
+
if (!['generated', 'extended'].includes(posix.basename(folder))) continue
|
|
71
|
+
if (!folders.has(folder)) folders.set(folder, new Set())
|
|
72
|
+
folders.get(folder).add(posix.basename(file.path))
|
|
73
|
+
}
|
|
74
|
+
for (const [folder, names] of folders) {
|
|
75
|
+
const path = join(dest, folder)
|
|
76
|
+
if (!existsSync(path)) continue
|
|
77
|
+
for (const entry of readdirSync(path, { withFileTypes: true })) {
|
|
78
|
+
if (!entry.isFile() || !entry.name.endsWith('.js') || names.has(entry.name)) continue
|
|
79
|
+
const relative = posix.join(folder, entry.name)
|
|
80
|
+
if (posix.basename(folder) === 'extended') {
|
|
81
|
+
warnings.push(`${relative}: its type is no longer in the schema, remove it by hand if it is not used`)
|
|
82
|
+
} else if (readFileSync(join(path, entry.name), 'utf8').startsWith(GENERATED)) {
|
|
83
|
+
rmSync(join(path, entry.name))
|
|
84
|
+
removed.push(relative)
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return { removed, warnings }
|
|
89
|
+
}
|