@@ -3,7 +3,7 @@ import { appendFile, mkdir, readFile as readFileImpl, writeFile } from 'node:fs/
33import { handleFileSystemError } from './error/filesystem-error' ;
44import type { FileReaderResult } from '../types' ;
55import filenamify from 'filenamify' ;
6- import { appendFileSync , mkdirSync } from 'node:fs' ;
6+ import { appendFileSync , mkdirSync , writeFileSync } from 'node:fs' ;
77
88export interface FileOptions {
99 mode ?: number ;
@@ -39,6 +39,28 @@ export const saveToFile = async (filePath: string, data: string, options?: FileO
3939 }
4040} ;
4141
42+ export const saveToFileSync = ( filePath : string , data : string , options ?: FileOptions ) => {
43+ const resolvedPath = parse ( filePath ) . dir ;
44+
45+ // Only attempt to create a directory if there's a directory part
46+ if ( resolvedPath ) {
47+ try {
48+ mkdirSync ( resolvedPath , { recursive : true } ) ;
49+ }
50+ catch ( mkdirError ) {
51+ handleFileSystemError ( 'mkdir' , mkdirError as Error ) ;
52+ return ; // Exit early if the directory creation fails
53+ }
54+ }
55+
56+ try {
57+ writeFileSync ( filePath , data , options as any ) ;
58+ }
59+ catch ( writeError ) {
60+ handleFileSystemError ( 'write' , writeError as Error ) ;
61+ }
62+ } ;
63+
4264export const appendToFile = async ( filePath : string , data : string , options ?: FileOptions ) => {
4365 const resolvedPath = parse ( filePath ) . dir ;
4466
@@ -99,6 +121,23 @@ export const resolvePath = (path: string | undefined, folder: string) => {
99121 return resolve ( resolve ( process . cwd ( ) , '.storyblok' ) , folder ) ;
100122} ;
101123
124+ /**
125+ * Resolves the absolute path for a specific command directory.
126+ *
127+ * If a `space` is provided, it is appended to the `commandPath` before resolution.
128+ *
129+ * @param commandPath - The relative path or name of the command category.
130+ * @param space - (Optional).
131+ * @param baseDir - (Optional) The base directory to resolve against.
132+ * @returns The fully resolved absolute path string.
133+ */
134+ export function resolveCommandPath ( commandPath : string , space ?: string , baseDir ?: string ) {
135+ if ( space ) {
136+ return resolvePath ( baseDir , join ( commandPath , space ) ) ;
137+ }
138+ return resolvePath ( baseDir , commandPath ) ;
139+ }
140+
102141/**
103142 * Extracts the component name from a migration filename
104143 * @param filename - The migration filename (e.g., "simple_component.js")
@@ -138,10 +177,3 @@ export async function readJsonFile<T>(filePath: string): Promise<FileReaderResul
138177export function importModule ( filePath : string ) {
139178 return import ( `file://${ filePath } ` ) ;
140179}
141-
142- export function getLogsPath ( logFileDir : string , space ?: string , baseDir ?: string ) {
143- if ( space ) {
144- return resolvePath ( baseDir , join ( logFileDir , space ) ) ;
145- }
146- return resolvePath ( baseDir , logFileDir ) ;
147- }
0 commit comments