definition-generator-framework 1.12.12 → 1.13.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.
@@ -1,6 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SingleDefinitionComponent = exports.DefinitionComponent = exports.DefinitionComponentDecorator = void 0;
4
+ function toPascalCase(input) {
5
+ return input
6
+ .replace(/[-_]/g, ' ')
7
+ .replace(/([a-z])([A-Z])/g, '$1 $2')
8
+ .replace(/\s+/g, ' ')
9
+ .trim()
10
+ .toLowerCase()
11
+ .split(' ')
12
+ .map(word => word.charAt(0).toUpperCase() + word.slice(1))
13
+ .join('');
14
+ }
4
15
  function DefinitionComponentDecorator(meta) {
5
16
  return function (ComponentClass) {
6
17
  if (meta.singleton) {
@@ -18,6 +29,7 @@ function DefinitionComponentDecorator(meta) {
18
29
  }
19
30
  ComponentClass.$meta = {
20
31
  componentName: meta.componentName,
32
+ pascalCaseComponentName: toPascalCase(meta.componentName),
21
33
  keyName: meta.keyName,
22
34
  singleton: meta.singleton,
23
35
  validationSchema: meta.validationSchema
@@ -21,7 +21,7 @@ class Context {
21
21
  this.output.push({
22
22
  path: request.path,
23
23
  content: await file_content_generator_1.FileContentGenerator.generate({
24
- preText: request.preText,
24
+ preText: helpers_lib_1.Comparator.isArray(request.preText) ? request.preText.join('\n') : request.preText,
25
25
  variableName: request.variableName,
26
26
  variableType: request.variableType,
27
27
  variable: request.variable,
@@ -0,0 +1,13 @@
1
+ export declare class IndexFileGenerator {
2
+ private static premadeInterfaces;
3
+ private static idTypes;
4
+ static addPremadeInterface(value: string): void;
5
+ static addIdType(constantName: string, keys: string[]): void;
6
+ static generateFile(pretext: string, outputIndexFile: string): Promise<void>;
7
+ private static createIndexFile;
8
+ private static generateFileDevMode;
9
+ private static generateExportsContent;
10
+ private static generateInterfacesContent;
11
+ private static getInterfacesFromFile;
12
+ static generateTypes(): string;
13
+ }
@@ -0,0 +1,139 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.IndexFileGenerator = void 0;
27
+ const fs = __importStar(require("fs"));
28
+ const glob = __importStar(require("glob"));
29
+ const helpers_lib_1 = require("helpers-lib");
30
+ const context_1 = require("../context");
31
+ const file_content_generator_1 = require("../file-content-generator/file-content-generator");
32
+ const START_COMMAND = '// ---- INDEX OUTPUT ----';
33
+ const END_COMMAND = '// ---- END ----';
34
+ class IndexFileGenerator {
35
+ static { this.premadeInterfaces = []; }
36
+ static { this.idTypes = []; }
37
+ static addPremadeInterface(value) {
38
+ this.premadeInterfaces.push(value);
39
+ }
40
+ static addIdType(constantName, keys) {
41
+ let value = `export type ${constantName}ID = ${keys.map(key => `'${key}'`).join(' | ')};`;
42
+ this.idTypes.push(value);
43
+ }
44
+ static async generateFile(pretext, outputIndexFile) {
45
+ if (context_1.Context.config.compilerModulesPath) {
46
+ let content = await this.generateFileDevMode(pretext);
47
+ if (content) {
48
+ await this.createIndexFile(content);
49
+ }
50
+ }
51
+ else {
52
+ await this.createIndexFile(outputIndexFile);
53
+ }
54
+ }
55
+ static async createIndexFile(outputIndexFile) {
56
+ let outputContent = outputIndexFile;
57
+ let typesContent = this.generateTypes();
58
+ if (typesContent) {
59
+ outputContent += '\n\n' + typesContent;
60
+ }
61
+ context_1.Context.output.push({
62
+ path: '/index.ts',
63
+ content: await file_content_generator_1.FileContentGenerator.format(outputContent)
64
+ });
65
+ }
66
+ static async generateFileDevMode(pretext) {
67
+ let fileUrls = glob.sync(`${context_1.Context.config.compilerModulesPath}/**/*.ts`).map(filePath => filePath.replace(/\\/g, '/'));
68
+ if (fileUrls.length === 0) {
69
+ helpers_lib_1.ConsoleHelper.log(`No typescript file found to generate index.ts file.`, 'red');
70
+ }
71
+ let exportsContent = this.generateExportsContent();
72
+ let interfacesContent = await this.generateInterfacesContent(fileUrls);
73
+ if (interfacesContent !== undefined) {
74
+ let output = `${pretext}\n${exportsContent}\n\n${interfacesContent}`;
75
+ output = await file_content_generator_1.FileContentGenerator.format(output);
76
+ fs.writeFileSync(`${context_1.Context.compilerExecutionPath}/output-index-file.ts`, `// AUTO GENERATED - CHANGES WILL BE REPLACED\nexport const OutputIndexFile = \`${output}\`;\n`);
77
+ helpers_lib_1.ConsoleHelper.log(`Index content 'output-index-file.ts' is generated.`, 'green');
78
+ return output;
79
+ }
80
+ }
81
+ static generateExportsContent() {
82
+ return context_1.Context.exports
83
+ .map(exportItem => `export { ${exportItem.variableName} } from '.${exportItem.path.substring(0, exportItem.path.lastIndexOf('.'))}';`)
84
+ .join('\n');
85
+ }
86
+ static async generateInterfacesContent(fileUrls) {
87
+ let errorCount = 0;
88
+ let outputs = [];
89
+ fileUrls.forEach(fileUrl => {
90
+ let fileName = fileUrl.substring(fileUrl.lastIndexOf('/') + 1, fileUrl.lastIndexOf('.'));
91
+ let fileContent = fs.readFileSync(fileUrl, 'utf-8');
92
+ try {
93
+ let output = this.getInterfacesFromFile(fileName, fileContent);
94
+ if (output) {
95
+ outputs.push(output);
96
+ }
97
+ }
98
+ catch (error) {
99
+ errorCount++;
100
+ helpers_lib_1.ConsoleHelper.log(`Error parsing file: ${fileUrl}, error: '${error.message}'`, 'red');
101
+ }
102
+ });
103
+ let output = `//-------------------------- DEFINITIONS --------------------------\n`;
104
+ if (this.premadeInterfaces.length > 0) {
105
+ output += '// ---- premade ----';
106
+ output += this.premadeInterfaces.join('\n');
107
+ output += '\n\n';
108
+ }
109
+ output += outputs.join('\n');
110
+ return errorCount === 0 ? output : undefined;
111
+ }
112
+ static getInterfacesFromFile(fileName, fileContent) {
113
+ let index = fileContent.indexOf(START_COMMAND);
114
+ let definitionsFound = index !== -1;
115
+ let contents = [];
116
+ while (index !== -1) {
117
+ let endIndex = fileContent.indexOf(END_COMMAND, index);
118
+ if (endIndex === -1) {
119
+ throw new Error(`No end command found.`);
120
+ }
121
+ contents.push(fileContent.substring(fileContent.indexOf('\n', index + START_COMMAND.length) + 1, endIndex));
122
+ index = fileContent.indexOf(START_COMMAND, endIndex);
123
+ }
124
+ if (definitionsFound) {
125
+ return `// ---- ${fileName} ----\n${contents.join('\n')}`;
126
+ }
127
+ }
128
+ static generateTypes() {
129
+ let output = '';
130
+ if (this.idTypes.length > 0) {
131
+ output += `//-------------------------- ID TYPES --------------------------\n`;
132
+ output += this.idTypes.join('\n');
133
+ output += '\n';
134
+ }
135
+ return output;
136
+ }
137
+ }
138
+ exports.IndexFileGenerator = IndexFileGenerator;
139
+ //# sourceMappingURL=index-file-generator.js.map
@@ -0,0 +1,6 @@
1
+ export declare class TypesFileGenerator {
2
+ private static idTypes;
3
+ static addIdType(constantName: string, keys: string[]): void;
4
+ static generateFile(pretext: string): Promise<void>;
5
+ private static generateContent;
6
+ }
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TypesFileGenerator = void 0;
4
+ const glob_1 = require("glob");
5
+ const helpers_lib_1 = require("helpers-lib");
6
+ const context_1 = require("../context");
7
+ const file_content_generator_1 = require("../file-content-generator/file-content-generator");
8
+ class TypesFileGenerator {
9
+ static { this.idTypes = []; }
10
+ static addIdType(constantName, keys) {
11
+ let value = `export type ${constantName}ID = ${keys.map(key => `'${key}'`).join(' | ')};`;
12
+ this.idTypes.push(value);
13
+ }
14
+ static async generateFile(pretext) {
15
+ let fileUrls = glob_1.glob.sync(`${context_1.Context.config.compilerModulesPath}/**/*.ts`).map(filePath => filePath.replace(/\\/g, '/'));
16
+ if (fileUrls.length === 0) {
17
+ helpers_lib_1.ConsoleHelper.log(`No typescript file found to generate index.ts file.`, 'red');
18
+ }
19
+ let output = this.generateContent();
20
+ context_1.Context.output.push({
21
+ path: '/types.ts',
22
+ content: await file_content_generator_1.FileContentGenerator.format(output)
23
+ });
24
+ }
25
+ static generateContent() {
26
+ let output = '';
27
+ if (this.idTypes.length > 0) {
28
+ output += `//-------------------------- ID TYPES --------------------------\n`;
29
+ output += this.idTypes.join('\n');
30
+ }
31
+ return output;
32
+ }
33
+ }
34
+ exports.TypesFileGenerator = TypesFileGenerator;
35
+ //# sourceMappingURL=types-file-generator.js.map
@@ -9,7 +9,7 @@ export interface AddFileOptions {
9
9
  path: string;
10
10
  variableName: string;
11
11
  variable: any;
12
- preText?: string;
12
+ preText?: string | string[];
13
13
  variableType?: string;
14
14
  replaceMap?: {
15
15
  target: string;
@@ -34,7 +34,7 @@ const _3_config_1 = require("../pipeline/3-config");
34
34
  const raw_data_parser_1 = require("../pipeline/raw-data-parser");
35
35
  const context_1 = require("./context");
36
36
  const definition_store_1 = require("./definition-store");
37
- const index_file_generator_1 = require("./index-file-generator/index-file-generator");
37
+ const index_file_generator_1 = require("./file-generators/index-file-generator");
38
38
  const KeyValueRegex = /^[a-z]+([a-zA-Z0-9]+)*$/;
39
39
  const HeavySeperator = '======================================================';
40
40
  const LightSeperator = '------------------------------------------------------';
@@ -219,13 +219,17 @@ class Kernel {
219
219
  for (let preparedItem of preparedItems) {
220
220
  if (preparedItem.definitionToProcess) {
221
221
  let module = new preparedItem.ModuleClass();
222
+ let keyName = preparedItem.ModuleClass.$meta.keyName;
223
+ if (keyName) {
224
+ index_file_generator_1.IndexFileGenerator.addIdType(preparedItem.ModuleClass.$meta.pascalCaseComponentName, preparedItem.definitionToProcess.map((definition) => definition[keyName]));
225
+ }
222
226
  await module.process(preparedItem.definitionToProcess, preparedItem.output);
223
227
  }
224
228
  }
225
229
  context_1.Context.flushErrorLogs(); // Still there might be some errors that customly added
226
230
  custom_validator_helper_1.CustomValidatorHelper.logDuplicates();
227
231
  if (context_1.Context.successful) {
228
- index_file_generator_1.IndexFileGenerator.generateIndexFile(indexFilePretext, outputIndexFile);
232
+ await index_file_generator_1.IndexFileGenerator.generateFile(indexFilePretext, outputIndexFile);
229
233
  }
230
234
  return this.getOutputFiles();
231
235
  }
package/dist/index.d.ts CHANGED
@@ -7,11 +7,11 @@ export { JoiCustomValidators } from './helpers/validator/joi-custom-validators';
7
7
  export { Validator } from './helpers/validator/validator';
8
8
  export * from './pre-made-components/_validators/shape-validators';
9
9
  export * from './pre-made-components/_validators/sprite-validators';
10
- export { SpriteComponent } from './pre-made-components/assets/1-sprite';
11
- export { SpriteGroupComponent } from './pre-made-components/assets/2-sprite-group';
12
- export { FontComponent } from './pre-made-components/assets/3-font';
13
- export { ScriptComponent } from './pre-made-components/events/1-script';
10
+ export { SpriteComponent, SpriteID } from './pre-made-components/assets/1-sprite';
11
+ export { SpriteGroupComponent, SpriteGroupID } from './pre-made-components/assets/2-sprite-group';
12
+ export { FontComponent, FontID } from './pre-made-components/assets/3-font';
13
+ export { ScriptComponent, ScriptID } from './pre-made-components/events/1-script';
14
14
  export { ScriptTestComponent } from './pre-made-components/events/2-script-test';
15
- export { EventComponent } from './pre-made-components/events/3-event';
15
+ export { EventComponent, EventID } from './pre-made-components/events/3-event';
16
16
  export { EventTestComponent } from './pre-made-components/events/4-event-test';
17
17
  export { ScriptTesting } from './pre-made-components/events/scripting-setup';
@@ -2,6 +2,7 @@ import { Vec2 } from 'helpers-lib';
2
2
  import { DefinitionComponent } from '../../decorators/definition-component';
3
3
  import { Output } from '../../framework/interfaces';
4
4
  import { ValidatedSpriteUrl } from '../../helpers/validator/custom-validators/sprite.custom-validator';
5
+ export type SpriteID = string;
5
6
  export interface RawSpriteDefinition {
6
7
  id: string;
7
8
  url: ValidatedSpriteUrl;
@@ -1,5 +1,6 @@
1
1
  import { DefinitionComponent } from '../../decorators/definition-component';
2
2
  import { Output } from '../../framework/interfaces';
3
+ export type SpriteGroupID = string;
3
4
  interface SpriteGroupDefinition {
4
5
  id: string;
5
6
  group: string[];
@@ -1,12 +1,13 @@
1
1
  import { DefinitionComponent } from '../../decorators/definition-component';
2
2
  import { Output } from '../../framework/interfaces';
3
+ export type FontID = string;
3
4
  interface RawFontDefinition {
4
5
  id: string;
5
6
  fontFamily: string;
6
7
  url: string;
7
8
  }
8
9
  export interface FontDefinition {
9
- readonly id: string;
10
+ readonly id: FontID;
10
11
  readonly fontFamily: string;
11
12
  }
12
13
  export declare class FontComponent extends DefinitionComponent<RawFontDefinition> {
@@ -16,7 +16,7 @@ exports.FontComponent = void 0;
16
16
  const helpers_lib_1 = require("helpers-lib");
17
17
  const joi_1 = __importDefault(require("joi"));
18
18
  const definition_component_1 = require("../../decorators/definition-component");
19
- const index_file_generator_1 = require("../../framework/index-file-generator/index-file-generator");
19
+ const index_file_generator_1 = require("../../framework/file-generators/index-file-generator");
20
20
  const INDEX_ENTRY = `
21
21
  export interface FontDefinition {
22
22
  readonly id: string;
@@ -1,6 +1,7 @@
1
1
  import { ScriptDefinition } from 'script-engine-lib';
2
2
  import { DefinitionComponent } from '../../decorators/definition-component';
3
3
  import { Output } from '../../framework/interfaces';
4
+ export type ScriptID = string;
4
5
  export declare const ScriptNames: Set<string>;
5
6
  export declare class ScriptComponent extends DefinitionComponent<ScriptDefinition> {
6
7
  process(definitions: ScriptDefinition[], output: Output): Promise<void>;
@@ -1,6 +1,7 @@
1
1
  import { ScriptDefinition } from 'script-engine-lib';
2
2
  import { DefinitionComponent } from '../../decorators/definition-component';
3
3
  import { Output } from '../../framework/interfaces';
4
+ export type EventID = string;
4
5
  export declare class EventComponent extends DefinitionComponent<ScriptDefinition> {
5
6
  process(definitions: ScriptDefinition[], output: Output): Promise<void>;
6
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "definition-generator-framework",
3
- "version": "1.12.12",
3
+ "version": "1.13.0",
4
4
  "description": "Definition Generator Framework",
5
5
  "main": "dist/index.js",
6
6
  "files": [