@simplix-boot/cli 0.0.0-stage → 2.0.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.
Files changed (54) hide show
  1. package/LICENSE +25 -0
  2. package/LICENSE_KO +28 -0
  3. package/README.md +144 -2
  4. package/bin/simplix-boot.js +4 -0
  5. package/package.json +44 -4
  6. package/src/cli.js +103 -0
  7. package/src/commands/config.js +129 -0
  8. package/src/commands/generate.js +291 -0
  9. package/src/commands/generated.js +125 -0
  10. package/src/commands/init.js +41 -0
  11. package/src/commands/promote.js +172 -0
  12. package/src/commands/templates.js +100 -0
  13. package/src/commands/version.js +134 -0
  14. package/src/lib/analysis/fields.js +217 -0
  15. package/src/lib/analysis/imports.js +76 -0
  16. package/src/lib/analysis/structure.js +193 -0
  17. package/src/lib/analysis/types.js +94 -0
  18. package/src/lib/args.js +69 -0
  19. package/src/lib/comment-extractor.js +141 -0
  20. package/src/lib/config/fields.js +321 -0
  21. package/src/lib/config/parents.js +108 -0
  22. package/src/lib/config/paths.js +156 -0
  23. package/src/lib/config/yml.js +61 -0
  24. package/src/lib/console.js +70 -0
  25. package/src/lib/controller-name.js +33 -0
  26. package/src/lib/entity-analyzer.js +49 -0
  27. package/src/lib/entity-type-resolver.js +80 -0
  28. package/src/lib/entity-type.js +35 -0
  29. package/src/lib/errors.js +17 -0
  30. package/src/lib/export-config.js +74 -0
  31. package/src/lib/file-utils.js +77 -0
  32. package/src/lib/generate/file-plan.js +155 -0
  33. package/src/lib/generate/search-fields.js +111 -0
  34. package/src/lib/generate/settings.js +111 -0
  35. package/src/lib/generate/support-classes.js +47 -0
  36. package/src/lib/generate/template-vars.js +183 -0
  37. package/src/lib/git-utils.js +18 -0
  38. package/src/lib/name-field.js +42 -0
  39. package/src/lib/project.js +121 -0
  40. package/src/lib/promote-files.js +132 -0
  41. package/src/lib/rendered-output.js +13 -0
  42. package/src/lib/search-operators.js +87 -0
  43. package/src/lib/templates.js +50 -0
  44. package/src/lib/tracked-render.js +200 -0
  45. package/src/lib/version-check.js +194 -0
  46. package/src/lib/version-utils.js +89 -0
  47. package/templates/controller/rest/EntityRestController.java.template +517 -0
  48. package/templates/controller/web/EntityWebController.java.template +211 -0
  49. package/templates/dto/EntityDTOs.java.template +981 -0
  50. package/templates/repository/EntityRepository.java.template +78 -0
  51. package/templates/service/EntityService.java.template +790 -0
  52. package/templates/service/EntityTreeService.java.template +352 -0
  53. package/templates/test/EntityServiceTest.java.template +937 -0
  54. package/templates/test/EntityTreeServiceTest.java.template +842 -0
