create-express-application
Version:
CLI tool to scaffold a boilerplate Express.js application
157 lines (127 loc) ⢠3.81 kB
JavaScript
const fs = require("fs");
const path = require("path");
const { execSync } = require("child_process");
const jsTemplate = require("../templates/jsTemplate");
const tsTemplate = require("../templates/tsTemplate");
const { createFolders, createExampleFiles } = require("./structureHelpers");
function setupProject(projectName, language, useDotenv, initGit) {
const isTS = language === "TypeScript";
const projectDir = path.join(process.cwd(), projectName);
if (fs.existsSync(projectDir)) {
console.error(
"ā Directory already exists. Please choose a different name."
);
process.exit(1);
}
fs.mkdirSync(projectDir);
process.chdir(projectDir);
console.log("š¦ Initializing npm...");
execSync("npm init -y", { stdio: "inherit" });
console.log("š„ Installing Express and CORS...");
execSync("npm install express cors", { stdio: "inherit" });
if (useDotenv) {
execSync("npm install dotenv", { stdio: "inherit" });
}
if (isTS) {
execSync(
"npm install -D typescript ts-node-dev @types/node @types/express @types/cors",
{ stdio: "inherit" }
);
}
fs.writeFileSync(".gitignore", "node_modules\ndist\n.env\n");
fs.writeFileSync(
"README.md",
`# ${projectName}
> Created with [create-express-app](https://github.com/your-org/create-express-app)
## š¦ Setup
\`\`\`bash
npm install
npm run dev
\`\`\`
## š Folder Structure
- \`controllers/\` ā Route handlers
- \`routes/\` ā Route definitions
- \`middlewares/\` ā Custom Express middleware
- \`models/\` ā Data models or types
- \`utils/\` ā Utility/helper functions
## ā
Features
- Express.js
- CORS
- ${useDotenv ? "dotenv support" : "No dotenv"}
- ${isTS ? "TypeScript" : "JavaScript"}
`
);
fs.writeFileSync(
".eslintrc.json",
JSON.stringify(
{
env: { node: true, es2021: true },
extends: ["eslint:recommended"],
parserOptions: { ecmaVersion: "latest", sourceType: "module" },
rules: {},
},
null,
2
)
);
fs.writeFileSync(
".prettierrc",
JSON.stringify(
{
semi: true,
singleQuote: true,
tabWidth: 2,
trailingComma: "es5",
},
null,
2
)
);
if (initGit) {
console.log("š§ Initializing Git repository...");
execSync("git init", { stdio: "inherit" });
execSync("git add .", { stdio: "inherit" });
execSync('git commit -m "Initial commit"', { stdio: "inherit" });
}
const pkg = JSON.parse(fs.readFileSync("package.json"));
if (isTS) {
console.log("š§ Setting up TypeScript...");
const tsConfig = {
compilerOptions: {
target: "ES6",
module: "commonjs",
rootDir: "./src",
outDir: "./dist",
esModuleInterop: true,
strict: true,
},
};
fs.writeFileSync("tsconfig.json", JSON.stringify(tsConfig, null, 2));
pkg.scripts = {
dev: "ts-node-dev --respawn --transpile-only src/index.ts",
build: "tsc",
start: "node dist/index.js",
};
fs.mkdirSync("src");
createFolders("src", true);
createExampleFiles("src", true);
fs.writeFileSync("src/index.ts", tsTemplate(useDotenv));
} else {
pkg.scripts = {
dev: "node --watch --watch-path=src src/index.js",
start: "node src/index.js",
};
fs.mkdirSync("src");
createFolders("src", false);
createExampleFiles("src", false);
fs.writeFileSync("src/index.js", jsTemplate(useDotenv));
}
fs.writeFileSync("package.json", JSON.stringify(pkg, null, 2));
if (useDotenv) {
fs.writeFileSync(".env", "PORT=3000\n");
}
console.log("\nā
Project created successfully!");
console.log(`š cd ${projectName}`);
console.log("š npm run dev");
}
module.exports = { setupProject };