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

Master #1

Merged
merged 5 commits into from
Sep 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/node_modules
.env
/dist
66 changes: 66 additions & 0 deletions cv-ssg.js
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"));
}
30 changes: 30 additions & 0 deletions helpers/createHtml.js
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;
};
11 changes: 11 additions & 0 deletions helpers/readFile.js
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"));
}
};
18 changes: 18 additions & 0 deletions helpers/validateFile.js
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;
};
61 changes: 61 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions package.json
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"
}
}