autoforce 0.1.17 → 0.1.19

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 (80) hide show
  1. package/.autoforce.json +8 -4
  2. package/CHANGELOG.md +20 -1
  3. package/lib/auto.js +50 -64
  4. package/lib/helpers/class.d.ts +1 -0
  5. package/lib/helpers/class.js +74 -80
  6. package/lib/helpers/connect.js +199 -213
  7. package/lib/helpers/context.d.ts +6 -3
  8. package/lib/helpers/context.js +279 -225
  9. package/lib/helpers/github-graphql.js +90 -113
  10. package/lib/helpers/github-project-graphql.js +100 -132
  11. package/lib/helpers/gitlab-graphql.js +68 -104
  12. package/lib/helpers/lwc.js +42 -48
  13. package/lib/helpers/metadata.js +1 -2
  14. package/lib/helpers/object.js +47 -55
  15. package/lib/helpers/openai.js +10 -22
  16. package/lib/helpers/taskFunctions.js +330 -386
  17. package/lib/helpers/tasks.d.ts +3 -6
  18. package/lib/helpers/tasks.js +132 -147
  19. package/lib/helpers/template.d.ts +9 -5
  20. package/lib/helpers/template.js +31 -18
  21. package/lib/helpers/util.d.ts +5 -6
  22. package/lib/helpers/util.js +131 -187
  23. package/models/dev/models.json +19 -0
  24. package/models/doc/models.json +9 -0
  25. package/models/git/models.json +9 -0
  26. package/models/project/models.json +9 -0
  27. package/package.json +16 -12
  28. package/commands/modelC/new/issue.json +0 -24
  29. package/commands/models.json +0 -18
  30. package/templates/modelB/openIssues.bash +0 -4
  31. package/templates/modelB/viewIssue.bash +0 -8
  32. package/templates/models.json +0 -12
  33. /package/{commands/modelA → models/dev/npm}/subtasks/checkout-branch.json +0 -0
  34. /package/{commands/modelA → models/dev/npm}/subtasks/create-pull.json +0 -0
  35. /package/{commands/modelC → models/dev/npm}/subtasks/pack.json +0 -0
  36. /package/{commands/modelA → models/dev/npm}/subtasks/publish-branch.json +0 -0
  37. /package/{commands/modelA → models/dev/npm}/tasks/cancel.json +0 -0
  38. /package/{commands/modelA → models/dev/npm}/tasks/finish.json +0 -0
  39. /package/{commands/modelC → models/dev/npm}/tasks/list.json +0 -0
  40. /package/{commands/modelC → models/dev/npm}/tasks/publish.json +0 -0
  41. /package/{commands/modelC → models/dev/npm}/tasks/start.json +0 -0
  42. /package/{commands/modelA → models/dev/npm}/tasks/stop.json +0 -0
  43. /package/{commands/modelA → models/dev/npm}/tasks/switch.json +0 -0
  44. /package/{commands/modelA → models/dev/npm}/tasks/view.json +0 -0
  45. /package/{commands/modelA → models/dev/scratchs}/subtasks/create-scratch.json +0 -0
  46. /package/{commands/modelA → models/dev/scratchs}/subtasks/deploy-code.json +0 -0
  47. /package/{commands/modelA → models/dev/scratchs}/subtasks/drop-scratch.json +0 -0
  48. /package/{commands/modelA → models/dev/scratchs}/subtasks/package-code.json +0 -0
  49. /package/{commands/modelA → models/dev/scratchs}/subtasks/validate-code.json +0 -0
  50. /package/{commands/modelA → models/dev/scratchs}/subtasks/validate-scratch.json +0 -0
  51. /package/{commands/modelC → models/dev/scratchs}/tasks/cancel.json +0 -0
  52. /package/{commands/modelA → models/dev/scratchs}/tasks/deploy.json +0 -0
  53. /package/{commands/modelC → models/dev/scratchs}/tasks/finish.json +0 -0
  54. /package/{commands/modelA → models/dev/scratchs}/tasks/rollback.json +0 -0
  55. /package/{commands/modelA → models/dev/scratchs}/tasks/start.json +0 -0
  56. /package/{commands/modelC → models/dev/scratchs}/tasks/stop.json +0 -0
  57. /package/{commands/modelC → models/dev/scratchs}/tasks/switch.json +0 -0
  58. /package/{commands/modelA → models/doc/processes}/new/process.json +0 -0
  59. /package/{commands/modelA/subtasks → models/doc/processes/subtask}/update-documentation.json +0 -0
  60. /package/{templates/modelA → models/doc/processes/templates}/dictionary/class-all.md +0 -0
  61. /package/{templates/modelA → models/doc/processes/templates}/dictionary/class-diagrama.md +0 -0
  62. /package/{templates/modelA → models/doc/processes/templates}/dictionary/class-inner.md +0 -0
  63. /package/{templates/modelA → models/doc/processes/templates}/dictionary/class-metodos.md +0 -0
  64. /package/{templates/modelA → models/doc/processes/templates}/dictionary/class-public.md +0 -0
  65. /package/{templates/modelA → models/doc/processes/templates}/dictionary/class-referencias.md +0 -0
  66. /package/{templates/modelA → models/doc/processes/templates}/dictionary/class.md +0 -0
  67. /package/{templates/modelA → models/doc/processes/templates}/dictionary/classes.md +0 -0
  68. /package/{templates/modelA → models/doc/processes/templates}/dictionary/object.md +0 -0
  69. /package/{templates/modelA → models/doc/processes/templates}/dictionary/objects.md +0 -0
  70. /package/{templates/modelA → models/doc/processes/templates}/intro.md +0 -0
  71. /package/{templates/modelA → models/doc/processes/templates}/process.md +0 -0
  72. /package/{commands/modelC/subtasks → models/git/githubflow/subtask}/checkout-branch.json +0 -0
  73. /package/{commands/modelC/subtasks → models/git/githubflow/subtask}/create-pull.json +0 -0
  74. /package/{commands/modelC/subtasks → models/git/githubflow/subtask}/publish-branch.json +0 -0
  75. /package/{commands/modelA → models/project/github-releases}/new/issue.json +0 -0
  76. /package/{commands/modelA/tasks → models/project/github-releases/task}/list.json +0 -0
  77. /package/{commands/modelC/tasks → models/project/github-releases/task}/view.json +0 -0
  78. /package/{templates/modelB → models/project/github-releases/templates}/changelog.md +0 -0
  79. /package/{templates/modelA → models/project/github-releases/templates}/openIssues.bash +0 -0
  80. /package/{templates/modelA → models/project/github-releases/templates}/viewIssue.bash +0 -0
