diff --git a/.gitignore b/.gitignore index 124b453..a567e48 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules /Sherlock Holmes Selected Stories/ z -dist \ No newline at end of file +dist +z.txt diff --git a/README.md b/README.md index 00e80bc..bf5df1e 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ node index.js -argument --argument input ``` To get quick instructions on the arguments do: - `-h` or `--help` +`-h` or `--help` To check the version of the app: @@ -45,13 +45,11 @@ To check the version of the app: `-v` or `--version` - To change language of the page (it's set as `en` by default. It will be put in as an attribute to ``, such as `` ): `-l` or `--lang` - To change the default directory from where the files would be read or the specific file: `-i` or `--input` @@ -60,7 +58,7 @@ To specify a config file to read from: `-c` or `--config` -\ + Example of usage: ``` node index.js -i cats diff --git a/index.js b/index.js index 38c13a9..264b0dc 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,8 @@ -const fs = require ('fs') +const fs = require('fs') const path = require('path') const { exit } = require('process') const { name, version } = require('../SSGNode/package.json') +const myUtils = require("./utils") let sourcePath = "./Sherlock Holmes Selected Stories/The Adventure of the Six Napoleans.txt" let endPath = "./dist" @@ -9,30 +10,27 @@ let lang = "en" let config = "" function run() { - let texts = ""; - let title = ""; - let html = ""; - if(fs.lstatSync(sourcePath).isDirectory()) { + if (fs.lstatSync(sourcePath).isDirectory()) { try { - fs.readdir(sourcePath, (err, files)=> { - if (err) { - throw err; - } - files.forEach(function(file){ - if(file.match(".*(\.txt|\.md)$")){ - console.log(file); - console.log("File mathched:"); - console.log(file); - let contentArray = readFile(`${sourcePath}/${file}`) - pageGenerator(contentArray) + fs.readdir(sourcePath, (err, files) => { + if (err) { + throw err; } + files.forEach(function (file) { + if (file.match(".*(\.txt|\.md)$")) { + console.log(file); + console.log("File mathched:"); + console.log(file); + let contentArray = readFile(`${sourcePath}/${file}`) + pageGenerator(contentArray) + } + }) }) - }) - } catch(err) { - console.log(`Error when reading a directory in ${sourcePath}.`); - console.log(err); - exit(-1); - } + } catch (err) { + console.log(`Error when reading a directory in ${sourcePath}.`); + console.log(err); + exit(-1); + } } else { try { let contentArray = readFile(sourcePath); @@ -43,30 +41,30 @@ function run() { exit(-1); } } - console.log("Website generated"); + console.log(`Website generated from ${sourcePath}`); exit(0); } function readFile(file) { - if(file.match(".*(\.txt|\.md)$")){ + if (file.match(".*(\.txt|\.md)$")) { //read the file let fullText = fs.readFileSync(file, 'utf8'); - //formatting if it's an .md file - if(file.match(".*(\.md)$")){ - //replacing strings - fullText = fullText.replace(/_ /g, "") - fullText = fullText.replace(/ _/g, "") - fullText = fullText.replace(/__ /g, "") - fullText = fullText.replace(/ __/g, "") - fullText = fullText.replace(/### /g, "

") - fullText = fullText.replace(/ ###/g, "

") - fullText = fullText.replace(/## /g, "

") - fullText = fullText.replace(/ ##/g, "

") - fullText = fullText.replace(/# /g, "

") - fullText = fullText.replace(/ #/g, "

") + //formatting if it's an .md file + if (file.match(".*(\.md)$")) { + //replacing strings + fullText = fullText.replace(/_ /g, "") + fullText = fullText.replace(/ _/g, "") + fullText = fullText.replace(/__ /g, "") + fullText = fullText.replace(/ __/g, "") + fullText = fullText.replace(/### /g, "

") + fullText = fullText.replace(/ ###/g, "

") + fullText = fullText.replace(/## /g, "

") + fullText = fullText.replace(/ ##/g, "

") + fullText = fullText.replace(/# /g, "

") + fullText = fullText.replace(/ #/g, "

") - fullText = fullText.replace(/---/g, "
") - } + fullText = fullText.replace(/---/g, "
") + } //future functionality of choosing the element you want to use let element = "p"; @@ -77,8 +75,8 @@ function readFile(file) { let htmlParagraphsArray = []; //put them all into an array - for (let i=0;i${title}`); else { htmlParagraphsArray.push(`<${element}>${paragraphs[i]}`); @@ -87,7 +85,7 @@ function readFile(file) { //put them all into a single string, every paragraph starts from a new line texts = htmlParagraphsArray.join('\n'); - return {"texts": texts, "title": title}; + return { "texts": texts, "title": title }; } } @@ -124,34 +122,17 @@ function genPage(texts, title) { } function output(html) { - mkExist(endPath); + myUtils.mkExist(endPath); deleteFiles(endPath); generateFile(html); genCss(endPath); } -//if folder/file doesn't exist, create it -function mkExist(toCreate, ifFolder=true) { - try{ - if(!fs.existsSync(toCreate)) { - if(ifFolder) { - fs.mkdirSync(toCreate); - } else { - fs.writeFileSync(toCreate, "utf8"); - } - } - } catch (err) { - console.log(`Error when creating a ${ifFolder?"folder":"file"} ${toCreate}.`); - console.log(err); - exit(-1); - } -} - //to remove all previous files function deleteFiles(folder) { - try{ - fs.rmSync(folder, {recursive:true}, err => { - if (err) throw {err}; + try { + fs.rmSync(folder, { recursive: true }, err => { + if (err) throw { err }; }); } catch (err) { console.log(`Error when deleting files in ${folder}.`); @@ -161,12 +142,12 @@ function deleteFiles(folder) { } function generateFile(html) { - mkExist(endPath); + myUtils.mkExist(endPath); - try{ - fs.writeFileSync(`${endPath}/output.html`, html, function(err) { - if(err) { - return console.log(err); + try { + fs.writeFileSync(`${endPath}/output.html`, html, function (err) { + if (err) { + return console.log(err); } console.log("web page generated"); }); @@ -177,36 +158,37 @@ function generateFile(html) { } } -function configReader(){ +function configReader() { try { //parse the file and check for the arguments. const configJSON = JSON.parse(fs.readFileSync(config, 'utf8')); - if(configJSON.lang !== "" && configJSON.lang !== undefined){ + if (configJSON.lang) { lang = configJSON.lang; } //validate the input for a file/folder else throws an error and close to program. - if(fs.lstatSync(configJSON.input).isDirectory() || fs.lstatSync(configJSON.input).isFile()){ + if (fs.lstatSync(configJSON.input).isDirectory() || fs.lstatSync(configJSON.input).isFile()) { sourcePath = configJSON.input; } - + //Output folder else default to ./dist - if(configJSON.output !== "" && configJSON.output !== undefined){ + if (configJSON.output) { endPath = configJSON.output; } } catch (error) { - console.log(`ERROR: ${error}`); + console.log("Error in reading a config file!"); + console.log(error); exit(-1); } } function genCss(dir) { - mkExist(dir); - mkExist(`${dir}/styles.css`, false); + myUtils.mkExist(dir); + myUtils.mkExist(`${dir}/styles.css`, false); - try{ + try { fs.copyFileSync("./styles.css", `${dir}/styles.css`); - } catch(err) { + } catch (err) { console.log(`Error when copying a css file to ${dir}.`); console.log(err); exit(-1); @@ -221,18 +203,18 @@ let pathchanged = false; let langchanged = false; let configchanged = false; -process.argv.forEach(function (val, index, array) { +process.argv.forEach(function (val) { //if path isn't the default one, change it for this next value - if(pathchanged){ - if(!val.match("^-")) { + if (pathchanged) { + if (!val.match("^-")) { //means this might be the directory sourcePath = val; console.log(`Path is now ${val}`); } - + } - if(langchanged){ - if(!val.match("^-")) { + if (langchanged) { + if (!val.match("^-")) { //means this might be the lang lang = val; console.log(`Lang is now ${val}`); @@ -240,7 +222,7 @@ process.argv.forEach(function (val, index, array) { } if (configchanged) { - if(!val.match("^-")) { + if (!val.match("^-")) { //means this might be the config config = val; console.log(`Config file is now ${config}`); @@ -248,7 +230,7 @@ process.argv.forEach(function (val, index, array) { } } - switch(val) { + switch (val) { case "--version": case "-v": console.log(`${name}, version ${version}.`); @@ -271,8 +253,8 @@ process.argv.forEach(function (val, index, array) { console.log("Hi, nice person. Hope you have a nice time of the day. If not, maybe this will help ( ._.)_ <3"); break; case "--input": - case "-i": - pathchanged=true; + case "-i": + pathchanged = true; break; case "--lang": case "-l": diff --git a/package-lock.json b/package-lock.json index bbf260d..1cb7a73 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,351 +1,16 @@ { "name": "ssgnode", "version": "0.0.1", - "lockfileVersion": 1, + "lockfileVersion": 2, "requires": true, - "dependencies": { - "align-text": { - "version": "0.1.4", - "resolved": "https://registry.npmjs.org/align-text/-/align-text-0.1.4.tgz", - "integrity": "sha1-DNkKVhCT810KmSVsIrcGlDP60Rc=", - "requires": { - "kind-of": "^3.0.2", - "longest": "^1.0.1", - "repeat-string": "^1.5.2" - }, - "dependencies": { - "kind-of": { - "version": "3.2.2", - "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-3.2.2.tgz", - "integrity": "sha1-MeohpzS6ubuw8yRm2JOupR5KPGQ=", - "requires": { - "is-buffer": "^1.1.5" - } - } + "packages": { + "": { + "name": "ssgnode", + "version": "0.0.1", + "license": "MIT", + "engines": { + "node": ">=14.14.0" } - }, - "any": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/any/-/any-1.0.0.tgz", - "integrity": "sha1-Cns0jFifr2unCNSiJBBp6lFii14=", - "requires": { - "for-own": "^0.1.2", - "make-iterator": "^0.1.0" - }, - "dependencies": { - "make-iterator": { - "version": "0.1.1", - "resolved": "https://registry.npmjs.org/make-iterator/-/make-iterator-0.1.1.tgz", - "integrity": "sha1-hz0nuBmKRlqBSDtvXRbaToY+z1s=", - "requires": { - "for-own": "^0.1.1" - } - } - } - }, - "arr-diff": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/arr-diff/-/arr-diff-1.1.0.tgz", - "integrity": "sha1-aHwydYFjWI/vfeezb6vklesaOZo=", - "requires": { - "arr-flatten": "^1.0.1", - "array-slice": "^0.2.3" - } - }, - "arr-flatten": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/arr-flatten/-/arr-flatten-1.1.0.tgz", - "integrity": "sha512-L3hKV5R/p5o81R7O02IGnwpDmkp6E982XhtbuwSe3O4qOtMMMtodicASA1Cny2U+aCXcNpml+m4dPsvsJ3jatg==" - }, - "arr-map": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/arr-map/-/arr-map-2.0.2.tgz", - "integrity": "sha1-Onc0X/wc814qkYJWAfnljy4kysQ=", - "requires": { - "make-iterator": "^1.0.0" - }, - "dependencies": { - "kind-of": { - "version": "6.0.3", - "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-6.0.3.tgz", - "integrity": "sha512-dcS1ul+9tmeD95T+x28/ehLgd9mENa3LsvDTtzm3vyBEO7RPptvAD+t44WVXaUjTBRcrpFeFlC8WCruUR456hw==" - }, - "make-iterator": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/make-iterator/-/make-iterator-1.0.1.tgz", - "integrity": "sha512-pxiuXh0iVEq7VM7KMIhs5gxsfxCux2URptUQaXo4iZZJxBAzTPOLE2BumO5dbfVYq/hBJFBR/a1mFDmOx5AGmw==", - "requires": { - "kind-of": "^6.0.2" - } - } - } - }, - "arr-union": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/arr-union/-/arr-union-3.1.0.tgz", - "integrity": "sha1-45sJrqne+Gao8gbiiK9jkZuuOcQ=" - }, - "array-each": { - "version": "0.1.1", - "resolved": "https://registry.npmjs.org/array-each/-/array-each-0.1.1.tgz", - "integrity": "sha1-xdUrqCJfNtcoF4unrsQTrPrd0Pk=" - }, - "array-slice": { - "version": "0.2.3", - "resolved": "https://registry.npmjs.org/array-slice/-/array-slice-0.2.3.tgz", - "integrity": "sha1-3Tz7gO15c6dRF82sabC5nshhhvU=" - }, - "array-unique": { - "version": "0.2.1", - "resolved": "https://registry.npmjs.org/array-unique/-/array-unique-0.2.1.tgz", - "integrity": "sha1-odl8yvy8JiXMcPrc6zalDFiwGlM=" - }, - "center-align": { - "version": "0.1.3", - "resolved": "https://registry.npmjs.org/center-align/-/center-align-0.1.3.tgz", - "integrity": "sha1-qg0yYptu6XIgBBHL1EYckHvCt60=", - "requires": { - "align-text": "^0.1.3", - "lazy-cache": "^1.0.3" - } - }, - "export-dirs": { - "version": "0.2.4", - "resolved": "https://registry.npmjs.org/export-dirs/-/export-dirs-0.2.4.tgz", - "integrity": "sha1-Lha5tG68LAestlfM3744RTk/50I=" - }, - "export-files": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/export-files/-/export-files-2.1.1.tgz", - "integrity": "sha1-u/ZFdAU6CeTrmOX0NQHVcrLDzn8=", - "requires": { - "lazy-cache": "^1.0.3" - } - }, - "for-in": { - "version": "0.1.8", - "resolved": "https://registry.npmjs.org/for-in/-/for-in-0.1.8.tgz", - "integrity": "sha1-2Hc5COMSVhCZUrH9ubP6hn0ndeE=" - }, - "for-own": { - "version": "0.1.5", - "resolved": "https://registry.npmjs.org/for-own/-/for-own-0.1.5.tgz", - "integrity": "sha1-UmXGgaTylNq78XyVCbZ2OqhFEM4=", - "requires": { - "for-in": "^1.0.1" - }, - "dependencies": { - "for-in": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz", - "integrity": "sha1-gQaNKVqBQuwKxybG4iAMMPttXoA=" - } - } - }, - "fs": { - "version": "0.0.1-security", - "resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz", - "integrity": "sha1-invTcYa23d84E/I4WLV+yq9eQdQ=" - }, - "has-values": { - "version": "0.1.4", - "resolved": "https://registry.npmjs.org/has-values/-/has-values-0.1.4.tgz", - "integrity": "sha1-bWHeldkd/Km5oCCJrThL/49it3E=" - }, - "is-buffer": { - "version": "1.1.6", - "resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-1.1.6.tgz", - "integrity": "sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==" - }, - "is-extendable": { - "version": "0.1.1", - "resolved": "https://registry.npmjs.org/is-extendable/-/is-extendable-0.1.1.tgz", - "integrity": "sha1-YrEQ4omkcUGOPsNqYX1HLjAd/Ik=" - }, - "is-number": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/is-number/-/is-number-2.1.0.tgz", - "integrity": "sha1-Afy7s5NGOlSPL0ZszhbezknbkI8=", - "requires": { - "kind-of": "^3.0.2" - }, - "dependencies": { - "kind-of": { - "version": "3.2.2", - "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-3.2.2.tgz", - "integrity": "sha1-MeohpzS6ubuw8yRm2JOupR5KPGQ=", - "requires": { - "is-buffer": "^1.1.5" - } - } - } - }, - "is-plain-object": { - "version": "2.0.4", - "resolved": "https://registry.npmjs.org/is-plain-object/-/is-plain-object-2.0.4.tgz", - "integrity": "sha512-h5PpgXkWitc38BBMYawTYMWJHFZJVnBquFE57xFpjB8pJFiF6gZ+bU+WyI/yqXiFR5mdLsgYNaPe8uao6Uv9Og==", - "requires": { - "isobject": "^3.0.1" - } - }, - "isobject": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/isobject/-/isobject-3.0.1.tgz", - "integrity": "sha1-TkMekrEalzFjaqH5yNHMvP2reN8=" - }, - "kind-of": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-2.0.1.tgz", - "integrity": "sha1-AY7HpM5+OobLkUG+UZ0kyPqpgbU=", - "requires": { - "is-buffer": "^1.0.2" - } - }, - "lazy-cache": { - "version": "1.0.4", - "resolved": "https://registry.npmjs.org/lazy-cache/-/lazy-cache-1.0.4.tgz", - "integrity": "sha1-odePw6UEdMuAhF07O24dpJpEbo4=" - }, - "longest": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/longest/-/longest-1.0.1.tgz", - "integrity": "sha1-MKCy2jj3N3DoKUoNIuZiXtd9AJc=" - }, - "make-iterator": { - "version": "0.2.1", - "resolved": "https://registry.npmjs.org/make-iterator/-/make-iterator-0.2.1.tgz", - "integrity": "sha1-oZxmATK1SubWT4gewUBWx0bb6XI=", - "requires": { - "kind-of": "^3.0.3" - }, - "dependencies": { - "kind-of": { - "version": "3.2.2", - "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-3.2.2.tgz", - "integrity": "sha1-MeohpzS6ubuw8yRm2JOupR5KPGQ=", - "requires": { - "is-buffer": "^1.1.5" - } - } - } - }, - "object.defaults": { - "version": "0.3.0", - "resolved": "https://registry.npmjs.org/object.defaults/-/object.defaults-0.3.0.tgz", - "integrity": "sha1-seucvHjEx71WysbK496tWnETiCo=", - "requires": { - "array-each": "^0.1.0", - "array-slice": "^0.2.3", - "for-own": "^0.1.3", - "isobject": "^1.0.0" - }, - "dependencies": { - "isobject": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/isobject/-/isobject-1.0.2.tgz", - "integrity": "sha1-8Pm4zpLdVA+gdAiC44NaLgIux4o=" - } - } - }, - "object.filter": { - "version": "0.3.0", - "resolved": "https://registry.npmjs.org/object.filter/-/object.filter-0.3.0.tgz", - "integrity": "sha1-EJHGk5S0T5bZ1jVt0IeFAorWv/k=", - "requires": { - "for-own": "^0.1.2", - "make-iterator": "^0.1.0" - }, - "dependencies": { - "make-iterator": { - "version": "0.1.1", - "resolved": "https://registry.npmjs.org/make-iterator/-/make-iterator-0.1.1.tgz", - "integrity": "sha1-hz0nuBmKRlqBSDtvXRbaToY+z1s=", - "requires": { - "for-own": "^0.1.1" - } - } - } - }, - "object.omit": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/object.omit/-/object.omit-2.0.1.tgz", - "integrity": "sha1-Gpx0SCnznbuFjHbKNXmuKlTr0fo=", - "requires": { - "for-own": "^0.1.4", - "is-extendable": "^0.1.1" - } - }, - "object.pick": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/object.pick/-/object.pick-1.3.0.tgz", - "integrity": "sha1-h6EKxMFpS9Lhy/U1kaZhQftd10c=", - "requires": { - "isobject": "^3.0.1" - } - }, - "object.reduce": { - "version": "0.1.7", - "resolved": "https://registry.npmjs.org/object.reduce/-/object.reduce-0.1.7.tgz", - "integrity": "sha1-0YDoT3LSGDSK9FNStVFlJGuVBG0=", - "requires": { - "for-own": "^0.1.3" - } - }, - "repeat-string": { - "version": "1.6.1", - "resolved": "https://registry.npmjs.org/repeat-string/-/repeat-string-1.6.1.tgz", - "integrity": "sha1-jcrkcOHIirwtYA//Sndihtp15jc=" - }, - "right-align": { - "version": "0.1.3", - "resolved": "https://registry.npmjs.org/right-align/-/right-align-0.1.3.tgz", - "integrity": "sha1-YTObci/mo1FWiSENJOFMlhSGE+8=", - "requires": { - "align-text": "^0.1.1" - } - }, - "striptags": { - "version": "2.2.1", - "resolved": "https://registry.npmjs.org/striptags/-/striptags-2.2.1.tgz", - "integrity": "sha1-TEULcI1BuL85zyTEn/I0/Gqr/TI=" - }, - "utils": { - "version": "0.3.1", - "resolved": "https://registry.npmjs.org/utils/-/utils-0.3.1.tgz", - "integrity": "sha1-BO+utCkKZwN6TN39L/VhRMw6RYE=", - "requires": { - "any": "^1.0.0", - "arr-diff": "^1.1.0", - "arr-flatten": "^1.0.1", - "arr-map": "^2.0.0", - "arr-union": "^3.0.0", - "array-each": "^0.1.1", - "array-slice": "^0.2.3", - "array-unique": "^0.2.1", - "center-align": "^0.1.1", - "export-dirs": "^0.2.4", - "export-files": "^2.1.0", - "for-in": "^0.1.4", - "for-own": "^0.1.3", - "has-values": "^0.1.3", - "is-number": "^2.0.2", - "is-plain-object": "^2.0.1", - "kind-of": "^2.0.1", - "make-iterator": "^0.2.0", - "object.defaults": "^0.3.0", - "object.filter": "^0.3.0", - "object.omit": "^2.0.0", - "object.pick": "^1.1.1", - "object.reduce": "^0.1.7", - "right-align": "^0.1.3", - "striptags": "^2.0.3", - "word-wrap": "^1.1.0" - } - }, - "word-wrap": { - "version": "1.2.3", - "resolved": "https://registry.npmjs.org/word-wrap/-/word-wrap-1.2.3.tgz", - "integrity": "sha512-Hz/mrNwitNRh/HUAtM/VT/5VH+ygD6DV7mYKZAtHOrbs8U7lvPS6xf7EJKMF0uW1KJCl0H701g3ZGus+muE5vQ==" } } } diff --git a/utils.js b/utils.js new file mode 100644 index 0000000..0004f88 --- /dev/null +++ b/utils.js @@ -0,0 +1,21 @@ +const fs = require('fs') +const { exit } = require('process') + +//if folder/file doesn't exist, create it +function mkExist(toCreate, ifFolder = true) { + try { + if (!fs.existsSync(toCreate)) { + if (ifFolder) { + fs.mkdirSync(toCreate); + } else { + fs.writeFileSync(toCreate, "utf8"); + } + } + } catch (err) { + console.log(`Error when creating a ${ifFolder ? "folder" : "file"} ${toCreate}.`); + console.log(err); + exit(-1); + } +} + +module.exports = { mkExist }; \ No newline at end of file