filesystem-sandbox 1.20.0 → 1.23.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.
@@ -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.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
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
35
  const uuid_1 = require("uuid");
34
36
  const mkdirp_1 = require("mkdirp");
35
37
  const rimraf_1 = require("rimraf");
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.v4());
85
- mkdirp_1.sync(this._path);
89
+ this._path = path_1.default.join(this._base, (0, uuid_1.v4)());
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.js CHANGED
@@ -1,13 +1,17 @@
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.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
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" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "filesystem-sandbox",
3
- "version": "1.20.0",
3
+ "version": "1.23.0",
4
4
  "description": "JavaScript module to provide filesystem sandboxes for testing",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -38,7 +38,7 @@
38
38
  },
39
39
  "homepage": "https://github.com/fluffynuts/filesystem-sandbox#readme",
40
40
  "devDependencies": {
41
- "@types/faker": "^4.1.12",
41
+ "@faker-js/faker": "^6.0.0",
42
42
  "@types/jest": "^25.2.3",
43
43
  "@types/lodash": "^4.14.153",
44
44
  "@types/mkdirp": "^1.0.0",
@@ -48,15 +48,14 @@
48
48
  "eslint": "^7.1.0",
49
49
  "expect-even-more-jest": "^1.0.0",
50
50
  "expect-more-jest": "^5.1.0",
51
- "faker": "^4.1.0",
52
- "jest": "^26.0.1",
51
+ "jest": "^28.1.1",
53
52
  "lodash": "^4.17.15",
54
53
  "nodemon": "^2.0.4",
55
- "simple-git": "^2.5.0",
56
- "ts-jest": "^26.0.0",
54
+ "ts-jest": "^28.0.4",
57
55
  "tslint": "^6.1.2",
58
- "typescript": "^3.9.3",
59
- "zarro": "^1.78.0"
56
+ "typescript": "^4.6.2",
57
+ "yafs": "^1.23.0",
58
+ "zarro": "^1.99.0"
60
59
  },
61
60
  "dependencies": {
62
61
  "mkdirp": "^1.0.4",