doc-detective 2.19.1-dev.0 → 3.0.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/index.js CHANGED
@@ -1,79 +1,49 @@
1
- #!/usr/bin/env node
2
-
3
- const { runTests, runCoverage } = require("doc-detective-core");
4
- const { setArgs, setConfig, outputResults, setMeta } = require("./utils");
5
- const { argv } = require("node:process");
6
- const path = require("path");
7
- const fs = require("fs");
8
- const prompt = require("prompt-sync")();
9
-
10
- function complete(commands) {
11
- return function (str) {
12
- var i;
13
- var ret = [];
14
- for (i = 0; i < commands.length; i++) {
15
- if (commands[i].indexOf(str) == 0) ret.push(commands[i]);
16
- }
17
- return ret;
18
- };
19
- }
20
-
21
- // Run
22
- setMeta();
23
- main(argv);
24
-
25
- // Run
26
- async function main(argv) {
27
- // Find index of `doc-detective` or `run` in argv
28
- const index = argv.findIndex(
29
- (arg) => arg.endsWith("doc-detective") || arg.endsWith("index.js")
30
- );
31
- // `command` is the next argument after `doc-detective` or `src/index.js`
32
- let command = argv[index + 1];
33
- // Set args
34
- argv = setArgs(argv);
35
- // Get .doc-detective.json config, if it exists
36
- const configPath = path.resolve(process.cwd(), ".doc-detective.json");
37
- let config = {};
38
- if (fs.existsSync(configPath)) {
39
- config = require(configPath);
40
- }
41
- // Set config
42
- config = await setConfig(config, argv);
43
- command = command || config.defaultCommand;
44
- // If no command, prompt user to select a command
45
- if (command !== "runTests" && command !== "runCoverage") {
46
- const ask = `
47
- Welcome to Doc Detective. Choose a command:
48
- - 'runTests' - Run tests defined in specifications and documentation source files.
49
- - 'runCoverage' - Calculate test coverage of doc content.
50
-
51
- You can skip this next time by running 'npx doc-detective <command>'. You can also set 'defaultCommand' in your .doc-detective.json config file.
52
-
53
- For more info, visit https://doc-detective.com.
54
-
55
- Command: `;
56
- command = prompt({
57
- ask,
58
- value: "runTests",
59
- autocomplete: complete(["runTests", "runCoverage"]),
60
- });
61
- }
62
-
63
- // Run command
64
- let results = {};
65
- let output;
66
- if (command === "runCoverage") {
67
- output = config?.runCoverage?.output || config.output;
68
- results = await runCoverage(config);
69
- } else if (command === "runTests") {
70
- output = config?.runTests?.output || config.output;
71
- results = await runTests(config);
72
- } else {
73
- console.error(`Sorry, that's not a recognized command. Please try again.`);
74
- process.exit(1);
75
- }
76
-
77
- // Output results
78
- await outputResults(config, output, results, { command });
79
- }
1
+ #!/usr/bin/env node
2
+
3
+ const { runTests, runCoverage } = require("doc-detective-core");
4
+ const { readFile } = require("doc-detective-common");
5
+ const { setArgs, setConfig, outputResults, setMeta } = require("./utils");
6
+ const { argv } = require("node:process");
7
+ const path = require("path");
8
+ const fs = require("fs");
9
+
10
+ // Run
11
+ setMeta();
12
+ main(argv);
13
+
14
+ // Run
15
+ async function main(argv) {
16
+ // Find index of `doc-detective` or `run` in argv
17
+ const index = argv.findIndex(
18
+ (arg) => arg.endsWith("doc-detective") || arg.endsWith("index.js")
19
+ );
20
+ // Set args
21
+ argv = setArgs(argv);
22
+ // Get .doc-detective JSON or YAML config, if it exists
23
+ const configPathJSON = path.resolve(process.cwd(), ".doc-detective.json");
24
+ const configPathYAML = path.resolve(process.cwd(), ".doc-detective.yaml");
25
+ const configPathYML = path.resolve(process.cwd(), ".doc-detective.yml");
26
+ const configPath = fs.existsSync(argv.config)
27
+ ? argv.config
28
+ : fs.existsSync(configPathJSON)
29
+ ? configPathJSON
30
+ : fs.existsSync(configPathYAML)
31
+ ? configPathYAML
32
+ : fs.existsSync(configPathYML)
33
+ ? configPathYML
34
+ : null;
35
+ // If config file exists, read it
36
+ let config = {};
37
+ if (configPath) {
38
+ config = await readFile({ fileURLOrPath: configPath });
39
+ }
40
+ // Set config
41
+ config = await setConfig(config, argv, configPath || ".");
42
+
43
+ // Run tests
44
+ const output = config.output;
45
+ const results = await runTests(config);
46
+
47
+ // Output results
48
+ await outputResults(config, output, results, { command: "runTests" });
49
+ }