diff --git a/packages/package-lock.json b/packages/package-lock.json index 90c4fc7..c92c8cb 100644 --- a/packages/package-lock.json +++ b/packages/package-lock.json @@ -1,12 +1,12 @@ { - "name": "gen-express-cli-test", - "version": "0.0.3", + "name": "gen-express-cli", + "version": "0.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { - "name": "gen-express-cli-test", - "version": "0.0.3", + "name": "gen-express-cli", + "version": "0.0.0", "license": "MIT", "dependencies": { "chalk": "^5.3.0", @@ -17,7 +17,7 @@ "nanospinner": "^1.1.0" }, "bin": { - "gen-express-cli-test": "index.js" + "gen-express-cli": "index.js" }, "engines": { "node": ">=18.0.0" @@ -247,6 +247,14 @@ "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz", "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==" }, + "node_modules/es-errors": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/es-errors/-/es-errors-1.3.0.tgz", + "integrity": "sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==", + "engines": { + "node": ">= 0.4" + } + }, "node_modules/escape-string-regexp": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", @@ -302,15 +310,19 @@ } }, "node_modules/get-intrinsic": { - "version": "1.2.2", - "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.2.2.tgz", - "integrity": "sha512-0gSo4ml/0j98Y3lngkFEot/zhiCeWsbYIlZ+uZOVgzLyLaUw7wxUL+nCTP0XJvJg1AXulJRI3UJi8GsbDuxdGA==", + "version": "1.2.3", + "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.2.3.tgz", + "integrity": "sha512-JIcZczvcMVE7AUOP+X72bh8HqHBRxFdz5PDHYtNG/lE3yk9b3KZBJlwFcTyPYjg3L4RLLmZJzvjxhaZVapxFrQ==", "dependencies": { + "es-errors": "^1.0.0", "function-bind": "^1.1.2", "has-proto": "^1.0.1", "has-symbols": "^1.0.3", "hasown": "^2.0.0" }, + "engines": { + "node": ">= 0.4" + }, "funding": { "url": "https://github.com/sponsors/ljharb" } @@ -441,9 +453,9 @@ "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" }, "node_modules/inquirer": { - "version": "9.2.13", - "resolved": "https://registry.npmjs.org/inquirer/-/inquirer-9.2.13.tgz", - "integrity": "sha512-mUlJNemjYioZgaZXqEFlQ0z9GD8/o+pavIF3JyhzWLX4Xa9M1wioGMCxQEFmps70un9lrah2WaBl3kSRVcoV3g==", + "version": "9.2.14", + "resolved": "https://registry.npmjs.org/inquirer/-/inquirer-9.2.14.tgz", + "integrity": "sha512-4ByIMt677Iz5AvjyKrDpzaepIyMewNvDcvwpVVRZNmy9dLakVoVgdCHZXbK1SlVJra1db0JZ6XkJyHsanpdrdQ==", "dependencies": { "@ljharb/through": "^2.3.12", "ansi-escapes": "^4.3.2", @@ -462,7 +474,7 @@ "wrap-ansi": "^6.2.0" }, "engines": { - "node": ">=14.18.0" + "node": ">=18" } }, "node_modules/is-fullwidth-code-point": { diff --git a/packages/package.json b/packages/package.json index 92d5e29..97f9f67 100644 --- a/packages/package.json +++ b/packages/package.json @@ -1,6 +1,6 @@ { - "name": "gen-express-cli-test", - "version": "0.0.3", + "name": "gen-express-cli", + "version": "0.0.0", "description": "A simple express cli to generate express app simply by running a command", "main": "index.js", "type": "module", @@ -28,7 +28,7 @@ "author": "RulerChen", "license": "MIT", "bin": { - "gen-express-cli-test": "index.js" + "gen-express-cli": "index.js" }, "dependencies": { "chalk": "^5.3.0", diff --git a/packages/scripts/createProject.js b/packages/scripts/createProject.js index 549a678..2d9e459 100644 --- a/packages/scripts/createProject.js +++ b/packages/scripts/createProject.js @@ -6,9 +6,11 @@ import { runCommand } from '../utils/exec.js'; async function createStructure(projectName, template) { const TEMPLATES_PATH = path.join(path.dirname(fileURLToPath(import.meta.url)), '../', 'templates'); + const COMMON_PATH = path.join(TEMPLATES_PATH, 'common'); const PROJECT_PATH = path.join(process.cwd(), projectName); try { await fsPromises.cp(path.join(TEMPLATES_PATH, template), PROJECT_PATH, { recursive: true }); + await fsPromises.cp(COMMON_PATH, PROJECT_PATH, { recursive: true }); } catch (error) { throw error; } diff --git a/packages/templates/common/.gitignore b/packages/templates/common/.gitignore new file mode 100644 index 0000000..d1f0b24 --- /dev/null +++ b/packages/templates/common/.gitignore @@ -0,0 +1,17 @@ +node_modules/ +build/ + +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +.pnpm-debug.log* + +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +.DS_Store diff --git a/packages/templates/javascript/.env b/packages/templates/javascript/.env new file mode 100644 index 0000000..83ebdaa --- /dev/null +++ b/packages/templates/javascript/.env @@ -0,0 +1 @@ +PORT=8000 \ No newline at end of file diff --git a/packages/templates/javascript/.env.development b/packages/templates/javascript/.env.development new file mode 100644 index 0000000..8f55cbd --- /dev/null +++ b/packages/templates/javascript/.env.development @@ -0,0 +1 @@ +PORT= \ No newline at end of file diff --git a/packages/templates/javascript/.eslintrc.json b/packages/templates/javascript/.eslintrc.json new file mode 100644 index 0000000..057c24c --- /dev/null +++ b/packages/templates/javascript/.eslintrc.json @@ -0,0 +1,26 @@ +{ + "env": { + "es2021": true, + "node": true + }, + "extends": ["standard", "prettier"], + "plugins": ["prettier"], + "parserOptions": { + "ecmaVersion": "latest", + "sourceType": "module" + }, + "rules": { + "prettier/prettier": [ + "error", + { + "semi": true, + "tabWidth": 2, + "printWidth": 140, + "singleQuote": true, + "trailingComma": "all", + "bracketSameLine": false, + "arrowParens": "always" + } + ] + } +} diff --git a/packages/templates/javascript/.prettierrc.json b/packages/templates/javascript/.prettierrc.json new file mode 100644 index 0000000..cfa3f4a --- /dev/null +++ b/packages/templates/javascript/.prettierrc.json @@ -0,0 +1,9 @@ +{ + "semi": true, + "printWidth": 140, + "tabWidth": 2, + "singleQuote": true, + "trailingComma": "all", + "arrowParens": "always", + "bracketSpacing": true +} diff --git a/packages/templates/javascript/package.json b/packages/templates/javascript/package.json new file mode 100644 index 0000000..0a43dcf --- /dev/null +++ b/packages/templates/javascript/package.json @@ -0,0 +1,30 @@ +{ + "name": "javascript", + "version": "1.0.0", + "description": "", + "main": "index.js", + "type": "module", + "scripts": { + "lint": "eslint --fix ./src/**/*.js", + "dev": "cross-env NODE_ENV=development nodemon ./src/index.js", + "start": "cross-env NODE_ENV=production node ./src/index.js" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "cors": "~2.8.5", + "dotenv": "~16.4.1", + "express": "~4.18.2" + }, + "devDependencies": { + "cross-env": "~7.0.3", + "eslint": "~8.56.0", + "eslint-config-standard": "~17.1.0", + "eslint-config-prettier": "~9.1.0", + "eslint-plugin-import": "~2.29.1", + "eslint-plugin-prettier": "~5.1.3", + "nodemon": "~3.0.3", + "prettier": "~3.2.5" + } +} diff --git a/packages/templates/javascript/src/controllers/user.js b/packages/templates/javascript/src/controllers/user.js new file mode 100644 index 0000000..48d4012 --- /dev/null +++ b/packages/templates/javascript/src/controllers/user.js @@ -0,0 +1,10 @@ +import usersModel from '../models/user.js'; + +function getAllUsers(req, res) { + const users = usersModel.getUsers(); + res.status(200).json(users); +} + +export default { + getAllUsers, +}; diff --git a/packages/templates/javascript/src/index.js b/packages/templates/javascript/src/index.js new file mode 100644 index 0000000..cbfdbc9 --- /dev/null +++ b/packages/templates/javascript/src/index.js @@ -0,0 +1,22 @@ +import express from 'express'; +import dotenv from 'dotenv'; +import cors from 'cors'; + +import routes from './routes/index.js'; + +dotenv.config(); + +const app = express(); +const PORT = process.env.PORT || 3000; + +app.use(cors()); +app.use(express.urlencoded({ extended: true })); +app.use(express.json()); + +app.use(`/api`, routes); + +app.listen(PORT, () => { + console.log(`Server is running on port ${PORT}`); +}); + +export default app; diff --git a/packages/templates/javascript/src/models/user.js b/packages/templates/javascript/src/models/user.js new file mode 100644 index 0000000..bae7364 --- /dev/null +++ b/packages/templates/javascript/src/models/user.js @@ -0,0 +1,16 @@ +class UsersModel { + constructor() { + this.users = [ + { name: 'RulerChen', descrition: 'Author of this project' }, + { name: 'joshtu0627', descrition: 'Author of this project' }, + ]; + } + + getUsers() { + return this.users; + } +} + +const usersModel = new UsersModel(); + +export default usersModel; diff --git a/packages/templates/javascript/src/routes/index.js b/packages/templates/javascript/src/routes/index.js new file mode 100644 index 0000000..95f2071 --- /dev/null +++ b/packages/templates/javascript/src/routes/index.js @@ -0,0 +1,13 @@ +import express from 'express'; + +import UserRoutes from './user.js'; + +const router = express.Router(); + +router.get('/', (req, res) => { + res.send('This is the API root!'); +}); + +router.use('/users', UserRoutes); + +export default router; diff --git a/packages/templates/javascript/src/routes/user.js b/packages/templates/javascript/src/routes/user.js new file mode 100644 index 0000000..8a01626 --- /dev/null +++ b/packages/templates/javascript/src/routes/user.js @@ -0,0 +1,9 @@ +import express from 'express'; + +import UserController from '../controllers/user.js'; + +const router = express.Router(); + +router.get('/', UserController.getAllUsers); + +export default router; diff --git a/packages/templates/typescript/.env b/packages/templates/typescript/.env new file mode 100644 index 0000000..83ebdaa --- /dev/null +++ b/packages/templates/typescript/.env @@ -0,0 +1 @@ +PORT=8000 \ No newline at end of file diff --git a/packages/templates/typescript/.env.development b/packages/templates/typescript/.env.development new file mode 100644 index 0000000..8f55cbd --- /dev/null +++ b/packages/templates/typescript/.env.development @@ -0,0 +1 @@ +PORT= \ No newline at end of file diff --git a/packages/templates/typescript/.eslintrc.json b/packages/templates/typescript/.eslintrc.json new file mode 100644 index 0000000..53d53b1 --- /dev/null +++ b/packages/templates/typescript/.eslintrc.json @@ -0,0 +1,26 @@ +{ + "env": { + "es2021": true, + "node": true + }, + "extends": ["standard-with-typescript", "prettier"], + "plugins": ["prettier"], + "parserOptions": { + "ecmaVersion": "latest", + "sourceType": "module" + }, + "rules": { + "prettier/prettier": [ + "error", + { + "semi": true, + "tabWidth": 2, + "printWidth": 140, + "singleQuote": true, + "trailingComma": "all", + "bracketSameLine": false, + "arrowParens": "always" + } + ] + } +} diff --git a/packages/templates/typescript/.prettierrc.json b/packages/templates/typescript/.prettierrc.json new file mode 100644 index 0000000..cfa3f4a --- /dev/null +++ b/packages/templates/typescript/.prettierrc.json @@ -0,0 +1,9 @@ +{ + "semi": true, + "printWidth": 140, + "tabWidth": 2, + "singleQuote": true, + "trailingComma": "all", + "arrowParens": "always", + "bracketSpacing": true +} diff --git a/packages/templates/typescript/index.ts b/packages/templates/typescript/index.ts deleted file mode 100644 index 6e163ac..0000000 --- a/packages/templates/typescript/index.ts +++ /dev/null @@ -1 +0,0 @@ -console.log("This is a template for a Typescript project.") diff --git a/packages/templates/typescript/package.json b/packages/templates/typescript/package.json index 1a4dcf4..9c35952 100644 --- a/packages/templates/typescript/package.json +++ b/packages/templates/typescript/package.json @@ -4,12 +4,35 @@ "description": "", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "lint": "eslint --fix ./src/**/*.{ts,js}", + "dev": "cross-env NODE_ENV=development nodemon --exec ts-node ./src/index.ts", + "build": "tsc", + "start": "tsc && cross-env NODE_ENV=production node ./build/index.js" }, "keywords": [], "author": "", "license": "ISC", + "devDependencies": { + "@types/cors": "~2.8.17", + "@types/express": "~4.17.21", + "@types/node": "~20.11.16", + "@typescript-eslint/eslint-plugin": "~6.20.0", + "cross-env": "~7.0.3", + "eslint": "~8.56.0", + "eslint-config-prettier": "~9.1.0", + "eslint-config-standard-with-typescript": "~43.0.1", + "eslint-plugin-import": "~2.29.1", + "eslint-plugin-n": "~16.6.2", + "eslint-plugin-prettier": "~5.1.3", + "eslint-plugin-promise": "~6.1.1", + "nodemon": "~3.0.3", + "prettier": "~3.2.5", + "ts-node": "~10.9.2", + "typescript": "~5.3.3" + }, "dependencies": { - "express": "^4.18.2" + "cors": "~2.8.5", + "dotenv": "~16.4.1", + "express": "~4.18.2" } } diff --git a/packages/templates/typescript/process.env.d.ts b/packages/templates/typescript/process.env.d.ts new file mode 100644 index 0000000..c887da4 --- /dev/null +++ b/packages/templates/typescript/process.env.d.ts @@ -0,0 +1,10 @@ +declare global { + namespace NodeJS { + interface ProcessEnv { + [key: string]: string | undefined; + PORT: string; + } + } +} + +export {}; diff --git a/packages/templates/typescript/src/controllers/user.ts b/packages/templates/typescript/src/controllers/user.ts new file mode 100644 index 0000000..568695c --- /dev/null +++ b/packages/templates/typescript/src/controllers/user.ts @@ -0,0 +1,11 @@ +import { type Request, type Response } from 'express'; +import usersModel from '../models/user'; + +function getAllUsers(req: Request, res: Response): void { + const users = usersModel.getUsers(); + res.status(200).json(users); +} + +export default { + getAllUsers, +}; diff --git a/packages/templates/typescript/src/index.ts b/packages/templates/typescript/src/index.ts new file mode 100644 index 0000000..ca39c68 --- /dev/null +++ b/packages/templates/typescript/src/index.ts @@ -0,0 +1,22 @@ +import express from 'express'; +import dotenv from 'dotenv'; +import cors from 'cors'; + +import routes from './routes/index'; + +dotenv.config(); + +const app = express(); +const PORT = process.env.PORT ?? 3000; + +app.use(cors()); +app.use(express.urlencoded({ extended: true })); +app.use(express.json()); + +app.use(`/api`, routes); + +app.listen(PORT, () => { + console.log(`Server is running on port ${PORT}`); +}); + +export default app; diff --git a/packages/templates/typescript/src/models/user.ts b/packages/templates/typescript/src/models/user.ts new file mode 100644 index 0000000..e0851ec --- /dev/null +++ b/packages/templates/typescript/src/models/user.ts @@ -0,0 +1,23 @@ +interface User { + name: string; + descrition: string; +} + +class UsersModel { + users: User[]; + + constructor() { + this.users = [ + { name: 'RulerChen', descrition: 'Author of this project' }, + { name: 'joshtu0627', descrition: 'Author of this project' }, + ]; + } + + getUsers(): User[] { + return this.users; + } +} + +const usersModel = new UsersModel(); + +export default usersModel; diff --git a/packages/templates/typescript/src/routes/index.ts b/packages/templates/typescript/src/routes/index.ts new file mode 100644 index 0000000..abdaa0b --- /dev/null +++ b/packages/templates/typescript/src/routes/index.ts @@ -0,0 +1,13 @@ +import express from 'express'; + +import UserRoutes from './user'; + +const router = express.Router(); + +router.get('/', (req, res) => { + res.send('This is the API root!'); +}); + +router.use('/users', UserRoutes); + +export default router; diff --git a/packages/templates/typescript/src/routes/user.ts b/packages/templates/typescript/src/routes/user.ts new file mode 100644 index 0000000..1eef3d0 --- /dev/null +++ b/packages/templates/typescript/src/routes/user.ts @@ -0,0 +1,9 @@ +import express from 'express'; + +import UserController from '../controllers/user'; + +const router = express.Router(); + +router.get('/', UserController.getAllUsers); + +export default router; diff --git a/packages/templates/typescript/tsconfig.json b/packages/templates/typescript/tsconfig.json new file mode 100644 index 0000000..347816d --- /dev/null +++ b/packages/templates/typescript/tsconfig.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "target": "es6", + "module": "NodeNext", + "moduleResolution": "NodeNext", + "baseUrl": "src", + "outDir": "build", + "sourceMap": true, + "strict": true, + "types": ["node"] + }, + "include": ["process.env.d.ts", "./src/**/*.ts"] +}