Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add readFile #66

Merged
merged 2 commits into from
Feb 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/api/inline-fixture-files.createiffresult.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface CreateIFFResult<T extends Directory>
| [addFixtures(additionalDirectory)](./inline-fixture-files.createiffresult.addfixtures.md) | Add fixtures to the fixture root directory. |
| [fork(additionalDirectory, forkOptions)](./inline-fixture-files.createiffresult.fork.md) | Change the root directory and take over the fixture you created. |
| [join(paths)](./inline-fixture-files.createiffresult.join.md) | Join <code>rootDir</code> and <code>paths</code>. It is equivalent to <code>require('path').join(rootDir, ...paths)</code>. |
| [readFile(path)](./inline-fixture-files.createiffresult.readfile.md) | Read the file. |
| [reset()](./inline-fixture-files.createiffresult.reset.md) | Delete the fixture root directory and write the fixtures specified in <code>directory</code> argument again. |
| [rmFixtures()](./inline-fixture-files.createiffresult.rmfixtures.md) | Delete files under the fixture root directory. |
| [rmRootDir()](./inline-fixture-files.createiffresult.rmrootdir.md) | Delete the fixture root directory. |
Expand Down
26 changes: 26 additions & 0 deletions docs/api/inline-fixture-files.createiffresult.readfile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@mizdra/inline-fixture-files](./inline-fixture-files.md) &gt; [CreateIFFResult](./inline-fixture-files.createiffresult.md) &gt; [readFile](./inline-fixture-files.createiffresult.readfile.md)

## CreateIFFResult.readFile() method

Read the file.

**Signature:**

```typescript
readFile(path: string): Promise<string>;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| path | string | The path to the file. |

**Returns:**

Promise&lt;string&gt;

The content of the file.

4 changes: 4 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ describe('CreateIFFResult', () => {
expect(iff.join('a.txt')).toBe(slash(join(fixtureDir, 'a.txt')));
});
});
test('readFile', async () => {
const iff = await createIFF({ 'a.txt': 'a' });
expect(await iff.readFile('a.txt')).toMatchInlineSnapshot('"a"');
});
test('rmFixtures', async () => {
const iff = await createIFF({
'a.txt': 'a',
Expand Down
11 changes: 10 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* The utility for writing fixture files inline.
*/

import { constants, cp, readdir, rm } from 'node:fs/promises';
import { constants, cp, readdir, readFile, rm } from 'node:fs/promises';
import { join } from 'node:path';
import type { Directory, MergeDirectory } from './create-iff-impl.js';
import { createIFFImpl } from './create-iff-impl.js';
Expand Down Expand Up @@ -135,6 +135,12 @@ export interface CreateIFFResult<T extends Directory> {
* ```
*/
join(...paths: string[]): string;
/**
* Read the file.
* @param path - The path to the file.
* @returns The content of the file.
*/
readFile(path: string): Promise<string>;
/**
* Delete the fixture root directory.
*/
Expand Down Expand Up @@ -263,6 +269,9 @@ export function defineIFFCreator(defineIFFCreatorOptions: DefineIFFCreatorOption
join(...paths) {
return unixStylePath ? slash(join(rootDir, ...paths)) : join(rootDir, ...paths);
},
readFile: async (path) => {
return await readFile(join(rootDir, path), 'utf-8');
},
async rmRootDir() {
await rm(rootDir, { recursive: true, force: true });
},
Expand Down