forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ensureDir/ensureFile for fs modules (denoland/std#264)
Original: denoland/std@9ae9413
- Loading branch information
Showing
5 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. | ||
/** | ||
* Ensures that the directory exists. If the directory structure does not exist, it is created. Like mkdir -p. | ||
* @export | ||
* @param {string} dir | ||
* @returns {Promise<void>} | ||
*/ | ||
export async function ensureDir(dir: string): Promise<void> { | ||
try { | ||
// if dir exists | ||
await Deno.stat(dir); | ||
} catch { | ||
// if dir not exists. then create it. | ||
await Deno.mkdir(dir, true); | ||
} | ||
} | ||
|
||
/** | ||
* Ensures that the directory exists. If the directory structure does not exist, it is created. Like mkdir -p. | ||
* @export | ||
* @param {string} dir | ||
* @returns {void} | ||
*/ | ||
export function ensureDirSync(dir: string): void { | ||
try { | ||
// if dir exists | ||
Deno.statSync(dir); | ||
} catch { | ||
// if dir not exists. then create it. | ||
Deno.mkdirSync(dir, true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. | ||
import { test } from "../testing/mod.ts"; | ||
import { assertThrows, assertThrowsAsync } from "../testing/asserts.ts"; | ||
import { ensureDir, ensureDirSync } from "./ensure_dir.ts"; | ||
import * as path from "./path/mod.ts"; | ||
|
||
const testdataDir = path.resolve("fs", "testdata"); | ||
|
||
test(async function ensureDirIfItNotExist() { | ||
const baseDir = path.join(testdataDir, "ensure_dir_not_exist"); | ||
const testDir = path.join(baseDir, "test"); | ||
|
||
await ensureDir(testDir); | ||
|
||
assertThrowsAsync(async () => { | ||
await Deno.stat(testDir).then(() => { | ||
throw new Error("test dir should exists."); | ||
}); | ||
}); | ||
|
||
await Deno.remove(baseDir, { recursive: true }); | ||
}); | ||
|
||
test(function ensureDirSyncIfItNotExist() { | ||
const baseDir = path.join(testdataDir, "ensure_dir_sync_not_exist"); | ||
const testDir = path.join(baseDir, "test"); | ||
|
||
ensureDirSync(testDir); | ||
|
||
assertThrows(() => { | ||
Deno.statSync(testDir); | ||
throw new Error("test dir should exists."); | ||
}); | ||
|
||
Deno.removeSync(baseDir, { recursive: true }); | ||
}); | ||
|
||
test(async function ensureDirIfItExist() { | ||
const baseDir = path.join(testdataDir, "ensure_dir_exist"); | ||
const testDir = path.join(baseDir, "test"); | ||
|
||
// create test directory | ||
await Deno.mkdir(testDir, true); | ||
|
||
await ensureDir(testDir); | ||
|
||
assertThrowsAsync(async () => { | ||
await Deno.stat(testDir).then(() => { | ||
throw new Error("test dir should still exists."); | ||
}); | ||
}); | ||
|
||
await Deno.remove(baseDir, { recursive: true }); | ||
}); | ||
|
||
test(function ensureDirSyncIfItExist() { | ||
const baseDir = path.join(testdataDir, "ensure_dir_sync_exist"); | ||
const testDir = path.join(baseDir, "test"); | ||
|
||
// create test directory | ||
Deno.mkdirSync(testDir, true); | ||
|
||
ensureDirSync(testDir); | ||
|
||
assertThrows(() => { | ||
Deno.statSync(testDir); | ||
throw new Error("test dir should still exists."); | ||
}); | ||
|
||
Deno.removeSync(baseDir, { recursive: true }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// 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"; | ||
/** | ||
* Ensures that the file exists. | ||
* If the file that is requested to be created is in directories that do not exist, these directories are created. If the file already exists, it is NOT MODIFIED. | ||
* @export | ||
* @param {string} filePath | ||
* @returns {Promise<void>} | ||
*/ | ||
export async function ensureFile(filePath: string): Promise<void> { | ||
try { | ||
// if file exists | ||
await Deno.stat(filePath); | ||
} catch { | ||
// if file not exists | ||
// ensure dir exists | ||
await ensureDir(path.dirname(filePath)); | ||
// create file | ||
await Deno.writeFile(filePath, new Uint8Array()); | ||
} | ||
} | ||
|
||
/** | ||
* Ensures that the file exists. | ||
* If the file that is requested to be created is in directories that do not exist, these directories are created. If the file already exists, it is NOT MODIFIED. | ||
* @export | ||
* @param {string} filePath | ||
* @returns {void} | ||
*/ | ||
export function ensureFileSync(filePath: string): void { | ||
try { | ||
// if file exists | ||
Deno.statSync(filePath); | ||
} catch { | ||
// if file not exists | ||
// ensure dir exists | ||
ensureDirSync(path.dirname(filePath)); | ||
// create file | ||
Deno.writeFileSync(filePath, new Uint8Array()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. | ||
import { test } from "../testing/mod.ts"; | ||
import { assertThrows, assertThrowsAsync } from "../testing/asserts.ts"; | ||
import { ensureFile, ensureFileSync } from "./ensure_file.ts"; | ||
import * as path from "./path/mod.ts"; | ||
|
||
const testdataDir = path.resolve("fs", "testdata"); | ||
|
||
test(async function ensureFileIfItNotExist() { | ||
const testDir = path.join(testdataDir, "ensure_file_1"); | ||
const testFile = path.join(testDir, "test.txt"); | ||
|
||
await ensureFile(testFile); | ||
|
||
assertThrowsAsync(async () => { | ||
await Deno.stat(testFile).then(() => { | ||
throw new Error("test file should exists."); | ||
}); | ||
}); | ||
|
||
await Deno.remove(testDir, { recursive: true }); | ||
}); | ||
|
||
test(function ensureFileSyncIfItNotExist() { | ||
const testDir = path.join(testdataDir, "ensure_file_2"); | ||
const testFile = path.join(testDir, "test.txt"); | ||
|
||
ensureFileSync(testFile); | ||
|
||
assertThrows(() => { | ||
Deno.statSync(testFile); | ||
throw new Error("test file should exists."); | ||
}); | ||
|
||
Deno.removeSync(testDir, { recursive: true }); | ||
}); | ||
|
||
test(async function ensureFileIfItExist() { | ||
const testDir = path.join(testdataDir, "ensure_file_3"); | ||
const testFile = path.join(testDir, "test.txt"); | ||
|
||
await Deno.mkdir(testDir, true); | ||
await Deno.writeFile(testFile, new Uint8Array()); | ||
|
||
await ensureFile(testFile); | ||
|
||
assertThrowsAsync(async () => { | ||
await Deno.stat(testFile).then(() => { | ||
throw new Error("test file should exists."); | ||
}); | ||
}); | ||
|
||
await Deno.remove(testDir, { recursive: true }); | ||
}); | ||
|
||
test(function ensureFileSyncIfItExist() { | ||
const testDir = path.join(testdataDir, "ensure_file_4"); | ||
const testFile = path.join(testDir, "test.txt"); | ||
|
||
Deno.mkdirSync(testDir, true); | ||
Deno.writeFileSync(testFile, new Uint8Array()); | ||
|
||
ensureFileSync(testFile); | ||
|
||
assertThrows(() => { | ||
Deno.statSync(testFile); | ||
throw new Error("test file should exists."); | ||
}); | ||
|
||
Deno.removeSync(testDir, { recursive: true }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters