-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
174 lines (147 loc) · 4.97 KB
/
main.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env node
require('dotenv').config()
const { program } = require('commander')
const fs = require('fs')
const sr = require('./service')
const pjson = require('./package.json')
const {
textConverter,
createIndexFile,
convertExtension,
} = require('./src/text-converter')
// For adding custom flags into the command line
program.version(`${pjson.name}: version-${pjson.version}`)
const outputFolder = process.env.OUTPUT_DIRECTORY
program
.description('Program Custom Flags')
.option('-H, --help')
.option('-I, --input <type>', 'Input to the dist folder')
.option(
'-C, --config <type>',
'Output to the folder specified in config file'
)
.option('-O, --output <type>', 'Output to a custom folder')
.option(
'-L, --lang <type>',
'Changes generated lang attributes on the html root'
)
program.parse()
const options = program.opts()
if (options.help) {
console.log(
'\n--Commands--\nVersion: -V, --version\nInput: -I, --input\nOutput: -O, --output'
)
console.log(
'\n--Help--\nDescription: The program is used to display the content of a .txt file into that of an .html'
)
console.log(
"Output: Typing 'my-ssg --output(or)-O <directory_name> in the command line will generate a new directory where the HTML5 files will be added to"
)
console.log(
"Input: By typing 'my-ssg --input(or)-I file.txt' the program will generate a valid HTML5 file\n"
)
console.log(
"Config: By typing 'my-ssg --config(or)-C file.json' the program will generate a valid HTML5 file based on configuration specified\n"
)
console.log(
"Lang: By typing 'my-ssg --lang(or)-L <language> the program will add a 'lang' attribute on the root html"
)
}
try {
if (options.input) {
fs.rmSync(outputFolder, { recursive: true, force: true })
sr.createFolder(outputFolder)
const stats = fs.statSync(program.opts().input)
// Determines whether the value that was given is a directory or a file
if (stats.isDirectory()) {
const directoryName = program.opts().input
// read the directory, and go through each individual file name using forEach
fs.readdir(directoryName, (err, files) => {
let navLinks
files.forEach((file) => {
const outputFileName = convertExtension(file)
if (navLinks) {
navLinks += `,${outputFileName}`
} else {
navLinks = outputFileName
}
})
files.forEach((file) => {
const filename = `${directoryName}/${file}`
textConverter(filename, navLinks)
})
createIndexFile(outputFolder, navLinks)
})
} else if (stats.isFile()) {
const filename = program.opts().input
textConverter(filename)
}
}
if (options.output) {
// Prevents the user from creating a directory that currently exists within the program
// Resets the value of the directory to 'dist'
if (fs.existsSync(options.output)) {
console.log(
'The name given already exists as a directory or file',
"\nSet directory to default: 'dist'"
)
sr.replaceDirectory('dist')
} else {
sr.replaceDirectory(options.output)
}
}
if (options.lang) {
sr.changeLanguage(options.lang)
}
if (options.config) {
const lines = fs.readFileSync(options.config, 'utf8')
const parsedObj = JSON.parse(lines)
if (parsedObj.lang) {
sr.changeLanguage(parsedObj.lang)
}
if (parsedObj.output) {
// Prevents the user from creating a directory that currently exists within the program
// Resets the value of the directory to 'dist'
if (fs.existsSync(parsedObj.output)) {
console.log(
'The name given already exists as a directory or file',
"\nSet directory to default: 'dist'"
)
sr.replaceDirectory('dist')
} else {
sr.replaceDirectory(parsedObj.output)
}
}
if (parsedObj.input) {
fs.rmSync(outputFolder, { recursive: true, force: true })
sr.createFolder(outputFolder)
const stats = fs.statSync(parsedObj.input)
// Determines whether the value that was given is a directory or a file
if (stats.isDirectory()) {
const directoryName = parsedObj.input
// read the directory, and go through each individual file name using forEach
fs.readdir(directoryName, (err, files) => {
let navLinks
files.forEach((file) => {
const outputFileName = convertExtension(file)
if (navLinks) {
navLinks += `,${outputFileName}`
} else {
navLinks = outputFileName
}
})
files.forEach((file) => {
const filename = `${directoryName}/${file}`
textConverter(filename, navLinks)
})
createIndexFile(outputFolder, navLinks)
})
} else if (stats.isFile()) {
const filename = parsedObj.input
textConverter(filename)
}
}
}
} catch (err) {
console.error(err.message)
}