filesystem-sandbox 1.21.0 → 1.24.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 +13 -0
- package/dist/filesystem-sandbox.js +51 -11
- package/dist/index.d.ts +1 -0
- package/dist/index.js +7 -2
- package/dist/uuid.d.ts +1 -0
- package/dist/uuid.js +11 -0
- package/package.json +63 -65
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
+
import { SpawnOptions } from "child_process";
|
|
2
3
|
import { StatsBase } from "fs";
|
|
3
4
|
export declare type Func<T> = () => T;
|
|
4
5
|
export declare type AsyncFunc<T> = () => Promise<T>;
|
|
@@ -26,6 +27,7 @@ export declare class Sandbox {
|
|
|
26
27
|
fileExists(at: string): Promise<boolean>;
|
|
27
28
|
private runOnStat;
|
|
28
29
|
run<T>(fn: Func<T> | AsyncFunc<T>, relativePath?: string): Promise<T>;
|
|
30
|
+
exec(command: string, args?: string[], options?: SpawnOptions): Promise<ProcessData>;
|
|
29
31
|
private _validatePathInsideSandbox;
|
|
30
32
|
/**
|
|
31
33
|
* Destroys all sandboxes created in this process up until now
|
|
@@ -42,3 +44,14 @@ export declare class Sandbox {
|
|
|
42
44
|
static setBaseTargetToSystemTempFolder(): Promise<void>;
|
|
43
45
|
static setBaseTarget(to: string): Promise<void>;
|
|
44
46
|
}
|
|
47
|
+
export interface ProcessData {
|
|
48
|
+
stdout: string[];
|
|
49
|
+
stderr: string[];
|
|
50
|
+
code: number;
|
|
51
|
+
}
|
|
52
|
+
export interface SpawnError extends Error {
|
|
53
|
+
command: string;
|
|
54
|
+
args: string[] | undefined;
|
|
55
|
+
options: SpawnOptions | undefined;
|
|
56
|
+
result: ProcessData;
|
|
57
|
+
}
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
5
9
|
}) : (function(o, m, k, k2) {
|
|
6
10
|
if (k2 === undefined) k2 = k;
|
|
7
11
|
o[k2] = m[k];
|
|
@@ -14,7 +18,7 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
14
18
|
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
19
|
if (mod && mod.__esModule) return mod;
|
|
16
20
|
var result = {};
|
|
17
|
-
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
22
|
__setModuleDefault(result, mod);
|
|
19
23
|
return result;
|
|
20
24
|
};
|
|
@@ -23,16 +27,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
23
27
|
};
|
|
24
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
29
|
exports.Sandbox = exports.basePrefix = void 0;
|
|
30
|
+
const child_process_1 = require("child_process");
|
|
26
31
|
const fs_1 = require("fs");
|
|
27
32
|
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
33
|
// require for "path" produces the cleanest js output
|
|
32
34
|
const path_1 = __importDefault(require("path"));
|
|
33
|
-
const uuid_1 = require("uuid");
|
|
34
35
|
const mkdirp_1 = require("mkdirp");
|
|
35
36
|
const rimraf_1 = require("rimraf");
|
|
37
|
+
const uuid_1 = require("./uuid");
|
|
38
|
+
if (!fs_1.promises) {
|
|
39
|
+
throw new Error("Yer node is olde! filesystem-sandboxes requires a Node with fs.promises");
|
|
40
|
+
}
|
|
36
41
|
const { writeFile, readFile, stat, appendFile } = fs_1.promises;
|
|
37
42
|
async function isFolder(p) {
|
|
38
43
|
try {
|
|
@@ -56,7 +61,7 @@ async function rimraf(p) {
|
|
|
56
61
|
}
|
|
57
62
|
return new Promise((resolve, reject) => {
|
|
58
63
|
try {
|
|
59
|
-
rimraf_1.sync(p, { maxBusyTries: 5 });
|
|
64
|
+
(0, rimraf_1.sync)(p, { maxBusyTries: 5 });
|
|
60
65
|
resolve();
|
|
61
66
|
}
|
|
62
67
|
catch (e) {
|
|
@@ -81,8 +86,8 @@ class Sandbox {
|
|
|
81
86
|
constructor(at) {
|
|
82
87
|
this._at = at;
|
|
83
88
|
this._base = path_1.default.join(at || baseTarget, exports.basePrefix);
|
|
84
|
-
this._path = path_1.default.join(this._base, uuid_1.
|
|
85
|
-
mkdirp_1.sync(this._path);
|
|
89
|
+
this._path = path_1.default.join(this._base, (0, uuid_1.uuid)());
|
|
90
|
+
(0, mkdirp_1.sync)(this._path);
|
|
86
91
|
sandboxes.push(this);
|
|
87
92
|
}
|
|
88
93
|
get path() {
|
|
@@ -141,7 +146,7 @@ class Sandbox {
|
|
|
141
146
|
resolve(fullpath);
|
|
142
147
|
}
|
|
143
148
|
try {
|
|
144
|
-
mkdirp_1.sync(fullpath);
|
|
149
|
+
(0, mkdirp_1.sync)(fullpath);
|
|
145
150
|
resolve(fullpath);
|
|
146
151
|
}
|
|
147
152
|
catch (e) {
|
|
@@ -204,6 +209,31 @@ class Sandbox {
|
|
|
204
209
|
process.chdir(start);
|
|
205
210
|
}
|
|
206
211
|
}
|
|
212
|
+
async exec(command, args, options) {
|
|
213
|
+
return await this.run(() => {
|
|
214
|
+
const spawnArgs = args || [], spawnOptions = options || {};
|
|
215
|
+
return new Promise((resolve, reject) => {
|
|
216
|
+
var _a, _b;
|
|
217
|
+
const child = (0, child_process_1.spawn)(command, spawnArgs, spawnOptions), result = {
|
|
218
|
+
stdout: [],
|
|
219
|
+
stderr: [],
|
|
220
|
+
code: -1
|
|
221
|
+
};
|
|
222
|
+
(_a = child.stderr) === null || _a === void 0 ? void 0 : _a.on("data", d => {
|
|
223
|
+
result.stderr.push(d.toString());
|
|
224
|
+
});
|
|
225
|
+
(_b = child.stdout) === null || _b === void 0 ? void 0 : _b.on("data", d => {
|
|
226
|
+
result.stdout.push(d.toString());
|
|
227
|
+
});
|
|
228
|
+
child.on("close", code => {
|
|
229
|
+
result.code = code;
|
|
230
|
+
return code
|
|
231
|
+
? reject(makeSpawnErrorFor(result, command, args, options))
|
|
232
|
+
: resolve(result);
|
|
233
|
+
});
|
|
234
|
+
});
|
|
235
|
+
});
|
|
236
|
+
}
|
|
207
237
|
_validatePathInsideSandbox(t) {
|
|
208
238
|
if (t.startsWith(this.path)) {
|
|
209
239
|
return;
|
|
@@ -239,9 +269,19 @@ class Sandbox {
|
|
|
239
269
|
}
|
|
240
270
|
static async setBaseTarget(to) {
|
|
241
271
|
if (!(await isFolder(to))) {
|
|
242
|
-
await mkdirp_1.sync(to);
|
|
272
|
+
await (0, mkdirp_1.sync)(to);
|
|
243
273
|
}
|
|
244
274
|
baseTarget = to;
|
|
245
275
|
}
|
|
246
276
|
}
|
|
247
277
|
exports.Sandbox = Sandbox;
|
|
278
|
+
function makeSpawnErrorFor(result, command, args, options) {
|
|
279
|
+
const e = new Error(`Error exit code ${result.code} for: ${command} ${(args || []).join(" ")}`);
|
|
280
|
+
return {
|
|
281
|
+
...e,
|
|
282
|
+
result,
|
|
283
|
+
command,
|
|
284
|
+
args,
|
|
285
|
+
options
|
|
286
|
+
};
|
|
287
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,15 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
5
9
|
}) : (function(o, m, k, k2) {
|
|
6
10
|
if (k2 === undefined) k2 = k;
|
|
7
11
|
o[k2] = m[k];
|
|
8
12
|
}));
|
|
9
13
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
-
for (var p in m) if (p !== "default" && !
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
15
|
};
|
|
12
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
17
|
// this is a generated file: do not edit
|
|
14
18
|
__exportStar(require("./filesystem-sandbox"), exports);
|
|
15
19
|
__exportStar(require("./index"), exports);
|
|
20
|
+
__exportStar(require("./uuid"), exports);
|
package/dist/uuid.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function uuid(): string;
|
package/dist/uuid.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.uuid = void 0;
|
|
7
|
+
const pure_uuid_1 = __importDefault(require("pure-uuid"));
|
|
8
|
+
function uuid() {
|
|
9
|
+
return new pure_uuid_1.default(4).toString();
|
|
10
|
+
}
|
|
11
|
+
exports.uuid = uuid;
|
package/package.json
CHANGED
|
@@ -1,66 +1,64 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "filesystem-sandbox",
|
|
3
|
-
"version": "1.
|
|
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
|
-
"@
|
|
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
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"expect-
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
"
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
"
|
|
63
|
-
|
|
64
|
-
"uuid": "^8.1.0"
|
|
65
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "filesystem-sandbox",
|
|
3
|
+
"version": "1.24.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
|
+
"@faker-js/faker": "^6.0.0",
|
|
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
|
+
"eslint": "^7.1.0",
|
|
48
|
+
"expect-even-more-jest": "^1.0.0",
|
|
49
|
+
"expect-more-jest": "^5.1.0",
|
|
50
|
+
"jest": "^28.1.1",
|
|
51
|
+
"lodash": "^4.17.15",
|
|
52
|
+
"nodemon": "^2.0.4",
|
|
53
|
+
"ts-jest": "^28.0.4",
|
|
54
|
+
"tslint": "^6.1.2",
|
|
55
|
+
"typescript": "^4.6.2",
|
|
56
|
+
"yafs": "^1.23.0",
|
|
57
|
+
"zarro": "^1.99.0"
|
|
58
|
+
},
|
|
59
|
+
"dependencies": {
|
|
60
|
+
"mkdirp": "^1.0.4",
|
|
61
|
+
"pure-uuid": "^1.6.2",
|
|
62
|
+
"rimraf": "^3.0.2"
|
|
63
|
+
}
|
|
66
64
|
}
|