-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring the project to improve code maintainability:
- 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
Showing
5 changed files
with
147 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters