import { readFile, writeFile } from '../fs.ts' import { install } from '../pipe.ts' import type { Files } from '../files.ts' import type { AbsolutePath } from '../paths.ts' import type { PipeParameters, Plug } from '../pipe.ts' declare module '../index.ts' { export interface Pipe { /** Edits the content of all files in a pipeline. */ edit(callback: (content: string, fileName: AbsolutePath) => string | void | Promise): Pipe } } /* ========================================================================== * * INSTALLATION / IMPLEMENTATION * * ========================================================================== */ /** Edits the content of all files in a pipeline. */ install('edit', class Edit implements Plug { private readonly _callback: (content: string, fileName: AbsolutePath) => string | void | Promise constructor(...args: PipeParameters<'edit'>) { this._callback = args[0] } async pipe(files: Files): Promise { for (const file of files.absolutePaths()) { const data = await readFile(file, 'utf-8') const edited = await this._callback(data, file) if (edited !== undefined) await writeFile(file, edited, 'utf-8') } return files } })