Add utility files: spec-parser, file-writer, helpers, wizard
This commit is contained in:
51
src/utils/file-writer.ts
Normal file
51
src/utils/file-writer.ts
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
import * as fs from 'fs';
|
||||||
|
import * as path from 'path';
|
||||||
|
|
||||||
|
export interface WriteOptions {
|
||||||
|
outputDir: string;
|
||||||
|
fileName: string;
|
||||||
|
content: string;
|
||||||
|
overwrite?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface WriteResult {
|
||||||
|
success: boolean;
|
||||||
|
filePath?: string;
|
||||||
|
error?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function writeFile(options: WriteOptions): WriteResult {
|
||||||
|
const { outputDir, fileName, content, overwrite = false } = options;
|
||||||
|
|
||||||
|
if (!fs.existsSync(outputDir)) {
|
||||||
|
fs.mkdirSync(outputDir, { recursive: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
const filePath = path.join(outputDir, fileName);
|
||||||
|
|
||||||
|
if (fs.existsSync(filePath) && !overwrite) {
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
error: `File already exists: ${filePath}`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
fs.writeFileSync(filePath, content, 'utf-8');
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
filePath,
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
error: `Failed to write file: ${error instanceof Error ? error.message : 'Unknown error'}`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function ensureDirectory(dirPath: string): void {
|
||||||
|
if (!fs.existsSync(dirPath)) {
|
||||||
|
fs.mkdirSync(dirPath, { recursive: true });
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user