@@ -1,12 +1,9 @@
1
1
  import { ITask } from "../types/helpers/tasks.js";
2
2
  import { AnyValue, CommandOptions, ObjectRecord } from "../types/auto.js";
3
- export declare const TASKS_FOLDER: string;
4
- export declare const SUBTASKS_FOLDER: string;
5
- export declare const NEW_FOLDER: string;
6
- export declare function getTaskFolder(command: string): string;
7
- export declare function getTasks(folder?: string): Record<string, ITask>;
3
+ export declare function getTaskFolders(command?: string): string[];
4
+ export declare function getTasks(command?: string): Record<string, ITask>;
8
5
  export declare function helpTask(task: ITask): Promise<boolean>;
9
6
  export declare function validateTask(task: ITask): boolean;
10
7
  export declare function runTask(task: ITask, taskContext: CommandOptions, tabs?: string): Promise<boolean>;
11
- export declare function previewTask(task: ITask, tabs?: string): Promise<void>;
8
+ export declare function previewTask(task: ITask, taskContext: CommandOptions, tabs?: string): Promise<boolean>;
12
9
  export declare function createObject(fields: ObjectRecord, values: AnyValue[]): ObjectRecord;
@@ -1,30 +1,15 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
1
  import context, { initializeContext } from "./context.js";
11
2
  import { logError, logStep } from "./color.js";
12
- import fs from "fs";
13
- import { getFiles, filterJson, searchInFolderHierarchy } from "./util.js";
3
+ import { getModelFolders, getFiles, filterJson, readJsonSync } from "./util.js";
14
4
  import { validateCommand, validateFunction, executeFunction, executeCommand, taskFunctions } from "./taskFunctions.js";
15
5
  import prompts from "prompts";