@@ -0,0 +1,193 @@
1
+ export function extractParentClass(entityContent) {
2
+ // Find class declaration with extends clause (handle generic types)
3
+ const classMatch = entityContent.match(/class\s+\w+(?:<[^>]+>)?\s+extends\s+(\w+)/);
4
+ if (!classMatch) {
5
+ return null;
6
+ }
7
+
8
+ const parentClassName = classMatch[1];
9
+
10
+ // Find import for parent class
11
+ const importMatch = entityContent.match(new RegExp(`import\\s+([\\w.]+\\.${parentClassName});`));
12
+
13
+ // If no import found, parent class might be in the same package
14
+ // Extract the package from the current file
15
+ let importPath = null;
16
+ if (importMatch) {
17
+ importPath = importMatch[1];
18
+ } else {
19
+ // Try to get the package from the current file
20
+ const packageMatch = entityContent.match(/package\s+([\w.]+);/);
21
+ if (packageMatch) {
22
+ // Assume parent is in the same package if no import is found
23
+ importPath = `${packageMatch[1]}.${parentClassName}`;
24
+ }
25
+ }
26
+
27
+ return {
28
+ name: parentClassName,
29
+ importPath: importPath,
30
+ };
31
+ }
32
+
33
+ /**
34
+ * Extracts implemented interfaces from entity class declaration.
35
+ * <p>
36
+ * Parses the implements clause to identify interfaces like TreeEntity.
37
+ * For generic interfaces like TreeEntity<T, ID>, extracts the type parameters.
38
+ *
39
+ * @param {string} entityContent - The entity file content
40
+ * @returns {Array} Array of interface objects with name and generic types
41
+ */
42
+ export function extractImplementedInterfaces(entityContent) {
43
+ const interfaces = [];
44
+
45
+ // Find class declaration with implements clause
46
+ // Pattern: class EntityName extends ParentClass implements Interface1, Interface2<T, ID>
47
+ // Or: class EntityName implements Interface1, Interface2<T, ID>
48
+ const classDeclarationMatch = entityContent.match(
49
+ /class\s+\w+(?:<[^>]+>)?(?:\s+extends\s+\w+(?:<[^>]+>)?)?\s+implements\s+([^{]+)/
50
+ );
51
+
52
+ if (!classDeclarationMatch) {
53
+ return interfaces;
54
+ }
55
+
56
+ const implementsClause = classDeclarationMatch[1].trim();
57
+
58
+ // Parse TreeEntity<EntityType, IdType> pattern
59
+ const treeEntityMatch = implementsClause.match(/TreeEntity<(\w+),\s*(\w+)>/);
60
+ if (treeEntityMatch) {
61
+ interfaces.push({
62
+ name: 'TreeEntity',
63
+ entityType: treeEntityMatch[1],
64
+ idType: treeEntityMatch[2],
65
+ });
66
+ }
67
+
68
+ // Parse other simple interfaces (Serializable, etc.) using matchAll
69
+ const simpleInterfaceMatches = implementsClause.matchAll(/\b(\w+)\b(?![<>])/g);
70
+ for (const match of simpleInterfaceMatches) {
71
+ const interfaceName = match[1];
72
+ // Skip if already added (like TreeEntity) or if it's a generic type parameter
73
+ if (
74
+ !interfaces.some(i => i.name === interfaceName) &&
75
+ interfaceName !== 'TreeEntity' &&
76
+ !['String', 'Long', 'Integer', 'UUID'].includes(interfaceName)
77
+ ) {
78
+ interfaces.push({
79
+ name: interfaceName,
80
+ });
81
+ }
82
+ }
83
+
84
+ return interfaces;
85
+ }
86
+
87
+ export function extractIdInfo(entityContent) {
88
+ let idType = 'String';
89
+ let embeddedIdField = null;
90
+ let idField = null;
91
+
92
+ const contentLines = entityContent.split('\n');
93
+ let isIdField = false;
94
+ let isEmbeddedIdField = false;
95
+ let inMultiLineAnnotation = false;
96
+
97
+ // ID field analysis
98
+ for (let i = 0; i < contentLines.length; i++) {
99
+ const trimmedLine = contentLines[i].trim();
100
+
101
+ if (trimmedLine === '@Id') {
102
+ isIdField = true;
103
+ continue;
104
+ }
105
+
106
+ if (trimmedLine === '@EmbeddedId') {
107
+ isEmbeddedIdField = true;
108
+ continue;
109
+ }
110
+
111
+ // Handle multi-line annotations (like @Column)
112
+ if ((isIdField || isEmbeddedIdField) && trimmedLine.startsWith('@')) {
113
+ // Check if this is a multi-line annotation
114
+ if (trimmedLine.includes('(') && !trimmedLine.includes(')')) {
115
+ inMultiLineAnnotation = true;
116
+ }
117
+
118
+ continue;
119
+ }
120
+
121
+ // Continue through multi-line annotation
122
+ if (inMultiLineAnnotation) {
123
+ if (trimmedLine.includes(')')) {
124
+ inMultiLineAnnotation = false;
125
+ }
126
+
127
+ continue;
128
+ }
129
+
130
+ // Check for the private field declaration
131
+ if ((isIdField || isEmbeddedIdField) && trimmedLine.startsWith('private ')) {
132
+ const fieldMatch = trimmedLine.match(/private\s+([^\s<>]+)(?:<[^>]+>)?\s+(\w+)/);
133
+ if (fieldMatch) {
134
+ if (isEmbeddedIdField) {
135
+ embeddedIdField = fieldMatch[2]; // field name
136
+ idType = fieldMatch[1]; // field type
137
+ idField = embeddedIdField;
138
+ } else {
139
+ idType = fieldMatch[1];
140
+ idField = fieldMatch[2];
141
+ }
142
+
143
+ break;
144
+ }
145
+ }
146
+
147
+ // Reset flags if we hit something that's not an annotation or private field
148
+ if (
149
+ (isIdField || isEmbeddedIdField) &&
150
+ !trimmedLine.startsWith('@') &&
151
+ !trimmedLine.startsWith('private') &&
152
+ !inMultiLineAnnotation &&
153
+ trimmedLine.length > 0
154
+ ) {
155
+ // Skip empty lines and comments
156
+ if (!trimmedLine.startsWith('//') && !trimmedLine.startsWith('/*') && !trimmedLine.startsWith('*')) {
157
+ isIdField = false;
158
+ isEmbeddedIdField = false;
159
+ }
160
+ }
161
+ }
162
+
163
+ return { idType, embeddedIdField, idField };
164
+ }
165
+
166
+ export function analyzeEmbeddedId(entityContent, embeddedIdField, idType) {
167
+ if (!embeddedIdField) {
168
+ return null;
169
+ }
170
+
171
+ // Find the embedded ID class definition
172
+ const classPattern = new RegExp(`public\\s+static\\s+class\\s+${idType}[^{]*\\{([^}]+(?:\\{[^}]*\\}[^}]*)*)\\}`, 's');
173
+ const classMatch = entityContent.match(classPattern);
174
+
175
+ if (!classMatch) {
176
+ return null;
177
+ }
178
+
179
+ const classBody = classMatch[1];
180
+ const fields = [];
181
+ const fieldPattern = /private\s+([^\s<>]+)(?:<[^>]+>)?\s+(\w+);/g;
182
+ let match;
183
+
184
+ while ((match = fieldPattern.exec(classBody)) !== null) {
185
+ fields.push({
186
+ type: match[1],
187
+ name: match[2],
188
+ parentField: embeddedIdField,
189
+ });
190
+ }
191
+
192
+ return fields;
193
+ }
@@ -0,0 +1,94 @@
1
+ /**
2
+ * Type classification configuration
3
+ */
4
+ export const TYPE_CLASSIFICATIONS = {
5
+ basic: new Set([
6
+ 'String',
7
+ 'Integer',
8
+ 'Long',
9
+ 'Double',
10
+ 'Float',
11
+ 'BigDecimal',
12
+ 'BigInteger',
13
+ 'Boolean',
14
+ 'boolean',
15
+ 'int',
16
+ 'long',
17
+ 'double',
18
+ 'float',
19
+ 'LocalDate',
20
+ 'LocalDateTime',
21
+ 'ZonedDateTime',
22
+ 'OffsetDateTime',
23
+ 'Instant',
24
+ 'Date',
25
+ 'Time',
26
+ 'Timestamp',
27
+ 'UUID',
28
+ 'byte[]',
29
+ 'Byte[]',
30
+ ]),
31
+ temporal: new Set([
32
+ 'LocalDate',
33
+ 'LocalDateTime',
34
+ 'ZonedDateTime',
35
+ 'OffsetDateTime',
36
+ 'Instant',
37
+ 'Date',
38
+ 'Time',
39
+ 'Timestamp',
40
+ ]),
41
+ numeric: new Set([
42
+ 'Integer',
43
+ 'Long',
44
+ 'Double',
45
+ 'Float',
46
+ 'BigDecimal',
47
+ 'BigInteger',
48
+ 'int',
49
+ 'long',
50
+ 'double',
51
+ 'float',
52
+ ]),
53
+ };
54
+
55
+ /**
56
+ * The kind of a field whose type is neither basic nor already known: an enum, a related entity, or
57
+ * a plain value when the type's own source declares no `@Entity` (a record, an embeddable or a
58
+ * converted value kept in the row, such as a JSON column). A type the resolver cannot answer for is
59
+ * read as a related entity.
60
+ */
61
+ export function classifyNonBasicType(type, importPath, { entityContent, imports, isEntityType }) {
62
+ if (isEnumType(type, entityContent, imports)) return { typeKind: 'enum', isRelation: true };
63
+ if (importPath && isEntityType?.(importPath) === false) return { typeKind: 'value' };
64
+ return { typeKind: 'entity', isRelation: true };
65
+ }
66
+
67
+ export function classifyType(type, imports) {
68
+ // Check if it's a basic type
69
+ if (TYPE_CLASSIFICATIONS.basic.has(type)) {
70
+ return 'basic';
71
+ }
72
+
73
+ // Check if it's an enum
74
+ const enumImport = imports.find(imp => imp.endsWith(`.${type}`) && imp.includes('.enums.'));
75
+ if (enumImport) {
76
+ return 'enum';
77
+ }
78
+
79
+ // Default to entity for non-basic types
80
+ return null;
81
+ }
82
+
83
+ function isEnumType(type, entityContent, imports) {
84
+ // Check if there's an enum import
85
+ const hasEnumImport = imports.some(
86
+ imp => imp.endsWith(`.${type}`) && (imp.includes('.enums.') || imp.includes('.enum.'))
87
+ );
88
+
89
+ if (hasEnumImport) return true;
90
+
91
+ // Check if enum is defined in the same file
92
+ const enumPattern = new RegExp(`enum\\s+${type}\\s*\\{`);
93
+ return enumPattern.test(entityContent);
94
+ }
@@ -0,0 +1,69 @@
1
+ import { CliError } from './errors.js';
2
+
3
+ /**
4
+ * Reads a command's arguments against its declaration.
5
+ *
6
+ * <p>A boolean option is `--name` or `--no-name`; a string option is `--name value` or
7
+ * `--name=value`. An option the command does not declare, a string option with no value and a
8
+ * positional beyond the declared ones are refused, so a mistyped flag never runs as its default.
9
+ *
10
+ * @param {string[]} argv the arguments after the command name
11
+ * @param {object} spec
12
+ * @param {string[]} [spec.positionals] the names of the positional arguments, in order
13
+ * @param {Record<string, { type: 'boolean' | 'string', default?: boolean | string }>} [spec.options]
14
+ * @returns {{ positionals: Record<string, string | undefined>, options: Record<string, boolean | string | undefined> }}
15
+ */
16
+ export function parseArguments(argv, { positionals = [], options = {} } = {}) {
17
+ const values = {};
18
+ const given = [];
19
+
20
+ for (const [name, declared] of Object.entries(options)) values[name] = declared.default;
21
+
22
+ for (let index = 0; index < argv.length; index++) {
23
+ const argument = argv[index];
24
+
25
+ if (!argument.startsWith('--') || argument === '--') {
26
+ given.push(argument);
27
+ continue;
28
+ }
29
+
30
+ const equals = argument.indexOf('=');
31
+ const name = argument.slice(2, equals === -1 ? undefined : equals);
32
+ const inline = equals === -1 ? undefined : argument.slice(equals + 1);
33
+
34
+ if (options[name]?.type === 'boolean') {
35
+ if (inline !== undefined) throw new CliError(`Option --${name} takes no value`);
36
+ values[name] = true;
37
+ continue;
38
+ }
39
+
40
+ if (name.startsWith('no-') && options[name.slice(3)]?.type === 'boolean') {
41
+ if (inline !== undefined) throw new CliError(`Option --${name} takes no value`);
42
+ values[name.slice(3)] = false;
43
+ continue;
44
+ }
45
+
46
+ if (options[name]?.type === 'string') {
47
+ const value = inline ?? argv[++index];
48
+ if (value === undefined || (inline === undefined && value.startsWith('--'))) {
49
+ throw new CliError(`Option --${name} needs a value`);
50
+ }
51
+
52
+ values[name] = value;
53
+ continue;
54
+ }
55
+
56
+ throw new CliError(`Unknown option: --${name}`);
57
+ }
58
+
59
+ if (given.length > positionals.length) {
60
+ throw new CliError(`Unexpected argument: ${given[positionals.length]}`);
61
+ }
62
+
63
+ const named = {};
64
+ positionals.forEach((name, index) => {
65
+ named[name] = given[index];
66
+ });
67
+
68
+ return { positionals: named, options: values };
69
+ }
@@ -0,0 +1,141 @@
1
+ export function extractComment(entityContent, fieldName) {
2
+ // First try to find JavaDoc comment
3
+ // Use \\b at the end to ensure exact field name match, the same way the @Comment branch below
4
+ // does: without it 'name' matches the declaration of 'nameSearch' and takes that field's
5
+ // JavaDoc, so a field whose name is another's prefix is documented as the wrong thing.
6
+ const javadocRegex = new RegExp(
7
+ `/\\*\\*([^*]|\\*(?!/))*\\*/\\s*(?:@[\\w\\.]+(?:\\([^)]*\\))?\\s*)*(?:private|public|protected)\\s+(?:[\\w<>\\[\\]]+)\\s+${fieldName}\\b`,
8
+ 's'
9
+ );
10
+ const javadocMatch = entityContent.match(javadocRegex);
11
+
12
+ if (javadocMatch) {
13
+ // Extract the comment content from JavaDoc
14
+ const javadocContent = javadocMatch[0];
15
+ const commentMatch = javadocContent.match(/\/\*\*([\s\S]*?)\*\//);
16
+ if (commentMatch) {
17
+ // Clean up the JavaDoc comment
18
+ let comment = commentMatch[1]
19
+ .split('\n')
20
+ .map(line => line.replace(/^\s*\*\s?/, '')) // Remove leading * and spaces
21
+ .join(' ')
22
+ .trim()
23
+ .replace(/<[^>]+>/g, '') // Remove HTML tags
24
+ .replace(/\s+/g, ' '); // Normalize whitespace
25
+
26
+ // Remove inline JavaDoc tags like {@code ...}, {@link ...}, {@literal ...}
27
+ // Preserve the content inside the tags
28
+ comment = comment.replace(/\{@\w+\s+([^}]*)}/g, '$1');
29
+
30
+ // Remove @param, @return, etc. (only standalone @ tags, not inside braces)
31
+ comment = comment.replace(/\s*@(?:param|return|throws|see|since|author|version|deprecated)\b[\s\S]*/i, '').trim();
32
+
33
+ if (comment) {
34
+ // Escape double quotes for use in Java annotations
35
+ comment = comment.replace(/"/g, '\\"');
36
+ return comment;
37
+ }
38
+ }
39
+ }
40
+
41
+ // Then try @Comment annotation
42
+ // Use negative lookahead (?!@Comment) to ensure we match the closest @Comment to the field
43
+ // This prevents matching the table-level @Comment at the top of the file
44
+ // Use \\b at the end to ensure exact field name match (avoid 'content' matching 'contentId')
45
+ // Never cross a ';': that ends an earlier field, so a @Comment before it belongs to that field and
46
+ // a field carrying none of its own (a @Formula, say) would otherwise take its neighbour's.
47
+ const commentRegex = new RegExp(
48
+ `@Comment\\s*\\("([^"]+)"\\)(?:(?!@Comment|;)[\\s\\S])*?(?:private|public|protected)\\s+[\\w<>\\[\\],]+\\s+${fieldName}\\b`
49
+ );
50
+ const match = entityContent.match(commentRegex);
51
+
52
+ if (match) return match[1];
53
+
54
+ // Default to camelCase to words
55
+ return camelToWords(fieldName);
56
+ }
57
+
58
+ export function extractTableComment(entityContent) {
59
+ // First try to find @Comment annotation at class level
60
+ const classCommentRegex =
61
+ /@Comment\s*\(\s*"([^"]+)"\s*\)\s*(?:@[^\n]*\n)*\s*(?:public\s+)?(?:class|interface|enum)\s+\w+/;
62
+ const commentMatch = entityContent.match(classCommentRegex);
63
+
64
+ if (commentMatch) {
65
+ return commentMatch[1];
66
+ }
67
+
68
+ // A class-level annotation that spans several lines (a multi-line @Table or
69
+ // @EntityEventConfig) defeats the line-based pattern above. Everything before the
70
+ // type declaration is the package, imports, the class JavaDoc and the class
71
+ // annotations, so the last @Comment there, outside comments, is the class comment.
72
+ const headerComment = extractClassHeaderComment(entityContent);
73
+
74
+ if (headerComment) {
75
+ return headerComment;
76
+ }
77
+
78
+ // Then try to find JavaDoc comment for the class
79
+ const classJavadocRegex = /\/\*\*([\s\S]*?)\*\/\s*(?:@[^\n]*\n)*\s*(?:public\s+)?(?:class|interface|enum)\s+\w+/;
80
+ const javadocMatch = entityContent.match(classJavadocRegex);
81
+
82
+ if (javadocMatch) {
83
+ // Extract the first line of the JavaDoc comment
84
+ const javadocContent = javadocMatch[1]
85
+ .split('\n')
86
+ .map(line => line.replace(/^\s*\*\s?/, ''))
87
+ .filter(line => line.trim())
88
+ .join(' ')
89
+ .trim();
90
+
91
+ // Get the first sentence or line
92
+ const firstSentence = javadocContent.split(/[.!?]/)[0].trim();
93
+ if (firstSentence) {
94
+ // Escape double quotes for use in Java annotations
95
+ return firstSentence.replace(/"/g, '\\"');
96
+ }
97
+ }
98
+
99
+ // Then try Hibernate @Table comment annotation
100
+ const tableCommentRegex =
101
+ /@org\.hibernate\.annotations\.Table\s*\(\s*appliesTo\s*=\s*"[^"]+"\s*,\s*comment\s*=\s*"([^"]+)"\s*\)/;
102
+ const match = entityContent.match(tableCommentRegex);
103
+
104
+ if (match) return match[1];
105
+
106
+ return generateDefaultTableComment(entityContent);
107
+ }
108
+
109
+ function extractClassHeaderComment(entityContent) {
110
+ const withoutComments = entityContent.replace(/\/\*[\s\S]*?\*\//g, '').replace(/\/\/[^\n]*/g, '');
111
+ const declaration = withoutComments.match(
112
+ /(?:^|\s)(?:public\s+|abstract\s+|final\s+)*(?:class|interface|enum)\s+\w+/
113
+ );
114
+
115
+ if (!declaration) return null;
116
+
117
+ const header = withoutComments.slice(0, declaration.index);
118
+ const matches = [...header.matchAll(/@Comment\s*\(\s*"((?:[^"\\]|\\.)+)"\s*\)/g)];
119
+
120
+ return matches.length > 0 ? matches[matches.length - 1][1] : null;
121
+ }
122
+
123
+ function camelToWords(str) {
124
+ return str
125
+ .replace(/([A-Z])/g, ' $1')
126
+ .replace(/^./, str => str.toUpperCase())
127
+ .trim();
128
+ }
129
+
130
+ function generateDefaultTableComment(entityContent) {
131
+ const tableNameRegex = /@Table\s*\(\s*name\s*=\s*"([^"]+)"\s*\)/;
132
+ const tableMatch = entityContent.match(tableNameRegex);
133
+ if (tableMatch) {
134
+ return tableMatch[1]
135
+ .split('_')
136
+ .map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
137
+ .join(' ');
138
+ }
139
+
140
+ return 'Entity Table';
141
+ }