Repository navigation
Expand file tree
/
Copy pathgovernance-file.cjs
More file actions
190 lines (171 loc) · 4.55 KB
/
Copy pathgovernance-file.cjs
File metadata and controls
190 lines (171 loc) · 4.55 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* GovernanceFile Parser (T004)
* Reads and parses governance files (YAML, Markdown, JSON)
* Returns structured representation with metadata and content
*/
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
class GovernanceFile {
constructor(filePath, content, metadata = {}) {
this.id = this._generateId(filePath);
this.path = filePath;
this.type = this._detectType(filePath);
this.format = this._detectFormat(filePath);
this.content = content;
this.locked = metadata.locked !== false; // Default to locked
this.metadata = metadata;
}
/**
* Generate unique identifier from file path
*/
_generateId(filePath) {
return path
.basename(filePath)
.replace(/\.[^/.]+$/, '')
.replace(/[^a-z0-9-]/gi, '-')
.toLowerCase();
}
/**
* Detect governance file type
*/
_detectType(filePath) {
const filename = path.basename(filePath);
if (filename === 'labels.yml' || filename === 'labels.yaml') {
return 'labels';
}
if (filename === 'issue-types.yml' || filename === 'issue-types.yaml') {
return 'issue-types';
}
if (filename.endsWith('.md') && filePath.includes('TEMPLATE')) {
return 'template';
}
const normalized = filePath.split(path.sep).join('/');
if (
/(^|\/)\.github\/workflows\//.test(normalized) &&
(filename.endsWith('.yml') || filename.endsWith('.yaml'))
) {
return 'workflow';
}
if (filename.endsWith('.json')) {
return 'json-config';
}
return 'unknown';
}
/**
* Detect file format
*/
_detectFormat(filePath) {
const ext = path.extname(filePath).toLowerCase();
if (ext === '.yml' || ext === '.yaml') return 'yaml';
if (ext === '.md' || ext === '.markdown') return 'markdown';
if (ext === '.json') return 'json';
return 'text';
}
/**
* Parse YAML content
*/
static parseYaml(content) {
try {
return yaml.load(content);
} catch (error) {
throw new Error(`YAML parse error: ${error.message}`, { cause: error });
}
}
/**
* Parse JSON content
*/
static parseJson(content) {
try {
return JSON.parse(content);
} catch (error) {
throw new Error(`JSON parse error: ${error.message}`, { cause: error });
}
}
/**
* Parse Markdown frontmatter
*/
static parseMarkdownFrontmatter(content) {
const frontmatterRegex = /^---\r?\n([\s\S]*?)(?:\r?\n)?---(?:\r?\n|$)([\s\S]*)$/;
const match = content.match(frontmatterRegex);
if (!match) {
return { metadata: {}, body: content };
}
try {
const loaded = yaml.load(match[1]);
const metadata = loaded && typeof loaded === 'object' && !Array.isArray(loaded) ? loaded : {};
return { metadata, body: match[2] };
} catch (error) {
throw new Error(`Frontmatter parse error: ${error.message}`, { cause: error });
}
}
/**
* Get parsed content based on format
*/
getParsedContent() {
switch (this.format) {
case 'yaml':
return GovernanceFile.parseYaml(this.content);
case 'json':
return GovernanceFile.parseJson(this.content);
case 'markdown': {
const { metadata, body } = GovernanceFile.parseMarkdownFrontmatter(this.content);
return { metadata, body };
}
default:
return { raw: this.content };
}
}
/**
* Serialize to JSON
*/
toJSON() {
return {
id: this.id,
path: this.path,
type: this.type,
format: this.format,
locked: this.locked,
metadata: this.metadata,
contentLength: this.content.length,
};
}
}
/**
* Load governance file from disk
*/
async function loadGovernanceFile(filePath, options = {}) {
try {
const resolvedPath = path.resolve(filePath);
const content = fs.readFileSync(resolvedPath, 'utf8');
return new GovernanceFile(filePath, content, {
locked: options.locked !== false,
...options.metadata,
});
} catch (error) {
throw new Error(`Failed to load governance file ${filePath}: ${error.message}`, {
cause: error,
});
}
}
/**
* Load multiple governance files
*/
async function loadGovernanceFiles(filePaths, options = {}) {
const files = [];
const errors = [];
for (const filePath of filePaths) {
try {
const file = await loadGovernanceFile(filePath, options);
files.push(file);
} catch (error) {
errors.push({ filePath, error: error.message });
}
}
return { files, errors };
}
module.exports = {
GovernanceFile,
loadGovernanceFile,
loadGovernanceFiles,
};