doc-detective 2.12.0 → 2.13.0-dev.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/src/utils.js CHANGED
@@ -1,190 +1,188 @@
1
- const yargs = require("yargs/yargs");
2
- const { hideBin } = require("yargs/helpers");
3
- const { validate } = require("doc-detective-common");
4
- const path = require("path");
5
- const fs = require("fs");
6
- const { spawn } = require("child_process");
7
- const os = require("os");
8
-
9
- exports.setArgs = setArgs;
10
- exports.setConfig = setConfig;
11
- exports.outputResults = outputResults;
12
- exports.spawnCommand = spawnCommand;
13
- exports.setMeta = setMeta;
14
-
15
- // Define args
16
- function setArgs(args) {
17
- if (!args) return {};
18
- let argv = yargs(hideBin(args))
19
- .option("config", {
20
- alias: "c",
21
- description: "Path to a `config.json` file.",
22
- type: "string",
23
- })
24
- .option("input", {
25
- alias: "i",
26
- description:
27
- "Path to test specifications and documentation source files. May be paths to specific files or to directories to scan for files.",
28
- type: "string",
29
- })
30
- .option("output", {
31
- alias: "o",
32
- description:
33
- "Path of the directory in which to store the output of Doc Detective commands.",
34
- type: "string",
35
- })
36
- .option("setup", {
37
- description:
38
- "Path to test specifications to perform before those specified by `input`. Useful for setting up testing environments.",
39
- type: "string",
40
- })
41
- .option("cleanup", {
42
- description:
43
- "Path to test specifications to perform after those specified by input. Useful for cleaning up testing environments.",
44
- type: "string",
45
- })
46
- .option("recursive", {
47
- alias: "r",
48
- description:
49
- "Boolean. If true searches input, setup, and cleanup paths recursively for test specificaions and source files. Defaults to `true`.",
50
- type: "string",
51
- })
52
- .option("logLevel", {
53
- alias: "l",
54
- description:
55
- "Detail level of logging events. Accepted values: silent, error, warning, info (default), debug",
56
- type: "string",
57
- })
58
- .help()
59
- .alias("help", "h").argv;
60
-
61
- return argv;
62
- }
63
-
64
- // Override config values based on args
65
- function setConfig(config, args) {
66
- // If no args, return config
67
- if (!args) return config;
68
-
69
- // Load config from file
70
- if (args.config) {
71
- const configPath = path.resolve(args.config);
72
- configContent = require(configPath);
73
- // Validate config
74
- const validation = validate("config_v2", configContent);
75
- if (validation.valid) {
76
- config = configContent;
77
- } else {
78
- // Output validation errors
79
- console.error("Invalid config file:");
80
- validation.errors.forEach((error) => {
81
- console.error(error);
82
- });
83
- process.exit(1);
84
- }
85
- }
86
-
87
- // Override config values
88
- if (args.input) config.input = args.input;
89
- if (args.output) config.output = args.output;
90
- if (args.recursive) config.recursive = args.recursive;
91
- if (args.logLevel) config.logLevel = args.logLevel;
92
- if (
93
- (args.setup || args.cleanup || args.input || args.output) &&
94
- !config.runTests
95
- )
96
- config.runTests = {};
97
- if (args.input) config.runTests.input = args.input;
98
- if (args.output) config.runTests.output = args.output;
99
- if (args.setup) config.runTests.setup = args.setup;
100
- if (args.cleanup) config.runTests.cleanup = args.cleanup;
101
-
102
- // Validate config
103
- const validation = validate("config_v2", config);
104
- if (!validation.valid) {
105
- // Output validation errors
106
- console.error("Invalid config.");
107
- validation.errors.forEach((error) => {
108
- console.error(error);
109
- });
110
- process.exit(1);
111
- }
112
-
113
- return config;
114
- }
115
-
116
- async function outputResults(config, path, results) {
117
- let data = JSON.stringify(results, null, 2);
118
- try {
119
- fs.writeFileSync(path, data);
120
- console.log(`See results at ${path}`);
121
- } catch (err) {
122
- console.error(`Error writing results to ${path}: ${err}`);
123
- }
124
- }
125
-
126
- // Perform a native command in the current working directory.
127
- async function spawnCommand(cmd, args) {
128
- // Split command into command and arguments
129
- if (cmd.includes(" ")) {
130
- const cmdArray = cmd.split(" ");
131
- cmd = cmdArray[0];
132
- cmdArgs = cmdArray.slice(1);
133
- // Add arguments to args array
134
- if (args) {
135
- args = cmdArgs.concat(args);
136
- } else {
137
- args = cmdArgs;
138
- }
139
- }
140
-
141
- const runCommand = spawn(cmd, args);
142
-
143
- // Capture stdout
144
- let stdout = "";
145
- for await (const chunk of runCommand.stdout) {
146
- stdout += chunk;
147
- }
148
- // Remove trailing newline
149
- stdout = stdout.replace(/\n$/, "");
150
-
151
- // Capture stderr
152
- let stderr = "";
153
- for await (const chunk of runCommand.stderr) {
154
- stderr += chunk;
155
- }
156
- // Remove trailing newline
157
- stderr = stderr.replace(/\n$/, "");
158
-
159
- // Capture exit code
160
- const exitCode = await new Promise((resolve, reject) => {
161
- runCommand.on("close", resolve);
162
- });
163
-
164
- return { stdout, stderr, exitCode };
165
- }
166
-
167
- function setMeta() {
168
- const platformMap = {
169
- win32: "windows",
170
- darwin: "mac",
171
- linux: "linux",
172
- };
173
-
174
- // Set meta
175
- const meta =
176
- process.env["DOC_DETECTIVE_META"] !== undefined
177
- ? JSON.parse(process.env["DOC_DETECTIVE_META"])
178
- : {};
179
- const package = require("../package.json");
180
- meta.distribution = "doc-detective";
181
- meta.dist_version = package.version;
182
- meta.dist_platform = platformMap[os.platform()] || os.platform();
183
- meta.dist_platform_version = os.release();
184
- meta.dist_platform_arch = os.arch();
185
- meta.dist_deployment = meta.dist_deployment || "node";
186
- meta.dist_deployment_version =
187
- meta.dist_deployment_version || process.version;
188
- meta.dist_interface = meta.dist_interface || "cli";
189
- process.env["DOC_DETECTIVE_META"] = JSON.stringify(meta);
190
- }
1
+ const yargs = require("yargs/yargs");
2
+ const { hideBin } = require("yargs/helpers");
3
+ const { validate } = require("doc-detective-common");
4
+ const path = require("path");
5
+ const fs = require("fs");
6
+ const { spawn } = require("child_process");
7
+ const os = require("os");
8
+
9
+ exports.setArgs = setArgs;
10
+ exports.setConfig = setConfig;
11
+ exports.outputResults = outputResults;
12
+ exports.spawnCommand = spawnCommand;
13
+ exports.setMeta = setMeta;
14
+
15
+ // Define args
16
+ function setArgs(args) {
17
+ if (!args) return {};
18
+ let argv = yargs(hideBin(args))
19
+ .option("config", {
20
+ alias: "c",
21
+ description: "Path to a `config.json` file.",
22
+ type: "string",
23
+ })
24
+ .option("input", {
25
+ alias: "i",
26
+ description:
27
+ "Path to test specifications and documentation source files. May be paths to specific files or to directories to scan for files.",
28
+ type: "string",
29
+ })
30
+ .option("output", {
31
+ alias: "o",
32
+ description:
33
+ "Path of the directory in which to store the output of Doc Detective commands.",
34
+ type: "string",
35
+ })
36
+ .option("setup", {
37
+ description:
38
+ "Path to test specifications to perform before those specified by `input`. Useful for setting up testing environments.",
39
+ type: "string",
40
+ })
41
+ .option("cleanup", {
42
+ description:
43
+ "Path to test specifications to perform after those specified by input. Useful for cleaning up testing environments.",
44
+ type: "string",
45
+ })
46
+ .option("recursive", {
47
+ alias: "r",
48
+ description:
49
+ "Boolean. If true searches input, setup, and cleanup paths recursively for test specificaions and source files. Defaults to `true`.",
50
+ type: "string",
51
+ })
52
+ .option("logLevel", {
53
+ alias: "l",
54
+ description:
55
+ "Detail level of logging events. Accepted values: silent, error, warning, info (default), debug",
56
+ type: "string",
57
+ })
58
+ .help()
59
+ .alias("help", "h").argv;
60
+
61
+ return argv;
62
+ }
63
+
64
+ // Override config values based on args
65
+ function setConfig(config, args) {
66
+ // If no args, return config
67
+ if (!args) return config;
68
+
69
+ // Load config from file
70
+ if (args.config) {
71
+ const configPath = path.resolve(args.config);
72
+ configContent = require(configPath);
73
+ // Validate config
74
+ const validation = validate("config_v2", configContent);
75
+ if (validation.valid) {
76
+ config = configContent;
77
+ } else {
78
+ // Output validation errors
79
+ console.error("Invalid config file:");
80
+ validation.errors.forEach((error) => {
81
+ console.error(error);
82
+ });
83
+ process.exit(1);
84
+ }
85
+ }
86
+
87
+ // Override config values
88
+ if (args.input) config.input = args.input;
89
+ if (args.output) config.output = args.output;
90
+ if (args.recursive) config.recursive = args.recursive;
91
+ if (args.logLevel) config.logLevel = args.logLevel;
92
+ if (
93
+ (args.setup || args.cleanup || args.input || args.output) &&
94
+ !config.runTests
95
+ )
96
+ config.runTests = {};
97
+ if (args.input) config.runTests.input = args.input;
98
+ if (args.output) config.runTests.output = args.output;
99
+ if (args.setup) config.runTests.setup = args.setup;
100
+ if (args.cleanup) config.runTests.cleanup = args.cleanup;
101
+
102
+ // Validate config
103
+ const validation = validate("config_v2", config);
104
+ if (!validation.valid) {
105
+ // Output validation errors
106
+ console.error("Invalid config.");
107
+ validation.errors.forEach((error) => {
108
+ console.error(error);
109
+ });
110
+ process.exit(1);
111
+ }
112
+
113
+ return config;
114
+ }
115
+
116
+ async function outputResults(config, path, results) {
117
+ let data = JSON.stringify(results, null, 2);
118
+ fs.writeFile(path, data, (err) => {
119
+ if (err) throw err;
120
+ });
121
+ console.log(`See results at ${path}`);
122
+ }
123
+
124
+ // Perform a native command in the current working directory.
125
+ async function spawnCommand(cmd, args) {
126
+ // Split command into command and arguments
127
+ if (cmd.includes(" ")) {
128
+ const cmdArray = cmd.split(" ");
129
+ cmd = cmdArray[0];
130
+ cmdArgs = cmdArray.slice(1);
131
+ // Add arguments to args array
132
+ if (args) {
133
+ args = cmdArgs.concat(args);
134
+ } else {
135
+ args = cmdArgs;
136
+ }
137
+ }
138
+
139
+ const runCommand = spawn(cmd, args);
140
+
141
+ // Capture stdout
142
+ let stdout = "";
143
+ for await (const chunk of runCommand.stdout) {
144
+ stdout += chunk;
145
+ }
146
+ // Remove trailing newline
147
+ stdout = stdout.replace(/\n$/, "");
148
+
149
+ // Capture stderr
150
+ let stderr = "";
151
+ for await (const chunk of runCommand.stderr) {
152
+ stderr += chunk;
153
+ }
154
+ // Remove trailing newline
155
+ stderr = stderr.replace(/\n$/, "");
156
+
157
+ // Capture exit code
158
+ const exitCode = await new Promise((resolve, reject) => {
159
+ runCommand.on("close", resolve);
160
+ });
161
+
162
+ return { stdout, stderr, exitCode };
163
+ }
164
+
165
+ function setMeta() {
166
+ const platformMap = {
167
+ win32: "windows",
168
+ darwin: "mac",
169
+ linux: "linux",
170
+ };
171
+
172
+ // Set meta
173
+ const meta =
174
+ process.env["DOC_DETECTIVE_META"] !== undefined
175
+ ? JSON.parse(process.env["DOC_DETECTIVE_META"])
176
+ : {};
177
+ const package = require("../package.json");
178
+ meta.distribution = "doc-detective";
179
+ meta.dist_version = package.version;
180
+ meta.dist_platform = platformMap[os.platform()] || os.platform();
181
+ meta.dist_platform_version = os.release();
182
+ meta.dist_platform_arch = os.arch();
183
+ meta.dist_deployment = meta.dist_deployment || "node";
184
+ meta.dist_deployment_version =
185
+ meta.dist_deployment_version || process.version;
186
+ meta.dist_interface = meta.dist_interface || "cli";
187
+ process.env["DOC_DETECTIVE_META"] = JSON.stringify(meta);
188
+ }
@@ -1,19 +1,19 @@
1
- {
2
- "id": "cleanup",
3
- "tests": [
4
- {
5
- "steps": [
6
- {
7
- "action": "setVariables",
8
- "path": ".env"
9
- },
10
- {
11
- "action": "runShell",
12
- "command": "echo",
13
- "args": ["cleanup"]
14
- }
15
- ]
16
- }
17
- ]
18
- }
1
+ {
2
+ "id": "cleanup",
3
+ "tests": [
4
+ {
5
+ "steps": [
6
+ {
7
+ "action": "setVariables",
8
+ "path": ".env"
9
+ },
10
+ {
11
+ "action": "runShell",
12
+ "command": "echo",
13
+ "args": ["cleanup"]
14
+ }
15
+ ]
16
+ }
17
+ ]
18
+ }
19
19
 
