Skip to content

Commit

Permalink
feat: use es2022
Browse files Browse the repository at this point in the history
Signed-off-by: Emilien Escalle <[email protected]>
  • Loading branch information
neilime committed Nov 9, 2024
1 parent 9a34bb9 commit d4c3679
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 54 deletions.
17 changes: 8 additions & 9 deletions src/services/file/JsonFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@ export interface JsonFileData {
}

export class JsonFile extends StdFile {
protected data?: JsonFileData;

getContent(): string {
return JSON.stringify(this.data, null, " ");
}
protected declare data?: JsonFileData;

appendContent(content: string): this {
return this.appendData(JSON.parse(content));
return this.appendData(this.parseContentToData(content));
}

appendData(data: JsonFileData): this {
Expand Down Expand Up @@ -45,16 +41,19 @@ export class JsonFile extends StdFile {
}

protected parseContent(content: string): string {
content = super.parseContent(content);
this.data = this.parseContentToData(super.parseContent(content));
return JSON.stringify(this.data, null, " ");
}

protected parseContentToData(content: string): JsonFileData {
try {
this.data = JSON.parse(content);
return JSON.parse(content);
} catch (error) {
throw new Error(
`An error occurred while parsing file content "${this.file}": ${JSON.stringify(
error instanceof Error ? error.message : error
)} => "${content.trim()}"`
);
}
return content;
}
}
1 change: 1 addition & 0 deletions src/services/file/StdFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { FileService } from "./FileService";

export class StdFile {
protected content = "";

constructor(
protected readonly cliService: CliService,
protected readonly directoryService: DirectoryService,
Expand Down
29 changes: 20 additions & 9 deletions src/services/file/TomlFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,18 @@ import { all } from "deepmerge";
import { StdFile } from "./StdFile";

export class TomlFile extends StdFile {
protected data?: JsonMap;

protected parseContent(content: string): string {
content = super.parseContent(content);
this.data = parse(content);
return content;
}
protected declare data?: JsonMap;

getContent(): string {
return stringify(this.data || {});
}

appendContent(content: string): this {
return this.appendData(parse(content));
return this.appendData(this.parseContentToData(content));
}

appendData(data: JsonMap): this {
const overwriteMerge = (destinationArray, sourceArray) => sourceArray;
const overwriteMerge = (_, sourceArray) => sourceArray;

const newData = all([(this.data || {}) as JsonMap, data as JsonMap], {
arrayMerge: overwriteMerge,
Expand All @@ -37,4 +31,21 @@ export class TomlFile extends StdFile {
}
return property ? this.data[property] : this.data;
}

protected parseContent(content: string): string {
this.data = this.parseContentToData(super.parseContent(content));
return stringify(this.data || {});
}

protected parseContentToData(content: string): JsonMap {
try {
return parse(content);
} catch (error) {
throw new Error(
`An error occurred while parsing file content "${this.file}": ${JSON.stringify(
error instanceof Error ? error.message : error
)} => "${content.trim()}"`
);
}
}
}
70 changes: 35 additions & 35 deletions src/services/file/TypescriptFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,40 @@ import { StdFile } from "./StdFile";
import { ITypescriptImport, ITypescriptImportModules, TypescriptImport } from "./TypescriptImport";

export class TypescriptFile extends StdFile {
protected imports?: Array<TypescriptImport>;
protected declarations?: Array<string>;
protected defaultDeclaration?: string | null;
protected declare imports?: Array<TypescriptImport>;
protected declare declarations?: Array<string>;
protected declare defaultDeclaration?: string | null;

setImports(
importsToAdd: Array<ITypescriptImport> = [],
importsToRemove: Array<ITypescriptImport> = []
): this {
const importToAddItems = importsToAdd.map(
(importItem) => new TypescriptImport(importItem.packageName, importItem.modules)
);
const importToRemoveItems = importsToRemove.map(
(importItem) => new TypescriptImport(importItem.packageName, importItem.modules)
);

this.addImports(importToAddItems);
this.removeImports(importToRemoveItems);
return this;
}

getContent(): string {
let importLines: string[] = [];
if (this.imports) {
this.imports.sort(this.sortImports);
importLines = this.imports
.map((importItem) => importItem.toString())
.filter((line) => !!line.length);
}

return [importLines.join("\n"), (this.declarations || []).join("\n")]
.map((value) => value.trim())
.join("\n\n")
.trim();
}

protected parseContent(content: string): string {
this.imports = [];
Expand All @@ -29,7 +60,7 @@ export class TypescriptFile extends StdFile {
this.parseImportDeclaration(bodyItem as ImportDeclaration);
break;
default:
this.declarations.push(content.substr(bodyItem.pos, bodyItem.end - bodyItem.pos));
this.declarations.push(content.substring(bodyItem.pos, bodyItem.end));
}
}

Expand Down Expand Up @@ -99,37 +130,6 @@ export class TypescriptFile extends StdFile {
}
}

getContent(): string {
let importLines: string[] = [];
if (this.imports) {
this.imports.sort(this.sortImports);
importLines = this.imports
.map((importItem) => importItem.toString())
.filter((line) => !!line.length);
}

return [importLines.join("\n"), (this.declarations || []).join("\n")]
.map((value) => value.trim())
.join("\n\n")
.trim();
}

setImports(
importsToAdd: Array<ITypescriptImport> = [],
importsToRemove: Array<ITypescriptImport> = []
): this {
const importToAddItems = importsToAdd.map(
(importItem) => new TypescriptImport(importItem.packageName, importItem.modules)
);
const importToRemoveItems = importsToRemove.map(
(importItem) => new TypescriptImport(importItem.packageName, importItem.modules)
);

this.addImports(importToAddItems);
this.removeImports(importToRemoveItems);
return this;
}

protected addImports(imports: TypescriptImport[]): void {
if (!this.imports) {
this.imports = [];
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"noLib": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es2018",
"target": "es2022",
"sourceMap": true,
"strict": true,
"outDir": "./dist",
Expand Down

0 comments on commit d4c3679

Please sign in to comment.