first-base 1.3.0 → 1.5.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/.node-version ADDED
@@ -0,0 +1 @@
1
+ v20.11.1
package/.npm-version ADDED
@@ -0,0 +1 @@
1
+ 10.2.4
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+
3
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
4
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
5
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
6
+ var fs = require("fs");
7
+ var _require = require("nice-path"),
8
+ Path = _require.Path;
9
+ var strongRootDirIndicators = [".git", ".hg"];
10
+ var weakRootDirIndicators = ["package-lock.json", ".gitignore", ".hgignore"];
11
+ var veryWeakRootDirIndicators = ["package.json", "README.md"];
12
+ function hasFile(dir, filename) {
13
+ var fullPath = dir.concat(filename).toString();
14
+ console.log("checking for ".concat(fullPath));
15
+ try {
16
+ return fs.existsSync(fullPath);
17
+ } catch (err) {
18
+ return false;
19
+ }
20
+ }
21
+ function findRootDir(startingDir) {
22
+ var start = new Path(startingDir).normalize();
23
+ var searchDirs = [start];
24
+ var currentPath = start.replaceLast([]);
25
+ while (currentPath.segments.length > 0) {
26
+ searchDirs.push(currentPath);
27
+ currentPath = currentPath.replaceLast([]);
28
+ }
29
+ for (var _i = 0, _searchDirs = searchDirs; _i < _searchDirs.length; _i++) {
30
+ var dir = _searchDirs[_i];
31
+ var _iterator = _createForOfIteratorHelper(strongRootDirIndicators),
32
+ _step;
33
+ try {
34
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
35
+ var indicator = _step.value;
36
+ if (hasFile(dir, indicator)) {
37
+ return dir.toString();
38
+ }
39
+ }
40
+ } catch (err) {
41
+ _iterator.e(err);
42
+ } finally {
43
+ _iterator.f();
44
+ }
45
+ }
46
+ for (var _i2 = 0, _searchDirs2 = searchDirs; _i2 < _searchDirs2.length; _i2++) {
47
+ var _dir = _searchDirs2[_i2];
48
+ var _iterator2 = _createForOfIteratorHelper(weakRootDirIndicators),
49
+ _step2;
50
+ try {
51
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
52
+ var _indicator = _step2.value;
53
+ if (hasFile(_dir, _indicator)) {
54
+ return _dir.toString();
55
+ }
56
+ }
57
+ } catch (err) {
58
+ _iterator2.e(err);
59
+ } finally {
60
+ _iterator2.f();
61
+ }
62
+ }
63
+ for (var _i3 = 0, _searchDirs3 = searchDirs; _i3 < _searchDirs3.length; _i3++) {
64
+ var _dir2 = _searchDirs3[_i3];
65
+ var _iterator3 = _createForOfIteratorHelper(veryWeakRootDirIndicators),
66
+ _step3;
67
+ try {
68
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
69
+ var _indicator2 = _step3.value;
70
+ if (hasFile(_dir2, _indicator2)) {
71
+ return _dir2.toString();
72
+ }
73
+ }
74
+ } catch (err) {
75
+ _iterator3.e(err);
76
+ } finally {
77
+ _iterator3.f();
78
+ }
79
+ }
80
+ return start.toString();
81
+ }
82
+ module.exports = {
83
+ findRootDir: findRootDir
84
+ };
package/dist/index.d.ts CHANGED
@@ -8,6 +8,7 @@ export type Options = {
8
8
  shell?: boolean | string;
9
9
  windowsVerbatimArguments?: boolean;
10
10
  windowsHide?: boolean;
11
+ pty?: boolean;
11
12
  };
12
13
 