@@ -1,46 +1,42 @@
1
- {
2
- "envVariables": "",
3
- "input": ".",
4
- "output": ".",
5
- "recursive": true,
6
- "logLevel": "debug",
7
- "runTests": {
8
- "input": "./test/artifacts/test.spec.json",
9
- "output": ".",
10
- "setup": "",
11
- "cleanup": "",
12
- "recursive": true,
13
- "detectSteps": false,
14
- "mediaDirectory": ".",
15
- "downloadDirectory": ".",
16
- "contexts": [
17
- {
18
- "app": { "name": "firefox" },
19
- "platforms": ["windows", "mac", "linux"]
20
- },
21
- {
22
- "app": { "name": "chrome", "options": { "headless": true } },
23
- "platforms": ["windows", "mac", "linux"]
24
- },
25
- {
26
- "app": { "name": "edge" },
27
- "platforms": ["windows", "mac", "linux"]
28
- }
29
- ]
30
- },
31
- "runCoverage": {
32
- "recursive": true,
33
- "input": ".dev/",
34
- "output": ".",
35
- "markup": []
36
- },
37
- "suggestTests": {
38
- "recursive": true,
39
- "input": ".",
40
- "output": ".",
41
- "markup": []
42
- },
43
- "telemetry": {
44
- "send": false
45
- }
46
- }
1
+ {
2
+ "envVariables": "",
3
+ "input": ".",
4
+ "output": ".",
5
+ "recursive": true,
6
+ "logLevel": "debug",
7
+ "runTests": {
8
+ "input": "./test/artifacts/test.spec.json",
9
+ "output": ".",
10
+ "setup": "",
11
+ "cleanup": "",
12
+ "recursive": true,
13
+ "detectSteps": false,
14
+ "mediaDirectory": ".",
15
+ "downloadDirectory": ".",
16
+ "contexts": [
17
+ {
18
+ "app": { "name": "firefox" },
19
+ "platforms": ["windows", "mac", "linux"]
20
+ },
21
+ {
22
+ "app": { "name": "chrome", "options": { "headless": true } },
23
+ "platforms": ["windows", "mac", "linux"]
24
+ }
25
+ ]
26
+ },
27
+ "runCoverage": {
28
+ "recursive": true,
29
+ "input": ".dev/",
30
+ "output": ".",
31
+ "markup": []
32
+ },
33
+ "suggestTests": {
34
+ "recursive": true,
35
+ "input": ".",
36
+ "output": ".",
37
+ "markup": []
38
+ },
39
+ "telemetry": {
40
+ "send": false
41
+ }
42
+ }
@@ -1,18 +1,18 @@
1
- # Doc Detective documentation overview
2
-
3
- [comment]: # (test start {"id":"search-kittens"})
4
-
5
- [Doc Detective documentation](http://doc-detective.com) is split into a few key sections:
6
- [comment]: # (step {"action":"goTo", "url":"http://doc-detective.com"})
7
-
8
- - The landing page discusses what Doc Detective is, what it does, and who might find it useful.
9
- - [Get started](https://doc-detective.com/get-started.html) covers how to quickly get up and running with Doc Detective.
10
- [comment]: # (step {"action":"goTo", "url":"https://doc-detective.com/get-started.html"})
11
- - The [references](https://doc-detective.com/reference/) detail the various JSON objects that Doc Detective expects for configs, test specifications, tests, actions, and more. Each object schema includes an object description, field definitions, and examples.
12
- [comment]: # (step {"action":"goTo", "url":"https://doc-detective.com/reference/"})
13
- [comment]: # (step {"action":"goTo", "url":"https://doc-detective.com/reference/schemas/config.html"})
14
- [comment]: # (step {"action":"find", "selector":"h2#description", "matchText":"Description"})
15
- [comment]: # (step {"action":"find", "selector":"h2#fields", "matchText":"Fields"})
16
- [comment]: # (step {"action":"find", "selector":"h2#examples", "matchText":"Examples"})
17
-
1
+ # Doc Detective documentation overview
2
+
3
+ [comment]: # (test start {"id":"search-kittens"})
4
+
5
+ [Doc Detective documentation](http://doc-detective.com) is split into a few key sections:
6
+ [comment]: # (step {"action":"goTo", "url":"http://doc-detective.com"})
7
+
8
+ - The landing page discusses what Doc Detective is, what it does, and who might find it useful.
9
+ - [Get started](https://doc-detective.com/get-started.html) covers how to quickly get up and running with Doc Detective.
10
+ [comment]: # (step {"action":"goTo", "url":"https://doc-detective.com/get-started.html"})
11
+ - The [references](https://doc-detective.com/reference/) detail the various JSON objects that Doc Detective expects for configs, test specifications, tests, actions, and more. Each object schema includes an object description, field definitions, and examples.
12
+ [comment]: # (step {"action":"goTo", "url":"https://doc-detective.com/reference/"})
13
+ [comment]: # (step {"action":"goTo", "url":"https://doc-detective.com/reference/schemas/config.html"})
14
+ [comment]: # (step {"action":"find", "selector":"h2#description", "matchText":"Description"})
15
+ [comment]: # (step {"action":"find", "selector":"h2#fields", "matchText":"Fields"})
16
+ [comment]: # (step {"action":"find", "selector":"h2#examples", "matchText":"Examples"})
17
+
18
18
  [comment]: # (test end)
@@ -1,3 +1,3 @@
1
- USER="John Doe"
2
- JOB="Software Engineer"
1
+ USER="John Doe"
2
+ JOB="Software Engineer"
3
3
  SECRET="YOUR_SECRET_KEY"