From 758ad8de5c2766c32e341e4361c536c2357517d7 Mon Sep 17 00:00:00 2001 From: Rafa Mel Date: Fri, 26 Apr 2019 09:21:27 +0200 Subject: [PATCH] feat(exposed/exists): adds exists --- src/exposed/file/exists.ts | 17 +++++++++++++++++ src/exposed/file/index.ts | 1 + 2 files changed, 18 insertions(+) create mode 100644 src/exposed/file/exists.ts diff --git a/src/exposed/file/exists.ts b/src/exposed/file/exists.ts new file mode 100644 index 0000000..266cc52 --- /dev/null +++ b/src/exposed/file/exists.ts @@ -0,0 +1,17 @@ +import path from 'path'; +import core from '~/core'; +import { exists as _exists } from '~/utils/file'; + +/** + * Verifies `file` exists and throws if it doesn't. `file` can be either a file or a directory, and be relative to the project's directory. + * @returns A `TScript`, as a function, that won't be executed until called by `kpo` -hence, calling `exists` won't have any effect until the returned function is called. + */ +export default function exists(file: string) { + return async function exists(): Promise { + if (!path.isAbsolute(file)) { + const paths = await core.paths(); + file = path.join(paths.directory, file); + } + await _exists(file, { fail: true }); + }; +} diff --git a/src/exposed/file/index.ts b/src/exposed/file/index.ts index f5b9aab..5213fef 100644 --- a/src/exposed/file/index.ts +++ b/src/exposed/file/index.ts @@ -1 +1,2 @@ export { default as json } from './json'; +export { default as exists } from './exists';