/** * File manipulation functions. * * @module */ import type { OpenMode } from 'node:fs'; import type { LogStep } from '@onerepo/logger'; export { isSigned, verifySignature } from './signing'; export type { SigningStatus } from './signing'; /** * Generic options for file functions */ export type Options = { /** * Avoid creating a new step in output for each function. * Pass a Logger Step to pipe all logs and output to that instead. */ step?: LogStep; }; /** * Step-wrapped `fs.existsSync` implementation. * * ```ts * await file.exists('/path/to/file.ts'); * ``` */ export declare function exists(filename: string, options?: Options): Promise; /** * Sign the contents for a given file without writing out. This function is typically useful for manually comparing signed file contents. * * ```ts * const filename = graph.root.resolve('README.md'); * const currentContents = await file.read(filename); * const newContents = generateReadme(); * if (currentContents !== await signContents(filename, contents)) { * logger.error('Contents mismatch'); * } * ``` */ export declare function signContents(filename: string, contents: string, options?: Options): Promise; export type WriteOptions = { /** * Avoid creating a new step in output for each function. * Pass a Logger Step to pipe all logs and output to that instead. */ step?: LogStep; /** * Optionally sign the contents for future verification. */ sign?: boolean; }; /** * Write to a file. This will attempt use Prettier to format the contents based on the `filename` given. If Prettier does not understand the file’s extension, no changes will be made. * * If `--dry-run` or `process.env.ONEREPO_DRY_RUN` is true, no files will be modified. * * ```ts * await file.write('/path/to/out.md', '# hello!'); * ``` */ export declare function write(filename: string, contents: string, options?: WriteOptions): Promise; /** * Copy a file from one location to another. * * If `--dry-run` or `process.env.ONEREPO_DRY_RUN` is true, no files will be modified. * * ```ts * await file.copy('/path/to/in.md', '/path/to/out.md'); * ``` */ export declare function copy(input: string, output: string, options?: Options): Promise; /** * Step-wrapped `fs.lstat` implementation. See the [node.js fs.Stats documentation](https://nodejs.org/api/fs.html#class-fsstats) for more on how to use the return data. * * @returns If the `filename` does not exist, `null` will be returned instead of a Stats object. * * ```ts * const stat = await file.lstat('/path/to/file.md'); * if (stat.isDirectory()) { /* ... *\/ } * ``` */ export declare function lstat(filename: string, options?: Options): Promise; /** * Read the contents of a file. * * ```ts * const contents = await file.read('/path/to/file.md'); * ``` */ export declare function read(filename: string, flag?: OpenMode, options?: Options): Promise; export type ReadJsonOptions = { /** * Parse the file as JSONC (JSON with comments). */ jsonc?: boolean; } & Options; /** * Read and parse a JSON files. * * Compatible with jsonc by stripping comments before running `JSON.parse()`. Pass `jsonc: true` to the options to enable jsonc. * * ```ts * const contents = await file.readJson('/path/to/package.json'); * const strippedJsonc = await file.readJson('/path/to/tsconfig.json', 'r', { jsonc: true }); * ``` */ export declare function readJson>(filename: string, flag?: OpenMode, options?: ReadJsonOptions): Promise; /** * Recursively create a directory. * * ```ts * await file.mkdirp('/path/to/something'); * ``` */ export declare function mkdirp(pathname: string, options?: Options): Promise; /** * Remove files and folders at a given path. Equivalent to `rm -rf {pathname}` * * ```ts * await file.remove('/path/to/something'); * ``` */ export declare function remove(pathname: string, options?: Options): Promise; export type WriteSafeOptions = { /** * Avoid creating a new step in output for each function. * Pass a Logger Step to pipe all logs and output to that instead. */ step?: LogStep; /** * Unique string to use as a start and end sentinel for the contents */ sentinel?: string; /** * Optionally sign the contents for future verification. */ sign?: boolean; }; /** * Safely write contents to a file, wrapped in a start and end sentinel. This allows writing to a file without overwriting the current content of the file – other than that which falls between the start and end sentinel. * * Write to `/path/to/out.md` between a section denoted by the sentinel `'some-unique-string'` while leaving the rest of the file intact. * ```ts * await file.writeSafe('/path/to/out.md', '# hello', { sentinel: 'some-unique-string' }); * ``` * * Write to a section of the file as signed content for verifying later. * ```ts * await file.writeSafe('/path/to/out.md', '# hello', { signed: true }); * ``` */ export declare function writeSafe(filename: string, contents: string, options?: WriteSafeOptions): Promise; export type ReadSafeOptions = { /** * Avoid creating a new step in output for each function. * Pass a Logger Step to pipe all logs and output to that instead. */ step?: LogStep; /** * Unique string to use as a start and end sentinel for the contents */ sentinel?: string; }; /** * Read a sentinel-wrapped portion of a file that was previously written with {@link writeSafe} and return both the wrapped portion as well as the full contents of the file. * * ```ts * const [portion, fullContents] = await file.readSafe('/path/to/file.md', { sentinel: 'tacos' }); * ``` */ export declare function readSafe(filename: string, options?: ReadSafeOptions): Promise<[string | null, string]>; /** * Create a tmp directory in the os tmpdir. * * ```ts * const dir = await file.makeTempDir('tacos-'); * ``` */ export declare function makeTempDir(prefix: string, options?: Options): Promise; /** * Change file permissions * * ```ts * await file.chmod('/foo', 'a+x'); * ``` */ export declare function chmod(filename: string, mode: string | number, options?: Options): Promise;