oneRepo API: file
File manipulation functions.
Type Aliases
Section titled Type AliasesOptions
Section titled Optionstype Options = { step: LogStep;};Defined in: modules/file/src/index.ts
Generic options for file functions
Type declaration
Section titled Type declarationstep?
Section titled step?optional step: LogStep;Avoid creating a new step in output for each function. Pass a Logger Step to pipe all logs and output to that instead.
ReadJsonOptions
Section titled ReadJsonOptionstype ReadJsonOptions = { jsonc: boolean;} & Options;Defined in: modules/file/src/index.ts
Type declaration
Section titled Type declarationjsonc?
Section titled jsonc?optional jsonc: boolean;Parse the file as JSONC (JSON with comments).
ReadSafeOptions
Section titled ReadSafeOptionstype ReadSafeOptions = { sentinel: string; step: LogStep;};Defined in: modules/file/src/index.ts
Type declaration
Section titled Type declarationsentinel?
Section titled sentinel?optional sentinel: string;Unique string to use as a start and end sentinel for the contents
step?
Section titled step?optional step: LogStep;Avoid creating a new step in output for each function. Pass a Logger Step to pipe all logs and output to that instead.
SigningStatus
Section titled SigningStatustype SigningStatus = 'valid' | 'invalid' | 'unsigned';Defined in: modules/file/src/signing.ts
WriteOptions
Section titled WriteOptionstype WriteOptions = { sign: boolean; step: LogStep;};Defined in: modules/file/src/index.ts
Type declaration
Section titled Type declarationsign?
Section titled sign?optional sign: boolean;Optionally sign the contents for future verification.
step?
Section titled step?optional step: LogStep;Avoid creating a new step in output for each function. Pass a Logger Step to pipe all logs and output to that instead.
WriteSafeOptions
Section titled WriteSafeOptionstype WriteSafeOptions = { sentinel: string; sign: boolean; step: LogStep;};Defined in: modules/file/src/index.ts
Type declaration
Section titled Type declarationsentinel?
Section titled sentinel?optional sentinel: string;Unique string to use as a start and end sentinel for the contents
sign?
Section titled sign?optional sign: boolean;Optionally sign the contents for future verification.
step?
Section titled step?optional step: LogStep;Avoid creating a new step in output for each function. Pass a Logger Step to pipe all logs and output to that instead.
Functions
Section titled Functionschmod()
Section titled chmod()function chmod(filename, mode, options?): Promise<void>;Defined in: modules/file/src/index.ts
Change file permissions
await file.chmod('/foo', 'a+x');Parameters:
| Parameter | Type |
|---|---|
filename | string |
mode | string | number |
options? | Options |
Returns: Promise<void>
copy()
Section titled copy()function copy(input, output, options?): Promise<void>;Defined in: modules/file/src/index.ts
Copy a file from one location to another.
If --dry-run or process.env.ONEREPO_DRY_RUN is true, no files will be modified.
await file.copy('/path/to/in/', '/path/to/out/');Parameters:
| Parameter | Type |
|---|---|
input | string |
output | string |
options? | Options |
Returns: Promise<void>
exists()
Section titled exists()function exists(filename, options?): Promise<boolean>;Defined in: modules/file/src/index.ts
Step-wrapped fs.existsSync implementation.
await file.exists('/path/to/file.ts');Parameters:
| Parameter | Type |
|---|---|
filename | string |
options? | Options |
Returns: Promise<boolean>
isSigned()
Section titled isSigned()function isSigned(contents): boolean;Defined in: modules/file/src/signing.ts
Checks whether a file is signed without verifying the signature.
Parameters:
| Parameter | Type |
|---|---|
contents | string |
Returns: boolean
lstat()
Section titled lstat()function lstat(filename, options?): Promise<null | Stats>;Defined in: modules/file/src/index.ts
Step-wrapped fs.lstat implementation. See the node.js fs.Stats documentation for more on how to use the return data.
Parameters:
| Parameter | Type |
|---|---|
filename | string |
options? | Options |
Returns: Promise<null | Stats>
If the filename does not exist, null will be returned instead of a Stats object.
const stat = await file.lstat('/path/to/file/');if (stat.isDirectory()) { /* ... */}makeTempDir()
Section titled makeTempDir()function makeTempDir(prefix, options?): Promise<string>;Defined in: modules/file/src/index.ts
Create a tmp directory in the os tmpdir.
const dir = await file.makeTempDir('tacos-');Parameters:
| Parameter | Type |
|---|---|
prefix | string |
options? | Options |
Returns: Promise<string>
mkdirp()
Section titled mkdirp()function mkdirp(pathname, options?): Promise<void>;Defined in: modules/file/src/index.ts
Recursively create a directory.
await file.mkdirp('/path/to/something');Parameters:
| Parameter | Type |
|---|---|
pathname | string |
options? | Options |
Returns: Promise<void>
read()
Section titled read()function read(filename, flag?, options?): Promise<string>;Defined in: modules/file/src/index.ts
Read the contents of a file.
const contents = await file.read('/path/to/file/');Parameters:
| Parameter | Type |
|---|---|
filename | string |
flag? | OpenMode |
options? | Options |
Returns: Promise<string>
readJson()
Section titled readJson()function readJson<T>(filename, flag?, options?): Promise<T>;Defined in: modules/file/src/index.ts
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.
const contents = await file.readJson('/path/to/package.json');const strippedJsonc = await file.readJson('/path/to/tsconfig.json', 'r', { jsonc: true });Type Parameters
Section titled Type Parameters| Type Parameter |
|---|
T extends Record<string, unknown> |
Parameters:
| Parameter | Type |
|---|---|
filename | string |
flag? | OpenMode |
options? | ReadJsonOptions |
Returns: Promise<T>
readSafe()
Section titled readSafe()function readSafe(filename, options?): Promise<[null | string, string]>;Defined in: modules/file/src/index.ts
Read a sentinel-wrapped portion of a file that was previously written with writeSafe and return both the wrapped portion as well as the full contents of the file.
const [portion, fullContents] = await file.readSafe('/path/to/file/', { sentinel: 'tacos' });Parameters:
| Parameter | Type |
|---|---|
filename | string |
options? | ReadSafeOptions |
Returns: Promise<[null | string, string]>
remove()
Section titled remove()function remove(pathname, options?): Promise<void>;Defined in: modules/file/src/index.ts
Remove files and folders at a given path. Equivalent to rm -rf {pathname}
await file.remove('/path/to/something');Parameters:
| Parameter | Type |
|---|---|
pathname | string |
options? | Options |
Returns: Promise<void>
signContents()
Section titled signContents()function signContents(filename, contents, options?): Promise<any>;Defined in: modules/file/src/index.ts
Sign the contents for a given file without writing out. This function is typically useful for manually comparing signed file contents.
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');}Parameters:
| Parameter | Type |
|---|---|
filename | string |
contents | string |
options? | Options |
Returns: Promise<any>
verifySignature()
Section titled verifySignature()function verifySignature(contents): SigningStatus;Defined in: modules/file/src/signing.ts
Verify the signature in a signed file.
Parameters:
| Parameter | Type |
|---|---|
contents | string |
Returns: SigningStatus
write()
Section titled write()function write(filename, contents, options?): Promise<void>;Defined in: modules/file/src/index.ts
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.
await file.write('/path/to/out/', '# hello!');Parameters:
| Parameter | Type |
|---|---|
filename | string |
contents | string |
options? | WriteOptions |
Returns: Promise<void>
writeSafe()
Section titled writeSafe()function writeSafe(filename, contents, options?): Promise<void>;Defined in: modules/file/src/index.ts
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/ between a section denoted by the sentinel 'some-unique-string' while leaving the rest of the file intact.
await file.writeSafe('/path/to/out/', '# hello', { sentinel: 'some-unique-string' });Write to a section of the file as signed content for verifying later.
await file.writeSafe('/path/to/out/', '# hello', { signed: true });Parameters:
| Parameter | Type |
|---|---|
filename | string |
contents | string |
options? | WriteSafeOptions |
Returns: Promise<void>