Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ator-iot into 110x
  • Loading branch information
jsantos98 committed Jun 18, 2024
2 parents 532fa92 + 73effd2 commit dbc7f8c
Showing 1 changed file with 37 additions and 36 deletions.
73 changes: 37 additions & 36 deletions generators/packagePacker/processors/libraryTemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ export class LibraryTemplatesProcessor {
public async process(templateRules: string | Template[], destination: string): Promise<void> {
this._logger.Info(` [Templates] Processing library templates`);
let json: any = io.readJSONSync(destination);

if (json?.criticalManufacturing?.tasksLibrary == null) {
throw new Error("Unable to read TasksLibrary section of the package.json file")
}

let libraryMetadata: any = json.criticalManufacturing.tasksLibrary;

this._finalTemplates = libraryMetadata.metadata ?? { };
this._finalTemplates = libraryMetadata.metadata ?? {};
if (this._finalTemplates != null && ((this._finalTemplates.converters?.length ?? 0) !== 0 || (this._finalTemplates.tasks?.length ?? 0) !== 0)) {
this._logger.Warn(" [Templates] Existing templates found in the package.json file found. Merging the new ones with the existing");
}
Expand All @@ -47,7 +47,7 @@ export class LibraryTemplatesProcessor {
throw new Error(` [Templates] Directory '${templateRules}' doesn't exist`);
} else {
this._templateDirectory = templateRules;

const files = io.readdirSync(templateRules);
for (let file of files) {
if (file.endsWith(".json")) {
Expand Down Expand Up @@ -102,9 +102,13 @@ export class LibraryTemplatesProcessor {

const newTemplates: LibraryMetadata = io.readJSONSync(templateFile);
if (newTemplates != null) {
this._logger.debug(` [Templates] Merging Tasks & Converters '${templateFile}'`);
await this.mergeConverters(newTemplates.converters ?? []);
this._logger.debug(` [Templates] Merged Converters '${templateFile}'`);
await this.mergeTasks(newTemplates.tasks ?? []);
this._logger.debug(` [Templates] Merged Tasks '${templateFile}'`);
}
this._logger.debug(` [Templates] Merged '${templateFile}'`);
}

private async mergeConverters(converters: LibraryConverter[]): Promise<void> {
Expand Down Expand Up @@ -132,7 +136,7 @@ export class LibraryTemplatesProcessor {
private async mergeTasks(tasks: LibraryTask[]): Promise<void> {
for (const task of tasks) {
const newOne = /*await this.preProcessTaskScripts*/(Object.assign({}, LibraryTaskDefaults, task));
const b = await this.preProcessTaskScripts(newOne);
const b = await this.preProcessTaskScripts(newOne);

// Check if there is another with the same name
const existing = (this._finalTemplates.tasks ?? []).find(c => c.name === newOne.name);
Expand All @@ -154,43 +158,40 @@ export class LibraryTemplatesProcessor {
}

private async preProcessTaskScripts(value: any): Promise<any> {
if (typeof (value) === "object") {
if (Array.isArray(value)) {
for (let i = 0; i < value.length; i++) {
value[i] = await this.preProcessTaskScripts(value[i]);
}
} else {
const keys = Object.keys(value);
for (const key of keys) {
value[key] = await this.preProcessTaskScripts(value[key]);
if (value != null) {
if (typeof (value) === "object") {
if (Array.isArray(value)) {
for (let i = 0; i < value.length; i++) {
value[i] = await this.preProcessTaskScripts(value[i]);
}
} else {
const keys = Object.keys(value);
for (const key of keys) {
value[key] = await this.preProcessTaskScripts(value[key]);
}
}
}
} else if (typeof(value) === "string") {
const regex = /\${script\((.*)\)}/i;
const matches = value.match(regex);
if (matches != null && matches.length === 2) {
this._logger.debug(` [Templates] Processing Script '${matches[1]}'`);
const scriptFile = path.resolve(this._templateDirectory, matches[1].toString());
const scriptContent = io.readFileSync(scriptFile).toString();
const transpiled = await this.transpile(scriptContent, false);
value = Buffer.from(transpiled).toString("base64");
} else {
const regex = /\${script\[\]\((.*)\)}/i;
} else if (typeof (value) === "string") {
const regex = /\${script\((.*)\)}/i;
const matches = value.match(regex);
if (matches != null && matches.length === 2) {
this._logger.debug(` [Templates] Processing Script as [] '${matches[1]}'`);
this._logger.debug(` [Templates] Processing Script '${matches[1]}'`);
const scriptFile = path.resolve(this._templateDirectory, matches[1].toString());
const scriptContent = io.readFileSync(scriptFile).toString();
const transpiled = await this.transpile(scriptContent, false);
value = {
type: "Script",
encoding: "Plain",
script: transpiled.split("\n"),
};
value = Buffer.from(transpiled).toString("base64");
} else {
const regex = /\${script\[\]\((.*)\)}/i;
const matches = value.match(regex);
if (matches != null && matches.length === 2) {
this._logger.debug(` [Templates] Processing Script as [] '${matches[1]}'`);
const scriptFile = path.resolve(this._templateDirectory, matches[1].toString());
const scriptContent = io.readFileSync(scriptFile).toString();
const transpiled = await this.transpile(scriptContent, false);
value = transpiled.split("\n");
}
}
}
}

}
return value;
}

Expand All @@ -199,9 +200,9 @@ export class LibraryTemplatesProcessor {
jsc: {
parser: {
syntax: "typescript",
},
},
transform: {

},
target: "es2016",
minify: {
Expand All @@ -210,7 +211,7 @@ export class LibraryTemplatesProcessor {
},
minify: compress,
});

return (res.code);
}
}

0 comments on commit dbc7f8c

Please sign in to comment.