filesystem-sandbox 1.19.0 → 1.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/filesystem-sandbox.d.ts +44 -0
- package/dist/filesystem-sandbox.js +247 -0
- package/dist/index.d.ts +2 -44
- package/dist/index.js +5 -237
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +65 -61
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { StatsBase } from "fs";
|
|
3
|
+
export declare type Func<T> = () => T;
|
|
4
|
+
export declare type AsyncFunc<T> = () => Promise<T>;
|
|
5
|
+
export declare const basePrefix = "__sandboxes__";
|
|
6
|
+
export declare class Sandbox {
|
|
7
|
+
private readonly _path;
|
|
8
|
+
private readonly _base;
|
|
9
|
+
private readonly _at;
|
|
10
|
+
get path(): string;
|
|
11
|
+
constructor(at?: string);
|
|
12
|
+
destroy(): Promise<void>;
|
|
13
|
+
private tryDestroyAt;
|
|
14
|
+
private tryDestroyContainer;
|
|
15
|
+
private destroyFolderIfEmpty;
|
|
16
|
+
private tryDestroySelf;
|
|
17
|
+
writeFile(at: string, contents: string | Buffer | string[]): Promise<string>;
|
|
18
|
+
appendFile(at: string, contents: string | Buffer | string[]): Promise<string>;
|
|
19
|
+
mkdir(name: string): Promise<string>;
|
|
20
|
+
private resolveRelativePath;
|
|
21
|
+
readTextFile(at: string): Promise<string>;
|
|
22
|
+
readFile(at: string): Promise<Buffer>;
|
|
23
|
+
fullPathFor(relativePath: string, ...parts: string[]): string;
|
|
24
|
+
stat(at: string): Promise<StatsBase<any> | null>;
|
|
25
|
+
folderExists(at: string): Promise<boolean>;
|
|
26
|
+
fileExists(at: string): Promise<boolean>;
|
|
27
|
+
private runOnStat;
|
|
28
|
+
run<T>(fn: Func<T> | AsyncFunc<T>, relativePath?: string): Promise<T>;
|
|
29
|
+
private _validatePathInsideSandbox;
|
|
30
|
+
/**
|
|
31
|
+
* Destroys all sandboxes created in this process up until now
|
|
32
|
+
*/
|
|
33
|
+
static destroyAll(): Promise<void>;
|
|
34
|
+
/**
|
|
35
|
+
* Destroys the default base folder for sandboxes -- useful as a once-off
|
|
36
|
+
* run before all tests to ensure that there are no lingering sandboxes
|
|
37
|
+
* - cannot destroy sandboxes in custom paths
|
|
38
|
+
*/
|
|
39
|
+
static destroyAny(): Promise<void>;
|
|
40
|
+
static create(at?: string): Promise<Sandbox>;
|
|
41
|
+
static setBaseTargetToCurrentWorkingDirectory(): Promise<void>;
|
|
42
|
+
static setBaseTargetToSystemTempFolder(): Promise<void>;
|
|
43
|
+
static setBaseTarget(to: string): Promise<void>;
|
|
44
|
+
}
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
22
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
23
|
+
};
|
|
24
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
+
exports.Sandbox = exports.basePrefix = void 0;
|
|
26
|
+
const fs_1 = require("fs");
|
|
27
|
+
const os = __importStar(require("os"));
|
|
28
|
+
if (!fs_1.promises) {
|
|
29
|
+
throw new Error("Yer node is olde! filesystem-sandboxes requires a Node with fs.promises");
|
|
30
|
+
}
|
|
31
|
+
// require for "path" produces the cleanest js output
|
|
32
|
+
const path_1 = __importDefault(require("path"));
|
|
33
|
+
const uuid_1 = require("uuid");
|
|
34
|
+
const mkdirp_1 = require("mkdirp");
|
|
35
|
+
const rimraf_1 = require("rimraf");
|
|
36
|
+
const { writeFile, readFile, stat, appendFile } = fs_1.promises;
|
|
37
|
+
async function isFolder(p) {
|
|
38
|
+
try {
|
|
39
|
+
const st = await stat(p);
|
|
40
|
+
return st && st.isDirectory();
|
|
41
|
+
}
|
|
42
|
+
catch (e) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
async function readdir(p) {
|
|
47
|
+
const exists = await isFolder(p);
|
|
48
|
+
if (!exists) {
|
|
49
|
+
return [];
|
|
50
|
+
}
|
|
51
|
+
return await fs_1.promises.readdir(p);
|
|
52
|
+
}
|
|
53
|
+
async function rimraf(p) {
|
|
54
|
+
if (!(await isFolder(p))) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
return new Promise((resolve, reject) => {
|
|
58
|
+
try {
|
|
59
|
+
rimraf_1.sync(p, { maxBusyTries: 5 });
|
|
60
|
+
resolve();
|
|
61
|
+
}
|
|
62
|
+
catch (e) {
|
|
63
|
+
reject(e);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
exports.basePrefix = "__sandboxes__";
|
|
68
|
+
let baseTarget = os.tmpdir();
|
|
69
|
+
const sandboxes = [];
|
|
70
|
+
async function writeData(fullPath, contents, writeFunction) {
|
|
71
|
+
const options = contents instanceof Buffer
|
|
72
|
+
? undefined
|
|
73
|
+
: { encoding: "utf8" };
|
|
74
|
+
if (Array.isArray(contents)) {
|
|
75
|
+
contents = contents.join("\n");
|
|
76
|
+
}
|
|
77
|
+
await writeFunction(fullPath, contents, options);
|
|
78
|
+
return fullPath;
|
|
79
|
+
}
|
|
80
|
+
class Sandbox {
|
|
81
|
+
constructor(at) {
|
|
82
|
+
this._at = at;
|
|
83
|
+
this._base = path_1.default.join(at || baseTarget, exports.basePrefix);
|
|
84
|
+
this._path = path_1.default.join(this._base, uuid_1.v4());
|
|
85
|
+
mkdirp_1.sync(this._path);
|
|
86
|
+
sandboxes.push(this);
|
|
87
|
+
}
|
|
88
|
+
get path() {
|
|
89
|
+
return this._path;
|
|
90
|
+
}
|
|
91
|
+
async destroy() {
|
|
92
|
+
await this.tryDestroySelf();
|
|
93
|
+
await this.tryDestroyContainer();
|
|
94
|
+
await this.tryDestroyAt();
|
|
95
|
+
}
|
|
96
|
+
async tryDestroyAt() {
|
|
97
|
+
if (!this._at) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
await this.destroyFolderIfEmpty(this._at);
|
|
101
|
+
}
|
|
102
|
+
async tryDestroyContainer() {
|
|
103
|
+
await this.destroyFolderIfEmpty(this._base);
|
|
104
|
+
}
|
|
105
|
+
async destroyFolderIfEmpty(p) {
|
|
106
|
+
const contents = await readdir(p);
|
|
107
|
+
if (!contents.length) {
|
|
108
|
+
await rimraf(p);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
async tryDestroySelf() {
|
|
112
|
+
const myIndex = sandboxes.indexOf(this);
|
|
113
|
+
if (myIndex > -1) {
|
|
114
|
+
sandboxes.splice(myIndex, 1);
|
|
115
|
+
}
|
|
116
|
+
await rimraf(this._path);
|
|
117
|
+
}
|
|
118
|
+
async writeFile(at, contents) {
|
|
119
|
+
try {
|
|
120
|
+
await this.mkdir(path_1.default.dirname(at));
|
|
121
|
+
return await writeData(this.fullPathFor(at), contents, writeFile);
|
|
122
|
+
}
|
|
123
|
+
catch (e) {
|
|
124
|
+
throw new Error(`Unable to write file at ${at}: ${e.message || e}`);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
async appendFile(at, contents) {
|
|
128
|
+
try {
|
|
129
|
+
await this.mkdir(path_1.default.dirname(at));
|
|
130
|
+
return await writeData(this.fullPathFor(at), contents, appendFile);
|
|
131
|
+
}
|
|
132
|
+
catch (e) {
|
|
133
|
+
throw new Error(`Unable to append to file at ${at}: ${e.message || e}`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
async mkdir(name) {
|
|
137
|
+
name = this.resolveRelativePath(name);
|
|
138
|
+
return new Promise(async (resolve, reject) => {
|
|
139
|
+
const fullpath = path_1.default.join(this._path, name);
|
|
140
|
+
if (await isFolder(fullpath)) {
|
|
141
|
+
resolve(fullpath);
|
|
142
|
+
}
|
|
143
|
+
try {
|
|
144
|
+
mkdirp_1.sync(fullpath);
|
|
145
|
+
resolve(fullpath);
|
|
146
|
+
}
|
|
147
|
+
catch (e) {
|
|
148
|
+
reject(e);
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
resolveRelativePath(at) {
|
|
153
|
+
if (!path_1.default.isAbsolute(at)) {
|
|
154
|
+
return at;
|
|
155
|
+
}
|
|
156
|
+
const result = path_1.default.relative(this._path, at);
|
|
157
|
+
if (result.startsWith("..")) {
|
|
158
|
+
throw new Error(`${at} is outside the sandbox at ${this._path}`);
|
|
159
|
+
}
|
|
160
|
+
return result;
|
|
161
|
+
}
|
|
162
|
+
async readTextFile(at) {
|
|
163
|
+
return readFile(this.fullPathFor(at), { encoding: "utf8" });
|
|
164
|
+
}
|
|
165
|
+
async readFile(at) {
|
|
166
|
+
return readFile(this.fullPathFor(at));
|
|
167
|
+
}
|
|
168
|
+
fullPathFor(relativePath, ...parts) {
|
|
169
|
+
relativePath = this.resolveRelativePath(relativePath);
|
|
170
|
+
return path_1.default.join(this._path, relativePath, ...parts);
|
|
171
|
+
}
|
|
172
|
+
async stat(at) {
|
|
173
|
+
at = this.resolveRelativePath(at);
|
|
174
|
+
try {
|
|
175
|
+
return await fs_1.promises.stat(this.fullPathFor(at));
|
|
176
|
+
}
|
|
177
|
+
catch (e) {
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
async folderExists(at) {
|
|
182
|
+
at = this.resolveRelativePath(at);
|
|
183
|
+
return this.runOnStat(at, st => !!st && st.isDirectory());
|
|
184
|
+
}
|
|
185
|
+
async fileExists(at) {
|
|
186
|
+
at = this.resolveRelativePath(at);
|
|
187
|
+
return this.runOnStat(at, st => !!st && st.isFile());
|
|
188
|
+
}
|
|
189
|
+
async runOnStat(relativePath, fn) {
|
|
190
|
+
const st = await this.stat(relativePath);
|
|
191
|
+
return fn(st);
|
|
192
|
+
}
|
|
193
|
+
async run(fn, relativePath) {
|
|
194
|
+
const start = process.cwd();
|
|
195
|
+
try {
|
|
196
|
+
const target = relativePath
|
|
197
|
+
? path_1.default.resolve(path_1.default.join(this.path, relativePath))
|
|
198
|
+
: this.path;
|
|
199
|
+
this._validatePathInsideSandbox(target);
|
|
200
|
+
process.chdir(target);
|
|
201
|
+
return await fn();
|
|
202
|
+
}
|
|
203
|
+
finally {
|
|
204
|
+
process.chdir(start);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
_validatePathInsideSandbox(t) {
|
|
208
|
+
if (t.startsWith(this.path)) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
throw new Error(`${t} is not inside sandbox at ${this.path}`);
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Destroys all sandboxes created in this process up until now
|
|
215
|
+
*/
|
|
216
|
+
static async destroyAll() {
|
|
217
|
+
const toDestroy = sandboxes.splice(0, sandboxes.length);
|
|
218
|
+
for (const sandbox of toDestroy) {
|
|
219
|
+
await sandbox.destroy();
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Destroys the default base folder for sandboxes -- useful as a once-off
|
|
224
|
+
* run before all tests to ensure that there are no lingering sandboxes
|
|
225
|
+
* - cannot destroy sandboxes in custom paths
|
|
226
|
+
*/
|
|
227
|
+
static async destroyAny() {
|
|
228
|
+
const target = path_1.default.join(baseTarget, exports.basePrefix);
|
|
229
|
+
await rimraf(target);
|
|
230
|
+
}
|
|
231
|
+
static async create(at) {
|
|
232
|
+
return new Sandbox(at);
|
|
233
|
+
}
|
|
234
|
+
static async setBaseTargetToCurrentWorkingDirectory() {
|
|
235
|
+
await this.setBaseTarget(process.cwd());
|
|
236
|
+
}
|
|
237
|
+
static async setBaseTargetToSystemTempFolder() {
|
|
238
|
+
await this.setBaseTarget(os.tmpdir());
|
|
239
|
+
}
|
|
240
|
+
static async setBaseTarget(to) {
|
|
241
|
+
if (!(await isFolder(to))) {
|
|
242
|
+
await mkdirp_1.sync(to);
|
|
243
|
+
}
|
|
244
|
+
baseTarget = to;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
exports.Sandbox = Sandbox;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,44 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export declare type Func<T> = () => T;
|
|
4
|
-
export declare type AsyncFunc<T> = () => Promise<T>;
|
|
5
|
-
export declare const basePrefix = "__sandboxes__";
|
|
6
|
-
export declare class Sandbox {
|
|
7
|
-
private readonly _path;
|
|
8
|
-
private readonly _base;
|
|
9
|
-
private readonly _at;
|
|
10
|
-
get path(): string;
|
|
11
|
-
constructor(at?: string);
|
|
12
|
-
destroy(): Promise<void>;
|
|
13
|
-
private tryDestroyAt;
|
|
14
|
-
private tryDestroyContainer;
|
|
15
|
-
private destroyFolderIfEmpty;
|
|
16
|
-
private tryDestroySelf;
|
|
17
|
-
writeFile(at: string, contents: string | Buffer | string[]): Promise<string>;
|
|
18
|
-
appendFile(at: string, contents: string | Buffer | string[]): Promise<string>;
|
|
19
|
-
mkdir(name: string): Promise<string>;
|
|
20
|
-
private resolveRelativePath;
|
|
21
|
-
readTextFile(at: string): Promise<string>;
|
|
22
|
-
readFile(at: string): Promise<Buffer>;
|
|
23
|
-
fullPathFor(relativePath: string, ...parts: string[]): string;
|
|
24
|
-
stat(at: string): Promise<StatsBase<any> | null>;
|
|
25
|
-
folderExists(at: string): Promise<boolean>;
|
|
26
|
-
fileExists(at: string): Promise<boolean>;
|
|
27
|
-
private runOnStat;
|
|
28
|
-
run<T>(fn: Func<T> | AsyncFunc<T>, relativePath?: string): Promise<T>;
|
|
29
|
-
private _validatePathInsideSandbox;
|
|
30
|
-
/**
|
|
31
|
-
* Destroys all sandboxes created in this process up until now
|
|
32
|
-
*/
|
|
33
|
-
static destroyAll(): Promise<void>;
|
|
34
|
-
/**
|
|
35
|
-
* Destroys the default base folder for sandboxes -- useful as a once-off
|
|
36
|
-
* run before all tests to ensure that there are no lingering sandboxes
|
|
37
|
-
* - cannot destroy sandboxes in custom paths
|
|
38
|
-
*/
|
|
39
|
-
static destroyAny(): Promise<void>;
|
|
40
|
-
static create(at?: string): Promise<Sandbox>;
|
|
41
|
-
static setBaseTargetToCurrentWorkingDirectory(): Promise<void>;
|
|
42
|
-
static setBaseTargetToSystemTempFolder(): Promise<void>;
|
|
43
|
-
static setBaseTarget(to: string): Promise<void>;
|
|
44
|
-
}
|
|
1
|
+
export * from "./filesystem-sandbox";
|
|
2
|
+
export * from "./index";
|
package/dist/index.js
CHANGED
|
@@ -6,242 +6,10 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
6
6
|
if (k2 === undefined) k2 = k;
|
|
7
7
|
o[k2] = m[k];
|
|
8
8
|
}));
|
|
9
|
-
var
|
|
10
|
-
|
|
11
|
-
}) : function(o, v) {
|
|
12
|
-
o["default"] = v;
|
|
13
|
-
});
|
|
14
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
-
if (mod && mod.__esModule) return mod;
|
|
16
|
-
var result = {};
|
|
17
|
-
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
-
__setModuleDefault(result, mod);
|
|
19
|
-
return result;
|
|
20
|
-
};
|
|
21
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
22
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
9
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
+
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
|
|
23
11
|
};
|
|
24
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
if (!fs_1.promises) {
|
|
29
|
-
throw new Error("Yer node is olde! filesystem-sandboxes requires a Node with fs.promises");
|
|
30
|
-
}
|
|
31
|
-
// require for "path" produces the cleanest js output
|
|
32
|
-
const path_1 = __importDefault(require("path"));
|
|
33
|
-
const uuid_1 = require("uuid");
|
|
34
|
-
const mkdirp_1 = require("mkdirp");
|
|
35
|
-
const rimraf_1 = require("rimraf");
|
|
36
|
-
const { writeFile, readFile, stat, appendFile } = fs_1.promises;
|
|
37
|
-
async function isFolder(p) {
|
|
38
|
-
try {
|
|
39
|
-
const st = await stat(p);
|
|
40
|
-
return st && st.isDirectory();
|
|
41
|
-
}
|
|
42
|
-
catch (e) {
|
|
43
|
-
return false;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
async function readdir(p) {
|
|
47
|
-
const exists = await isFolder(p);
|
|
48
|
-
if (!exists) {
|
|
49
|
-
return [];
|
|
50
|
-
}
|
|
51
|
-
return await fs_1.promises.readdir(p);
|
|
52
|
-
}
|
|
53
|
-
async function rimraf(p) {
|
|
54
|
-
if (!(await isFolder(p))) {
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
return new Promise((resolve, reject) => {
|
|
58
|
-
try {
|
|
59
|
-
rimraf_1.sync(p, { maxBusyTries: 5 });
|
|
60
|
-
resolve();
|
|
61
|
-
}
|
|
62
|
-
catch (e) {
|
|
63
|
-
reject(e);
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
exports.basePrefix = "__sandboxes__";
|
|
68
|
-
let baseTarget = os.tmpdir();
|
|
69
|
-
const sandboxes = [];
|
|
70
|
-
async function writeData(fullPath, contents, writeFunction) {
|
|
71
|
-
const options = contents instanceof Buffer
|
|
72
|
-
? undefined
|
|
73
|
-
: { encoding: "utf8" };
|
|
74
|
-
if (Array.isArray(contents)) {
|
|
75
|
-
contents = contents.join("\n");
|
|
76
|
-
}
|
|
77
|
-
await writeFunction(fullPath, contents, options);
|
|
78
|
-
return fullPath;
|
|
79
|
-
}
|
|
80
|
-
class Sandbox {
|
|
81
|
-
constructor(at) {
|
|
82
|
-
this._at = at;
|
|
83
|
-
this._base = path_1.default.join(at || baseTarget, exports.basePrefix);
|
|
84
|
-
this._path = path_1.default.join(this._base, uuid_1.v4());
|
|
85
|
-
mkdirp_1.sync(this._path);
|
|
86
|
-
sandboxes.push(this);
|
|
87
|
-
}
|
|
88
|
-
get path() {
|
|
89
|
-
return this._path;
|
|
90
|
-
}
|
|
91
|
-
async destroy() {
|
|
92
|
-
await this.tryDestroySelf();
|
|
93
|
-
await this.tryDestroyContainer();
|
|
94
|
-
await this.tryDestroyAt();
|
|
95
|
-
}
|
|
96
|
-
async tryDestroyAt() {
|
|
97
|
-
if (!this._at) {
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
await this.destroyFolderIfEmpty(this._at);
|
|
101
|
-
}
|
|
102
|
-
async tryDestroyContainer() {
|
|
103
|
-
await this.destroyFolderIfEmpty(this._base);
|
|
104
|
-
}
|
|
105
|
-
async destroyFolderIfEmpty(p) {
|
|
106
|
-
const contents = await readdir(p);
|
|
107
|
-
if (!contents.length) {
|
|
108
|
-
await rimraf(p);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
async tryDestroySelf() {
|
|
112
|
-
const myIndex = sandboxes.indexOf(this);
|
|
113
|
-
if (myIndex > -1) {
|
|
114
|
-
sandboxes.splice(myIndex, 1);
|
|
115
|
-
}
|
|
116
|
-
await rimraf(this._path);
|
|
117
|
-
}
|
|
118
|
-
async writeFile(at, contents) {
|
|
119
|
-
try {
|
|
120
|
-
await this.mkdir(path_1.default.dirname(at));
|
|
121
|
-
return await writeData(this.fullPathFor(at), contents, writeFile);
|
|
122
|
-
}
|
|
123
|
-
catch (e) {
|
|
124
|
-
throw new Error(`Unable to write file at ${at}: ${e.message || e}`);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
async appendFile(at, contents) {
|
|
128
|
-
try {
|
|
129
|
-
await this.mkdir(path_1.default.dirname(at));
|
|
130
|
-
return await writeData(this.fullPathFor(at), contents, appendFile);
|
|
131
|
-
}
|
|
132
|
-
catch (e) {
|
|
133
|
-
throw new Error(`Unable to append to file at ${at}: ${e.message || e}`);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
async mkdir(name) {
|
|
137
|
-
name = this.resolveRelativePath(name);
|
|
138
|
-
return new Promise(async (resolve, reject) => {
|
|
139
|
-
const fullpath = path_1.default.join(this._path, name);
|
|
140
|
-
if (await isFolder(fullpath)) {
|
|
141
|
-
resolve(fullpath);
|
|
142
|
-
}
|
|
143
|
-
try {
|
|
144
|
-
mkdirp_1.sync(fullpath);
|
|
145
|
-
resolve(fullpath);
|
|
146
|
-
}
|
|
147
|
-
catch (e) {
|
|
148
|
-
reject(e);
|
|
149
|
-
}
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
resolveRelativePath(at) {
|
|
153
|
-
if (!path_1.default.isAbsolute(at)) {
|
|
154
|
-
return at;
|
|
155
|
-
}
|
|
156
|
-
const result = path_1.default.relative(this._path, at);
|
|
157
|
-
if (result.startsWith("..")) {
|
|
158
|
-
throw new Error(`${at} is outside the sandbox at ${this._path}`);
|
|
159
|
-
}
|
|
160
|
-
return result;
|
|
161
|
-
}
|
|
162
|
-
async readTextFile(at) {
|
|
163
|
-
return readFile(this.fullPathFor(at), { encoding: "utf8" });
|
|
164
|
-
}
|
|
165
|
-
async readFile(at) {
|
|
166
|
-
return readFile(this.fullPathFor(at));
|
|
167
|
-
}
|
|
168
|
-
fullPathFor(relativePath, ...parts) {
|
|
169
|
-
relativePath = this.resolveRelativePath(relativePath);
|
|
170
|
-
return path_1.default.join(this._path, relativePath, ...parts);
|
|
171
|
-
}
|
|
172
|
-
async stat(at) {
|
|
173
|
-
at = this.resolveRelativePath(at);
|
|
174
|
-
try {
|
|
175
|
-
return await fs_1.promises.stat(this.fullPathFor(at));
|
|
176
|
-
}
|
|
177
|
-
catch (e) {
|
|
178
|
-
return null;
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
async folderExists(at) {
|
|
182
|
-
at = this.resolveRelativePath(at);
|
|
183
|
-
return this.runOnStat(at, st => !!st && st.isDirectory());
|
|
184
|
-
}
|
|
185
|
-
async fileExists(at) {
|
|
186
|
-
at = this.resolveRelativePath(at);
|
|
187
|
-
return this.runOnStat(at, st => !!st && st.isFile());
|
|
188
|
-
}
|
|
189
|
-
async runOnStat(relativePath, fn) {
|
|
190
|
-
const st = await this.stat(relativePath);
|
|
191
|
-
return fn(st);
|
|
192
|
-
}
|
|
193
|
-
async run(fn, relativePath) {
|
|
194
|
-
const start = process.cwd();
|
|
195
|
-
try {
|
|
196
|
-
const target = relativePath
|
|
197
|
-
? path_1.default.resolve(path_1.default.join(this.path, relativePath))
|
|
198
|
-
: this.path;
|
|
199
|
-
this._validatePathInsideSandbox(target);
|
|
200
|
-
process.chdir(target);
|
|
201
|
-
return await fn();
|
|
202
|
-
}
|
|
203
|
-
finally {
|
|
204
|
-
process.chdir(start);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
_validatePathInsideSandbox(t) {
|
|
208
|
-
if (t.startsWith(this.path)) {
|
|
209
|
-
return;
|
|
210
|
-
}
|
|
211
|
-
throw new Error(`${t} is not inside sandbox at ${this.path}`);
|
|
212
|
-
}
|
|
213
|
-
/**
|
|
214
|
-
* Destroys all sandboxes created in this process up until now
|
|
215
|
-
*/
|
|
216
|
-
static async destroyAll() {
|
|
217
|
-
const toDestroy = sandboxes.splice(0, sandboxes.length);
|
|
218
|
-
for (const sandbox of toDestroy) {
|
|
219
|
-
await sandbox.destroy();
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
/**
|
|
223
|
-
* Destroys the default base folder for sandboxes -- useful as a once-off
|
|
224
|
-
* run before all tests to ensure that there are no lingering sandboxes
|
|
225
|
-
* - cannot destroy sandboxes in custom paths
|
|
226
|
-
*/
|
|
227
|
-
static async destroyAny() {
|
|
228
|
-
const target = path_1.default.join(baseTarget, exports.basePrefix);
|
|
229
|
-
await rimraf(target);
|
|
230
|
-
}
|
|
231
|
-
static async create(at) {
|
|
232
|
-
return new Sandbox(at);
|
|
233
|
-
}
|
|
234
|
-
static async setBaseTargetToCurrentWorkingDirectory() {
|
|
235
|
-
await this.setBaseTarget(process.cwd());
|
|
236
|
-
}
|
|
237
|
-
static async setBaseTargetToSystemTempFolder() {
|
|
238
|
-
await this.setBaseTarget(os.tmpdir());
|
|
239
|
-
}
|
|
240
|
-
static async setBaseTarget(to) {
|
|
241
|
-
if (!(await isFolder(to))) {
|
|
242
|
-
await mkdirp_1.sync(to);
|
|
243
|
-
}
|
|
244
|
-
baseTarget = to;
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
exports.Sandbox = Sandbox;
|
|
13
|
+
// this is a generated file: do not edit
|
|
14
|
+
__exportStar(require("./filesystem-sandbox"), exports);
|
|
15
|
+
__exportStar(require("./index"), exports);
|
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./dist";
|
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require("./dist");
|
package/package.json
CHANGED
|
@@ -1,62 +1,66 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "filesystem-sandbox",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "JavaScript module to provide filesystem sandboxes for testing",
|
|
5
|
-
"main": "
|
|
6
|
-
"types": "
|
|
7
|
-
"files": [
|
|
8
|
-
"dist/**/*"
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
},
|
|
29
|
-
"
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
"@types/
|
|
42
|
-
"@types/
|
|
43
|
-
"@types/
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
"
|
|
55
|
-
"
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
|
|
61
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "filesystem-sandbox",
|
|
3
|
+
"version": "1.21.0",
|
|
4
|
+
"description": "JavaScript module to provide filesystem sandboxes for testing",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist/**/*",
|
|
9
|
+
"index.js",
|
|
10
|
+
"index.d.ts"
|
|
11
|
+
],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"test": "jest",
|
|
14
|
+
"prebuild": "run-p clean-dist generate-index",
|
|
15
|
+
"clean-dist": "rimraf dist",
|
|
16
|
+
"generate-index": "node generate-index.js",
|
|
17
|
+
"build": "run-p tsc lint",
|
|
18
|
+
"autobuild": "nodemon -V -w src -w tests -e ts -x \"run-s -s build autobuild-message\"",
|
|
19
|
+
"autobuild-message": "node -e \"console.log('Auto-build completed', new Date());\"",
|
|
20
|
+
"lint": "tslint -p .",
|
|
21
|
+
"increment-package-version-beta": "cross-env VERSION_INCREMENT_STRATEGY=patch run-s \"zarro increment-package-json-version\"",
|
|
22
|
+
"prerelease-beta": "run-s build test",
|
|
23
|
+
"release-beta": "cross-env BETA=1 VERSION_INCREMENT_STRATEGY=patch run-s \"zarro release-npm\"",
|
|
24
|
+
"prerelease": "run-s build test",
|
|
25
|
+
"release": "cross-env VERSION_INCREMENT_STRATEGY=minor run-s \"zarro release-npm\"",
|
|
26
|
+
"tsc": "tsc",
|
|
27
|
+
"zarro": "zarro"
|
|
28
|
+
},
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "git+ssh://git@github.com/fluffynuts/filesystem-sandbox.git"
|
|
32
|
+
},
|
|
33
|
+
"keywords": [],
|
|
34
|
+
"author": "",
|
|
35
|
+
"license": "ISC",
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/fluffynuts/filesystem-sandbox/issues"
|
|
38
|
+
},
|
|
39
|
+
"homepage": "https://github.com/fluffynuts/filesystem-sandbox#readme",
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@types/faker": "^4.1.12",
|
|
42
|
+
"@types/jest": "^25.2.3",
|
|
43
|
+
"@types/lodash": "^4.14.153",
|
|
44
|
+
"@types/mkdirp": "^1.0.0",
|
|
45
|
+
"@types/node": "^14.0.5",
|
|
46
|
+
"@types/rimraf": "^3.0.0",
|
|
47
|
+
"@types/uuid": "^8.0.0",
|
|
48
|
+
"eslint": "^7.1.0",
|
|
49
|
+
"expect-even-more-jest": "^1.0.0",
|
|
50
|
+
"expect-more-jest": "^5.1.0",
|
|
51
|
+
"faker": "^4.1.0",
|
|
52
|
+
"jest": "^26.0.1",
|
|
53
|
+
"lodash": "^4.17.15",
|
|
54
|
+
"nodemon": "^2.0.4",
|
|
55
|
+
"simple-git": "^2.5.0",
|
|
56
|
+
"ts-jest": "^26.0.0",
|
|
57
|
+
"tslint": "^6.1.2",
|
|
58
|
+
"typescript": "^3.9.3",
|
|
59
|
+
"zarro": "^1.99.0"
|
|
60
|
+
},
|
|
61
|
+
"dependencies": {
|
|
62
|
+
"mkdirp": "^1.0.4",
|
|
63
|
+
"rimraf": "^3.0.2",
|
|
64
|
+
"uuid": "^8.1.0"
|
|
65
|
+
}
|
|
62
66
|
}
|