Skip to content

Commit

Permalink
Using 2 main classes instead of functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tuanthanh2067 committed Oct 15, 2021
1 parent f919ef4 commit 3fcb0c8
Show file tree
Hide file tree
Showing 10 changed files with 276 additions and 212 deletions.
144 changes: 62 additions & 82 deletions bin/cv-ssg.js
@@ -1,16 +1,10 @@
#!/usr/bin/env node
const fs = require("fs");
const chalk = require("chalk");
const { program } = require("commander");

const {
validateExtension,
validateString,
validateConfigFile,
} = require("./helpers/validateFile");
const { createFile, createFolder } = require("./helpers/createFile");
const { readFolder } = require("./helpers/readFolder");
const { readConfigFile } = require("./helpers/readConfigFile");
const ReadPath = require("./helpers/readPath");
const ProduceFile = require("./helpers/produceFile");
const ProduceFolder = require("./helpers/produceFolder");

program
.option("-i, --input <type>", "input file or folder")
Expand All @@ -29,85 +23,71 @@ program.parse(process.argv);

const args = program.opts();

let userInput;
let file;

if (args.config || args.c) {
file = args.config || args.c;
if (validateConfigFile(file.trim())) {
fs.stat(file.trim(), (err) => {
if (err) {
console.log(chalk.yellow(`Can not open ${file}`));
return process.exit(1);
}
});

userInput = readConfigFile(file.trim());
const getParams = (args) => {
// stylesheet option
let styleSheetLink = "";
if (args.stylesheet || args.s) {
// style sheet default option
if (args.stylesheet === "default" || args.s === "default") {
styleSheetLink = "https://cdn.jsdelivr.net/npm/water.css@2/out/water.css";
} else {
styleSheetLink = args.styleSheetLink || args.s;
}
}

if (args.config || args.c) {
// sets option's value based on config file
args.input = userInput.input ? userInput.input : "";
args.stylesheet = userInput.stylesheet ? userInput.stylesheet : "";
return {
path: args.config || args.c,
styleSheetLink,
};
}

args.version = false; // ignore -v when -c is specified
args.help = false; // ignore -h when -c is specified
}

if (args.version || args.v) {
console.log(`v${require("../package.json").version}`);
return process.exit(0);
}

if (args.help || args.h) {
console.log(program.help());
return process.exit(0);
}

// stylesheet option
let stylesheetLink;
if (args.stylesheet || args.s) {
// style sheet default option
if (args.stylesheet === "default" || args.s === "default") {
stylesheetLink = "https://cdn.jsdelivr.net/npm/water.css@2/out/water.css";
} else {
stylesheetLink = args.stylesheet || args.s;
stylesheetLink = stylesheetLink.trim();
if (args.version || args.v) {
console.log(`v${require("../package.json").version}`);
return process.exit(0);
}
if (args.help || args.h) {
console.log(program.help());
return process.exit(0);
}
}

if (args.input || args.i) {
file = args.input || args.i;
file = file.trim();

// folder input
fs.stat(file, (err, stat) => {
if (err) {
console.log(chalk.yellow(`Can not open ${file} ${err}`));
return process.exit(1);
}
if (args.input || args.i) {
return {
path: args.input || args.i,
styleSheetLink,
};
}
};

const main = async () => {
const { path, styleSheetLink } = getParams(args);
let readPathVar;
try {
readPathVar = new ReadPath(path);
} catch (err) {
console.log(chalk.yellow(err));
}
const readResult = await readPathVar.read();

const folder = createFolder();
const folder = new ProduceFolder();
const folderPath = folder.getPath();

if (stat && stat.isDirectory()) {
readFolder(file, (err, results) => {
if (err) {
console.log(chalk.yellow(`Can not read ${file}`));
return process.exit(1);
}
results.forEach((file) => {
createFile(file, stylesheetLink, folder);
});
return;
if (readPathVar.isFolder()) {
Promise.all(readResult).then((data) => {
data.forEach((e) => {
const file = new ProduceFile(e.results, e.path, e.ext);
file.produce(styleSheetLink, folderPath);
});
} else {
// file input
if (!validateString(file) || !validateExtension(file))
return process.exit(1);
createFile(file, stylesheetLink, folder);
}
return process.exit(0);
});
} else {
console.log(chalk.yellow(`Invalid input`));
console.log(program.help());
}
});
return;
}
const produceFile = new ProduceFile(
readResult.results,
readResult.path,
readResult.ext
);
produceFile.produce(styleSheetLink, folderPath);
};

main();
37 changes: 0 additions & 37 deletions bin/helpers/createFile.js

This file was deleted.

30 changes: 0 additions & 30 deletions bin/helpers/createHtml.js

This file was deleted.

73 changes: 73 additions & 0 deletions bin/helpers/produceFile.js
@@ -0,0 +1,73 @@
const fs = require("fs");
const chalk = require("chalk");

module.exports = class ProduceFile {
constructor(results, path, ext) {
this.results = results;
this.path = path;
this.ext = ext;
}

createFile(data, styleSheetLink, folder) {
// read file and return an array of strings
if (!data) return; // nothing in the array

// generate dom
const dom = this.createHtmlFile(data, styleSheetLink);
try {
// remove full path first and then extension
const filename = this.path
.replace(/^.*[\\\/]/, "")
.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"));
return process.exit(1);
}
}

// createFiles(styleSheetLink, folder) {
// this.results.forEach((file) => {
// this.createFile(file, styleSheetLink, folder);
// });
// }

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;
}

produce(styleSheetLink = "", folder) {
this.createFile(this.results, styleSheetLink, folder);
}
};
24 changes: 24 additions & 0 deletions bin/helpers/produceFolder.js
@@ -0,0 +1,24 @@
const fs = require("fs");

module.exports = class ProduceFolder {
constructor() {
this.path = `${process.cwd()}/dist`;
this.createFolder();
}

createFolder() {
this.removeDir();

// create new dir
fs.mkdirSync(this.path);
}

getPath() {
return this.path;
}

removeDir() {
// remove dir
fs.rmdirSync(this.path, { recursive: true });
}
};
11 changes: 0 additions & 11 deletions bin/helpers/readConfigFile.js

This file was deleted.

18 changes: 0 additions & 18 deletions bin/helpers/readFile.js

This file was deleted.

8 changes: 0 additions & 8 deletions bin/helpers/readFolder.js

This file was deleted.

0 comments on commit 3fcb0c8

Please sign in to comment.