filesystem-sandbox
Advanced tools
Comparing version 1.18.0 to 1.19.0
@@ -18,2 +18,3 @@ /// <reference types="node" /> | ||
writeFile(at: string, contents: string | Buffer | string[]): Promise<string>; | ||
appendFile(at: string, contents: string | Buffer | string[]): Promise<string>; | ||
mkdir(name: string): Promise<string>; | ||
@@ -20,0 +21,0 @@ private resolveRelativePath; |
@@ -36,3 +36,3 @@ "use strict"; | ||
const rimraf_1 = require("rimraf"); | ||
const { writeFile, readFile, stat } = fs_1.promises; | ||
const { writeFile, readFile, stat, appendFile } = fs_1.promises; | ||
async function isFolder(p) { | ||
@@ -71,2 +71,12 @@ try { | ||
const sandboxes = []; | ||
async function writeData(fullPath, contents, writeFunction) { | ||
const options = contents instanceof Buffer | ||
? undefined | ||
: { encoding: "utf8" }; | ||
if (Array.isArray(contents)) { | ||
contents = contents.join("\n"); | ||
} | ||
await writeFunction(fullPath, contents, options); | ||
return fullPath; | ||
} | ||
class Sandbox { | ||
@@ -111,18 +121,18 @@ constructor(at) { | ||
async writeFile(at, contents) { | ||
const providedAt = at; | ||
at = this.resolveRelativePath(at); | ||
const fullPath = this.fullPathFor(at), options = contents instanceof Buffer | ||
? undefined | ||
: { encoding: "utf8" }; | ||
if (Array.isArray(contents)) { | ||
contents = contents.join("\n"); | ||
try { | ||
await this.mkdir(path_1.default.dirname(at)); | ||
return await writeData(this.fullPathFor(at), contents, writeFile); | ||
} | ||
await this.mkdir(path_1.default.dirname(at)); | ||
catch (e) { | ||
throw new Error(`Unable to write file at ${at}: ${e.message || e}`); | ||
} | ||
} | ||
async appendFile(at, contents) { | ||
try { | ||
await writeFile(fullPath, contents, options); | ||
await this.mkdir(path_1.default.dirname(at)); | ||
return await writeData(this.fullPathFor(at), contents, appendFile); | ||
} | ||
catch (e) { | ||
throw new Error(`Unable to write file at: ${providedAt}: ${e.message || e}`); | ||
throw new Error(`Unable to append to file at ${at}: ${e.message || e}`); | ||
} | ||
return fullPath; | ||
} | ||
@@ -129,0 +139,0 @@ async mkdir(name) { |
{ | ||
"name": "filesystem-sandbox", | ||
"version": "1.18.0", | ||
"version": "1.19.0", | ||
"description": "JavaScript module to provide filesystem sandboxes for testing", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -0,0 +0,0 @@ filesystem-sandbox |
Sorry, the diff of this file is not supported yet
15777
291