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

43
dist/index.js vendored
View File

@ -28,8 +28,10 @@ var __importStar = (this && this.__importStar) || function (mod) {
Object.defineProperty(exports, "__esModule", ({ value: true })); Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.fileExistsSync = exports.existsSync = exports.directoryExistsSync = void 0; exports.fileExistsSync = exports.existsSync = exports.directoryExistsSync = void 0;
const fs = __importStar(__nccwpck_require__(7147)); const fs = __importStar(__nccwpck_require__(7147));
function isErrorObject(error) {
return typeof error === 'object' && error !== null;
}
function directoryExistsSync(path, required) { function directoryExistsSync(path, required) {
var _a, _b, _c;
if (!path) { if (!path) {
throw new Error("Arg 'path' must not be empty"); throw new Error("Arg 'path' must not be empty");
} }
@ -38,13 +40,18 @@ function directoryExistsSync(path, required) {
stats = fs.statSync(path); stats = fs.statSync(path);
} }
catch (error) { catch (error) {
if (((_a = error) === null || _a === void 0 ? void 0 : _a.code) === 'ENOENT') { if (isErrorObject(error)) {
if (!required) { if (error.code === 'ENOENT') {
return false; 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()) { if (stats.isDirectory()) {
return true; return true;
@ -56,7 +63,6 @@ function directoryExistsSync(path, required) {
} }
exports.directoryExistsSync = directoryExistsSync; exports.directoryExistsSync = directoryExistsSync;
function existsSync(path) { function existsSync(path) {
var _a, _b, _c;
if (!path) { if (!path) {
throw new Error("Arg 'path' must not be empty"); throw new Error("Arg 'path' must not be empty");
} }
@ -64,16 +70,20 @@ function existsSync(path) {
fs.statSync(path); fs.statSync(path);
} }
catch (error) { catch (error) {
if (((_a = error) === null || _a === void 0 ? void 0 : _a.code) === 'ENOENT') { if (isErrorObject(error)) {
return false; 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; return true;
} }
exports.existsSync = existsSync; exports.existsSync = existsSync;
function fileExistsSync(path) { function fileExistsSync(path) {
var _a, _b, _c;
if (!path) { if (!path) {
throw new Error("Arg 'path' must not be empty"); throw new Error("Arg 'path' must not be empty");
} }
@ -82,10 +92,15 @@ function fileExistsSync(path) {
stats = fs.statSync(path); stats = fs.statSync(path);
} }
catch (error) { catch (error) {
if (((_a = error) === null || _a === void 0 ? void 0 : _a.code) === 'ENOENT') { if (isErrorObject(error)) {
return false; 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()) { if (!stats.isDirectory()) {
return true; return true;

View File

@ -1,5 +1,11 @@
import * as fs from 'fs' 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 { export function directoryExistsSync(path: string, required?: boolean): boolean {
if (!path) { if (!path) {
throw new Error("Arg 'path' must not be empty") throw new Error("Arg 'path' must not be empty")
@ -9,17 +15,24 @@ export function directoryExistsSync(path: string, required?: boolean): boolean {
try { try {
stats = fs.statSync(path) stats = fs.statSync(path)
} catch (error) { } catch (error) {
if ((error as any)?.code === 'ENOENT') { if (isErrorObject(error)) {
if (!required) { if (error.code === 'ENOENT') {
return false 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( throw new Error(
`Encountered an error when checking whether path '${path}' exists: ${(error as any) `Encountered an error when checking whether path '${path}' exists: ${error}`
?.message ?? error}`
) )
} }
@ -40,13 +53,20 @@ export function existsSync(path: string): boolean {
try { try {
fs.statSync(path) fs.statSync(path)
} catch (error) { } catch (error) {
if ((error as any)?.code === 'ENOENT') { if (isErrorObject(error)) {
return false 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( throw new Error(
`Encountered an error when checking whether path '${path}' exists: ${(error as any) `Encountered an error when checking whether path '${path}' exists: ${error}`
?.message ?? error}`
) )
} }
@ -62,13 +82,20 @@ export function fileExistsSync(path: string): boolean {
try { try {
stats = fs.statSync(path) stats = fs.statSync(path)
} catch (error) { } catch (error) {
if ((error as any)?.code === 'ENOENT') { if (isErrorObject(error)) {
return false 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( throw new Error(
`Encountered an error when checking whether path '${path}' exists: ${(error as any) `Encountered an error when checking whether path '${path}' exists: ${error}`
?.message ?? error}`
) )
} }