16
- import { fileURLToPath } from 'url';
17
- const COMMAND_FOLDER = searchInFolderHierarchy('commands', fileURLToPath(import.meta.url));
18
- export const TASKS_FOLDER = `${COMMAND_FOLDER}/${context.model}/tasks`;
19
- export const SUBTASKS_FOLDER = `${COMMAND_FOLDER}/${context.model}/subtasks`;
20
- export const NEW_FOLDER = `${COMMAND_FOLDER}/${context.model}/new`;
21
- export function getTaskFolder(command) {
6
+ export function getTaskFolders(command = 'task') {
22
7
  const folders = {
23
- 'task': TASKS_FOLDER,
24
- 'subtask': SUBTASKS_FOLDER,
25
- 'tasks': TASKS_FOLDER,
26
- 'subtasks': SUBTASKS_FOLDER,
27
- 'new': NEW_FOLDER
8
+ 'task': getModelFolders('tasks'),
9
+ 'subtask': getModelFolders('subtasks'),
10
+ 'tasks': getModelFolders('tasks'),
11
+ 'subtasks': getModelFolders('subtasks'),
12
+ 'new': getModelFolders('new')
28
13
  };
29
14
  return folders[command.toLowerCase()] || folders.tasks;
30
15
  }
@@ -32,22 +17,33 @@ function getTaskLists(folder) {
32
17
  const files = getFiles(folder, filterJson);
33
18
  return files.map(filename => filename.split(".")[0]);
34
19
  }
35
- export function getTasks(folder = TASKS_FOLDER) {
20
+ export function getTasks(command = 'tasks') {
21
+ const folders = getTaskFolders(command);
36
22
  const tasks = {};
37
- for (const taskName of getTaskLists(folder)) {
38
- tasks[taskName] = getTask(taskName, folder);
23
+ for (const folder of folders) {
24
+ for (const taskName of getTaskLists(folder)) {
25
+ const filename = folder + "/" + taskName + ".json";
26
+ const task = readJsonSync(filename);
27
+ tasks[taskName] = tasks[taskName] ? mergeTask(tasks[taskName], task) : task;
28
+ }
39
29
  }
40
30
  return tasks;
41
31
  }
42
- function getTask(taskName, folder) {
43
- const filename = folder + "/" + taskName + ".json";
44
- const content = fs.readFileSync(filename, "utf8");
45
- try {
46
- return JSON.parse(content);
47
- }
48
- catch (_a) {
49
- throw new Error(`Verifique que el ${filename} sea json valido`);
32
+ function mergeTask(newTask, oldTask) {
33
+ const newSteps = newTask.steps ? [...newTask.steps, ...oldTask.steps] : oldTask.steps;
34
+ const newArguments = newTask.arguments ? { ...newTask.arguments, ...oldTask.arguments } : oldTask.arguments;
35
+ const newGuards = newTask.guards ? [...newTask.guards, ...oldTask.guards] : oldTask.guards;
36
+ const newVerbose = newTask.verbose || oldTask.verbose;
37
+ const newDescription = newTask.description ? newTask.description + ' ' + oldTask.description : oldTask.description;
38
+ const mergedTask = { name: newTask.name, verbose: newVerbose, description: newDescription, arguments: newArguments, guards: newGuards, steps: newSteps };
39
+ return mergedTask;
40
+ }
41
+ function getTask(taskName, subfolder) {
42
+ const tasks = getTasks(subfolder);
43
+ if (!tasks[taskName]) {
44
+ throw new Error(`Verifique que el ${taskName} exista en alguno las subcarpetas de ${subfolder}`);
50
45
  }
46
+ return tasks[taskName];
51
47
  }
52
48
  function isCriteriaMet(criteria) {
53
49
  if (!criteria) {
@@ -62,14 +58,12 @@ function isCriteriaMet(criteria) {
62
58
  const result = context[field] == value;
63
59
  return result;
64
60
  }
65
- export function helpTask(task) {
66
- return __awaiter(this, void 0, void 0, function* () {
67
- console.log('Nombre:', task.name);
68
- console.log('Descripcion:', task.description);
69
- console.log('Guards:', task.guards);
70
- console.log('Argumentos:', task.arguments);
71
- return true;
72
- });
61
+ export async function helpTask(task) {
62
+ console.log('Nombre:', task.name);
63
+ console.log('Descripcion:', task.description);
64
+ console.log('Guards:', task.guards);
65
+ console.log('Argumentos:', task.arguments);
66
+ return true;
73
67
  }
74
68
  export function validateTask(task) {
75
69
  initializeContext();
@@ -92,11 +86,11 @@ export function validateTask(task) {
92
86
  validateStep = validateFunction(step);
93
87
  }
94
88
  else if (typeof step.subtask === 'string') {
95
- const subtask = getTask(step.subtask, SUBTASKS_FOLDER);
89
+ const subtask = getTask(step.subtask, 'subtasks');
96
90
  validateStep = validateTask(subtask);
97
91
  }
98
92
  else if (typeof step.task === 'string') {
99
- const subtask = getTask(step.task, TASKS_FOLDER);
93
+ const subtask = getTask(step.task, 'tasks');
100
94
  validateStep = validateTask(subtask);
101
95
  }
102
96
  else {
@@ -108,54 +102,51 @@ export function validateTask(task) {
108
102
  }
109
103
  return true;
110
104
  }
111
- export function runTask(task_1, taskContext_1) {
112
- return __awaiter(this, arguments, void 0, function* (task, taskContext, tabs = '') {
113
- initializeContext();
114
- // Valida que este ya esten las variables de enotorno y configuracion
115
- if (task.guards) {
116
- yield context.validate(task.guards);
117
- }
118
- // Pide datos de entrada y los deja en context
119
- if (task.arguments) {
120
- if (taskContext) {
121
- context.setObject(taskContext);
122
- }
123
- yield context.askForArguments(task.arguments);
124
- }
125
- if (task.verbose) {
126
- logStep(`[INICIO] ${task.name}`, tabs);
105
+ export async function runTask(task, taskContext, tabs = '') {
106
+ initializeContext();
107
+ // Valida que este ya esten las variables de enotorno y configuracion
108
+ if (task.guards) {
109
+ await context.validate(task.guards);
110
+ }
111
+ // Pide datos de entrada y los deja en context
112
+ if (task.arguments) {
113
+ if (taskContext) {
114
+ context.setObject(taskContext);
127
115
  }
128
- for (const step of task.steps) {
129
- if (isCriteriaMet(step.criteria)) {
130
- if (!(yield executeStep(step, tabs + '\t', task.verbose))) {
131
- return false;
132
- }
116
+ await context.askForArguments(task.arguments);
117
+ }
118
+ if (task.verbose) {
119
+ logStep(`[INICIO] ${task.name}`, tabs);
120
+ }
121
+ for (const step of task.steps) {
122
+ if (isCriteriaMet(step.criteria)) {
123
+ if (!await executeStep(step, tabs + '\t', task.verbose)) {
124
+ return false;
133
125
  }
134
126
  }
135
- if (task.verbose) {
136
- logStep(`[FIN] ${task.name}`, tabs);
137
- }
138
- return true;
139
- });
127
+ }
128
+ if (task.verbose) {
129
+ logStep(`[FIN] ${task.name}`, tabs);
130
+ }
131
+ return true;
140
132
  }
141
- export function previewTask(task_1) {
142
- return __awaiter(this, arguments, void 0, function* (task, tabs = '') {
143
- initializeContext();
144
- logStep(`${task.name}: ${task.description}`, tabs);
145
- for (const step of task.steps) {
146
- previewStep(step, tabs);
147
- }
148
- });
133
+ export async function previewTask(task, taskContext, tabs = '') {
134
+ initializeContext();
135
+ logStep(`${task.name}: ${task.description}`, tabs);
136
+ for (const step of task.steps) {
137
+ previewStep(step, taskContext, tabs);
138
+ }
139
+ return true;
149
140
  }
150
- function previewStep(step, tabs = '') {
141
+ function previewStep(step, taskContext, tabs = '') {
151
142
  if (step.criteria) {
152
143
  logStep(`Si ${step.criteria.field} ${step.criteria.operator || '=='} ${step.criteria.value}`, tabs);
153
144
  tabs += '\t';
154
145
  }
155
146
  if (step.subtask) {
156
147
  tabs += '\t';
157
- const subtask = getTask(step.subtask, SUBTASKS_FOLDER);
158
- previewTask(subtask, tabs);
148
+ const subtask = getTask(step.subtask, 'subtasks');
149
+ previewTask(subtask, taskContext, tabs);
159
150
  }
160
151
  else {
161
152
  logStep(`${step.name}`, tabs);
@@ -172,83 +163,77 @@ export function createObject(fields, values) {
172
163
  }
173
164
  return argsObject;
174
165
  }
175
- function runStep(step, tabs) {
176
- return __awaiter(this, void 0, void 0, function* () {
177
- if (typeof step.command === 'string') {
178
- return executeCommand(step);
179
- }
180
- else if (typeof step.function === 'string') {
181
- return yield executeFunction(step);
166
+ async function runStep(step, tabs) {
167
+ if (typeof step.command === 'string') {
168
+ return executeCommand(step);
169
+ }
170
+ else if (typeof step.function === 'string') {
171
+ return await executeFunction(step);
172
+ }
173
+ else if (typeof step.subtask === 'string' || typeof step.task === 'string') {
174
+ const subtask = typeof step.subtask === 'string' ? getTask(step.subtask, 'subtask') : getTask(step.task, 'tasks');
175
+ let stepContext = step.arguments ? context.mergeArgs(step.arguments) : {};
176
+ if (Array.isArray(stepContext)) {
177
+ stepContext = createObject(subtask.arguments, stepContext);
182
178
  }
183
- else if (typeof step.subtask === 'string' || typeof step.task === 'string') {
184
- const subtask = typeof step.subtask === 'string' ? getTask(step.subtask, SUBTASKS_FOLDER) : getTask(step.task, TASKS_FOLDER);
185
- let stepContext = step.arguments ? context.mergeArgs(step.arguments) : {};
186
- if (Array.isArray(stepContext)) {
187
- stepContext = createObject(subtask.arguments, stepContext);
188
- }
189
- return yield runTask(subtask, stepContext, tabs);
190
- }
191
- throw new Error(`No se pudo ejecutar el step ${step.name} porque no tiene command, function o subtasks`);
192
- });
193
- }
194
- function askForContinueOrRetry() {
195
- return __awaiter(this, void 0, void 0, function* () {
196
- if (!context.isVerbose) {
197
- return 'quit';
198
- }
199
- const answer = yield prompts([
200
- {
201
- type: "select",
202
- name: "continue",
203
- message: "No se pudo ejecutar el step, ¿que quiere hacer? ",
204
- choices: [{ title: 'Salir', value: 'quit' }, { title: 'Continuar', value: 'continue' }, { title: 'Reintentar', value: 'retry' }],
205
- }
206
- ]);
207
- return answer.continue;
208
- });
179
+ return await runTask(subtask, stepContext, tabs);
180
+ }
181
+ throw new Error(`No se pudo ejecutar el step ${step.name} porque no tiene command, function o subtasks`);
182
+ }
183
+ async function askForContinueOrRetry() {
184
+ if (!context.isVerbose) {
185
+ return 'quit';
186
+ }
187
+ const answer = await prompts([
188
+ {
189
+ type: "select",
190
+ name: "continue",
191
+ message: "No se pudo ejecutar el step, ¿que quiere hacer? ",
192
+ choices: [{ title: 'Salir', value: 'quit' }, { title: 'Continuar', value: 'continue' }, { title: 'Reintentar', value: 'retry' }],
193
+ }
194
+ ]);
195
+ return answer.continue;
209
196
  }
210
197
  function getStepError(step, stepName) {
211
198
  return step.errorMessage ? context.merge(step.errorMessage) : stepName ? `Fallo el step ${stepName}` : '';
212
199
  }
213
- function executeStep(step_1, tabs_1) {
214
- return __awaiter(this, arguments, void 0, function* (step, tabs, verbose = false) {
215
- const stepName = step.name ? context.merge(step.name) : undefined;
216
- if (verbose && stepName) {
217
- logStep(`[INICIO] ${stepName}`, tabs);
218
- }
219
- let retry = false;
220
- let success = false;
221
- do {
222
- try {
223
- success = yield runStep(step, tabs);
224
- if (!success) {
225
- logError(getStepError(step, stepName), tabs);
226
- // Si tiene un custom handler cuando hay un error
227
- if (step.onError) {
228
- const errorHandler = taskFunctions[step.onError];
229
- if (typeof errorHandler === 'function') {
230
- success = yield errorHandler();
231
- }
200
+ async function executeStep(step, tabs, verbose = false) {
201
+ const stepName = step.name ? context.merge(step.name) : undefined;
202
+ if (verbose && stepName) {
203
+ logStep(`[INICIO] ${stepName}`, tabs);
204
+ }
205
+ let retry = false;
206
+ let success = false;
207
+ do {
208
+ try {
209
+ success = await runStep(step, tabs);
210
+ if (!success) {
211
+ logError(getStepError(step, stepName), tabs);
212
+ // Si tiene un custom handler cuando hay un error
213
+ if (step.onError) {
214
+ const errorHandler = taskFunctions[step.onError];
215
+ if (typeof errorHandler === 'function') {
216
+ success = await errorHandler();
232
217
  }
233
218
  }
234
219
  }
235
- catch (error) {
236
- if (error instanceof Error) {
237
- logError(error.message, tabs);
238
- }
239
- }
240
- if (!success) {
241
- const result = yield askForContinueOrRetry();
242
- retry = result == 'retry';
243
- success = result == 'continue';
220
+ }
221
+ catch (error) {
222
+ if (error instanceof Error) {
223
+ logError(error.message, tabs);
244
224
  }
245
- } while (!success && retry);
246
- if (verbose && stepName) {
247
- logStep(`[FIN] ${stepName}`, tabs);
248
225
  }
249
226
  if (!success) {
250
- process.exit(!context.isVerbose ? -1 : 0);
227
+ const result = await askForContinueOrRetry();
228
+ retry = result == 'retry';
229
+ success = result == 'continue';
251
230
  }
252
- return success;
253
- });
231
+ } while (!success && retry);
232
+ if (verbose && stepName) {
233
+ logStep(`[FIN] ${stepName}`, tabs);
234
+ }
235
+ if (!success) {
236
+ process.exit(!context.isVerbose ? -1 : 0);
237
+ }
238
+ return success;
254
239
  }
@@ -1,15 +1,19 @@
1
- declare class TemplateEngine {
1
+ export declare class TemplateEngine {
2
2
  _template: HandlebarsTemplateDelegate | undefined;
3
3
  _rendered: string | undefined;
4
4
  _extension: string;
5
- _sourceFolder: string;
6
- constructor(source: string, extension?: string);
5
+ _sourceFolders: string[];
6
+ constructor(sources: string[], extension?: string);
7
7
  getTemplates(): string[];
8
- getNameAndExtension(templateName: string): string[];
8
+ findTemplateByName(templateName: string): {
9
+ folder: string;
10
+ name: string;
11
+ extension: string;
12
+ };
9
13
  read(templateName: string): void;
10
14
  render(context: object, options?: RuntimeOptions): void;
11
15
  get rendered(): string | undefined;
12
16
  save(filename: string, folder: string, options?: SaveTemplateOptions): void;
13
17
  }
14
- declare const _default: (source: string, extension: string) => TemplateEngine;
18
+ declare const _default: (source: string[], extension: string) => TemplateEngine;
15
19
  export default _default;
@@ -1,7 +1,7 @@
1
1
  import fs from "fs";
2
2
  import Handlebars from "handlebars";
3
3
  import { merge } from "./merge.js";
4
- import { getFiles } from "./util.js";
4
+ import { getFiles, getFilesInFolders } from "./util.js";
5
5
  function isObjectEmpty(objectName) {
6
6
  return (objectName &&
7
7
  Object.keys(objectName).length === 0 &&
@@ -22,44 +22,57 @@ function openTemplate(sourceFolder, templateName, extension) {
22
22
  }
23
23
  return content;
24
24
  }
25
- class TemplateEngine {
26
- constructor(source, extension = '*') {
27
- this._sourceFolder = source;
28
- if (!fs.existsSync(this._sourceFolder)) {
29
- throw new Error(`La carpeta source ${this._sourceFolder} no existe!`);
30
- }
25
+ export class TemplateEngine {
26
+ _template;
27
+ _rendered;
28
+ _extension;
29
+ _sourceFolders;
30
+ constructor(sources, extension = '*') {
31
+ this._sourceFolders = sources;
31
32
  this._extension = extension;
33
+ for (const sourceFolder of this._sourceFolders) {
34
+ if (!fs.existsSync(sourceFolder)) {
35
+ throw new Error(`La carpeta source ${sourceFolder} no existe!`);
36
+ }
37
+ }
32
38
  }
33
39
  ;
34
40
  getTemplates() {
35
41
  const filterThisExtension = (file) => file.endsWith(`.${this._extension}`) || this._extension === '*';
36
42
  const templates = [];
37
- const files = getFiles(this._sourceFolder, filterThisExtension, true, ['dictionary']);
43
+ const files = getFilesInFolders(this._sourceFolders, filterThisExtension, true, ['dictionary']);
38
44
  for (const filename of files) {
39
45
  const [name] = filename.split(".");
40
46
  templates.push(name);
41
47
  }
42
48
  return templates;
43
49
  }
44
- getNameAndExtension(templateName) {
50
+ findTemplateByName(templateName) {
51
+ let folder;
52
+ let name = templateName;
53
+ let extension = this._extension;
45
54
  // Si viene la extension en el nombre la extrae
46
55
  if (templateName.split(".").length > 1) {
47
- return templateName.split(".");
56
+ [name, extension] = templateName.split(".");
48
57
  }
49
- // Si viene la extension * busca cual puede ser en el directorio
50
- if (this._extension === '*' || this._extension === '') {
51
- const fileNames = getFiles(this._sourceFolder, fileName => fileName.split(".")[0].endsWith(templateName));
58
+ // Busca en las carpetas el archivo
59
+ for (const currentFolder of this._sourceFolders) {
60
+ folder = currentFolder;
61
+ const filterWithExtension = (fileName) => fileName === `${name}.${extension}`;
62
+ const filterWithoutExtension = (fileName) => fileName.split(".")[0].endsWith(name);
63
+ const filter = (extension === '*' || extension === '') ? filterWithoutExtension : filterWithExtension;
64
+ const fileNames = getFiles(folder, filter);
52
65
  if (fileNames.length > 0) {
53
- return fileNames[0].split(".");
66
+ [name, extension] = fileNames[0].split(".");
67
+ return { folder, name, extension };
54
68
  }
55
69
  }
56
- // Por defecto usa el templateName como nombre y la extension
57
- return [templateName, this._extension];
70
+ throw new Error(`No se encontro el template ${templateName} en ninguna de las carpetas ${this._sourceFolders}`);
58
71
  }
59
72
  read(templateName) {
60
73
  // Por defecto usa el templateName como nombre y la extension
61
- const [name, extension] = this.getNameAndExtension(templateName);
62
- const rawTemplate = openTemplate(this._sourceFolder, name, extension);
74
+ const { folder, name, extension } = this.findTemplateByName(templateName);
75
+ const rawTemplate = openTemplate(folder, name, extension);
63
76
  this._template = Handlebars.compile(rawTemplate);
64
77
  }
65
78
  render(context, options = {}) {
@@ -1,10 +1,8 @@
1
- import prompts, { Choice } from "prompts";
1
+ import { Choice } from "prompts";
2
2
  import { AnyValue } from "../types/auto.js";
3
+ export declare const WORKING_FOLDER: string;
3
4
  export declare const CONFIG_FILE: string;
4
- export declare const TEMPLATES_FOLDER: string;
5
- export declare const TEMPLATE_MODEL_FOLDER: string;
6
5
  export declare const DICTIONARY_FOLDER: string;
7
- export declare const WORKING_FOLDER: string;
8
6
  export declare const filterJson: (fullPath: string) => boolean;
9
7
  export declare const filterDirectory: (fullPath: string) => boolean;
10
8
  export declare const filterFiles: (fullPath: string) => boolean;
@@ -20,8 +18,9 @@ export declare function titlesToChoices(list: string[], titleToValue?: (title: s
20
18
  title: string;
21
19
  value: string;
22
20
  }[];
23
- export declare function getDataFromPackage(): Record<string, string>;
24
21
  export declare function findChoicesPosition(choices: Choice[], value: string): number;
22
+ export declare function getFilesInFolders(folders: string[], filter: (fullPath: string) => boolean, recursive?: boolean, ignoreList?: string[]): string[];
23
+ export declare function getModelFolders(subfolder: string): string[];
25
24
  export declare function createConfigurationFile(taskName?: string): Promise<boolean>;
26
25
  export declare function getConfigFile(file: string, variable: string, defaultValue: AnyValue): any;
27
26
  export declare function getConfig(variable: string, defaultValue: AnyValue): any;
@@ -52,4 +51,4 @@ export declare function searchInFolderHierarchy(element: string, parentFolder: s
52
51
  export declare function getFiles(source: string, filter?: (file: string) => boolean, recursive?: boolean, ignoreList?: string[]): string[];
53
52
  export declare function convertNameToKey(name: string): string;
54
53
  export declare function convertKeyToName(key: string): string;
55
- export declare function readJsonSync(filename: string): prompts.Choice[];
54
+ export declare function readJsonSync<T>(filename: string): T;