13
14
  export type RunContext = {
@@ -17,6 +18,16 @@ export type RunContext = {
17
18
  code: null | number;
18
19
  error: boolean;
19
20
  };
21
+ /**
22
+ * Same as {@link RunContext.result}, but with {@link sanitizers} run on
23
+ * stdout/stderr.
24
+ */
25
+ cleanResult(): {
26
+ stdout: string;
27
+ stderr: string;
28
+ code: null | number;
29
+ error: boolean;
30
+ };
20
31
  completion: Promise<void>;
21
32
  debug(): RunContext;
22
33
  outputContains(value: string | RegExp): Promise<void>;
@@ -31,3 +42,25 @@ export const spawn: ((cmd: string) => RunContext) &
31
42
  ((cmd: string, args: Array<string>) => RunContext) &
32
43
  ((cmd: string, options: Options) => RunContext) &
33
44
  ((cmd: string, args: Array<string>, options: Options) => RunContext);
45
+
46
+ /**
47
+ * An array of functions that will be run on stdout/stderr when calling
48
+ * {@link RunContext.cleanResult}.
49
+ *
50
+ * By default, it contains 5 functions, which are run in order:
51
+ *
52
+ * - `stripAnsi`: Removes ANSI control characters
53
+ * - `replaceRootDir`: Replaces eg `/home/suchipi/Code/first-base/src/index.js` with `<rootDir>/src/index.js`
54
+ * - This function searches upwards for the root dir using a heuristic, and caches results in the {@link Map} `replaceRootDir.cache`.
55
+ * - The heuristic is:
56
+ * - Look upwards for a folder containing `.git` or `.hg`
57
+ * - if none is found, look upwards for a folder containing `package-lock.json`, `.gitignore` or `.hgignore`,
58
+ * - if none is found, look upwards for a folder containing `package.json` or `README.md`
59
+ * - if none is found, consider the present working directory to be the root dir.
60
+ * - `replaceCwd`: Replaces the current working directory with `<cwd>`
61
+ * - `collapseStackTrace`: For Node.JS-style stack traces, replaces the long chain of "at ..." lines with a single "at somewhere" line
62
+ * - `omitThrowLineNumber`: For Node.JS error source display, removes the line number
63
+ *
64
+ * You can remove them or replace them or add to them by mutating the `sanitizers` Array.
65
+ */
66
+ export const sanitizers: Array<(str: string) => string>;
package/dist/index.js CHANGED
@@ -1,37 +1,33 @@
1
1
  "use strict";
2
2
 
3
- function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
4
-
3
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
5
4
  var normalSpawn = require("child_process").spawn;
5
+ var ptySpawn = require("node-pty").spawn;
6
+ var stripAnsi = require("strip-ansi");
7
+ var _require = require("./sanitizers"),
8
+ sanitizers = _require.sanitizers;
6
9
 
7
- var stripAnsi = require("strip-ansi"); // Run a child process and return a "run context" object
10
+ // Run a child process and return a "run context" object
8
11
  // to interact with it. Function signature is the same as
9
12
  // child_process spawn, except you can pass `pty: true` in
10
13
  // options to run the process in a psuedo-tty.
11
-
12
-
13
14
  var spawn = function spawn(cmd, argsOrOptions, passedOptions) {
14
15
  var args;
15
16
  var options;
16
-
17
17
  if (Array.isArray(argsOrOptions)) {
18
18
  args = argsOrOptions;
19
19
  } else if (_typeof(argsOrOptions) === "object") {
20
20
  options = argsOrOptions;
21
21
  }
22
-
23
22
  if (passedOptions && !options) {
24
23
  options = passedOptions;
25
24
  }
26
-
27
25
  if (!args) {
28
26
  args = [];
29
27
  }
30
-
31
28
  if (!options) {
32
29
  options = {};
33
30
  }
34
-
35
31
  var child;
36
32
  var stdin;
37
33
  var stdout;
@@ -41,15 +37,12 @@ var spawn = function spawn(cmd, argsOrOptions, passedOptions) {
41
37
  var _debug = false;
42
38
  var outputContainsBuffer = "";
43
39
  var pendingOutputContainsRequests = new Set();
44
-
45
40
  var debugLog = function debugLog() {
46
41
  if (_debug) {
47
42
  var _console;
48
-
49
43
  (_console = console).log.apply(_console, arguments);
50
44
  }
51
45
  };
52
-
53
46
  var runContext = {
54
47
  result: {
55
48
  // All of the stdout and stderr the process has written so far.
@@ -60,6 +53,17 @@ var spawn = function spawn(cmd, argsOrOptions, passedOptions) {
60
53
  // true if the process errored out
61
54
  error: false
62
55
  },
56
+ // Return a version of result which has had the string sanitizers run on it
57
+ cleanResult: function cleanResult() {
58
+ return Object.assign({}, runContext.result, {
59
+ stdout: sanitizers.reduce(function (str, transformFn) {
60
+ return transformFn(str);
61
+ }, runContext.result.stdout),
62
+ stderr: sanitizers.reduce(function (str, transformFn) {
63
+ return transformFn(str);
64
+ }, runContext.result.stderr)
65
+ });
66
+ },
63
67
  // Promise that gets resolved when the child process completes.
64
68
  completion: null,
65
69
  debug: function debug() {
@@ -75,17 +79,14 @@ var spawn = function spawn(cmd, argsOrOptions, passedOptions) {
75
79
  var request = {
76
80
  value: value
77
81
  };
78
-
79
82
  request.resolve = function () {
80
- pendingOutputContainsRequests.delete(request);
83
+ pendingOutputContainsRequests["delete"](request);
81
84
  resolve();
82
85
  };
83
-
84
86
  request.reject = function () {
85
- pendingOutputContainsRequests.delete(request);
87
+ pendingOutputContainsRequests["delete"](request);
86
88
  reject();
87
89
  };
88
-
89
90
  pendingOutputContainsRequests.add(request);
90
91
  });
91
92
  },
@@ -104,19 +105,16 @@ var spawn = function spawn(cmd, argsOrOptions, passedOptions) {
104
105
  stdin.end();
105
106
  break;
106
107
  }
107
-
108
108
  case "stdout":
109
109
  {
110
110
  stdout.end();
111
111
  break;
112
112
  }
113
-
114
113
  case "stderr":
115
114
  {
116
115
  stderr.end();
117
116
  break;
118
117
  }
119
-
120
118
  default:
121
119
  {
122
120
  throw new Error("Invalid stream name: '".concat(stream, "'. Valid names are 'stdin', 'stdout', or 'stderr'."));
@@ -127,19 +125,20 @@ var spawn = function spawn(cmd, argsOrOptions, passedOptions) {
127
125
  // You can pass "SIGTERM", "SIGKILL", etc. Defaults to "SIGINT".
128
126
  kill: function kill() {
129
127
  var signal = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "SIGINT";
130
-
131
128
  if (running) {
132
129
  child.kill(signal);
133
130
  }
134
-
135
131
  if (unreffable) {
136
132
  unreffable.unref();
137
133
  }
138
134
  }
139
135
  };
140
-
141
136
  if (options.pty) {
142
- throw new Error("pty mode is no longer supported due to lack of support for new node.js versions in the node-pty module");
137
+ child = ptySpawn(cmd, args, options);
138
+ stdin = child;
139
+ stdout = child;
140
+ stderr = null; // no way to tell between stdout and stderr with pty
141
+ unreffable = child.socket;
143
142
  } else {
144
143
  child = normalSpawn(cmd, args, options);
145
144
  stdin = child.stdin;
@@ -147,9 +146,7 @@ var spawn = function spawn(cmd, argsOrOptions, passedOptions) {
147
146
  stderr = child.stderr;
148
147
  unreffable = child;
149
148
  }
150
-
151
149
  running = true;
152
-
153
150
  var checkForPendingOutputRequestsToResolve = function checkForPendingOutputRequestsToResolve() {
154
151
  pendingOutputContainsRequests.forEach(function (request) {
155
152
  if (typeof request.value === "string") {
@@ -163,16 +160,13 @@ var spawn = function spawn(cmd, argsOrOptions, passedOptions) {
163
160
  }
164
161
  });
165
162
  };
166
-
167
163
  stdout.setEncoding("utf-8");
168
-
169
164
  var handleStdoutData = function handleStdoutData(data) {
170
165
  runContext.result.stdout += data;
171
166
  outputContainsBuffer += data;
172
167
  debugLog("STDOUT: ".concat(data.toString()));
173
168
  checkForPendingOutputRequestsToResolve();
174
169
  };
175
-
176
170
  if (stdout.onData) {
177
171
  // the pty instance returned by node-pty
178
172
  // requires attaching handlers differently
@@ -180,11 +174,11 @@ var spawn = function spawn(cmd, argsOrOptions, passedOptions) {
180
174
  } else {
181
175
  stdout.on("data", handleStdoutData);
182
176
  }
183
-
184
177
  if (stderr) {
185
- stderr.setEncoding("utf-8"); // this is never a pty instance,
186
- // so we don't need to deal with onData here:
178
+ stderr.setEncoding("utf-8");
187
179
 
180
+ // this is never a pty instance,
181
+ // so we don't need to deal with onData here:
188
182
  stderr.on("data", function (data) {
189
183
  runContext.result.stderr += data;
190
184
  outputContainsBuffer += data;
@@ -192,7 +186,6 @@ var spawn = function spawn(cmd, argsOrOptions, passedOptions) {
192
186
  checkForPendingOutputRequestsToResolve();
193
187
  });
194
188
  }
195
-
196
189
  runContext.completion = new Promise(function (resolve) {
197
190
  var finish = function finish(reason) {
198
191
  debugLog("Process ".concat(reason));
@@ -203,7 +196,6 @@ var spawn = function spawn(cmd, argsOrOptions, passedOptions) {
203
196
  request.reject(new Error("Child process ".concat(reason, " before its output contained the requested content: ").concat(request.value)));
204
197
  });
205
198
  };
206
-
207
199
  child.once("exit", function (code) {
208
200
  runContext.result.code = code;
209
201
  finish("exited");
@@ -215,7 +207,7 @@ var spawn = function spawn(cmd, argsOrOptions, passedOptions) {
215
207
  });
216
208
  return runContext;
217
209
  };
218
-
219
210
  module.exports = {
220
- spawn: spawn
211
+ spawn: spawn,
212
+ sanitizers: sanitizers
221
213
  };
@@ -9,6 +9,7 @@ export type Options = {
9
9
  shell?: ?boolean | string,
10
10
  windowsVerbatimArguments?: ?boolean,
11
11
  windowsHide?: ?boolean,
12
+ pty?: ?boolean,
12
13
  };
13
14
 
14
15
  export type RunContext = {
@@ -32,6 +33,8 @@ interface Exports {
32
33
  spawn(cmd: string, args: Array<string>): RunContext;
33
34
  spawn(cmd: string, options: Options): RunContext;
34
35
  spawn(cmd: string, args: Array<string>, options: Options): RunContext;
36
+
37
+ sanitizers: Array<(str: string) => string>;
35
38
  }
36
39
 
37
40
  declare module.exports: Exports;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+
3
+ var stripAnsi = require("strip-ansi");
4
+ var _require = require("./find-root-dir"),
5
+ findRootDir = _require.findRootDir;
6
+ function escapeRegex(str) {
7
+ return str.replace(/[/\-\\^$*+?.()|[\]{}]/g, "\\$&");
8
+ }
9
+ var sanitizers = [stripAnsi, Object.assign(function replaceRootDir(str) {
10
+ var here = process.cwd();
11
+ var rootDir = replaceRootDir.cache.get(here) || findRootDir(here);
12
+ replaceRootDir.cache.set(here, rootDir);
13
+ return str.replace(new RegExp(escapeRegex(rootDir), "g"), "<rootDir>");
14
+ }, {
15
+ cache: new Map()
16
+ }), function replaceCwd(str) {
17
+ return str.replace(new RegExp(escapeRegex(process.cwd()), "g"), "<cwd>");
18
+ }, function collapseStackTrace(str) {
19
+ return str
20
+ // replace stack trace lines with a single "at somewhere" line
21
+ // explanation of regexp:
22
+ // newline, optional ansi escape, zero or more whitespace(s), "at",
23
+ // whitespace(s), several non-newline characters... and that whole
24
+ // thing can happen more than once
25
+ .replaceAll(/(?:\n(?:\x1B\[\d+m)?(\s*)at\s+[^\n]+)+/g, "\n$1at somewhere");
26
+ }, function omitThrowLineNumber(str) {
27
+ // omit line number from eg. "<rootDir>/dist/match.js:57\n\t\t\t\tthrow"
28
+ return str.replaceAll(/(\.js):\d+(\s+throw)/g, "$1$2");
29
+ }];
30
+ module.exports = {
31
+ sanitizers: sanitizers
32
+ };
package/package.json CHANGED
@@ -1,28 +1,36 @@
1
1
  {
2
2
  "name": "first-base",
3
- "version": "1.3.0",
3
+ "version": "1.5.0",
4
4
  "description": "Integration testing for CLI applications",
5
5
  "main": "dist/index.js",
6
- "repository": "https://github.com/suchipi/first-base",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/suchipi/first-base.git"
9
+ },
7
10
  "author": "Lily Skye <me@suchipi.com>",
8
11
  "license": "MIT",
9
12
  "dependencies": {
10
- "strip-ansi": "^5.0.0"
13
+ "node-pty": "^1.0.0",
14
+ "strip-ansi": "^5.0.0",
15
+ "nice-path": "^2.0.0"
11
16
  },
12
17
  "devDependencies": {
13
- "@babel/cli": "^7.2.3",
14
- "@babel/core": "^7.2.2",
15
- "@babel/preset-env": "^7.2.3",
18
+ "@babel/cli": "^7.23.4",
19
+ "@babel/core": "^7.23.7",
20
+ "@babel/preset-env": "^7.23.8",
16
21
  "babel-core": "^7.0.0-bridge.0",
17
- "babel-jest": "^23.6.0",
18
- "eslint": "^5.11.1",
19
- "eslint-config-unobtrusive": "^1.2.2",
20
- "eslint-plugin-import": "^2.14.0",
21
- "jest": "^23.6.0",
22
- "prettier": "^1.15.3"
22
+ "babel-jest": "^29.7.0",
23
+ "eslint": "^8.56.0",
24
+ "eslint-config-unobtrusive": "^1.2.5",
25
+ "eslint-plugin-import": "^2.29.1",
26
+ "jest": "^29.7.0",
27
+ "prettier": "^3.2.4"
23
28
  },
24
29
  "scripts": {
25
- "build": "mkdir -p dist; rm -rf dist/*; babel src/index.js --out-dir dist && cp src/index.js.flow dist/ && cp src/index.d.ts dist/",
30
+ "build": "mkdir -p dist; rm -rf dist/*; babel src --out-dir dist && cp src/index.js.flow dist/ && cp src/index.d.ts dist/",
26
31
  "test": "jest"
32
+ },
33
+ "jest": {
34
+ "prettierPath": null
27
35
  }
28
36
  }
package/.eslintignore DELETED
@@ -1 +0,0 @@
1
- *.flow