summaryrefslogtreecommitdiff
path: root/fs/ensure_file.ts
diff options
context:
space:
mode:
Diffstat (limited to 'fs/ensure_file.ts')
-rw-r--r--fs/ensure_file.ts29
1 files changed, 25 insertions, 4 deletions
diff --git a/fs/ensure_file.ts b/fs/ensure_file.ts
index 56632f150..6fe8e7822 100644
--- a/fs/ensure_file.ts
+++ b/fs/ensure_file.ts
@@ -1,6 +1,7 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import * as path from "./path/mod.ts";
import { ensureDir, ensureDirSync } from "./ensure_dir.ts";
+import { getFileInfoType } from "./utils.ts";
/**
* Ensures that the file exists.
@@ -8,10 +9,20 @@ import { ensureDir, ensureDirSync } from "./ensure_dir.ts";
* these directories are created. If the file already exists, it is NOT MODIFIED.
*/
export async function ensureFile(filePath: string): Promise<void> {
+ let pathExists = false;
try {
// if file exists
- await Deno.stat(filePath);
- } catch {
+ const stat = await Deno.lstat(filePath);
+ pathExists = true;
+ if (!stat.isFile()) {
+ throw new Error(
+ `Ensure path exists, expected 'file', got '${getFileInfoType(stat)}'`
+ );
+ }
+ } catch (err) {
+ if (pathExists) {
+ throw err;
+ }
// if file not exists
// ensure dir exists
await ensureDir(path.dirname(filePath));
@@ -26,10 +37,20 @@ export async function ensureFile(filePath: string): Promise<void> {
* these directories are created. If the file already exists, it is NOT MODIFIED.
*/
export function ensureFileSync(filePath: string): void {
+ let pathExists = false;
try {
// if file exists
- Deno.statSync(filePath);
- } catch {
+ const stat = Deno.statSync(filePath);
+ pathExists = true;
+ if (!stat.isFile()) {
+ throw new Error(
+ `Ensure path exists, expected 'file', got '${getFileInfoType(stat)}'`
+ );
+ }
+ } catch (err) {
+ if (pathExists) {
+ throw err;
+ }
// if file not exists
// ensure dir exists
ensureDirSync(path.dirname(filePath));