-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·102 lines (92 loc) · 2.58 KB
/
index.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env node
import { processFolder } from './src/main.js';
import readline from 'readline';
import yargs from 'yargs';
import chalk from 'chalk';
import fs from 'fs/promises';
import { hideBin } from 'yargs/helpers';
const fileExists = async (path) => {
return await fs.access(path).then(() => true).catch(() => false);
};
const folderEmpty = async (p) => {
try {
const outputFiles = await fs.readdir(p);
return outputFiles.length === 0;
} catch {
return false
}
};
const argv = yargs(hideBin(process.argv))
.command("$0 [input]", "default command", (yargs) => {
yargs.positional("input", {
describe: "Input folder path or file path",
default: "./docs",
});
})
.option("output", {
alias: "o",
type: "string",
describe: "Output folder path",
default: "./dist",
})
.option("tags", {
alias: "t",
type: "array",
describe: "Tags for the operation",
})
.option("force", {
alias: "f",
type: "boolean",
describe: "Force reset of output folder",
})
.option("baseUrl", {
alias: "base",
type: "string",
describe: "Base URL for sitemap",
}).argv;
console.log(chalk.blue("Input: "), chalk.green(argv.input));
if (argv.tags) {
console.log(
chalk.blue("Filtering match files by tags: "),
chalk.green(argv.tags)
);
}
console.log(chalk.blue("Output folder: "), chalk.green(argv.output));
const startProcessing = async () => {
// create output folder if doesn't exist, other wise erase output folder
const isFileExists = await fileExists(argv.output);
if (!isFileExists) {
await fs.mkdir(argv.output, { recursive: true });
} else {
await fs.rm(argv.output, { recursive: true });
}
const tagsKeyValue = argv.tags && argv.tags.map((tag) => tag.split(":"));
await processFolder(argv.input, argv.output, {
tags: tagsKeyValue,
// Default to option but take env variable if not provided, for sitemap generation
baseUrl: argv.baseUrl || process.env.BASE_URL,
});
};
const outputFolderExists = await fileExists(argv.output);
const needsToConfirm = !(argv.force || !outputFolderExists || await folderEmpty(argv.output));
if (needsToConfirm) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question(
chalk.red(
`Output folder ${argv.output} is not empty, do you want to continue? (y/n) `
),
async (answer) => {
if (answer !== 'y') {
console.log(chalk.red('Exiting...'));
process.exit(1);
}
rl.close();
await startProcessing();
}
);
} else {
await startProcessing();
};