Skip to content

Commit

Permalink
Refactoring the project to improve code maintainability:
Browse files Browse the repository at this point in the history
 - Remove global variable fileExt and extracted fileExt function to helper.ts
 - Separate command-line argument parsing from index.ts file
 - Extract 'removeDir' function to helper.ts to avoid code duplication
 - Extract 'writeFile' and 'writeMutipleFiles' function to helper.ts to reduce code duplication
 - Update filename 'htmlCreator.ts' for consistency
 - Update function name 'readFile' for consistency
  • Loading branch information
seog-jun committed Oct 11, 2023
1 parent bbff6a4 commit f80844f
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 114 deletions.
146 changes: 33 additions & 113 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,115 +1,53 @@
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 {
fileExt,
removeDir,
writeFile,
writeMultipleFiles,
} from "./utility/helper";
import {
options,
arg,
langValue,
outputValue,
styleValue,
} from "./utility/argumentParser";
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`));

removeDir();
// a single .txt file is used as an input
if (fileExt === ".txt") {
if (fileExt(arg) === ".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,
""
)
);
writeFile(filename, arg, 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, "", "")
);
writeFile(filename, arg, "", langValue);
}
// option is not selected
else {
writeFile(filename, arg, "", "");
}
}
// a directory is used as an input
else if (!arg.includes(".")) {
const filesArray = readDirectory(path.join("../../", arg));

// if option -s is selected
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,
""
)
);
}
writeMultipleFiles(filesArray, arg, styleValue, "");
// if option -l is selected
} 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,
"",
""
)
);
}
writeMultipleFiles(filesArray, arg, "", langValue);
}
// option is not selected
else {
writeMultipleFiles(filesArray, arg, "", "");
}
} else {
console.log("Invalid fileName/dirName");
Expand All @@ -118,35 +56,17 @@ if (fs.existsSync(arg)) {
}
// 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}`));
removeDir(outputValue);

// a single .txt file is used as an input
if (fileExt === ".txt") {
if (fileExt(arg) === ".txt") {
const filename = path.parse(arg).name;
fs.writeFileSync(
path.join(__dirname, `../${outputValue}/${filename}.html`),
htmlCreator(syncReadFile(path.join("../../", arg)), filename)
);
writeFile(filename, arg, "", "", outputValue);
}
// 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
)
);
}
writeMultipleFiles(filesArray, arg, "", "", outputValue);
}
}
process.exit(0);
Expand Down
25 changes: 25 additions & 0 deletions src/utility/argumentParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Command } from "commander";
import myInfo from "../../package.json";

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");
export const options = program.opts();
program.parse(process.argv);
export const arg = program.args[0];
export const langValue = program.opts().lang;
export const outputValue = program.opts().output;
export const styleValue = program.opts().stylesheet;
88 changes: 88 additions & 0 deletions src/utility/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import path from "path";
import fs from "fs";
import { readFile } from "../utility/readFile";
import { htmlCreator } from "./htmlCreator";
export function fileExt(ext: string) {
if (ext) {
return ext.substring(ext.lastIndexOf("."));
} else {
return process.exit(-1);
}
}

export function removeDir(outputValue: string = "") {
if (outputValue) {
// 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}`));
} else {
fs.rmSync(path.join(__dirname, `../../til`), {
recursive: true,
force: true,
});
fs.mkdirSync(path.join(__dirname, `../../til`));
}
}

export function writeFile(
filename: string,
arg: string,
styleValue: string = "",
langValue: string = "",
outputValue: string = ""
) {
if (outputValue) {
fs.writeFileSync(
path.join(__dirname, `../../${outputValue}/${filename}.html`),
htmlCreator(readFile(path.join("../../", arg)), filename)
);
} else {
fs.writeFileSync(
path.join(__dirname, `../../til/${filename}.html`),
htmlCreator(
readFile(path.join("../../", arg)),
filename,
styleValue,
langValue
)
);
}
}

export function writeMultipleFiles(
filesArray: string[],
arg: string,
styleValue: string = "",
langValue: string = "",
outputValue: string = ""
) {
if (outputValue) {
for (let i = 0; i < filesArray.length; i++) {
const filename = path.parse(filesArray[i]).name;
fs.writeFileSync(
path.join(__dirname, `../../${outputValue}/${filename}.html`),
htmlCreator(readFile(path.join("../../", arg, filesArray[i])), filename)
);
}
} 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(
readFile(path.join("../../", arg, filesArray[i])),
filename,
styleValue,
langValue
)
);
}
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion src/utility/readFile.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from "path";
import fs from "fs";

export function syncReadFile(filename: string) {
export function readFile(filename: string) {
try {
const contents = fs.readFileSync(path.join(__dirname, filename), "utf-8");
const arr = contents.split(/\r?\n/);
Expand Down

0 comments on commit f80844f

Please sign in to comment.