-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from tuanthanh2067/master
Master
- Loading branch information
Showing
7 changed files
with
213 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/node_modules | ||
.env | ||
/dist |
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,66 @@ | ||
const clear = require("clear"); | ||
const args = require("minimist")(process.argv.slice(2)); | ||
const fs = require("fs"); | ||
const chalk = require("chalk"); | ||
|
||
const { validateExtension, validateString } = require("./helpers/validateFile"); | ||
const { readFile } = require("./helpers/readFile"); | ||
const { createHtmlFile } = require("./helpers/createHtml"); | ||
|
||
// version | ||
if (args.version || args.v) { | ||
console.log(chalk.green(`v${require("./package.json").version}`)); | ||
return; | ||
} | ||
|
||
// help | ||
if (args.help || args.h) { | ||
console.log("--version || -v", "for version"); | ||
console.log("--input || -i", "file input"); | ||
return; | ||
} | ||
|
||
// stylesheet option | ||
let stylesheetLink; | ||
if (args.stylesheet || args.s) { | ||
console.log(args.stylesheet, args.s); | ||
stylesheetLink = args.stylesheet || args.s; | ||
stylesheetLink = stylesheetLink.trim(); | ||
} | ||
|
||
clear(); | ||
|
||
// file input | ||
let file; | ||
if (args.input || args.i) { | ||
file = args.input || args.i; | ||
file = file.trim(); | ||
} | ||
|
||
if (!validateString(file) || !validateExtension(file)) return; | ||
|
||
// read file and return an array of strings | ||
const data = readFile(file); | ||
if (!data) return; // nothing in the array | ||
|
||
// generate dom | ||
const dom = createHtmlFile(data, stylesheetLink); | ||
try { | ||
const folder = `${process.cwd()}/dist`; | ||
|
||
// remove dir | ||
fs.rmdirSync(folder, { recursive: true }); | ||
|
||
// create new dir | ||
fs.mkdirSync(folder); | ||
|
||
// remove extension | ||
const filename = file.replace(/\.[^/.]+$/, ""); | ||
|
||
// write the html to the dist folder | ||
fs.writeFileSync(`${folder}/${filename}.html`, dom); | ||
|
||
console.log(chalk.yellow("Convert to html successfully")); | ||
} catch (err) { | ||
console.log(chalk.yellow("Can not convert file")); | ||
} |
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,30 @@ | ||
module.exports.createHtmlFile = (data, stylesheetLink) => { | ||
let dom = ""; | ||
data.forEach((e) => { | ||
if (e !== "") { | ||
dom += `<p>${e}</p>`; | ||
} else { | ||
dom += "\n\n"; | ||
} | ||
}); | ||
const result = ` | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Filename</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
${ | ||
stylesheetLink | ||
? `<link rel='stylesheet' href='${stylesheetLink}'>` | ||
: "" | ||
} | ||
</head> | ||
<body> | ||
${dom} | ||
</body> | ||
</html> | ||
`; | ||
|
||
return result; | ||
}; |
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,11 @@ | ||
module.exports.readFile = (file) => { | ||
let results; | ||
try { | ||
results = require("fs").readFileSync(file, "utf-8"); | ||
|
||
return results.split(/\r?\n\r?\n/).map((e) => e.replace(/\r?\n/, " ")); | ||
} catch (err) { | ||
// TODO: check for errors before parsing when giving an invalid file | ||
console.log(require("chalk").yellow("Error occurred while reading file")); | ||
} | ||
}; |
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,18 @@ | ||
const chalk = require("chalk"); | ||
const path = require("path"); | ||
|
||
module.exports.validateString = (file) => { | ||
if (file === "" || typeof file !== "string") { | ||
console.log(chalk.yellow("wrong input, please try again")); | ||
return false; | ||
} | ||
return true; | ||
}; | ||
|
||
module.exports.validateExtension = (file) => { | ||
if (path.extname(file) !== ".txt") { | ||
console.log(chalk.yellow("wrong file extension, please try again")); | ||
return false; | ||
} | ||
return true; | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,24 @@ | ||
{ | ||
"name": "cv-ssg", | ||
"version": "0.1", | ||
"description": "", | ||
"main": "cv-ssg.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/tuanthanh2067/cv-ssg.git" | ||
}, | ||
"author": "Tuan Thanh Tan", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/tuanthanh2067/cv-ssg/issues" | ||
}, | ||
"homepage": "https://github.com/tuanthanh2067/cv-ssg#readme", | ||
"dependencies": { | ||
"chalk": "^4.1.2", | ||
"clear": "^0.1.0", | ||
"minimist": "^1.2.5" | ||
} | ||
} |