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

chore: Use fs promises for filesystem APIs #789

Merged
merged 1 commit into from
Jul 24, 2023
Merged
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
17 changes: 7 additions & 10 deletions packages/storycap/src/node/file.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fs from 'fs';
import fs from 'fs/promises';
import path from 'path';
import * as mkdirp from 'mkdirp';
import { MainOptions } from './types';
import sanitize from 'sanitize-filename';

Expand All @@ -18,7 +17,7 @@ export class FileSystem {
* @returns Absolute file path
*
**/
save(kind: string, story: string, suffix: string[], buffer: Buffer) {
async save(kind: string, story: string, suffix: string[], buffer: Buffer) {
const name = this.opt.flat
? sanitize((kind + '_' + story).replace(/\//g, '_'))
: kind
Expand All @@ -28,12 +27,10 @@ export class FileSystem {
'/' +
sanitize(story);
const filePath = path.join(this.opt.outDir, name + (suffix.length ? `_${suffix.join('_')}` : '') + '.png');
return new Promise<string>((resolve, reject) => {
mkdirp.sync(path.dirname(filePath));
fs.writeFile(filePath, buffer, err => {
if (err) reject(err);
resolve(filePath);
});
});

await fs.mkdir(path.dirname(filePath), { recursive: true });
await fs.writeFile(filePath, buffer);

return filePath;
}
}