-
-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathgenerate.ts
More file actions
311 lines (261 loc) · 10.2 KB
/
generate.ts
File metadata and controls
311 lines (261 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import { invariant, singleDebounce } from '@zenstackhq/common-helpers';
import { ZModelLanguageMetaData } from '@zenstackhq/language';
import { isPlugin, type AbstractDeclaration, type Model } from '@zenstackhq/language/ast';
import { getLiteral, getLiteralArray } from '@zenstackhq/language/utils';
import { type CliPlugin } from '@zenstackhq/sdk';
import { watch } from 'chokidar';
import colors from 'colors';
import path from 'node:path';
import ora, { type Ora } from 'ora';
import semver from 'semver';
import { CliError } from '../cli-error';
import * as corePlugins from '../plugins';
import {
getOutputPath,
getPluginProvider,
getSchemaFile,
getZenStackPackages,
loadPluginModule,
loadSchemaDocument,
startUsageTipsFetch,
} from './action-utils';
type Options = {
schema?: string;
output?: string;
silent: boolean;
watch: boolean;
lite?: boolean;
liteOnly?: boolean;
generateModels?: boolean;
generateInput?: boolean;
tips?: boolean;
};
/**
* CLI action for generating code from schema
*/
export async function run(options: Options) {
try {
await checkForMismatchedPackages(process.cwd());
} catch (err) {
console.warn(colors.yellow(`Failed to check for mismatched ZenStack packages: ${err}`));
}
const maybeShowUsageTips = options.tips && !options.silent && !options.watch ? startUsageTipsFetch() : undefined;
const model = await pureGenerate(options, false);
await maybeShowUsageTips?.();
if (options.watch) {
const logsEnabled = !options.silent;
if (logsEnabled) {
console.log(colors.green(`\nEnabled watch mode!`));
}
const schemaExtensions = ZModelLanguageMetaData.fileExtensions;
// Get real models file path (cuz its merged into single document -> we need use cst nodes)
const getRootModelWatchPaths = (model: Model) =>
new Set<string>(
(
model.declarations.filter(
(v) =>
v.$cstNode?.parent?.element.$type === 'Model' &&
!!v.$cstNode.parent.element.$document?.uri?.fsPath,
) as AbstractDeclaration[]
).map((v) => v.$cstNode!.parent!.element.$document!.uri!.fsPath),
);
const watchedPaths = getRootModelWatchPaths(model);
if (logsEnabled) {
const logPaths = [...watchedPaths].map((at) => `- ${at}`).join('\n');
console.log(`Watched file paths:\n${logPaths}`);
}
const watcher = watch([...watchedPaths], {
alwaysStat: false,
ignoreInitial: true,
ignorePermissionErrors: true,
ignored: (at) => !schemaExtensions.some((ext) => at.endsWith(ext)),
});
// prevent save multiple files and run multiple times
const reGenerateSchema = singleDebounce(
async () => {
if (logsEnabled) {
console.log('Got changes, run generation!');
}
try {
const newModel = await pureGenerate(options, true);
const allModelsPaths = getRootModelWatchPaths(newModel);
const newModelPaths = [...allModelsPaths].filter((at) => !watchedPaths.has(at));
const removeModelPaths = [...watchedPaths].filter((at) => !allModelsPaths.has(at));
if (newModelPaths.length) {
if (logsEnabled) {
const logPaths = newModelPaths.map((at) => `- ${at}`).join('\n');
console.log(`Added file(s) to watch:\n${logPaths}`);
}
newModelPaths.forEach((at) => watchedPaths.add(at));
watcher.add(newModelPaths);
}
if (removeModelPaths.length) {
if (logsEnabled) {
const logPaths = removeModelPaths.map((at) => `- ${at}`).join('\n');
console.log(`Removed file(s) from watch:\n${logPaths}`);
}
removeModelPaths.forEach((at) => watchedPaths.delete(at));
watcher.unwatch(removeModelPaths);
}
} catch (e) {
console.error(e);
}
},
500,
true,
);
watcher.on('unlink', (pathAt) => {
if (logsEnabled) {
console.log(`Removed file from watch: ${pathAt}`);
}
watchedPaths.delete(pathAt);
watcher.unwatch(pathAt);
reGenerateSchema();
});
watcher.on('change', () => {
reGenerateSchema();
});
}
}
async function pureGenerate(options: Options, fromWatch: boolean) {
const start = Date.now();
const schemaFile = getSchemaFile(options.schema);
const model = await loadSchemaDocument(schemaFile);
const outputPath = getOutputPath(options, schemaFile);
await runPlugins(schemaFile, model, outputPath, options);
if (!options.silent) {
console.log(colors.green(`Generation completed successfully in ${Date.now() - start}ms.\n`));
if (!fromWatch) {
console.log(`You can now create a ZenStack client with it.
\`\`\`ts
import { ZenStackClient } from '@zenstackhq/orm';
import { schema } from '${path.relative('.', outputPath)}/schema';
const client = new ZenStackClient(schema, {
dialect: { ... }
});
\`\`\`
Check documentation: https://zenstack.dev/docs/`);
}
}
return model;
}
async function runPlugins(schemaFile: string, model: Model, outputPath: string, options: Options) {
const plugins = model.declarations.filter(isPlugin);
const processedPlugins: { cliPlugin: CliPlugin; pluginOptions: Record<string, unknown> }[] = [];
for (const plugin of plugins) {
const provider = getPluginProvider(plugin);
let cliPlugin: CliPlugin | undefined;
if (provider.startsWith('@core/')) {
cliPlugin = (corePlugins as any)[provider.slice('@core/'.length)];
if (!cliPlugin) {
throw new CliError(`Unknown core plugin: ${provider}`);
}
} else {
cliPlugin = await loadPluginModule(provider, path.dirname(schemaFile));
}
if (cliPlugin) {
const pluginOptions = getPluginOptions(plugin);
// merge CLI options
if (provider === '@core/typescript') {
if (options.lite !== undefined) {
pluginOptions['lite'] = options.lite;
}
if (options.liteOnly !== undefined) {
pluginOptions['liteOnly'] = options.liteOnly;
}
if (options.generateModels !== undefined) {
pluginOptions['generateModels'] = options.generateModels;
}
if (options.generateInput !== undefined) {
pluginOptions['generateInput'] = options.generateInput;
}
}
processedPlugins.push({ cliPlugin, pluginOptions });
}
}
const defaultPlugins = [
{
plugin: corePlugins['typescript'],
options: {
lite: options.lite,
liteOnly: options.liteOnly,
generateModels: options.generateModels,
generateInput: options.generateInput,
},
},
];
defaultPlugins.forEach(({ plugin, options }) => {
if (!processedPlugins.some((p) => p.cliPlugin === plugin)) {
// default plugins are run before user plugins
processedPlugins.unshift({ cliPlugin: plugin, pluginOptions: options });
}
});
for (const { cliPlugin, pluginOptions } of processedPlugins) {
invariant(
typeof cliPlugin.generate === 'function',
`Plugin ${cliPlugin.name} does not have a generate function`,
);
// run plugin generator
let spinner: Ora | undefined;
if (!options.silent) {
spinner = ora(cliPlugin.statusText ?? `Running plugin ${cliPlugin.name}`).start();
}
try {
await cliPlugin.generate({
schemaFile,
model,
defaultOutputPath: outputPath,
pluginOptions,
});
spinner?.succeed();
} catch (err) {
spinner?.fail();
console.error(err);
}
}
}
function getPluginOptions(plugin: Parameters<typeof getPluginProvider>[0]): Record<string, unknown> {
const result: Record<string, unknown> = {};
for (const field of plugin.fields) {
if (field.name === 'provider') {
continue; // skip provider
}
const value = getLiteral(field.value) ?? getLiteralArray(field.value);
if (value === undefined) {
console.warn(`Plugin "${plugin.name}" option "${field.name}" has unsupported value, skipping`);
continue;
}
result[field.name] = value;
}
return result;
}
async function checkForMismatchedPackages(projectPath: string) {
const packages = await getZenStackPackages(projectPath);
if (!packages.length) {
return false;
}
const versions = new Set<string>();
for (const { version } of packages) {
if (version) {
versions.add(version);
}
}
if (versions.size > 1) {
const message =
'WARNING: Multiple versions of ZenStack packages detected.\n\tThis will probably cause issues and break your types.';
const slashes = '/'.repeat(73);
const latestVersion = semver.sort(Array.from(versions)).reverse()[0]!;
console.warn(colors.yellow(`${slashes}\n\n\t${message}\n`));
for (const { pkg, version } of packages) {
if (!version) continue;
if (version === latestVersion) {
console.log(`\t${pkg.padEnd(32)}\t${colors.green(version)}`);
} else {
console.log(`\t${pkg.padEnd(32)}\t${colors.yellow(version)}`);
}
}
console.warn(`\n${colors.yellow(slashes)}`);
return true;
}
return false;
}