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.
- package/.autoforce.json +8 -4
- package/CHANGELOG.md +20 -1
- package/lib/auto.js +50 -64
- package/lib/helpers/class.d.ts +1 -0
- package/lib/helpers/class.js +74 -80
- package/lib/helpers/connect.js +199 -213
- package/lib/helpers/context.d.ts +6 -3
- package/lib/helpers/context.js +279 -225
- package/lib/helpers/github-graphql.js +90 -113
- package/lib/helpers/github-project-graphql.js +100 -132
- package/lib/helpers/gitlab-graphql.js +68 -104
- package/lib/helpers/lwc.js +42 -48
- package/lib/helpers/metadata.js +1 -2
- package/lib/helpers/object.js +47 -55
- package/lib/helpers/openai.js +10 -22
- package/lib/helpers/taskFunctions.js +330 -386
- package/lib/helpers/tasks.d.ts +3 -6
- package/lib/helpers/tasks.js +132 -147
- package/lib/helpers/template.d.ts +9 -5
- package/lib/helpers/template.js +31 -18
- package/lib/helpers/util.d.ts +5 -6
- package/lib/helpers/util.js +131 -187
- package/models/dev/models.json +19 -0
- package/models/doc/models.json +9 -0
- package/models/git/models.json +9 -0
- package/models/project/models.json +9 -0
- package/package.json +16 -12
- package/commands/modelC/new/issue.json +0 -24
- package/commands/models.json +0 -18
- package/templates/modelB/openIssues.bash +0 -4
- package/templates/modelB/viewIssue.bash +0 -8
- package/templates/models.json +0 -12
- /package/{commands/modelA → models/dev/npm}/subtasks/checkout-branch.json +0 -0
- /package/{commands/modelA → models/dev/npm}/subtasks/create-pull.json +0 -0
- /package/{commands/modelC → models/dev/npm}/subtasks/pack.json +0 -0
- /package/{commands/modelA → models/dev/npm}/subtasks/publish-branch.json +0 -0
- /package/{commands/modelA → models/dev/npm}/tasks/cancel.json +0 -0
- /package/{commands/modelA → models/dev/npm}/tasks/finish.json +0 -0
- /package/{commands/modelC → models/dev/npm}/tasks/list.json +0 -0
- /package/{commands/modelC → models/dev/npm}/tasks/publish.json +0 -0
- /package/{commands/modelC → models/dev/npm}/tasks/start.json +0 -0
- /package/{commands/modelA → models/dev/npm}/tasks/stop.json +0 -0
- /package/{commands/modelA → models/dev/npm}/tasks/switch.json +0 -0
- /package/{commands/modelA → models/dev/npm}/tasks/view.json +0 -0
- /package/{commands/modelA → models/dev/scratchs}/subtasks/create-scratch.json +0 -0
- /package/{commands/modelA → models/dev/scratchs}/subtasks/deploy-code.json +0 -0
- /package/{commands/modelA → models/dev/scratchs}/subtasks/drop-scratch.json +0 -0
- /package/{commands/modelA → models/dev/scratchs}/subtasks/package-code.json +0 -0
- /package/{commands/modelA → models/dev/scratchs}/subtasks/validate-code.json +0 -0
- /package/{commands/modelA → models/dev/scratchs}/subtasks/validate-scratch.json +0 -0
- /package/{commands/modelC → models/dev/scratchs}/tasks/cancel.json +0 -0
- /package/{commands/modelA → models/dev/scratchs}/tasks/deploy.json +0 -0
- /package/{commands/modelC → models/dev/scratchs}/tasks/finish.json +0 -0
- /package/{commands/modelA → models/dev/scratchs}/tasks/rollback.json +0 -0
- /package/{commands/modelA → models/dev/scratchs}/tasks/start.json +0 -0
- /package/{commands/modelC → models/dev/scratchs}/tasks/stop.json +0 -0
- /package/{commands/modelC → models/dev/scratchs}/tasks/switch.json +0 -0
- /package/{commands/modelA → models/doc/processes}/new/process.json +0 -0
- /package/{commands/modelA/subtasks → models/doc/processes/subtask}/update-documentation.json +0 -0
- /package/{templates/modelA → models/doc/processes/templates}/dictionary/class-all.md +0 -0
- /package/{templates/modelA → models/doc/processes/templates}/dictionary/class-diagrama.md +0 -0
- /package/{templates/modelA → models/doc/processes/templates}/dictionary/class-inner.md +0 -0
- /package/{templates/modelA → models/doc/processes/templates}/dictionary/class-metodos.md +0 -0
- /package/{templates/modelA → models/doc/processes/templates}/dictionary/class-public.md +0 -0
- /package/{templates/modelA → models/doc/processes/templates}/dictionary/class-referencias.md +0 -0
- /package/{templates/modelA → models/doc/processes/templates}/dictionary/class.md +0 -0
- /package/{templates/modelA → models/doc/processes/templates}/dictionary/classes.md +0 -0
- /package/{templates/modelA → models/doc/processes/templates}/dictionary/object.md +0 -0
- /package/{templates/modelA → models/doc/processes/templates}/dictionary/objects.md +0 -0
- /package/{templates/modelA → models/doc/processes/templates}/intro.md +0 -0
- /package/{templates/modelA → models/doc/processes/templates}/process.md +0 -0
- /package/{commands/modelC/subtasks → models/git/githubflow/subtask}/checkout-branch.json +0 -0
- /package/{commands/modelC/subtasks → models/git/githubflow/subtask}/create-pull.json +0 -0
- /package/{commands/modelC/subtasks → models/git/githubflow/subtask}/publish-branch.json +0 -0
- /package/{commands/modelA → models/project/github-releases}/new/issue.json +0 -0
- /package/{commands/modelA/tasks → models/project/github-releases/task}/list.json +0 -0
- /package/{commands/modelC/tasks → models/project/github-releases/task}/view.json +0 -0
- /package/{templates/modelB → models/project/github-releases/templates}/changelog.md +0 -0
- /package/{templates/modelA → models/project/github-releases/templates}/openIssues.bash +0 -0
- /package/{templates/modelA → models/project/github-releases/templates}/viewIssue.bash +0 -0
package/lib/helpers/tasks.d.ts
CHANGED
@@ -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
|
4
|
-
export declare
|
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<
|
8
|
+
export declare function previewTask(task: ITask, taskContext: CommandOptions, tabs?: string): Promise<boolean>;
|
12
9
|
export declare function createObject(fields: ObjectRecord, values: AnyValue[]): ObjectRecord;
|
package/lib/helpers/tasks.js
CHANGED
@@ -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
|
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
|
-
|
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':
|
24
|
-
'subtask':
|
25
|
-
'tasks':
|
26
|
-
'subtasks':
|
27
|
-
'new':
|
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(
|
20
|
+
export function getTasks(command = 'tasks') {
|
21
|
+
const folders = getTaskFolders(command);
|
36
22
|
const tasks = {};
|
37
|
-
for (const
|
38
|
-
|
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
|
43
|
-
const
|
44
|
-
const
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
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
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
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,
|
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,
|
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(
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
if (
|
120
|
-
|
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
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
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
|
-
|
136
|
-
|
137
|
-
}
|
138
|
-
|
139
|
-
|
127
|
+
}
|
128
|
+
if (task.verbose) {
|
129
|
+
logStep(`[FIN] ${task.name}`, tabs);
|
130
|
+
}
|
131
|
+
return true;
|
140
132
|
}
|
141
|
-
export function previewTask(
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
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,
|
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
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
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
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
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(
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
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
|
-
|
236
|
-
|
237
|
-
|
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
|
-
|
227
|
+
const result = await askForContinueOrRetry();
|
228
|
+
retry = result == 'retry';
|
229
|
+
success = result == 'continue';
|
251
230
|
}
|
252
|
-
|
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
|
-
|
6
|
-
constructor(
|
5
|
+
_sourceFolders: string[];
|
6
|
+
constructor(sources: string[], extension?: string);
|
7
7
|
getTemplates(): string[];
|
8
|
-
|
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;
|
package/lib/helpers/template.js
CHANGED
@@ -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
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
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 =
|
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
|
-
|
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
|
-
|
56
|
+
[name, extension] = templateName.split(".");
|
48
57
|
}
|
49
|
-
//
|
50
|
-
|
51
|
-
|
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
|
-
|
66
|
+
[name, extension] = fileNames[0].split(".");
|
67
|
+
return { folder, name, extension };
|
54
68
|
}
|
55
69
|
}
|
56
|
-
|
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
|
62
|
-
const rawTemplate = openTemplate(
|
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 = {}) {
|
package/lib/helpers/util.d.ts
CHANGED
@@ -1,10 +1,8 @@
|
|
1
|
-
import
|
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):
|
54
|
+
export declare function readJsonSync<T>(filename: string): T;
|