Skip to content

Commit

Permalink
use yargs instead of commender
Browse files Browse the repository at this point in the history
  • Loading branch information
tuanthanh2067 committed Nov 17, 2021
1 parent 8709224 commit 80710db
Show file tree
Hide file tree
Showing 6 changed files with 2,783 additions and 2,811 deletions.
73 changes: 36 additions & 37 deletions bin/cv-ssg.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,70 @@
/* global process */

const chalk = require("chalk");
const { program } = require("commander");
const yargs = require("yargs");

const ReadPath = require("./helpers/readPath");
const ProduceFile = require("./helpers/produceFile");
const ProduceFolder = require("./helpers/produceFolder");
const HandleFile = require("./helpers/handleFile");
const CopyFolder = require("./helpers/copyFolder");

program
.option("-i, --input <type>", "input file or folder")
.option(
"-s, --stylesheet <type>",
"use your custom stylesheet or <default> for default stylesheet"
)
.option(
"-c, --config <type>",
"input a config.json file containing other options' values"
)
.option("-v, --version", "will display current version")
.option("-h, --help", "will display help for command");

program.parse(process.argv);

const args = program.opts();

const getParams = (args) => {
const argv = yargs
.usage("$0 <args> [options]")
.option("input", {
describe: "input file or folder",
alias: "i",
type: "string",
})
.option("stylesheet", {
describe: "use your custom stylesheet or <default> for default stylesheet",
alias: "s",
type: "string",
default: "",
})
.option("config", {
describe: "input a config.json file containing other options' values",
alias: "c",
type: "string",
default: "",
})
.alias("v", "version")
.alias("h", "help")
.help().argv;

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

if (args.config || args.c) {
if (argv.config || argv.c) {
// sets option's value based on config file
return {
path: args.config || args.c,
path: argv.config || argv.c,
styleSheetLink,
};
}

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) {
if (argv.input || argv.i) {
return {
path: args.input || args.i,
path: argv.input || argv.i,
styleSheetLink,
};
}

yargs.showHelp();
return process.exit(1);
};

const main = async () => {
const { path, styleSheetLink } = getParams(args);
const { path, styleSheetLink } = getParams(argv);
let readPath;
try {
readPath = new ReadPath(path);
Expand All @@ -87,7 +87,6 @@ const main = async () => {

// assets available here or null
// copy assets folder to dist folder
console.log(returnResult);
if (returnResult && returnResult.assets) {
const copyFolder = new CopyFolder(returnResult.assets);
try {
Expand Down
2 changes: 2 additions & 0 deletions bin/helpers/handleFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ module.exports = class HandleFile {
return await this.handleFolder(results);
}
if (ext === ".txt" || ext === ".md") {
console.log("hello");
const returnResults = await this.handleFile(ext, results);

return {
results: returnResults.results,
metaData: returnResults.metaData,
Expand Down
37 changes: 10 additions & 27 deletions bin/test/__snapshots__/e2e.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,31 +1,14 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`end-to-end integration prints error and help message when no arguments given 1`] = `
"internal/modules/cjs/loader.js:905
throw err;
^
Error: Cannot find module '/Users/tuanthanh2067/Desktop/Study/osd600/cv-ssg.js'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
at Function.Module._load (internal/modules/cjs/loader.js:746:27)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}"
`;

exports[`end-to-end integration prints help message when --help given 1`] = `
"internal/modules/cjs/loader.js:905
throw err;
^
Error: Cannot find module '/Users/tuanthanh2067/Desktop/Study/osd600/cv-ssg.js'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
at Function.Module._load (internal/modules/cjs/loader.js:746:27)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}"
"cv-ssg.js <args> [options]
Options:
--input, -i input file or folder [string]
--stylesheet, -s use your custom stylesheet or <default> for default
stylesheet [string] [default: \\"\\"]
--config, -c input a config.json file containing other options' values
[string] [default: \\"\\"]
-v, --version Show version number [boolean]
-h, --help Show help [boolean]"
`;
2 changes: 1 addition & 1 deletion bin/test/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const execa = require("execa");

async function run(...args) {
try {
const results = await execa.node("../cv-ssg.js", args);
const results = await execa.node("./bin/cv-ssg.js", args);
return results;
} catch (err) {
return err;
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
"homepage": "https://github.com/tuanthanh2067/cv-ssg#readme",
"dependencies": {
"chalk": "^4.1.2",
"commander": "^8.2.0",
"execa": "^5.1.1",
"fs-extra": "^10.0.0",
"node-dir": "^0.1.17",
"pretty": "^2.0.0",
"showdown": "^1.9.1"
"showdown": "^1.9.1",
"yargs": "^17.2.1"
},
"devDependencies": {
"eslint": "^8.1.0",
Expand Down
Loading

0 comments on commit 80710db

Please sign in to comment.