From 828051d3b2dea8c1a68ce95cee2dfd64555373c0 Mon Sep 17 00:00:00 2001 From: Garfield Lee Date: Mon, 17 Apr 2023 06:54:45 +0000 Subject: [PATCH] feat: improve typescript type assertion --- dist/index.js | 43 +++++++++++++++++++++++++------------ src/fs-helper.ts | 55 ++++++++++++++++++++++++++++++++++++------------ 2 files changed, 70 insertions(+), 28 deletions(-) diff --git a/dist/index.js b/dist/index.js index 13a291a..8edd99c 100644 --- a/dist/index.js +++ b/dist/index.js @@ -28,8 +28,10 @@ var __importStar = (this && this.__importStar) || function (mod) { Object.defineProperty(exports, "__esModule", ({ value: true })); exports.fileExistsSync = exports.existsSync = exports.directoryExistsSync = void 0; const fs = __importStar(__nccwpck_require__(7147)); +function isErrorObject(error) { + return typeof error === 'object' && error !== null; +} function directoryExistsSync(path, required) { - var _a, _b, _c; if (!path) { throw new Error("Arg 'path' must not be empty"); } @@ -38,13 +40,18 @@ function directoryExistsSync(path, required) { stats = fs.statSync(path); } catch (error) { - if (((_a = error) === null || _a === void 0 ? void 0 : _a.code) === 'ENOENT') { - if (!required) { - return false; + if (isErrorObject(error)) { + if (error.code === 'ENOENT') { + if (!required) { + return false; + } + throw new Error(`Directory '${path}' does not exist`); + } + if (error.message) { + throw new Error(`Encountered an error when checking whether path '${path}' exists: ${error.message}`); } - throw new Error(`Directory '${path}' does not exist`); } - throw new Error(`Encountered an error when checking whether path '${path}' exists: ${(_c = (_b = error) === null || _b === void 0 ? void 0 : _b.message) !== null && _c !== void 0 ? _c : error}`); + throw new Error(`Encountered an error when checking whether path '${path}' exists: ${error}`); } if (stats.isDirectory()) { return true; @@ -56,7 +63,6 @@ function directoryExistsSync(path, required) { } exports.directoryExistsSync = directoryExistsSync; function existsSync(path) { - var _a, _b, _c; if (!path) { throw new Error("Arg 'path' must not be empty"); } @@ -64,16 +70,20 @@ function existsSync(path) { fs.statSync(path); } catch (error) { - if (((_a = error) === null || _a === void 0 ? void 0 : _a.code) === 'ENOENT') { - return false; + if (isErrorObject(error)) { + if (error.code === 'ENOENT') { + return false; + } + if (error.message) { + throw new Error(`Encountered an error when checking whether path '${path}' exists: ${error.message}`); + } } - throw new Error(`Encountered an error when checking whether path '${path}' exists: ${(_c = (_b = error) === null || _b === void 0 ? void 0 : _b.message) !== null && _c !== void 0 ? _c : error}`); + throw new Error(`Encountered an error when checking whether path '${path}' exists: ${error}`); } return true; } exports.existsSync = existsSync; function fileExistsSync(path) { - var _a, _b, _c; if (!path) { throw new Error("Arg 'path' must not be empty"); } @@ -82,10 +92,15 @@ function fileExistsSync(path) { stats = fs.statSync(path); } catch (error) { - if (((_a = error) === null || _a === void 0 ? void 0 : _a.code) === 'ENOENT') { - return false; + if (isErrorObject(error)) { + if (error.code === 'ENOENT') { + return false; + } + if (error.message) { + throw new Error(`Encountered an error when checking whether path '${path}' exists: ${error.message}`); + } } - throw new Error(`Encountered an error when checking whether path '${path}' exists: ${(_c = (_b = error) === null || _b === void 0 ? void 0 : _b.message) !== null && _c !== void 0 ? _c : error}`); + throw new Error(`Encountered an error when checking whether path '${path}' exists: ${error}`); } if (!stats.isDirectory()) { return true; diff --git a/src/fs-helper.ts b/src/fs-helper.ts index 3741d35..0e10ceb 100644 --- a/src/fs-helper.ts +++ b/src/fs-helper.ts @@ -1,5 +1,11 @@ import * as fs from 'fs' +function isErrorObject( + error: unknown +): error is {code?: string; message?: string} { + return typeof error === 'object' && error !== null +} + export function directoryExistsSync(path: string, required?: boolean): boolean { if (!path) { throw new Error("Arg 'path' must not be empty") @@ -9,17 +15,24 @@ export function directoryExistsSync(path: string, required?: boolean): boolean { try { stats = fs.statSync(path) } catch (error) { - if ((error as any)?.code === 'ENOENT') { - if (!required) { - return false + if (isErrorObject(error)) { + if (error.code === 'ENOENT') { + if (!required) { + return false + } + + throw new Error(`Directory '${path}' does not exist`) } - throw new Error(`Directory '${path}' does not exist`) + if (error.message) { + throw new Error( + `Encountered an error when checking whether path '${path}' exists: ${error.message}` + ) + } } throw new Error( - `Encountered an error when checking whether path '${path}' exists: ${(error as any) - ?.message ?? error}` + `Encountered an error when checking whether path '${path}' exists: ${error}` ) } @@ -40,13 +53,20 @@ export function existsSync(path: string): boolean { try { fs.statSync(path) } catch (error) { - if ((error as any)?.code === 'ENOENT') { - return false + if (isErrorObject(error)) { + if (error.code === 'ENOENT') { + return false + } + + if (error.message) { + throw new Error( + `Encountered an error when checking whether path '${path}' exists: ${error.message}` + ) + } } throw new Error( - `Encountered an error when checking whether path '${path}' exists: ${(error as any) - ?.message ?? error}` + `Encountered an error when checking whether path '${path}' exists: ${error}` ) } @@ -62,13 +82,20 @@ export function fileExistsSync(path: string): boolean { try { stats = fs.statSync(path) } catch (error) { - if ((error as any)?.code === 'ENOENT') { - return false + if (isErrorObject(error)) { + if (error.code === 'ENOENT') { + return false + } + + if (error.message) { + throw new Error( + `Encountered an error when checking whether path '${path}' exists: ${error.message}` + ) + } } throw new Error( - `Encountered an error when checking whether path '${path}' exists: ${(error as any) - ?.message ?? error}` + `Encountered an error when checking whether path '${path}' exists: ${error}` ) }