Skip to content

Commit

Permalink
Merge pull request #12 from tcvan0707/issue-11
Browse files Browse the repository at this point in the history
Configuration Support
  • Loading branch information
LuigiZaccagnini committed Oct 9, 2021
2 parents a36bcf8 + b236bb9 commit bbb930e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 654 deletions.
17 changes: 16 additions & 1 deletion bin/app.js
@@ -1,6 +1,8 @@
#!/usr/bin/env node
const { argv, option } = require("yargs");
const yargs = require(`yargs`);
const package = require(`../package.json`);
const fs = require("fs");

//Import fileFunctions
const fileFunctions = require("./fileFunctions");
Expand All @@ -10,13 +12,20 @@ const options = yargs
alias: `i`,
describe: `Path to file`,
type: `string`,
demandOption: true,
demandOption: false,
})
.option(`output`, {
alias: `o`,
describe: `Output directory for html parsed files`,
type: `string`,
})
.options(`config`, {
alias: `c`,
describe: `Configuration file`,
default: "",
type: `array`,
demandOption: false,
})
.option(`lang`, {
alias: `l`,
describe: `Language attribute in HTML file`,
Expand All @@ -30,6 +39,12 @@ const options = yargs
//Using fileFuntions methods
fileFunctions.addDirectory(options.output ? options.output : `./dist`);

//ignore the rest if Configuration is specified
const filePath = options.config || options.input;
if (!filePath) {
console.log(`Please choose either -i option or -c option!!!`);
}

fileFunctions.getPathInfo(
`${options.input}`,
`${options.output ? options.output : `./dist`}`,
Expand Down
35 changes: 34 additions & 1 deletion bin/fileFunctions.js
Expand Up @@ -2,6 +2,7 @@ const fs = require(`fs`);
const pathModule = require("path");
const readline = require(`readline`);
const { once } = require("events");
const path = require("path");

/**
* Function that creates a directory at the given path
Expand Down Expand Up @@ -180,14 +181,46 @@ const getPathInfo = (path, output, lang) => {
writeFile(pathModule.basename(path, ".md") + ".html", doc, output);
});
}

return console.log("The tool only supports .txt and .md files!!");
}
});
};

const readJsonFile = (inputPath) => {
fs.readFile(inputPath, "utf8", (err, json) => {
if (err) {
console.log(err);
process.exit(-1);
}

const data = JSON.parse(json);
const lang = data.lang || "en-CA";

fs.stat(data.input, (err, stats) => {
if (err) {
console.log(err);
}

if (stats.isDirectory()) {
fs.readdirSync(path).forEach((file) => {
getPathInfo(`${path}/${file}`, output, lang);
});
} else if (stats.isFile() && path.extname(data.input) === ".txt") {
getPathInfo(`${path}/${file}`, output, lang);
} else if (stats.isFile() && path.extname(data.input) === ".md") {
getPathInfo(`${path}/${file}`, output, lang);
} else if (stats.isFile() && path.extname(data.input) === ".json") {
getPathInfo(`${path}/${file}`, output, lang);
} else {
console.log("The tool only supports .txt and .md files!!");
}
});
});
};

//Fixed module will export functions created
module.exports = {
getPathInfo,
addDirectory,
readJsonFile,
};

0 comments on commit bbb930e

Please sign in to comment.