Skip to content

Commit

Permalink
Merge pull request #2 from RulerChen/feat/javascript
Browse files Browse the repository at this point in the history
feat: javascript template
  • Loading branch information
RulerChen authored Feb 5, 2024
2 parents 27f8691 + 9178fee commit 157f8ef
Show file tree
Hide file tree
Showing 27 changed files with 346 additions and 18 deletions.
36 changes: 24 additions & 12 deletions packages/package-lock.json

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

6 changes: 3 additions & 3 deletions packages/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions packages/scripts/createProject.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
17 changes: 17 additions & 0 deletions packages/templates/common/.gitignore
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions packages/templates/javascript/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PORT=8000
1 change: 1 addition & 0 deletions packages/templates/javascript/.env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PORT=
26 changes: 26 additions & 0 deletions packages/templates/javascript/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
}
9 changes: 9 additions & 0 deletions packages/templates/javascript/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"semi": true,
"printWidth": 140,
"tabWidth": 2,
"singleQuote": true,
"trailingComma": "all",
"arrowParens": "always",
"bracketSpacing": true
}
30 changes: 30 additions & 0 deletions packages/templates/javascript/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
10 changes: 10 additions & 0 deletions packages/templates/javascript/src/controllers/user.js
Original file line number Diff line number Diff line change
@@ -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,
};
22 changes: 22 additions & 0 deletions packages/templates/javascript/src/index.js
Original file line number Diff line number Diff line change
@@ -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;
16 changes: 16 additions & 0 deletions packages/templates/javascript/src/models/user.js
Original file line number Diff line number Diff line change
@@ -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;
13 changes: 13 additions & 0 deletions packages/templates/javascript/src/routes/index.js
Original file line number Diff line number Diff line change
@@ -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;
9 changes: 9 additions & 0 deletions packages/templates/javascript/src/routes/user.js
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions packages/templates/typescript/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PORT=8000
1 change: 1 addition & 0 deletions packages/templates/typescript/.env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PORT=
26 changes: 26 additions & 0 deletions packages/templates/typescript/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
}
9 changes: 9 additions & 0 deletions packages/templates/typescript/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"semi": true,
"printWidth": 140,
"tabWidth": 2,
"singleQuote": true,
"trailingComma": "all",
"arrowParens": "always",
"bracketSpacing": true
}
1 change: 0 additions & 1 deletion packages/templates/typescript/index.ts

This file was deleted.

27 changes: 25 additions & 2 deletions packages/templates/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
10 changes: 10 additions & 0 deletions packages/templates/typescript/process.env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
declare global {
namespace NodeJS {
interface ProcessEnv {
[key: string]: string | undefined;
PORT: string;
}
}
}

export {};
11 changes: 11 additions & 0 deletions packages/templates/typescript/src/controllers/user.ts
Original file line number Diff line number Diff line change
@@ -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,
};
Loading

0 comments on commit 157f8ef

Please sign in to comment.