Skip to content

Commit

Permalink
adding in render to file
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredwray committed Jan 28, 2025
1 parent d743ad8 commit 45524a7
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,28 @@ export type resultData = {
}
```
For `beforeSaveToFile` the data object is an object with the `filePath` and `content`. Here is the interface for `saveToFileData`:
```typescript
export type saveToFileData = {
filePath: string;
content: string;
}
```
This is called when you call `saveToFile`, `saveToFileSync`.
For `beforeRenderToFile` the data object is an object with the `filePath` and `content`. Here is the interface for `renderToFileData`:
```typescript
export type renderToFileData = {
filePath: string;
content: string;
}
```
This is called when you call `renderToFile`, `renderToFileSync`.
# Code of Conduct and Contributing
[Code of Conduct](CODE_OF_CONDUCT.md) and [Contributing](CONTRIBUTING.md) guidelines.
Expand Down
17 changes: 15 additions & 2 deletions src/writr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export enum WritrHooks {
beforeRender = 'beforeRender',
afterRender = 'afterRender',
beforeSaveToFile = 'beforeSaveToFile',
beforeRenderToFile = 'beforeRenderToFile',
beforeLoadFromFile = 'beforeLoadFromFile',
afterLoadFromFile = 'afterLoadFromFile',
}
Expand Down Expand Up @@ -341,7 +342,12 @@ export class Writr extends Hookified {
const directoryPath = dirname(filePath);
const content = await this.render(options);
await mkdir(directoryPath, {recursive: true});
await writeFile(filePath, content, 'utf8');
const data = {
filePath,
content,
};
await this.hook(WritrHooks.beforeRenderToFile, data);
await writeFile(data.filePath, data.content);
/* c8 ignore next 6 */
} catch (error) {
this.emit('error', error);
Expand All @@ -361,7 +367,14 @@ export class Writr extends Hookified {
const directoryPath = dirname(filePath);
const content = this.renderSync(options);
fs.mkdirSync(directoryPath, {recursive: true});
fs.writeFileSync(filePath, content, 'utf8');
const data = {
filePath,
content,
};
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.hook(WritrHooks.beforeRenderToFile, data);

fs.writeFileSync(data.filePath, data.content);
/* c8 ignore next 6 */
} catch (error) {
this.emit('error', error);
Expand Down
30 changes: 30 additions & 0 deletions test/writr-hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,34 @@ describe('Writr Render Hooks', async () => {
// Cleanup
await fs.promises.rm(filePath);
});

test('it should change the content before render to file', async () => {
const filePath = './test-render-to-file.txt';
const writr = new Writr('Hello, World!');
writr.onHook(WritrHooks.beforeRenderToFile, data => {
data.content = 'Hello, File!';
});
await writr.renderToFile(filePath);
const fileContent = await fs.promises.readFile(filePath);

expect(fileContent.toString()).toContain('Hello, File!');

// Cleanup
await fs.promises.rm(filePath);
});

test('it should change the content before render to file sync', async () => {
const filePath = './test-render-to-file-sync.txt';
const writr = new Writr('Hello, World!');
writr.onHook(WritrHooks.beforeRenderToFile, data => {
data.content = 'Hello, File Sync!';
});
writr.renderToFileSync(filePath);
const fileContent = await fs.promises.readFile(filePath);

expect(fileContent.toString()).toContain('Hello, File Sync!');

// Cleanup
await fs.promises.rm(filePath);
});
});

0 comments on commit 45524a7

Please sign in to comment.