Skip to content

Commit c8b0727

Browse files
committed
Refactored file parser code
1 parent 604ebee commit c8b0727

20 files changed

Lines changed: 139 additions & 156 deletions

File tree

src/commands/apply/orchestrator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import readline from 'node:readline'
22

3-
import { ConfigReader } from '../../config-compiler/index.js';
3+
import { ConfigParser } from '../../config-parser/index.js';
44
import { PluginCollection } from '../../plugins/plugin-collection.js';
55

66
const rl = readline.createInterface({
@@ -12,7 +12,7 @@ const rl = readline.createInterface({
1212
export const ApplyOrchestrator = {
1313

1414
async run(rootDirectory: string): Promise<string> {
15-
const project = await ConfigReader.parseProject(rootDirectory);
15+
const project = await ConfigParser.parseProject(rootDirectory);
1616
if (project.isEmpty()) {
1717
console.log('Empty project. Taking no action');
1818
return '';

src/commands/plan/orchestrator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { ConfigReader } from '../../config-compiler/index.js';
1+
import { ConfigParser } from '../../config-parser/index.js';
22
import { PluginCollection } from '../../plugins/plugin-collection.js';
33

44
export const PlanOrchestrator = {
55
async run(rootDirectory: string): Promise<string> {
6-
const project = await ConfigReader.parseProject(rootDirectory);
6+
const project = await ConfigParser.parseProject(rootDirectory);
77
if (project.isEmpty()) {
88
console.log('Empty project. Taking no action');
99
return '';

src/config-compiler/loader/entities/module.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/config-compiler/loader/entities/project.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/config-compiler/loader/index.test.ts

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/config-compiler/loader/index.ts

Lines changed: 0 additions & 59 deletions
This file was deleted.

src/config-compiler/parser/index.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,45 @@ import { ProjectConfig } from '../entities/project-config.js';
33
import { ResourceConfig } from '../entities/resource-config.js';
44
import { InternalError } from '../utils/errors.js';
55
import { ConfigClass } from './language-definition.js';
6-
import { ConfigLoader } from './loader/index.js';
76
import { FileParser } from './parser/index.js';
87
import { JsonFileParser } from './parser/json/file-parser.js';
8+
import { ProjectReader } from './reader/index.js';
9+
import { ConfigBlock } from '../entities/index.js';
910

10-
export class ConfigReader {
11+
export class ConfigParser {
1112

1213
static readonly supportedParsers: Record<string, FileParser> = {
1314
'json': new JsonFileParser(),
1415
}
1516

1617
static async parseProject(directory: string): Promise<Project> {
17-
const loadedProject = await (new ConfigLoader().loadProject(directory));
18+
const configReader = new ProjectReader();
19+
const loadedProject = await configReader.readProject(directory);
1820

19-
const configBlocksResult = await Promise.all(loadedProject.coreModule.files.map((file) => {
20-
const parser = ConfigReader.supportedParsers[file.fileType];
21+
const configBlocksResult = await Promise.all(loadedProject.files.map((file) => {
22+
const parser = ConfigParser.supportedParsers[file.fileType];
2123
if (!parser) {
2224
throw new InternalError(`Unsupported file format loaded into parser: ${file.fileName}`);
2325
}
2426

2527
return parser.parse(file);
2628
}));
29+
2730
const configBlocks = configBlocksResult.flat(1);
31+
const projectConfig = ConfigParser.findProjectConfig(configBlocks);
2832

33+
return new Project(
34+
projectConfig,
35+
configBlocks.filter((u) => u.configClass !== ConfigClass.PROJECT) as ResourceConfig[],
36+
)
37+
}
38+
39+
private static findProjectConfig(configBlocks: ConfigBlock[]): ProjectConfig | null {
2940
const parsedProjectConfigs = configBlocks.filter((u) => u.configClass === ConfigClass.PROJECT);
3041
if (parsedProjectConfigs.length > 1) {
3142
throw new Error('One or zero project config can be specified');
3243
}
3344

34-
const projectConfig = parsedProjectConfigs[0] as unknown as ProjectConfig | undefined;
35-
return new Project(
36-
projectConfig ?? null,
37-
configBlocks.filter((u) => u.configClass !== ConfigClass.PROJECT) as ResourceConfig[],
38-
)
45+
return (parsedProjectConfigs[0] ?? null) as unknown as ProjectConfig | null;
3946
}
4047
}
File renamed without changes.

src/config-parser/parser/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { ConfigBlock } from '../../entities/index.js';
2+
import { File } from '../reader/entities/file.js';
3+
4+
export interface FileParser {
5+
parse(file: File): Promise<ConfigBlock[]>
6+
}

0 commit comments

Comments
 (0)