MCPcopy
hub / github.com/Fission-AI/OpenSpec / readChangeMetadata

Function readChangeMetadata

src/utils/change-metadata.ts:94–147  ·  view source on GitHub ↗
(
  changeDir: string,
  projectRoot?: string
)

Source from the content-addressed store, hash-verified

92 * @throws ChangeMetadataError if the file exists but is invalid
93 */
94export function readChangeMetadata(
95 changeDir: string,
96 projectRoot?: string
97): ChangeMetadata | null {
98 const metaPath = path.join(changeDir, METADATA_FILENAME);
99
100 if (!fs.existsSync(metaPath)) {
101 return null;
102 }
103
104 let content: string;
105 try {
106 content = fs.readFileSync(metaPath, 'utf-8');
107 } catch (err) {
108 const ioError = err instanceof Error ? err : new Error(String(err));
109 throw new ChangeMetadataError(
110 `Failed to read metadata: ${ioError.message}`,
111 metaPath,
112 ioError
113 );
114 }
115
116 let parsed: unknown;
117 try {
118 parsed = yaml.parse(content);
119 } catch (err) {
120 const parseError = err instanceof Error ? err : new Error(String(err));
121 throw new ChangeMetadataError(
122 `Invalid YAML in metadata file: ${parseError.message}`,
123 metaPath,
124 parseError
125 );
126 }
127
128 // Validate with Zod
129 const parseResult = ChangeMetadataSchema.safeParse(parsed);
130 if (!parseResult.success) {
131 throw new ChangeMetadataError(
132 `Invalid metadata: ${parseResult.error.message}`,
133 metaPath
134 );
135 }
136
137 // Validate that the schema exists
138 const availableSchemas = listSchemas(projectRoot);
139 if (!availableSchemas.includes(parseResult.data.schema)) {
140 throw new ChangeMetadataError(
141 `Unknown schema '${parseResult.data.schema}'. Available: ${availableSchemas.join(', ')}`,
142 metaPath
143 );
144 }
145
146 return parseResult.data;
147}
148
149export interface ResolveSchemaForChangeOptions {
150 metadata?: ChangeMetadata | null;

Callers 4

resolveSchemaForChangeFunction · 0.85
loadChangeContextFunction · 0.85

Calls 1

listSchemasFunction · 0.85

Tested by

no test coverage detected