Skip to content

Commit

Permalink
feat: create commander cli and implement skip feat
Browse files Browse the repository at this point in the history
created commander cli
implement init command with
-s -f flags to
skip and -f to override
  • Loading branch information
maverox committed Jun 7, 2024
1 parent bb5e201 commit 9bf21bf
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions packages/create-webpack-app/bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,46 @@
import { Command } from "commander";
const program = new Command();
import { resolve, join } from "path";
import nodePlop from "node-plop";
// Cspell:ignore plopfile, plopfile.js

import pkg from "../package.json" with { type: "json" };
const program = new Command();
const plop = await nodePlop("./lib/plopfile.js");
const defaultValues = {
init: {
projectName: "webpack-project",
projectPath: resolve(join(process.cwd(), ".")),
langType: "none",
devServer: true,
htmlWebpackPlugin: true,
workboxWebpackPlugin: true,
cssType: "CSS only",
isCSS: true,
isPostCSS: true,
extractPlugin: "No",
packageManager: "npm",
},
};

program.version(pkg.version, "-v, --version");
program.version("1.0.0", "-v, --version");
program.helpOption("-h, --help", "Display help for command");

program
.option("-s, --skip", "Skip the prompt and use the default values")
.option("-f, --force", "Force the generator actions to override existing files");

program.parse(process.argv);
.command("init")
.description("Initialize a new Webpack project")
.option("-s, --skip", "Skip the prompt and use the default values", false)
.option("-f, --force", "Force the generator actions to override existing files", false)
.action(function (opts) {
console.log("Initializing a new Webpack project");
const { skip, force } = opts;
const initGenerator = plop.getGenerator("init");
if (skip) {
console.log("Skipping the prompt and using the default values");
initGenerator.runActions(defaultValues.init);
} else {
initGenerator.runPrompts([]).then((answers) => {
initGenerator.runActions(answers);
});
}
});

console.log(program.opts());
program.parse();

0 comments on commit 9bf21bf

Please sign in to comment.