@simplix-boot/cli 0.0.0-stage → 1.3.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/LICENSE +25 -0
- package/LICENSE_KO +28 -0
- package/README.md +146 -2
- package/bin/simplix-boot.js +6 -0
- package/package.json +45 -4
- package/src/cli.js +103 -0
- package/src/commands/config.js +129 -0
- package/src/commands/generate.js +291 -0
- package/src/commands/generated.js +125 -0
- package/src/commands/init.js +41 -0
- package/src/commands/promote.js +172 -0
- package/src/commands/templates.js +100 -0
- package/src/commands/version.js +134 -0
- package/src/lib/analysis/fields.js +217 -0
- package/src/lib/analysis/imports.js +76 -0
- package/src/lib/analysis/structure.js +193 -0
- package/src/lib/analysis/types.js +94 -0
- package/src/lib/args.js +69 -0
- package/src/lib/comment-extractor.js +141 -0
- package/src/lib/config/fields.js +321 -0
- package/src/lib/config/parents.js +108 -0
- package/src/lib/config/paths.js +156 -0
- package/src/lib/config/yml.js +61 -0
- package/src/lib/console.js +83 -0
- package/src/lib/controller-name.js +33 -0
- package/src/lib/entity-analyzer.js +49 -0
- package/src/lib/entity-type-resolver.js +80 -0
- package/src/lib/entity-type.js +35 -0
- package/src/lib/errors.js +17 -0
- package/src/lib/export-config.js +74 -0
- package/src/lib/file-utils.js +77 -0
- package/src/lib/generate/file-plan.js +155 -0
- package/src/lib/generate/search-fields.js +111 -0
- package/src/lib/generate/settings.js +111 -0
- package/src/lib/generate/support-classes.js +47 -0
- package/src/lib/generate/template-vars.js +183 -0
- package/src/lib/git-utils.js +18 -0
- package/src/lib/name-field.js +42 -0
- package/src/lib/project.js +121 -0
- package/src/lib/promote-files.js +132 -0
- package/src/lib/rendered-output.js +13 -0
- package/src/lib/search-operators.js +87 -0
- package/src/lib/templates.js +50 -0
- package/src/lib/tracked-render.js +200 -0
- package/src/lib/version-check.js +195 -0
- package/src/lib/version-utils.js +97 -0
- package/templates/controller/rest/EntityRestController.java.template +517 -0
- package/templates/controller/web/EntityWebController.java.template +211 -0
- package/templates/dto/EntityDTOs.java.template +981 -0
- package/templates/repository/EntityRepository.java.template +78 -0
- package/templates/service/EntityService.java.template +790 -0
- package/templates/service/EntityTreeService.java.template +352 -0
- package/templates/test/EntityServiceTest.java.template +937 -0
- package/templates/test/EntityTreeServiceTest.java.template +842 -0
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import ejs from 'ejs';
|
|
4
|
+
import lodash from 'lodash';
|
|
5
|
+
import { extractTableComment } from '../lib/comment-extractor.js';
|
|
6
|
+
import { symbols } from '../lib/console.js';
|
|
7
|
+
import { resolveControllerName } from '../lib/controller-name.js';
|
|
8
|
+
import { analyzeEntity } from '../lib/entity-analyzer.js';
|
|
9
|
+
import { createEntityTypeResolver } from '../lib/entity-type-resolver.js';
|
|
10
|
+
import { resolveEntityType } from '../lib/entity-type.js';
|
|
11
|
+
import { CliError } from '../lib/errors.js';
|
|
12
|
+
import { resolveExportConfig } from '../lib/export-config.js';
|
|
13
|
+
import { findEntityFile, validateProjectStructure } from '../lib/file-utils.js';
|
|
14
|
+
import { buildFileGenerationList, treeServiceFiles } from '../lib/generate/file-plan.js';
|
|
15
|
+
import { prepareSearchFields } from '../lib/generate/search-fields.js';
|
|
16
|
+
import { createSupportClasses } from '../lib/generate/support-classes.js';
|
|
17
|
+
import { deriveRepositoryPackage, resolveGenerateSettings } from '../lib/generate/settings.js';
|
|
18
|
+
import { buildTemplateVars, transformImportForService } from '../lib/generate/template-vars.js';
|
|
19
|
+
import { getGitConfig } from '../lib/git-utils.js';
|
|
20
|
+
import { readEntityConfig, requireProjectConfig } from '../lib/project.js';
|
|
21
|
+
import { tidyRenderedOutput } from '../lib/rendered-output.js';
|
|
22
|
+
import { assertGeneratorVersion } from '../lib/version-check.js';
|
|
23
|
+
import { getGeneratorVersion } from '../lib/version-utils.js';
|
|
24
|
+
|
|
25
|
+
export const usage = 'simplix-boot generate <EntityName> [--force] [--no-test] [--skip-version-check]';
|
|
26
|
+
|
|
27
|
+
export const summary = 'Generate the service, controller, DTOs and tests of an entity from its yml';
|
|
28
|
+
|
|
29
|
+
export const argumentSpec = {
|
|
30
|
+
positionals: ['entityName'],
|
|
31
|
+
options: {
|
|
32
|
+
force: { type: 'boolean', default: false },
|
|
33
|
+
test: { type: 'boolean', default: true },
|
|
34
|
+
'skip-version-check': { type: 'boolean', default: false },
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Tells the operator when the yml names a different field than the entity marks @NameField.
|
|
40
|
+
* SimpliX Meta publishes the entity's mark, so a screen scaffolded from Meta names its records
|
|
41
|
+
* by that field whatever the yml says.
|
|
42
|
+
*/
|
|
43
|
+
export function warnOnNameFieldConflict(fields, ymlName, entityConfig, log) {
|
|
44
|
+
const declared = fields.find(f => f.isNameField)?.name;
|
|
45
|
+
const configured = entityConfig?.nameField;
|
|
46
|
+
if (declared && configured && declared !== configured) {
|
|
47
|
+
log(
|
|
48
|
+
`${symbols.warning} ${ymlName}: the entity marks ${declared} @NameField but ` +
|
|
49
|
+
`.simplix/entity/${ymlName}.yml says nameField: ${configured}. ` +
|
|
50
|
+
`SimpliX Meta publishes ${declared}; set nameField: ${declared} in the yml`
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Renders one template with the entity's variables.
|
|
57
|
+
*
|
|
58
|
+
* @param {string} templatePath the template file
|
|
59
|
+
* @param {object} templateVars the variables
|
|
60
|
+
* @returns {string} the tidied output
|
|
61
|
+
*/
|
|
62
|
+
export function renderTemplate(templatePath, templateVars) {
|
|
63
|
+
const rendered = ejs.render(fs.readFileSync(templatePath, 'utf8'), templateVars, {
|
|
64
|
+
escape: text => text,
|
|
65
|
+
filename: templatePath,
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
return tidyRenderedOutput(rendered);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Generates the files of one entity into the project's generated tree.
|
|
73
|
+
*
|
|
74
|
+
* @param {object} run
|
|
75
|
+
* @param {string} run.root the project root
|
|
76
|
+
* @param {string} run.entityName the entity yml's name, which prefixes every generated class
|
|
77
|
+
* @param {boolean} [run.force] whether existing files are overwritten
|
|
78
|
+
* @param {boolean} [run.test] whether test files are generated
|
|
79
|
+
* @param {boolean} [run.skipVersionCheck] whether the version gate is skipped
|
|
80
|
+
* @param {(message?: string) => void} run.log where progress goes
|
|
81
|
+
* @throws {CliError} when the entity cannot be generated, or a template fails to render
|
|
82
|
+
*/
|
|
83
|
+
export function generate({ root, entityName: ymlName, force = false, test = true, skipVersionCheck = false, log }) {
|
|
84
|
+
if (!ymlName) {
|
|
85
|
+
throw new CliError(`Entity name is required\nUsage: ${usage}`);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const entityConfig = readEntityConfig(root, ymlName);
|
|
89
|
+
const config = requireProjectConfig(root);
|
|
90
|
+
const settings = resolveGenerateSettings(config, root, log);
|
|
91
|
+
|
|
92
|
+
assertGeneratorVersion({ config, log, skip: skipVersionCheck });
|
|
93
|
+
|
|
94
|
+
if (entityConfig === undefined || entityConfig === null) {
|
|
95
|
+
throw new CliError(
|
|
96
|
+
`.simplix/entity/${ymlName}.yml not found. Run "simplix-boot config ${ymlName}" to create it.`
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const modulePath = entityConfig.modulePath;
|
|
101
|
+
if (!modulePath) {
|
|
102
|
+
throw new CliError(`modulePath is not configured in ${ymlName}.yml`);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const packagePaths = {};
|
|
106
|
+
const templatePath =
|
|
107
|
+
entityConfig.thymeleafBaseDir || lodash.kebabCase(ymlName.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase());
|
|
108
|
+
|
|
109
|
+
Object.entries(settings.targetPaths).forEach(([key, targetPath]) => {
|
|
110
|
+
packagePaths[key] = `${settings.basePackage}.${targetPath.replace('{modulePath}', modulePath).replace(/\//g, '.')}`;
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
if (!settings.repositoryDeriveFromEntity && settings.repositoryBasePackage) {
|
|
114
|
+
const repoModulePath = entityConfig.repositoryModulePath || modulePath;
|
|
115
|
+
packagePaths.repository = `${settings.repositoryBasePackage}.${repoModulePath.replace(/\//g, '.')}`;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const { files, missingTemplates } = buildFileGenerationList({
|
|
119
|
+
root,
|
|
120
|
+
settings,
|
|
121
|
+
ymlName,
|
|
122
|
+
entityConfig,
|
|
123
|
+
modulePath,
|
|
124
|
+
withTests: test,
|
|
125
|
+
});
|
|
126
|
+
const entityName = resolveEntityType(ymlName, entityConfig);
|
|
127
|
+
const exportConfig = resolveExportConfig(ymlName, entityConfig, templatePath);
|
|
128
|
+
|
|
129
|
+
const srcPath = path.join(root, settings.baseEntityPath);
|
|
130
|
+
validateProjectStructure(srcPath, root);
|
|
131
|
+
|
|
132
|
+
const entityPath = findEntityFile(srcPath, entityName);
|
|
133
|
+
if (!entityPath) {
|
|
134
|
+
throw new CliError(`Entity file not found\nLooking for: ${entityName}.java\nSearched in: ${srcPath}`);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
log(`\n${symbols.success} Found entity file:\n ${path.relative(root, entityPath)}`);
|
|
138
|
+
|
|
139
|
+
const entityContent = fs.readFileSync(entityPath, 'utf8');
|
|
140
|
+
const packageMatch = entityContent.match(/package\s+([\w.]+);/);
|
|
141
|
+
if (!packageMatch) {
|
|
142
|
+
throw new CliError(`Package name not found in entity file: ${path.relative(root, entityPath)}`);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const entityPackage = packageMatch[1];
|
|
146
|
+
|
|
147
|
+
if (settings.repositoryDeriveFromEntity) {
|
|
148
|
+
const derivedRepoPackage = deriveRepositoryPackage(entityPackage);
|
|
149
|
+
if (derivedRepoPackage) {
|
|
150
|
+
packagePaths.repository = derivedRepoPackage;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const props = {
|
|
155
|
+
gitUser: getGitConfig('user.name', root) ?? 'Unknown',
|
|
156
|
+
gitEmail: getGitConfig('user.email', root) ?? 'unknown@example.com',
|
|
157
|
+
generatorVersion: getGeneratorVersion(),
|
|
158
|
+
entityName,
|
|
159
|
+
classPrefix: ymlName,
|
|
160
|
+
basePackage: settings.basePackage,
|
|
161
|
+
modulePath,
|
|
162
|
+
targetPaths: settings.targetPaths,
|
|
163
|
+
packagePaths,
|
|
164
|
+
templatePath,
|
|
165
|
+
exportConfig,
|
|
166
|
+
entityPackage,
|
|
167
|
+
entityBasePackage: settings.entityBasePackage,
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
const entityComment = extractTableComment(entityContent);
|
|
171
|
+
const analysis = analyzeEntity(entityContent, { isEntityType: createEntityTypeResolver(srcPath) });
|
|
172
|
+
|
|
173
|
+
const treeEntityInterface = (analysis.implementedInterfaces || []).find(i => i.name === 'TreeEntity');
|
|
174
|
+
const hasTreeEntity = Boolean(treeEntityInterface);
|
|
175
|
+
if (hasTreeEntity) {
|
|
176
|
+
log(`\n${symbols.success} Detected TreeEntity interface implementation`);
|
|
177
|
+
files.push(...treeServiceFiles({ root, settings, ymlName, modulePath }));
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const fields = analysis.fields.map(field => {
|
|
181
|
+
if (field.typeKind === 'entity' || field.isRelation || field.relationType === 'ManyToOne') {
|
|
182
|
+
const entityType = field.isCollection ? field.actualType : field.type;
|
|
183
|
+
const entityImport = field.importPath || `${entityPackage}.${entityType}`;
|
|
184
|
+
|
|
185
|
+
field.serviceImport = transformImportForService(entityImport, entityType);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return field;
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
warnOnNameFieldConflict(fields, ymlName, entityConfig, log);
|
|
192
|
+
|
|
193
|
+
const { idType, embeddedIdField, embeddedIdFields } = analysis;
|
|
194
|
+
const hasEmbeddedId = Boolean(embeddedIdField);
|
|
195
|
+
let embeddedIdPropertyNames = [];
|
|
196
|
+
|
|
197
|
+
if (hasEmbeddedId) {
|
|
198
|
+
log(`\n${symbols.success} Detected EmbeddedId: ${embeddedIdField} of type ${idType}`);
|
|
199
|
+
if (embeddedIdFields && embeddedIdFields.length > 0) {
|
|
200
|
+
embeddedIdPropertyNames = embeddedIdFields.map(field => field.name);
|
|
201
|
+
log(` With properties: ${embeddedIdPropertyNames.join(', ')}`);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const searchFields = prepareSearchFields({
|
|
206
|
+
ymlName,
|
|
207
|
+
entityConfig,
|
|
208
|
+
log,
|
|
209
|
+
fields,
|
|
210
|
+
embeddedIdFields,
|
|
211
|
+
embeddedIdPropertyNames,
|
|
212
|
+
embeddedIdField,
|
|
213
|
+
hasEmbeddedId,
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
const templateVars = buildTemplateVars({
|
|
217
|
+
props,
|
|
218
|
+
entityConfig,
|
|
219
|
+
entityComment,
|
|
220
|
+
analysis,
|
|
221
|
+
fields,
|
|
222
|
+
controllerName: resolveControllerName(ymlName, entityConfig),
|
|
223
|
+
dtoExcludeFields: settings.dtoExcludeFields,
|
|
224
|
+
searchFields,
|
|
225
|
+
hasTreeEntity,
|
|
226
|
+
treeEntityInterface,
|
|
227
|
+
supportClasses: createSupportClasses(config.supportClasses, ymlName),
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
log(`\n${symbols.info} Files to generate:`);
|
|
231
|
+
|
|
232
|
+
const skip = (targetPath, reason) => {
|
|
233
|
+
log(` ${symbols.warning} SKIP: ${path.relative(root, targetPath)} (${reason})`);
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
for (const file of missingTemplates) {
|
|
237
|
+
skip(file.path, `template not found: ${path.relative(root, file.projectTemplate)}`);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const failures = [];
|
|
241
|
+
|
|
242
|
+
for (const file of files) {
|
|
243
|
+
if (fs.existsSync(file.path) && !force) {
|
|
244
|
+
skip(file.path, 'already exists, use --force to overwrite');
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (!file.template) {
|
|
249
|
+
skip(file.path, `template not found: ${path.relative(root, file.projectTemplate)}`);
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
let rendered;
|
|
254
|
+
try {
|
|
255
|
+
rendered = renderTemplate(file.template.path, templateVars);
|
|
256
|
+
} catch (error) {
|
|
257
|
+
// A template is the project's own code: whatever it throws is reported against the file it
|
|
258
|
+
// was rendering, and the run fails after the remaining files have been tried.
|
|
259
|
+
failures.push(file);
|
|
260
|
+
log(` ${symbols.error} ${path.relative(root, file.path)}`);
|
|
261
|
+
log(` Failed to render ${file.templateFile}: ${error.message}`);
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
log(` ${symbols.success} ${path.relative(root, file.path)}`);
|
|
266
|
+
fs.mkdirSync(path.dirname(file.path), { recursive: true });
|
|
267
|
+
fs.writeFileSync(file.path, rendered);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if (failures.length > 0) {
|
|
271
|
+
throw new CliError(
|
|
272
|
+
`${failures.length} of ${files.length} files failed to render for ${ymlName}: ${failures
|
|
273
|
+
.map(file => path.basename(file.path))
|
|
274
|
+
.join(', ')}`
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
log(`\n${symbols.success} Successfully generated CRUD files for ${ymlName}!`);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** Runs `generate` from the command line. */
|
|
282
|
+
export function run({ root, positionals, options, output }) {
|
|
283
|
+
generate({
|
|
284
|
+
root,
|
|
285
|
+
entityName: positionals.entityName,
|
|
286
|
+
force: options.force,
|
|
287
|
+
test: options.test,
|
|
288
|
+
skipVersionCheck: options['skip-version-check'],
|
|
289
|
+
log: output.log,
|
|
290
|
+
});
|
|
291
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { CliError } from '../lib/errors.js';
|
|
4
|
+
import { listEntityNames, readEntityConfig, requireProjectConfig } from '../lib/project.js';
|
|
5
|
+
import { findPromotedModule, promoteFiles } from '../lib/promote-files.js';
|
|
6
|
+
import { isUntouched, planTracked, readBase, stampRender } from '../lib/tracked-render.js';
|
|
7
|
+
import { generate } from './generate.js';
|
|
8
|
+
|
|
9
|
+
export const usage = 'simplix-boot generated [--no-render] [--json]';
|
|
10
|
+
|
|
11
|
+
export const summary = 'Report the state of every promoted file against its base and the current templates';
|
|
12
|
+
|
|
13
|
+
export const argumentSpec = {
|
|
14
|
+
positionals: [],
|
|
15
|
+
options: {
|
|
16
|
+
render: { type: 'boolean', default: true },
|
|
17
|
+
json: { type: 'boolean', default: false },
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The states that fail the report: a promote would rewrite the file, would collide with its
|
|
23
|
+
* edits, or cannot merge into it at all.
|
|
24
|
+
*/
|
|
25
|
+
export const FAILING_STATES = ['stale', 'conflict', 'no-base'];
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Judges one promoted file against its stored base and the current templates.
|
|
29
|
+
*
|
|
30
|
+
* - `untouched`: still exactly the render promote laid down, and what it would lay down now
|
|
31
|
+
* - `edited`: changed since it was laid down; the current render merges into it cleanly
|
|
32
|
+
* - `stale`: untouched, but the current render differs; a promote would rewrite it
|
|
33
|
+
* - `conflict`: edited, and the current render collides with the edits
|
|
34
|
+
* - `no-base`: present with no stored base, so no later template change can be merged into it
|
|
35
|
+
* - `removed`: a base is stored and the file is gone
|
|
36
|
+
* - `absent`: neither a file nor a base
|
|
37
|
+
*
|
|
38
|
+
* @param {string} root the project root
|
|
39
|
+
* @param {string} dest the promoted file
|
|
40
|
+
* @param {string|undefined} fresh the current render, stamped, or undefined when not rendered
|
|
41
|
+
* @returns {string} the state
|
|
42
|
+
*/
|
|
43
|
+
export function judgePromoted(root, dest, fresh) {
|
|
44
|
+
const base = readBase(root, dest);
|
|
45
|
+
if (!fs.existsSync(dest)) return base === undefined ? 'absent' : 'removed';
|
|
46
|
+
if (base === undefined) return 'no-base';
|
|
47
|
+
const held = fs.readFileSync(dest, 'utf8');
|
|
48
|
+
if (isUntouched(held)) return fresh === undefined || fresh === held ? 'untouched' : 'stale';
|
|
49
|
+
if (fresh === undefined) return 'edited';
|
|
50
|
+
return planTracked(root, dest, fresh).outcome === 'conflict' ? 'conflict' : 'edited';
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Collects the state of every promoted file of a project. With `render`, each promoted entity is
|
|
55
|
+
* generated again first (into the generated tree), so a stale file and a conflicting merge are
|
|
56
|
+
* found before a promote meets them.
|
|
57
|
+
*
|
|
58
|
+
* @param {object} report
|
|
59
|
+
* @param {string} report.root the project root
|
|
60
|
+
* @param {boolean} [report.render] whether every promoted entity is rendered before it is judged
|
|
61
|
+
* @returns {{ files: { path: string, entity: string, state: string }[], counts: Record<string, number> }}
|
|
62
|
+
* @throws {CliError} when an entity fails to render, so no state can be judged
|
|
63
|
+
*/
|
|
64
|
+
export function collectGeneratedState({ root, render = true }) {
|
|
65
|
+
const config = requireProjectConfig(root);
|
|
66
|
+
const files = [];
|
|
67
|
+
|
|
68
|
+
for (const entityName of listEntityNames(root)) {
|
|
69
|
+
const entityConfig = readEntityConfig(root, entityName);
|
|
70
|
+
const moduleName = findPromotedModule(root, config, entityName, entityConfig);
|
|
71
|
+
|
|
72
|
+
if (moduleName === undefined) continue;
|
|
73
|
+
|
|
74
|
+
if (render) renderEntity(root, entityName);
|
|
75
|
+
|
|
76
|
+
const origin = `promote ${entityName} --module ${moduleName}`;
|
|
77
|
+
|
|
78
|
+
for (const file of promoteFiles({ root, config, entityName, entityConfig, moduleName })) {
|
|
79
|
+
const fresh =
|
|
80
|
+
render && fs.existsSync(file.src) ? stampRender(fs.readFileSync(file.src, 'utf8'), origin) : undefined;
|
|
81
|
+
const state = judgePromoted(root, file.dest, fresh);
|
|
82
|
+
|
|
83
|
+
if (state !== 'absent') files.push({ path: path.relative(root, file.dest), entity: entityName, state });
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const counts = {};
|
|
88
|
+
for (const file of files) counts[file.state] = (counts[file.state] ?? 0) + 1;
|
|
89
|
+
|
|
90
|
+
return { files, counts };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function renderEntity(root, entityName) {
|
|
94
|
+
const lines = [];
|
|
95
|
+
|
|
96
|
+
try {
|
|
97
|
+
generate({ root, entityName, force: true, skipVersionCheck: true, log: message => lines.push(message ?? '') });
|
|
98
|
+
} catch (error) {
|
|
99
|
+
if (!(error instanceof CliError)) throw error;
|
|
100
|
+
const tail = [...lines.join('\n').trim().split('\n').slice(-3), error.message].join('\n');
|
|
101
|
+
throw new CliError(`Rendering ${entityName} failed, so no state can be judged:\n${tail}`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Runs `generated` from the command line. */
|
|
106
|
+
export function run({ root, options, output }) {
|
|
107
|
+
const { files, counts } = collectGeneratedState({ root, render: options.render });
|
|
108
|
+
|
|
109
|
+
if (options.json) {
|
|
110
|
+
output.result(JSON.stringify({ files, counts }));
|
|
111
|
+
} else {
|
|
112
|
+
for (const file of files) output.log(`${file.state.padEnd(10)} ${file.path}`);
|
|
113
|
+
output.log(
|
|
114
|
+
`\n${Object.entries(counts)
|
|
115
|
+
.map(([state, count]) => `${state} ${count}`)
|
|
116
|
+
.join(' · ')}`
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (files.some(file => FAILING_STATES.includes(file.state))) {
|
|
121
|
+
return 1;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return 0;
|
|
125
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { style } from '../lib/console.js';
|
|
4
|
+
import { CONFIG_FILE_NAME, SIMPLIX_DIR } from '../lib/project.js';
|
|
5
|
+
import { getGeneratorVersion } from '../lib/version-utils.js';
|
|
6
|
+
|
|
7
|
+
export const usage = 'simplix-boot init';
|
|
8
|
+
|
|
9
|
+
export const summary = 'Create the .simplix folder in the current directory';
|
|
10
|
+
|
|
11
|
+
export const argumentSpec = { positionals: [], options: {} };
|
|
12
|
+
|
|
13
|
+
/** `init` creates the project folder, so it runs where it is called instead of inside a project. */
|
|
14
|
+
export const needsProject = false;
|
|
15
|
+
|
|
16
|
+
/** Runs `init` from the command line. */
|
|
17
|
+
export function run({ cwd, output }) {
|
|
18
|
+
const { log } = output;
|
|
19
|
+
const simplixPath = path.join(cwd, SIMPLIX_DIR);
|
|
20
|
+
|
|
21
|
+
log(`\n${style.cyan('SimpliX Boot CLI')} v${getGeneratorVersion()} - Initialize\n`);
|
|
22
|
+
|
|
23
|
+
if (fs.existsSync(simplixPath)) {
|
|
24
|
+
log(style.yellow(`${SIMPLIX_DIR} folder already exists.\n`));
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
for (const folder of ['entity', 'templates']) {
|
|
29
|
+
const keep = path.join(simplixPath, folder, '.gitkeep');
|
|
30
|
+
fs.mkdirSync(path.dirname(keep), { recursive: true });
|
|
31
|
+
fs.writeFileSync(keep, '');
|
|
32
|
+
log(` create ${path.relative(cwd, keep)}`);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
log(style.green('SimpliX initialized successfully.\n'));
|
|
36
|
+
log('Next steps:');
|
|
37
|
+
log(` 1. Create ${style.cyan(`${SIMPLIX_DIR}/${CONFIG_FILE_NAME}`)} configuration file`);
|
|
38
|
+
log(` 2. Run ${style.cyan('simplix-boot version --write')} to pin the CLI version`);
|
|
39
|
+
log(` 3. Run ${style.cyan('simplix-boot config <EntityName>')} to generate entity config`);
|
|
40
|
+
log(` 4. Run ${style.cyan('simplix-boot generate <EntityName>')} to generate CRUD files\n`);
|
|
41
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { symbols } from '../lib/console.js';
|
|
4
|
+
import { CliError } from '../lib/errors.js';
|
|
5
|
+
import { readEntityConfig, requireProjectConfig } from '../lib/project.js';
|
|
6
|
+
import { promoteFiles } from '../lib/promote-files.js';
|
|
7
|
+
import { carryStamp, readBase, recordBase, stampRender, writeTracked } from '../lib/tracked-render.js';
|
|
8
|
+
import { assertGeneratorVersion } from '../lib/version-check.js';
|
|
9
|
+
|
|
10
|
+
export const usage = 'simplix-boot promote <EntityName> --module <module> [--adopt] [--force] [--skip-version-check]';
|
|
11
|
+
|
|
12
|
+
export const summary = 'Move generated files into a module as tracked renders, merging later template changes';
|
|
13
|
+
|
|
14
|
+
export const argumentSpec = {
|
|
15
|
+
positionals: ['entityName'],
|
|
16
|
+
options: {
|
|
17
|
+
module: { type: 'string' },
|
|
18
|
+
force: { type: 'boolean', default: false },
|
|
19
|
+
adopt: { type: 'boolean', default: false },
|
|
20
|
+
'skip-version-check': { type: 'boolean', default: false },
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Promotes one stamped render under the tracked-render rule, or overwrites with `force`, or with
|
|
26
|
+
* `adopt` takes the render as the base of an existing file that has none.
|
|
27
|
+
*/
|
|
28
|
+
function promoteOne(root, file, stamped, { force, adopt }) {
|
|
29
|
+
if (force) {
|
|
30
|
+
fs.mkdirSync(path.dirname(file.dest), { recursive: true });
|
|
31
|
+
fs.writeFileSync(file.dest, stamped);
|
|
32
|
+
recordBase(root, file.dest, stamped);
|
|
33
|
+
return { outcome: 'written' };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Adopting a file the customisation deleted records its base, so later promotes leave it deleted.
|
|
37
|
+
if (adopt && !fs.existsSync(file.dest)) {
|
|
38
|
+
if (readBase(root, file.dest) === undefined) recordBase(root, file.dest, stamped);
|
|
39
|
+
return { outcome: 'removed' };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (adopt && readBase(root, file.dest) === undefined) {
|
|
43
|
+
const held = fs.readFileSync(file.dest, 'utf8');
|
|
44
|
+
fs.writeFileSync(file.dest, carryStamp(held, stamped));
|
|
45
|
+
recordBase(root, file.dest, stamped);
|
|
46
|
+
return { outcome: 'adopted' };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return writeTracked(root, file.dest, stamped);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Promotes the generated files of one entity into a module.
|
|
54
|
+
*
|
|
55
|
+
* <p>Every promoted file is a tracked render: it opens with a fingerprint of the render it was
|
|
56
|
+
* laid down as, and that render is stored under `.simplix/bases/`. A later promote rewrites an
|
|
57
|
+
* untouched file, merges the template change three ways into an edited one, and never overwrites
|
|
58
|
+
* an edited file whose merge conflicts.
|
|
59
|
+
*
|
|
60
|
+
* @param {object} run
|
|
61
|
+
* @param {string} run.root the project root
|
|
62
|
+
* @param {string} run.entityName the entity yml's name
|
|
63
|
+
* @param {string} run.moduleName the target module below the module root
|
|
64
|
+
* @param {boolean} [run.force] whether existing files are overwritten, edits included
|
|
65
|
+
* @param {boolean} [run.adopt] whether this render becomes the base of files promoted without one
|
|
66
|
+
* @param {boolean} [run.skipVersionCheck] whether the version gate is skipped
|
|
67
|
+
* @param {(message?: string) => void} run.log where progress goes
|
|
68
|
+
* @throws {CliError} when a file conflicts or has no base, or the target module does not exist
|
|
69
|
+
*/
|
|
70
|
+
export function promote({ root, entityName, moduleName, force = false, adopt = false, skipVersionCheck = false, log }) {
|
|
71
|
+
if (!entityName) {
|
|
72
|
+
throw new CliError(`Entity name is required\nUsage: ${usage}`);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const config = requireProjectConfig(root);
|
|
76
|
+
assertGeneratorVersion({ config, log, skip: skipVersionCheck });
|
|
77
|
+
|
|
78
|
+
if (!moduleName) {
|
|
79
|
+
throw new CliError(
|
|
80
|
+
`--module option is required.\nUsage: ${usage}\nExample: simplix-boot promote UserAccount --module user-admin`
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
log(`\n${symbols.info} Promoting ${entityName} to module "${moduleName}"...`);
|
|
85
|
+
|
|
86
|
+
const entityConfig = readEntityConfig(root, entityName);
|
|
87
|
+
if (entityConfig === undefined || entityConfig === null) {
|
|
88
|
+
throw new CliError(`Entity configuration not found: .simplix/entity/${entityName}.yml`);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const moduleRoot = config.promote?.moduleRoot || 'modules';
|
|
92
|
+
if (!fs.existsSync(path.join(root, moduleRoot, moduleName))) {
|
|
93
|
+
throw new CliError(`Target module directory not found: ${moduleRoot}/${moduleName}`);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
log(`${symbols.info} Module path: ${entityConfig.modulePath}`);
|
|
97
|
+
log(`${symbols.info} Target module: ${moduleRoot}/${moduleName}`);
|
|
98
|
+
|
|
99
|
+
const origin = `promote ${entityName} --module ${moduleName}`;
|
|
100
|
+
const counts = {};
|
|
101
|
+
let failed = false;
|
|
102
|
+
|
|
103
|
+
for (const file of promoteFiles({ root, config, entityName, entityConfig, moduleName })) {
|
|
104
|
+
log(`\n${symbols.info} ${file.name}:`);
|
|
105
|
+
log(` From: ${path.relative(root, file.src)}`);
|
|
106
|
+
log(` To: ${path.relative(root, file.dest)}`);
|
|
107
|
+
|
|
108
|
+
if (!fs.existsSync(file.src)) {
|
|
109
|
+
log(` ${symbols.warning} Source file not found; run simplix-boot generate ${entityName} first`);
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const stamped = stampRender(fs.readFileSync(file.src, 'utf8'), origin);
|
|
114
|
+
const outcome = promoteOne(root, file, stamped, { force, adopt });
|
|
115
|
+
counts[outcome.outcome] = (counts[outcome.outcome] ?? 0) + 1;
|
|
116
|
+
|
|
117
|
+
switch (outcome.outcome) {
|
|
118
|
+
case 'written':
|
|
119
|
+
log(` ${symbols.success} Written (absent or untouched)`);
|
|
120
|
+
break;
|
|
121
|
+
case 'merged':
|
|
122
|
+
log(` ${symbols.success} Merged the template change into the edits`);
|
|
123
|
+
break;
|
|
124
|
+
case 'current':
|
|
125
|
+
log(` ${symbols.success} Edited, and already carries the template change`);
|
|
126
|
+
break;
|
|
127
|
+
case 'removed':
|
|
128
|
+
log(` ${symbols.info} Removed: deleted after it was laid down, so it stays deleted`);
|
|
129
|
+
break;
|
|
130
|
+
case 'adopted':
|
|
131
|
+
log(` ${symbols.success} Adopted: stamped, its edits kept against this render as base`);
|
|
132
|
+
break;
|
|
133
|
+
case 'conflict':
|
|
134
|
+
failed = true;
|
|
135
|
+
log(` ${symbols.error} Conflict: the file was kept; the merge with markers is at`);
|
|
136
|
+
log(` ${path.relative(root, outcome.report)}`);
|
|
137
|
+
break;
|
|
138
|
+
default:
|
|
139
|
+
failed = true;
|
|
140
|
+
log(` ${symbols.error} No base: the file is edited and was never promoted with a stored base.`);
|
|
141
|
+
log(` Kept. Run with --adopt to take this render as its base, or --force to overwrite it.`);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// A conflict keeps the render in generated/, so the promote can run again after the fix.
|
|
145
|
+
if (outcome.outcome !== 'conflict' && outcome.outcome !== 'no-base') fs.rmSync(file.src);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
log(`\n${symbols.info} Summary:`);
|
|
149
|
+
for (const [outcome, count] of Object.entries(counts)) log(` ${outcome}: ${count}`);
|
|
150
|
+
|
|
151
|
+
if (failed) {
|
|
152
|
+
throw new CliError(`Promotion of ${entityName} left files that need attention.`);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
log(`\n${symbols.success} Promotion complete!`);
|
|
156
|
+
log(`\nNext steps:`);
|
|
157
|
+
log(` 1. Run: simplix-boot generated`);
|
|
158
|
+
log(` 2. Run: ./gradlew clean build -x test`);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Runs `promote` from the command line. */
|
|
162
|
+
export function run({ root, positionals, options, output }) {
|
|
163
|
+
promote({
|
|
164
|
+
root,
|
|
165
|
+
entityName: positionals.entityName,
|
|
166
|
+
moduleName: options.module,
|
|
167
|
+
force: options.force,
|
|
168
|
+
adopt: options.adopt,
|
|
169
|
+
skipVersionCheck: options['skip-version-check'],
|
|
170
|
+
log: output.log,
|
|
171
|
+
});
|
|
172
|
+
}
|