Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes issue #14 by adding support for -C option #15

Merged
merged 9 commits into from
Oct 13, 2022
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ Write "my-ssg" before using any commands or it will not work
**Command Structure**: "my-ssg --input [fileName/directoryName]"
<br />
<br />
**Config** (--config, -C)
<br />
&nbsp;&nbsp;This command will allow the user to be able to pass a .json file with supported configuration keys such as input, output, and lang. The command will then be able to convert .txt or .md files specified in the input property to that of an HTML5. The output directory by default is dist, but if output key is defined in config file, the command will replace the default directory with the value of output key.
<br />
**Command Structure**: "my-ssg --config [fileName]"
<br />
**Output** (--output, -O)
<br />
&nbsp;&nbsp;This command will allow the user to change to create their own default folder for all the HTML5 that they convert.
Expand Down Expand Up @@ -58,10 +64,10 @@ Write "my-ssg" before using any commands or it will not work
- [ ] Allow for multi-level directory conversion
- [ ] Generate an index.js which contains all links to the HTML files within a directory
- [ ] Stylized each Generated HTML page
<<<<<<< HEAD
=======
<<<<<<< HEAD
=======
- [x] Add Language Command
>>>>>>> 7b62708eb7b2d10f174df447e33d0cf2cd7530c3
> > > > > > > 7b62708eb7b2d10f174df447e33d0cf2cd7530c3

<br />

Expand Down
49 changes: 49 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ 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>`,
Expand All @@ -40,6 +44,9 @@ if (options.help) {
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"
);
Expand Down Expand Up @@ -81,6 +88,48 @@ try {
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) {
if (fs.existsSync(parsedObj.output)) {
console.log("The name given already exists as a directory or file");
console.log("Set directory to default: 'dist'");
sr.replaceDirectory("dist");
}
sr.replaceDirectory(parsedObj.output);
fs.rmSync(outputFolder, { recursive: true, force: true });
sr.createFolder(outputFolder);
}

if (!parsedObj.output) {
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) => {
files.forEach((file) => {
const filename = `${directoryName}/${file}`;
textConverter(filename);
});
});
} else if (stats.isFile()) {
const filename = parsedObj.input;
textConverter(filename);
}
}
} catch (err) {
console.error(err.message);
}
21 changes: 6 additions & 15 deletions src/text-converter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,13 @@ module.exports = {
},
//Lab 3: Created a function to prevent repetition when changing key-pair values
readFileEnv: (key, msg, str) => {
let result;
let result = fs.readFileSync("./.env", { encoding: "utf8", flag: "r" });

fs.readFile("./.env", "utf-8", (err, contents) => {
if (err) {
console.log(err);
return;
}
if (key == "HTML_LANGUAGE")
result = contents.replace(process.env.HTML_LANGUAGE, str);
else if (key == "OUTPUT_DIRECTORY")
result = contents.replace(process.env.OUTPUT_DIRECTORY, str);

fs.writeFile("./.env", result, "utf-8", () => {
console.log(`${msg} "${str}"`);
});
});
if (key == "HTML_LANGUAGE")
result = result.replace(process.env.HTML_LANGUAGE, str);
else if (key == "OUTPUT_DIRECTORY")
result = result.replace(process.env.OUTPUT_DIRECTORY, str);
fs.writeFileSync("./.env", result);
},
};

Expand Down