Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(preprocess): null check #32

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 37 additions & 32 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,39 +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 = 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 @@ -195,9 +200,9 @@ export class LibraryTemplatesProcessor {
jsc: {
parser: {
syntax: "typescript",
},
},
transform: {

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

return (res.code);
}
}