-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·70 lines (62 loc) · 1.89 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env node
"use strict";
const argv = require("yargs")
.usage("Usage: $0 <directory-path> [<output-file>] [options]")
.example(
"$0 ./Development/tools-and-resources dev-tools-and-resources --depth 3 --ignore 'code-snippets'"
)
.command({
command: "<directory-path> [<output-file>] [options]",
desc: "Specify the directory to crawl, and optionally the output file name"
})
.option("depth", {
type: "number",
alias: "d",
describe: "Max sub-directory depth to search into",
choices: [2, 3, 4, 5, 6],
default: 5
})
.option("ignore", {
type: "array",
alias: "i",
describe: "Ignore directories in the base- and sub-directories",
default: []
})
.option("description", {
type: "string",
alias: "t",
describe: "Describe the generated resource"
})
.option("log", {
type: "boolean",
alias: "l",
describe: "Create log file for errors in link retrieval"
})
.demandCommand(1, "Please provide a directory to crawl")
.help().argv;
const path = require("path");
const print = require("./lib/print");
const { setOutputFile } = require("./lib/helpers");
let dirpath = argv._[0];
let outputFileName = argv._[1];
const options = {};
options.depth = argv.depth;
options.ignore = argv.ignore;
options.description = argv.description;
options.log = argv.log;
if (path.isAbsolute(dirpath) === false) dirpath = path.resolve(dirpath);
const baseName = path.parse(dirpath).base;
if (path.extname(baseName)) {
console.error(
`Path provided is a file path. Did you mean "${path.dirname(dirpath)}"?`
);
return;
}
if (!outputFileName) outputFileName = baseName;
const outputFile = path.join("output", setOutputFile(outputFileName));
// const outputFile = path.join(dirpath, setOutputFile(outputFileName)); // switch back to generate in crawled folder
try {
print(outputFile, dirpath, options);
} catch (error) {
console.error(error);
}