forked from finos/architecture-as-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate-bundle-file-loader.ts
More file actions
159 lines (124 loc) · 4.98 KB
/
template-bundle-file-loader.ts
File metadata and controls
159 lines (124 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import fs from 'fs';
import path from 'path';
import { IndexFile, TemplateEntry } from './types.js';
import { initLogger, Logger } from '../logger.js';
export interface ITemplateBundleLoader {
getConfig(): IndexFile;
getTemplateFiles(): Record<string, string>;
}
export class SelfProvidedTemplateLoader implements ITemplateBundleLoader {
private readonly config: IndexFile;
private readonly templateFiles: Record<string, string>;
constructor(templatePath: string, outputPath: string) {
const templateName = path.basename(templatePath);
const isDir = !path.extname(outputPath) || (
fs.existsSync(outputPath) && fs.statSync(outputPath).isDirectory()
);
const outputFile = isDir
? 'output.md'
: path.basename(outputPath);
this.templateFiles = {
[templateName]: fs.readFileSync(templatePath, 'utf8')
};
this.config = {
name: 'Self Provided Template',
templates: [{
template: templateName,
from: 'document',
output: outputFile,
'output-type': 'single'
}]
};
}
getConfig(): IndexFile {
return this.config;
}
getTemplateFiles(): Record<string, string> {
return this.templateFiles;
}
}
export class SelfProvidedDirectoryTemplateLoader implements ITemplateBundleLoader {
private readonly config: IndexFile;
private readonly templateFiles: Record<string, string> = {};
constructor(templateDir: string) {
const entries: TemplateEntry[] = [];
const allFiles = fs.readdirSync(templateDir);
for (const file of allFiles) {
this.templateFiles[file] = fs.readFileSync(path.join(templateDir, file), 'utf8');
entries.push({
template: file,
from: 'document',
output: file, // output file = template file
'output-type': 'single'
});
}
this.config = {
name: 'Self Provided Template Directory',
templates: entries
};
}
getConfig(): IndexFile {
return this.config;
}
getTemplateFiles(): Record<string, string> {
return this.templateFiles;
}
}
export class TemplateBundleFileLoader implements ITemplateBundleLoader {
private readonly templateBundlePath: string;
private readonly config: IndexFile;
private readonly templateFiles: Record<string, string>;
private static _logger: Logger | undefined;
private static get logger(): Logger {
if (!this._logger) {
this._logger = initLogger(process.env.DEBUG === 'true', TemplateBundleFileLoader.name);
}
return this._logger;
}
constructor(templateBundlePath: string) {
this.templateBundlePath = templateBundlePath;
this.config = this.loadConfig();
this.templateFiles = this.loadTemplateFiles();
}
private loadConfig(): IndexFile {
const logger = TemplateBundleFileLoader.logger;
const indexFilePath = path.join(this.templateBundlePath, 'index.json');
if (!fs.existsSync(indexFilePath)) {
logger.error(`❌ index.json not found: ${indexFilePath}`);
throw new Error(`index.json not found in template bundle: ${indexFilePath}`);
}
try {
logger.info(`📥 Loading index.json from ${indexFilePath}`);
const rawConfig = JSON.parse(fs.readFileSync(indexFilePath, 'utf8'));
if (!rawConfig.name || !Array.isArray(rawConfig.templates)) {
logger.error('❌ Invalid index.json format: Missing required fields');
throw new Error('Invalid index.json format: Missing required fields');
}
logger.info(`✅ Successfully loaded template bundle: ${rawConfig.name}`);
return rawConfig as IndexFile;
} catch (error) {
logger.error(`❌ Error reading index.json: ${error.message}`);
throw new Error(`Failed to parse index.json: ${error.message}`);
}
}
private loadTemplateFiles(): Record<string, string> {
const logger = TemplateBundleFileLoader.logger;
const templates: Record<string, string> = {};
const templateDir = this.templateBundlePath;
logger.info(`📂 Loading template files from: ${templateDir}`);
const templateFiles = fs.readdirSync(templateDir).filter(file => file.includes('.'));
for (const file of templateFiles) {
const filePath = path.join(templateDir, file);
templates[file] = fs.readFileSync(filePath, 'utf8');
logger.debug(`✅ Loaded template file: ${file}`);
}
logger.info(`🎯 Total Templates Loaded: ${Object.keys(templates).length}`);
return templates;
}
public getConfig(): IndexFile {
return this.config;
}
public getTemplateFiles(): Record<string, string> {
return this.templateFiles;
}
}