@@ -1,5 +1,5 @@
|
||||
import { readdir, readFile, rm, stat, writeFile } from 'node:fs/promises'
|
||||
import { join, relative, sep } from 'node:path'
|
||||
import { mkdir, readdir, readFile, rm, stat, writeFile } from 'node:fs/promises'
|
||||
import { dirname, join, relative, sep } from 'node:path'
|
||||
import { listFiles } from './search-service'
|
||||
|
||||
export interface FileNode {
|
||||
@@ -120,6 +120,20 @@ export async function writeProjectFile(root: string, rel: string, content: strin
|
||||
await writeFile(join(root, rel), content, 'utf8')
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new, empty text file (relative path). Creates parent folders as
|
||||
* needed, refuses to escape the project root, and throws if the file already
|
||||
* exists so an accidental name collision never clobbers existing content.
|
||||
*/
|
||||
export async function createProjectFile(root: string, rel: string): Promise<void> {
|
||||
const target = join(root, rel)
|
||||
if (relative(root, target).startsWith('..')) throw new Error('outside project root')
|
||||
const existing = await stat(target).catch(() => null)
|
||||
if (existing) throw new Error('file already exists')
|
||||
await mkdir(dirname(target), { recursive: true })
|
||||
await writeFile(target, '', { encoding: 'utf8', flag: 'wx' })
|
||||
}
|
||||
|
||||
/** Delete a project file or folder (relative path). Stays inside the project root. */
|
||||
export async function deleteProjectFile(root: string, rel: string): Promise<void> {
|
||||
const target = join(root, rel)
|
||||
|
||||
Reference in New Issue
Block a user