feat: improve typescript type assertion

This commit is contained in:
Garfield Lee 2023-04-17 06:54:45 +00:00
parent 47fbe2df0a
commit 828051d3b2
No known key found for this signature in database
GPG Key ID: EF60F5438ADC14D0
2 changed files with 70 additions and 28 deletions

33
dist/index.js vendored
View File

@ -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 (isErrorObject(error)) {
if (error.code === 'ENOENT') {
if (!required) {
return false;
}
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}`);
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}`);
}
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') {
if (isErrorObject(error)) {
if (error.code === 'ENOENT') {
return false;
}
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}`);
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}`);
}
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') {
if (isErrorObject(error)) {
if (error.code === 'ENOENT') {
return false;
}
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}`);
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}`);
}
if (!stats.isDirectory()) {
return true;

View File

@ -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,7 +15,8 @@ export function directoryExistsSync(path: string, required?: boolean): boolean {
try {
stats = fs.statSync(path)
} catch (error) {
if ((error as any)?.code === 'ENOENT') {
if (isErrorObject(error)) {
if (error.code === 'ENOENT') {
if (!required) {
return false
}
@ -17,9 +24,15 @@ export function directoryExistsSync(path: string, required?: boolean): boolean {
throw new Error(`Directory '${path}' does not exist`)
}
if (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.message}`
)
}
}
throw new 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') {
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 as any)
?.message ?? 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}`
)
}
@ -62,13 +82,20 @@ export function fileExistsSync(path: string): boolean {
try {
stats = fs.statSync(path)
} catch (error) {
if ((error as any)?.code === 'ENOENT') {
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 as any)
?.message ?? 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}`
)
}