zenstack 0.4.2 → 0.6.0-pre.1
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/{LICENSE.md → LICENSE} +0 -0
- package/bin/cli +1 -1
- package/package.json +18 -13
- package/bin/post-install.js +0 -24
- package/bundle/asset/logo-256-bg.png +0 -0
- package/bundle/asset/logo-dark-256.png +0 -0
- package/bundle/asset/logo-light-256.png +0 -0
- package/bundle/cli/index.js +0 -6952
- package/bundle/extension.js +0 -39
- package/bundle/language-server/main.js +0 -6208
- package/bundle/res/package.template.json +0 -9
- package/bundle/res/prism-zmodel.js +0 -22
- package/bundle/res/stdlib.zmodel +0 -218
- package/bundle/res/tsconfig.template.json +0 -17
- package/src/cli/cli-error.ts +0 -4
- package/src/cli/cli-util.ts +0 -214
- package/src/cli/index.ts +0 -246
- package/src/extension.ts +0 -76
- package/src/generator/ast-utils.ts +0 -18
- package/src/generator/constants.ts +0 -6
- package/src/generator/field-constraint/index.ts +0 -304
- package/src/generator/index.ts +0 -86
- package/src/generator/prisma/expression-writer.ts +0 -360
- package/src/generator/prisma/index.ts +0 -44
- package/src/generator/prisma/prisma-builder.ts +0 -370
- package/src/generator/prisma/query-guard-generator.ts +0 -249
- package/src/generator/prisma/schema-generator.ts +0 -313
- package/src/generator/prisma/typescript-expression-transformer.ts +0 -108
- package/src/generator/react-hooks/index.ts +0 -273
- package/src/generator/service/index.ts +0 -113
- package/src/generator/tsc/index.ts +0 -59
- package/src/generator/types.ts +0 -20
- package/src/global.d.ts +0 -3
- package/src/language-server/constants.ts +0 -29
- package/src/language-server/generated/ast.ts +0 -643
- package/src/language-server/generated/grammar.ts +0 -2492
- package/src/language-server/generated/module.ts +0 -24
- package/src/language-server/langium-ext.d.ts +0 -22
- package/src/language-server/lsp/zmodel-definition-provider.ts +0 -87
- package/src/language-server/main.ts +0 -13
- package/src/language-server/types.ts +0 -25
- package/src/language-server/utils.ts +0 -21
- package/src/language-server/validator/attribute-validator.ts +0 -11
- package/src/language-server/validator/datamodel-validator.ts +0 -426
- package/src/language-server/validator/datasource-validator.ts +0 -102
- package/src/language-server/validator/enum-validator.ts +0 -14
- package/src/language-server/validator/expression-validator.ts +0 -48
- package/src/language-server/validator/schema-validator.ts +0 -31
- package/src/language-server/validator/utils.ts +0 -158
- package/src/language-server/validator/zmodel-validator.ts +0 -91
- package/src/language-server/zmodel-linker.ts +0 -457
- package/src/language-server/zmodel-module.ts +0 -136
- package/src/language-server/zmodel-scope.ts +0 -45
- package/src/language-server/zmodel-workspace-manager.ts +0 -23
- package/src/language-server/zmodel.langium +0 -207
- package/src/res/package.template.json +0 -9
- package/src/res/prism-zmodel.js +0 -22
- package/src/res/stdlib.zmodel +0 -218
- package/src/res/tsconfig.template.json +0 -17
- package/src/telemetry.ts +0 -119
- package/src/utils/exec-utils.ts +0 -8
- package/src/utils/indent-string.ts +0 -9
- package/src/utils/pkg-utils.ts +0 -63
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
// based on: https://github.com/prisma/docs/blob/c72eb087fcf57f3c00d153f86c549ef28b3d0f44/src/components/customMdx/prism/prism-prisma.js
|
|
2
|
-
|
|
3
|
-
(function (Prism) {
|
|
4
|
-
Prism.languages.zmodel = Prism.languages.extend('clike', {
|
|
5
|
-
keyword:
|
|
6
|
-
/\b(?:datasource|enum|generator|model|attribute|function|null|this)\b/,
|
|
7
|
-
'type-class-name': /(\b()\s+)[\w.\\]+/,
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
Prism.languages.javascript['class-name'][0].pattern =
|
|
11
|
-
/(\b(?:model|datasource|enum|generator)\s+)[\w.\\]+/;
|
|
12
|
-
|
|
13
|
-
Prism.languages.insertBefore('zmodel', 'function', {
|
|
14
|
-
annotation: {
|
|
15
|
-
pattern: /(^|[^.])@+\w+/,
|
|
16
|
-
lookbehind: true,
|
|
17
|
-
alias: 'punctuation',
|
|
18
|
-
},
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
Prism.languages.json5 = Prism.languages.js;
|
|
22
|
-
})(Prism);
|
package/bundle/res/stdlib.zmodel
DELETED
|
@@ -1,218 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Enum representing referential integrity related actions
|
|
3
|
-
*/
|
|
4
|
-
enum ReferentialAction {
|
|
5
|
-
/*
|
|
6
|
-
* Used with "onDelete": deleting a referenced record will trigger the deletion of referencing record.
|
|
7
|
-
* Used with "onUpdate": updates the relation scalar fields if the referenced scalar fields of the dependent record are updated.
|
|
8
|
-
*/
|
|
9
|
-
Cascade
|
|
10
|
-
|
|
11
|
-
/*
|
|
12
|
-
* Used with "onDelete": prevents the deletion if any referencing records exist.
|
|
13
|
-
* Used with "onUpdate": prevents the identifier of a referenced record from being changed.
|
|
14
|
-
*/
|
|
15
|
-
Restrict
|
|
16
|
-
|
|
17
|
-
/*
|
|
18
|
-
* Similar to 'Restrict', the difference between the two is dependent on the database being used.
|
|
19
|
-
* See details: https://www.prisma.io/docs/concepts/components/prisma-schema/relations/referential-actions#noaction
|
|
20
|
-
*/
|
|
21
|
-
NoAction
|
|
22
|
-
|
|
23
|
-
/*
|
|
24
|
-
* Used with "onDelete": the scalar field of the referencing object will be set to NULL.
|
|
25
|
-
* Used with "onUpdate": when updating the identifier of a referenced object, the scalar fields of the referencing objects will be set to NULL.
|
|
26
|
-
*/
|
|
27
|
-
SetNull
|
|
28
|
-
|
|
29
|
-
/*
|
|
30
|
-
* Used with "onDelete": the scalar field of the referencing object will be set to the fields default value.
|
|
31
|
-
* Used with "onUpdate": the scalar field of the referencing object will be set to the fields default value.
|
|
32
|
-
*/
|
|
33
|
-
SetDefault
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/*
|
|
37
|
-
* Enum representing all possible field types
|
|
38
|
-
*/
|
|
39
|
-
enum AttributeTargetField {
|
|
40
|
-
StringField
|
|
41
|
-
IntField
|
|
42
|
-
FloatField
|
|
43
|
-
DecimalField
|
|
44
|
-
BooleanField
|
|
45
|
-
DateTimeField
|
|
46
|
-
JsonField
|
|
47
|
-
BytesField
|
|
48
|
-
ModelField
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/*
|
|
52
|
-
* Reads value from an environment variable.
|
|
53
|
-
*/
|
|
54
|
-
function env(name: String): String {}
|
|
55
|
-
|
|
56
|
-
/*
|
|
57
|
-
* Gets the current login user.
|
|
58
|
-
*/
|
|
59
|
-
function auth(): Any {}
|
|
60
|
-
|
|
61
|
-
/*
|
|
62
|
-
* Gets current date-time (as DateTime type).
|
|
63
|
-
*/
|
|
64
|
-
function now(): DateTime {}
|
|
65
|
-
|
|
66
|
-
/*
|
|
67
|
-
* Generates a globally unique identifier based on the UUID specs.
|
|
68
|
-
*/
|
|
69
|
-
function uuid(): String {}
|
|
70
|
-
|
|
71
|
-
/*
|
|
72
|
-
* Generates a globally unique identifier based on the CUID spec.
|
|
73
|
-
*/
|
|
74
|
-
function cuid(): String {}
|
|
75
|
-
|
|
76
|
-
/*
|
|
77
|
-
* Creates a sequence of integers in the underlying database and assign the incremented
|
|
78
|
-
* values to the ID values of the created records based on the sequence.
|
|
79
|
-
*/
|
|
80
|
-
function autoincrement(): Int {}
|
|
81
|
-
|
|
82
|
-
/*
|
|
83
|
-
* Represents default values that cannot be expressed in the Prisma schema (such as random()).
|
|
84
|
-
*/
|
|
85
|
-
function dbgenerated(expr: String): Any {}
|
|
86
|
-
|
|
87
|
-
attribute @@@targetField(targetField: AttributeTargetField[])
|
|
88
|
-
|
|
89
|
-
attribute @@@prisma()
|
|
90
|
-
|
|
91
|
-
/*
|
|
92
|
-
* Defines an ID on the model.
|
|
93
|
-
*/
|
|
94
|
-
attribute @id(map: String?) @@@prisma
|
|
95
|
-
|
|
96
|
-
/*
|
|
97
|
-
* Defines a default value for a field.
|
|
98
|
-
*/
|
|
99
|
-
attribute @default(_ value: ContextType) @@@prisma
|
|
100
|
-
|
|
101
|
-
/*
|
|
102
|
-
* Defines a unique constraint for this field.
|
|
103
|
-
*/
|
|
104
|
-
attribute @unique(map: String?) @@@prisma
|
|
105
|
-
|
|
106
|
-
/*
|
|
107
|
-
* Defines a compound unique constraint for the specified fields.
|
|
108
|
-
*/
|
|
109
|
-
attribute @@unique(_ fields: FieldReference[], name: String?, map: String?) @@@prisma
|
|
110
|
-
|
|
111
|
-
/*
|
|
112
|
-
* Defines an index in the database.
|
|
113
|
-
*/
|
|
114
|
-
attribute @@index(_ fields: FieldReference[], map: String?) @@@prisma
|
|
115
|
-
|
|
116
|
-
/*
|
|
117
|
-
* Defines meta information about the relation.
|
|
118
|
-
*/
|
|
119
|
-
attribute @relation(_ name: String?, fields: FieldReference[]?, references: FieldReference[]?, onDelete: ReferentialAction?, onUpdate: ReferentialAction?, map: String?) @@@prisma
|
|
120
|
-
|
|
121
|
-
/*
|
|
122
|
-
* Maps a field name or enum value from the schema to a column with a different name in the database.
|
|
123
|
-
*/
|
|
124
|
-
attribute @map(_ name: String) @@@prisma
|
|
125
|
-
|
|
126
|
-
/*
|
|
127
|
-
* Maps the schema model name to a table with a different name, or an enum name to a different underlying enum in the database.
|
|
128
|
-
*/
|
|
129
|
-
attribute @@map(_ name: String) @@@prisma
|
|
130
|
-
|
|
131
|
-
/*
|
|
132
|
-
* Automatically stores the time when a record was last updated.
|
|
133
|
-
*/
|
|
134
|
-
attribute @updatedAt() @@@targetField([DateTimeField]) @@@prisma
|
|
135
|
-
|
|
136
|
-
/*
|
|
137
|
-
* Defines an access policy that allows a set of operations when the given condition is true.
|
|
138
|
-
*/
|
|
139
|
-
attribute @@allow(_ operation: String, _ condition: Boolean)
|
|
140
|
-
|
|
141
|
-
/*
|
|
142
|
-
* Defines an access policy that denies a set of operations when the given condition is true.
|
|
143
|
-
*/
|
|
144
|
-
attribute @@deny(_ operation: String, _ condition: Boolean)
|
|
145
|
-
|
|
146
|
-
/*
|
|
147
|
-
* Indicates that the field is a password field and needs to be hashed before persistence.
|
|
148
|
-
*
|
|
149
|
-
* ZenStack uses `bcryptjs` library to hash password. You can use the `saltLength` parameter
|
|
150
|
-
* to configure the cost of hashing, or use `salt` parameter to provide an explicit salt.
|
|
151
|
-
* By default, salt length of 12 is used.
|
|
152
|
-
*
|
|
153
|
-
* @see https://www.npmjs.com/package/bcryptjs for details
|
|
154
|
-
*
|
|
155
|
-
* @saltLength: length of salt to use (cost factor for the hash function)
|
|
156
|
-
* @salt: salt to use (a pregenerated valid salt)
|
|
157
|
-
*/
|
|
158
|
-
attribute @password(saltLength: Int?, salt: String?) @@@targetField([StringField])
|
|
159
|
-
|
|
160
|
-
/*
|
|
161
|
-
* Indicates that the field should be omitted when read from the generated services.
|
|
162
|
-
*/
|
|
163
|
-
attribute @omit()
|
|
164
|
-
|
|
165
|
-
/*
|
|
166
|
-
* Validates length of a string field.
|
|
167
|
-
*/
|
|
168
|
-
attribute @length(_ min: Int?, _ max: Int?) @@@targetField([StringField])
|
|
169
|
-
|
|
170
|
-
/*
|
|
171
|
-
* Validates a string field value matches a regex.
|
|
172
|
-
*/
|
|
173
|
-
attribute @regex(_ regex: String) @@@targetField([StringField])
|
|
174
|
-
|
|
175
|
-
/*
|
|
176
|
-
* Validates a string field value starts with the given text.
|
|
177
|
-
*/
|
|
178
|
-
attribute @startsWith(_ text: String) @@@targetField([StringField])
|
|
179
|
-
|
|
180
|
-
/*
|
|
181
|
-
* Validates a string field value ends with the given text.
|
|
182
|
-
*/
|
|
183
|
-
attribute @endsWith(_ text: String) @@@targetField([StringField])
|
|
184
|
-
|
|
185
|
-
/*
|
|
186
|
-
* Validates a string field value is a valid email address.
|
|
187
|
-
*/
|
|
188
|
-
attribute @email() @@@targetField([StringField])
|
|
189
|
-
|
|
190
|
-
/*
|
|
191
|
-
* Validates a string field value is a valid ISO datetime.
|
|
192
|
-
*/
|
|
193
|
-
attribute @datetime() @@@targetField([StringField])
|
|
194
|
-
|
|
195
|
-
/*
|
|
196
|
-
* Validates a string field value is a valid url.
|
|
197
|
-
*/
|
|
198
|
-
attribute @url() @@@targetField([StringField])
|
|
199
|
-
|
|
200
|
-
/*
|
|
201
|
-
* Validates a number field is greater than the given value.
|
|
202
|
-
*/
|
|
203
|
-
attribute @gt(_ value: Int) @@@targetField([IntField, FloatField, DecimalField])
|
|
204
|
-
|
|
205
|
-
/*
|
|
206
|
-
* Validates a number field is greater than or equal to the given value.
|
|
207
|
-
*/
|
|
208
|
-
attribute @gte(_ value: Int) @@@targetField([IntField, FloatField, DecimalField])
|
|
209
|
-
|
|
210
|
-
/*
|
|
211
|
-
* Validates a number field is less than the given value.
|
|
212
|
-
*/
|
|
213
|
-
attribute @lt(_ value: Int) @@@targetField([IntField, FloatField, DecimalField])
|
|
214
|
-
|
|
215
|
-
/*
|
|
216
|
-
* Validates a number field is less than or equal to the given value.
|
|
217
|
-
*/
|
|
218
|
-
attribute @lte(_ value: Int) @@@targetField([IntField, FloatField, DecimalField])
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES6",
|
|
4
|
-
"module": "CommonJS",
|
|
5
|
-
"lib": ["ESNext", "DOM"],
|
|
6
|
-
"sourceMap": true,
|
|
7
|
-
"outDir": "lib",
|
|
8
|
-
"strict": true,
|
|
9
|
-
"moduleResolution": "node",
|
|
10
|
-
"esModuleInterop": true,
|
|
11
|
-
"skipLibCheck": true,
|
|
12
|
-
"declaration": true,
|
|
13
|
-
"resolveJsonModule": true
|
|
14
|
-
},
|
|
15
|
-
"include": ["**/*.ts"],
|
|
16
|
-
"exclude": ["node_modules", ".prisma", "**/*.d.ts"]
|
|
17
|
-
}
|
package/src/cli/cli-error.ts
DELETED
package/src/cli/cli-util.ts
DELETED
|
@@ -1,214 +0,0 @@
|
|
|
1
|
-
import { STD_LIB_MODULE_NAME } from '@lang/constants';
|
|
2
|
-
import { Model } from '@lang/generated/ast';
|
|
3
|
-
import { createZModelServices } from '@lang/zmodel-module';
|
|
4
|
-
import colors from 'colors';
|
|
5
|
-
import fs from 'fs';
|
|
6
|
-
import { LangiumServices } from 'langium';
|
|
7
|
-
import { NodeFileSystem } from 'langium/node';
|
|
8
|
-
import path from 'path';
|
|
9
|
-
import { installPackage, PackageManagers } from '../utils/pkg-utils';
|
|
10
|
-
import { URI } from 'vscode-uri';
|
|
11
|
-
import { ZenStackGenerator } from '../generator';
|
|
12
|
-
import { GENERATED_CODE_PATH } from '../generator/constants';
|
|
13
|
-
import { Context, GeneratorError } from '../generator/types';
|
|
14
|
-
import { CliError } from './cli-error';
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Initializes an existing project for ZenStack
|
|
18
|
-
*/
|
|
19
|
-
export async function initProject(
|
|
20
|
-
projectPath: string,
|
|
21
|
-
packageManager: PackageManagers | undefined
|
|
22
|
-
) {
|
|
23
|
-
if (!fs.existsSync(projectPath)) {
|
|
24
|
-
console.error(`Path does not exist: ${projectPath}`);
|
|
25
|
-
throw new CliError('project path does not exist');
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const schema = path.join(projectPath, 'zenstack', 'schema.zmodel');
|
|
29
|
-
let schemaGenerated = false;
|
|
30
|
-
|
|
31
|
-
if (fs.existsSync(schema)) {
|
|
32
|
-
console.warn(colors.yellow(`Model already exists: ${schema}`));
|
|
33
|
-
} else {
|
|
34
|
-
// create a default model
|
|
35
|
-
if (!fs.existsSync(path.join(projectPath, 'zenstack'))) {
|
|
36
|
-
fs.mkdirSync(path.join(projectPath, 'zenstack'));
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
fs.writeFileSync(
|
|
40
|
-
schema,
|
|
41
|
-
`// This is a sample model to get you started.
|
|
42
|
-
// Learn how to model you app: https://zenstack.dev/#/modeling-your-app.
|
|
43
|
-
|
|
44
|
-
/*
|
|
45
|
-
* A sample data source using local sqlite db.
|
|
46
|
-
* See how to use a different db: https://zenstack.dev/#/zmodel-data-source.
|
|
47
|
-
*/
|
|
48
|
-
datasource db {
|
|
49
|
-
provider = 'sqlite'
|
|
50
|
-
url = 'file:./todo.db'
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/*
|
|
54
|
-
* User model
|
|
55
|
-
*/
|
|
56
|
-
model User {
|
|
57
|
-
id String @id @default(cuid())
|
|
58
|
-
email String @unique @email
|
|
59
|
-
password String @password @omit @length(8, 16)
|
|
60
|
-
posts Post[]
|
|
61
|
-
|
|
62
|
-
// everybody can signup
|
|
63
|
-
@@allow('create', true)
|
|
64
|
-
|
|
65
|
-
// full access by self
|
|
66
|
-
@@allow('all', auth() == this)
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/*
|
|
70
|
-
* Post model
|
|
71
|
-
*/
|
|
72
|
-
model Post {
|
|
73
|
-
id String @id @default(cuid())
|
|
74
|
-
createdAt DateTime @default(now())
|
|
75
|
-
updatedAt DateTime @updatedAt
|
|
76
|
-
title String @length(1, 256)
|
|
77
|
-
content String
|
|
78
|
-
published Boolean @default(false)
|
|
79
|
-
author User? @relation(fields: [authorId], references: [id])
|
|
80
|
-
authorId String?
|
|
81
|
-
|
|
82
|
-
// allow read for all signin users
|
|
83
|
-
@@allow('read', auth() != null && published)
|
|
84
|
-
|
|
85
|
-
// full access by author
|
|
86
|
-
@@allow('all', author == auth())
|
|
87
|
-
}
|
|
88
|
-
`
|
|
89
|
-
);
|
|
90
|
-
|
|
91
|
-
// add zenstack/schema.prisma to .gitignore
|
|
92
|
-
const gitIgnorePath = path.join(projectPath, '.gitignore');
|
|
93
|
-
let gitIgnoreContent = '';
|
|
94
|
-
if (fs.existsSync(gitIgnorePath)) {
|
|
95
|
-
gitIgnoreContent =
|
|
96
|
-
fs.readFileSync(gitIgnorePath, { encoding: 'utf-8' }) + '\n';
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
if (!gitIgnoreContent.includes('zenstack/schema.prisma')) {
|
|
100
|
-
gitIgnoreContent += 'zenstack/schema.prisma\n';
|
|
101
|
-
fs.writeFileSync(gitIgnorePath, gitIgnoreContent);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
schemaGenerated = true;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
installPackage('zenstack', true, packageManager, projectPath);
|
|
108
|
-
installPackage('@zenstackhq/runtime', false, packageManager, projectPath);
|
|
109
|
-
|
|
110
|
-
if (schemaGenerated) {
|
|
111
|
-
console.log(`Sample model generated at: ${colors.blue(schema)}
|
|
112
|
-
|
|
113
|
-
Please check the following guide on how to model your app:
|
|
114
|
-
https://zenstack.dev/#/modeling-your-app.
|
|
115
|
-
`);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
console.log(colors.green('\nProject initialized successfully!'));
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Loads a zmodel document from a file.
|
|
123
|
-
* @param fileName File name
|
|
124
|
-
* @param services Language services
|
|
125
|
-
* @returns Parsed and validated AST
|
|
126
|
-
*/
|
|
127
|
-
export async function loadDocument(
|
|
128
|
-
fileName: string,
|
|
129
|
-
services: LangiumServices
|
|
130
|
-
): Promise<Model> {
|
|
131
|
-
const extensions = services.LanguageMetaData.fileExtensions;
|
|
132
|
-
if (!extensions.includes(path.extname(fileName))) {
|
|
133
|
-
console.error(
|
|
134
|
-
colors.yellow(`Please choose a file with extension: ${extensions}.`)
|
|
135
|
-
);
|
|
136
|
-
throw new CliError('invalid schema file');
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
if (!fs.existsSync(fileName)) {
|
|
140
|
-
console.error(colors.red(`File ${fileName} does not exist.`));
|
|
141
|
-
throw new CliError('schema file does not exist');
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
// load standard library
|
|
145
|
-
const stdLib =
|
|
146
|
-
services.shared.workspace.LangiumDocuments.getOrCreateDocument(
|
|
147
|
-
URI.file(
|
|
148
|
-
path.resolve(
|
|
149
|
-
path.join(__dirname, '../res', STD_LIB_MODULE_NAME)
|
|
150
|
-
)
|
|
151
|
-
)
|
|
152
|
-
);
|
|
153
|
-
|
|
154
|
-
// load the document
|
|
155
|
-
const document =
|
|
156
|
-
services.shared.workspace.LangiumDocuments.getOrCreateDocument(
|
|
157
|
-
URI.file(path.resolve(fileName))
|
|
158
|
-
);
|
|
159
|
-
|
|
160
|
-
// build the document together with standard library
|
|
161
|
-
await services.shared.workspace.DocumentBuilder.build([stdLib, document], {
|
|
162
|
-
validationChecks: 'all',
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
const validationErrors = (document.diagnostics ?? []).filter(
|
|
166
|
-
(e) => e.severity === 1
|
|
167
|
-
);
|
|
168
|
-
if (validationErrors.length > 0) {
|
|
169
|
-
console.error(colors.red('Validation errors:'));
|
|
170
|
-
for (const validationError of validationErrors) {
|
|
171
|
-
console.error(
|
|
172
|
-
colors.red(
|
|
173
|
-
`line ${validationError.range.start.line + 1}: ${
|
|
174
|
-
validationError.message
|
|
175
|
-
} [${document.textDocument.getText(validationError.range)}]`
|
|
176
|
-
)
|
|
177
|
-
);
|
|
178
|
-
}
|
|
179
|
-
throw new CliError('schema validation errors');
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
return document.parseResult.value as Model;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
export async function runGenerator(
|
|
186
|
-
options: { schema: string; packageManager: PackageManagers | undefined },
|
|
187
|
-
includedGenerators?: string[],
|
|
188
|
-
clearOutput = true
|
|
189
|
-
) {
|
|
190
|
-
const services = createZModelServices(NodeFileSystem).ZModel;
|
|
191
|
-
const model = await loadDocument(options.schema, services);
|
|
192
|
-
|
|
193
|
-
const context: Context = {
|
|
194
|
-
schema: model,
|
|
195
|
-
outDir: path.dirname(options.schema),
|
|
196
|
-
// TODO: make this configurable
|
|
197
|
-
generatedCodeDir: GENERATED_CODE_PATH,
|
|
198
|
-
};
|
|
199
|
-
|
|
200
|
-
try {
|
|
201
|
-
await new ZenStackGenerator().generate(
|
|
202
|
-
context,
|
|
203
|
-
includedGenerators,
|
|
204
|
-
clearOutput
|
|
205
|
-
);
|
|
206
|
-
} catch (err) {
|
|
207
|
-
if (err instanceof GeneratorError) {
|
|
208
|
-
console.error(colors.red(err.message));
|
|
209
|
-
throw new CliError(err.message);
|
|
210
|
-
} else {
|
|
211
|
-
throw err;
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
}
|