Skip to content

Commit

Permalink
added path option to add, added other select to frameworks (still wor…
Browse files Browse the repository at this point in the history
…king)
  • Loading branch information
Tpleme committed Jan 15, 2024
1 parent 94a8c82 commit 7479be0
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 29 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<img alt="NPM Downloads" src="https://img.shields.io/npm/dt/%40tpleme%2Fmonorepo-automator?logo=npm">
</p>

## `This project currently only supports creation of React + vite and raw JS projects with Biomejs as lint and formatter`
## `This project currently only supports creation of React + vite and vanilla JS projects with Biomejs as lint and formatter`

# Monorepo Automator

Expand Down Expand Up @@ -56,6 +56,8 @@ monorepo-automator create [name] [options]
Option | Description
---|---
`-p` or `--path` | Provide path where you want to create the monorepo project
<!-- TODO: add apps list option -->


##
- ### Add
Expand All @@ -67,6 +69,9 @@ monorepo-automator add <name> [options]
Option | Description
---|---
`-e` or `--env` | Provide a development environment to the new app, ex: vite
`-p` or `--path` | Provide path where you want to create new app
<!-- TODO: add path option -->


## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Expand Down
51 changes: 33 additions & 18 deletions commands/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const errorStyle = chalk.red.bold;
export default async (cmd, opts, appDir) => {
let appName = cmd ?? null;
let devEnv = opts.env ?? null;
let appPath;
let appPath = opts.path ?? null;

try {
appName = appName.replace(/\s/g, "_");
Expand All @@ -22,15 +22,17 @@ export default async (cmd, opts, appDir) => {
handleError("Invalid project name. Project name must be a valid folder name format.");
}

await promisifyQuestion(
`❔ Where do you want to create the app ${appName}? (leave empty for current path)\n`,
).then(path => {
if (path === "." || path.length === 0) {
appPath = "./";
} else {
appPath = path;
}
});
if (!appPath) {
await promisifyQuestion(
`❔ Where do you want to create the app ${appName}? (leave empty for current path)\n`,
).then(path => {
if (path === "." || path.length === 0) {
appPath = "./";
} else {
appPath = path;
}
});
}

//check if parent dir exists
if (!existsSync(appPath)) {
Expand All @@ -55,24 +57,35 @@ export default async (cmd, opts, appDir) => {
message: "❔ Do you want to install any development environment?",
choices: ["vite", "none"],
}).then(res => {
if (res.devEnv !== "none") {
devEnv = res.devEnv;
}
devEnv = res.devEnv;
});
}

startAnimation();

if (devEnv) {
if (devEnv !== "none") {
if (devEnv !== "vite") {
handleError(`${devEnv} is not supported dev environment. Use vite instead.`, `${appPath}${appName}`);
return;
}

//Substituir por @inquirer/select
const framework = await list({
name: "framework",
message: `❔ Pick a framework for the app ${appName}?`,
choices: ["vue", "react", "preact", "lit", "svelte", "solid", "qwik"],
});

console.log(framework);

startAnimation();

setMessage(`Creating ${appName} - Installing and initializing vite`);
//init vite
await runCommandOnFolder(`${appPath}`, `npm create vite@latest ${appName} -- --template react`);
await runCommandOnFolder(
`${appPath}`,
`npm create vite@latest ${appName} -- --template ${framework.framework}`,
);

console.log("ehre");
//install dependencies
await runCommandOnFolder(`${appPath}${appName}`, "npm install");

Expand All @@ -83,6 +96,7 @@ export default async (cmd, opts, appDir) => {
await fsPromises.rm(`${appPath}${appName}/README.md`);

setMessage(`Creating ${appName} - Setting package.json scripts`);

//change package.json scripts, vite config and create envDir
const contents = await fsPromises.readFile(`${appPath}${appName}/package.json`, "utf-8");
const replacement = contents
Expand All @@ -94,7 +108,6 @@ export default async (cmd, opts, appDir) => {
.replace(/"lint": "eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0",\n/, "");

await fsPromises.writeFile(`${appPath}${appName}/package.json`, replacement);

//copy vite config template
setMessage(`Creating ${appName} - Updating vite config`);
await fsPromises.copyFile(`${appDir}/filesTemplate/vite.config.js`, `${appPath}${appName}/vite.config.js`);
Expand All @@ -107,6 +120,8 @@ export default async (cmd, opts, appDir) => {
await fsPromises.writeFile(`${appPath}${appName}/envDir/.env.production`, "", "utf-8");
await fsPromises.writeFile(`${appPath}${appName}/envDir/.env.development`, "", "utf-8");
} else {
startAnimation();

//create server folder
setMessage(`Creating ${appName}`);
await createDirectory(`${appPath}${appName}`);
Expand Down
6 changes: 2 additions & 4 deletions commands/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ export default async (cmd, opts, appDir) => {
message: `❔ Do you want to install any development environment on ${app.name}?`,
choices: ["vite", "none"],
}).then(res => {
if (res.devEnv !== "none") {
app.devEnv = res.devEnv;
}
app.devEnv = res.devEnv;
});
}

Expand All @@ -97,7 +95,7 @@ export default async (cmd, opts, appDir) => {

setMessage(`Creating ${app.name}`);

if (app.devEnv) {
if (app.devEnv !== "none") {
if (app.devEnv !== "vite") {
handleError(
`${app.devEnv} is not supported dev environment. Use vite instead.`,
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ program
.command("add <appName>")
.description("Add new app to project. Ex: add server")
.option("-e, --env [ENV]", "Set development environment to the app. Ex: add client -e vite")
.option("-p, --path [PATH]", "Set the path for the new project. EX: add client -p ./my_project_folder")
.action((cmd, opts) => add(cmd, opts, rootDir));

program.parse();
11 changes: 6 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
"type": "module",
"keywords": [
"monorepo",
"boilerplate"
"boilerplate",
"cli",
"command-line",
"generator",
"javascript"
],
"description": "Monorepo project auto generator",
"main": "index.js",
Expand Down

0 comments on commit 7479be0

Please sign in to comment.