From 25dfe9fa86979a7720e463ca8e94cc4a62c561ec Mon Sep 17 00:00:00 2001 From: Michael Orenstein Date: Mon, 24 Jul 2023 13:34:52 +0930 Subject: [PATCH] chore: Use fs promises for filesystem APIs --- packages/storycap/src/node/file.ts | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/packages/storycap/src/node/file.ts b/packages/storycap/src/node/file.ts index 7af1e3217..a0893c2e7 100644 --- a/packages/storycap/src/node/file.ts +++ b/packages/storycap/src/node/file.ts @@ -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'; @@ -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 @@ -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((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; } }