diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4fb826c --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/node_modules +.env +/dist \ No newline at end of file diff --git a/cv-ssg.js b/cv-ssg.js new file mode 100644 index 0000000..ba5ea89 --- /dev/null +++ b/cv-ssg.js @@ -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")); +} diff --git a/helpers/createHtml.js b/helpers/createHtml.js new file mode 100644 index 0000000..3ddfcaf --- /dev/null +++ b/helpers/createHtml.js @@ -0,0 +1,30 @@ +module.exports.createHtmlFile = (data, stylesheetLink) => { + let dom = ""; + data.forEach((e) => { + if (e !== "") { + dom += `

${e}

`; + } else { + dom += "\n\n"; + } + }); + const result = ` + + + + + Filename + + ${ + stylesheetLink + ? `` + : "" + } + + + ${dom} + + + `; + + return result; +}; diff --git a/helpers/readFile.js b/helpers/readFile.js new file mode 100644 index 0000000..3bd17bf --- /dev/null +++ b/helpers/readFile.js @@ -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")); + } +}; diff --git a/helpers/validateFile.js b/helpers/validateFile.js new file mode 100644 index 0000000..1ff8658 --- /dev/null +++ b/helpers/validateFile.js @@ -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; +}; diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..11715ce --- /dev/null +++ b/package-lock.json @@ -0,0 +1,61 @@ +{ + "name": "cv-ssg", + "version": "0.1", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "requires": { + "color-convert": "^2.0.1" + } + }, + "chalk": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", + "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "requires": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + } + }, + "clear": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/clear/-/clear-0.1.0.tgz", + "integrity": "sha512-qMjRnoL+JDPJHeLePZJuao6+8orzHMGP04A8CdwCNsKhRbOnKRjefxONR7bwILT3MHecxKBjHkKL/tkZ8r4Uzw==" + }, + "color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "requires": { + "color-name": "~1.1.4" + } + }, + "color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" + }, + "has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==" + }, + "minimist": { + "version": "1.2.5", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", + "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==" + }, + "supports-color": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", + "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "requires": { + "has-flag": "^4.0.0" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..c80f4ea --- /dev/null +++ b/package.json @@ -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" + } +}