Files
sausage/extensions/models/files.ts
T

12 lines
633 B
TypeScript

/** World paths and optional files shared by the core extensions. */
export async function optional(fs: any, path: string): Promise<string | undefined> {
try { return await fs.read(path) } catch (error) { if (error.code === 'not_found') return undefined; throw error }
}
export const parent = (path: string) => path.replace(/\/+$/, '').replace(/\/[^/]+$/, '') || '/'
export async function repoRoot(fs: any, cwd: string): Promise<string> {
for (let at = cwd; ; at = parent(at)) {
try { await fs.stat(`${at}/.git`); return at } catch (error) { if (error.code !== 'not_found') throw error }
if (at === '/') return cwd
}
}