-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
156 lines (149 loc) · 4.9 KB
/
index.ts
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { Command } from "commander";
import myInfo from "../package.json";
import { syncReadFile } from "./utility/readFile";
import { htmlCreator } from "./utility/writeFile";
import { readDirectory } from "./utility/readDirectory";
import path from "path";
import fs from "fs";
const program = new Command();
// Command line inter face to use options
program
.name("ts-node src/index.ts")
.usage("<fileName/dirName> -o output <dirname> -s <stylesheetURL>");
program.version(
myInfo.name + " " + myInfo.version,
"-v, --version",
"outputs the current version"
);
program.argument("<fileName/dirName>", "converts file to html");
program.option("-l, --lang <language>", "indicates the language to use");
program.option("-o, --output <dirName>", "creates a specified directory");
program.option("-s, --stylesheet <stylesheetURL>", "sets a stylesheet to HTML");
const options = program.opts();
program.parse(process.argv);
const arg = program.args[0];
const langValue = program.opts().lang;
const outputValue = program.opts().output;
const styleValue = program.opts().stylesheet;
const fileExt = arg.substring(arg.lastIndexOf("."));
if (fs.existsSync(arg)) {
{
fs.rmSync(path.join(__dirname, `../til`), { recursive: true, force: true });
fs.mkdirSync(path.join(__dirname, `../til`));
// a single .txt file is used as an input
if (fileExt === ".txt") {
const filename = path.parse(arg).name;
// if option -s is selected
if (options.stylesheet) {
fs.writeFileSync(
path.join(__dirname, `../til/${filename}.html`),
htmlCreator(
syncReadFile(path.join("../../", arg)),
filename,
styleValue,
""
)
);
// if option -l is selected
} else if (options.lang) {
fs.writeFileSync(
path.join(__dirname, `../til/${filename}.html`),
htmlCreator(
syncReadFile(path.join("../../", arg)),
filename,
"",
langValue
)
);
} else {
fs.writeFileSync(
path.join(__dirname, `../til/${filename}.html`),
htmlCreator(syncReadFile(path.join("../../", arg)), filename, "", "")
);
}
}
// a directory is used as an input
else if (!arg.includes(".")) {
const filesArray = readDirectory(path.join("../../", arg));
if (options.stylesheet) {
for (let i = 0; i < filesArray.length; i++) {
const filename = path.parse(filesArray[i]).name;
fs.writeFileSync(
path.join(__dirname, `../til/${filename}.html`),
htmlCreator(
syncReadFile(path.join("../../", arg, filesArray[i])),
filename,
styleValue,
""
)
);
}
} else if (options.lang) {
for (let i = 0; i < filesArray.length; i++) {
const filename = path.parse(filesArray[i]).name;
fs.writeFileSync(
path.join(__dirname, `../til/${filename}.html`),
htmlCreator(
syncReadFile(path.join("../../", arg, filesArray[i])),
filename,
"",
langValue
)
);
}
} else {
for (let i = 0; i < filesArray.length; i++) {
const filename = path.parse(filesArray[i]).name;
fs.writeFileSync(
path.join(__dirname, `../til/${filename}.html`),
htmlCreator(
syncReadFile(path.join("../../", arg, filesArray[i])),
filename,
"",
""
)
);
}
}
} else {
console.log("Invalid fileName/dirName");
process.exit(-1);
}
}
// options is output, overwrite the file/dir above
if (options.output) {
// remove the existing dir and til directory
fs.rmSync(path.join(__dirname, `../til`), { recursive: true, force: true });
fs.rmSync(path.join(__dirname, `../${outputValue}`), {
recursive: true,
force: true,
});
fs.mkdirSync(path.join(__dirname, `../${outputValue}`));
// a single .txt file is used as an input
if (fileExt === ".txt") {
const filename = path.parse(arg).name;
fs.writeFileSync(
path.join(__dirname, `../${outputValue}/${filename}.html`),
htmlCreator(syncReadFile(path.join("../../", arg)), filename)
);
}
// a directory is used as an input
else {
const filesArray = readDirectory(path.join("../../", arg));
for (let i = 0; i < filesArray.length; i++) {
const filename = path.parse(filesArray[i]).name;
fs.writeFileSync(
path.join(__dirname, `../${outputValue}/${filename}.html`),
htmlCreator(
syncReadFile(path.join("../../", arg, filesArray[i])),
filename
)
);
}
}
}
process.exit(0);
} else {
console.log("Please type valid fileName/dirName");
process.exit(-1);
}