From 42f8cbae91fcef1199f13446c133130a9e5bc8ee Mon Sep 17 00:00:00 2001 From: Nabadeep Date: Sun, 11 Dec 2022 21:48:18 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=96=20DOC:=20=20Doc=20updated,=20creat?= =?UTF-8?q?e=20npx=20starter=20command?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 143 ++++++++++++++++++++++++++++++++++++++++++++++++++- bin/cli.js | 80 ++++++++++++++++++++++++++++ package.json | 5 +- 3 files changed, 224 insertions(+), 4 deletions(-) create mode 100644 bin/cli.js diff --git a/README.md b/README.md index a1c52d6..2f49332 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,66 @@ # Typescript-Node-Sequelize-Boilerplate -- Node (TypeScript) +A boilerplate/starter project for quickly building RESTful APIs using Node.js,Typescript, Express, and Sequelize. + + +- Node +- Typescript +- Express - MySql - + +## Table of Contents + +- [Typescript-Node-Sequelize-Boilerplate](#typescript-node-sequelize-boilerplate) + - [Table of Contents](#table-of-contents) + - [Quick start](#quick-start) + - [Manual Installation](#manual-installation) + - [Getting started](#getting-started) + - [For development](#for-development) + - [Sample .ENV](#sample-env) + - [Commands](#commands) + - [Project Structure](#project-structure) + - [API Documentation](#api-documentation) + - [API Endpoints](#api-endpoints) + - [Linting](#linting) + - [Inspirations](#inspirations) + + + +## Quick start + +create boillerplate with single command +``` + npx @nabadeep25/create-ts-node-app myapp + +``` + + + +## Manual Installation + +steps: + +Clone the repo: + +``` +git clone --depth 1 https://github.com/nabadeep25/typescript-node-sequelize-boilerplate.git foldername + +cd folder name +npx rimraf ./.git +``` + +Install the dependencies: + +``` +npm install +``` + +Set the environment variables: + +``` +cp .env.example .env + +``` ## Getting started ``` @@ -50,3 +108,84 @@ OTP_SECRET=shgdbnbgw + +## Commands + + +```bash +# run in development +npm run watch + +# run in production +npm run start + +# lint files +npm run lint + +# format files +npm run format + +``` + + + + +## Project Structure + +``` +src\ + |--config\ # Environment variables and configuration related things + |--controllers\ # Route controllers + |--helpers\ # Helper function files + |--middlewares\ # Custom express middlewares + |--model\ # Sequelize models + |--routes\ # Routes + |--services\ # Service + |--utils\ # Utility classes and functions + |--validations\ # Request data validation schemas + |--app.ts # Express app + |--server.ts # App entry point +``` + +## API Documentation + +To view the list of available APIs and their specifications, run the server and go to `http://localhost:5000/api/v1/docs` in your browser. This documentation page is automatically generated using the [swagger](https://swagger.io/) definitions written as comments in the route files. + +### API Endpoints + +List of available routes: + +**Auth routes**:\ +`POST api/v1/auth/register` - register\ +`POST api/v1/auth/login` - login\ +`POST api/v1/auth/forgot-password` - send reset password email\ +`POST api/v1/auth/reset-password` - reset password\ + + +**User routes**:\ +`GET api/v1/user` - get user info\ +`PATCH api/v1/user` - update user\ + + +## Linting + +Linting is done using [ESLint](https://eslint.org/) and [Prettier](https://prettier.io). + +In this app, ESLint is configured to follow the [Airbnb JavaScript style guide](https://github.com/airbnb/javascript/tree/master/packages/eslint-config-airbnb-base) with some modifications. It also extends [eslint-config-prettier](https://github.com/prettier/eslint-config-prettier) to turn off all rules that are unnecessary or might conflict with Prettier. + +To modify the ESLint configuration, update the `.eslintrc.json` file. To modify the Prettier configuration, update the `.prettierrc.json` file. + +To prevent a certain file or directory from being linted, add it to `.eslintignore` and `.prettierignore`. + +To maintain a consistent coding style across different IDEs, the project contains `.editorconfig` + + + +## Inspirations +- [hagopj13/node-express-boilerplate](https://github.com/hagopj13/node-express-boilerplate) +- [microsoft/typescript-node-starter](https://github.com/microsoft/TypeScript-Node-Starter) + + + + + diff --git a/bin/cli.js b/bin/cli.js new file mode 100644 index 0000000..e9fd7fc --- /dev/null +++ b/bin/cli.js @@ -0,0 +1,80 @@ +#!/usr/bin/env node +/* eslint-disable @typescript-eslint/no-var-requires */ +const path = require("path"); +const fs = require("fs"); +const { execSync } = require("child_process"); + +async function runCommand(command) { + try { + execSync(`${command}`, { stdio: "inherit" }); + } catch (e) { + console.log(`Failed to execute ${command}`, error); + return false; + } + return true; +} + +if (process.argv.length < 3) { + console.log("Please specify the target project directory."); + console.log("For example:"); + console.log(" npx @nabadeep25/create-ts-node-app my-app"); + process.exit(1); +} + +const currentPath = process.cwd(); +const folderName = process.argv[2]; +const appPath = path.join(currentPath, folderName); +const repo = + "https://github.com/nabadeep25/typescript-node-sequelize-boilerplate.git"; + +try { + fs.mkdirSync(appPath); +} catch (err) { + if (err.code === "EEXIST") { + console.log( + "Folder already exists. Please choose another name for the project." + ); + } else { + console.log(err); + } + process.exit(1); +} + +async function setup() { + try { + // Clone repo + console.log(`Downloading files from ${repo}`); + let cloned = runCommand(`git clone --depth 1 ${repo} ${folderName}`); + if (!cloned) process.exit(-1); + console.log("Cloned successfully.\n"); + + process.chdir(appPath); + + console.log("Installing dependencies...\n"); + runCommand("npm install"); + + console.log("Dependencies installed successfully.\n"); + + fs.copyFileSync( + path.join(appPath, ".env.example"), + path.join(appPath, ".env") + ); + console.log("Environment files copied.\n"); + + runCommand("npx rimraf ./.git"); + + fs.unlinkSync(path.join(appPath, "bin", "cli.js")); + fs.rmdirSync(path.join(appPath, "bin")); + + console.log("Installation is now complete!\n"); + console.log("To start developing follow :"); + console.log(` cd ${folderName}`); + console.log(" npm run watch"); + console.log(); + console.log("🎉 Happy coding 💻!"); + } catch (error) { + console.log(error); + } +} + +setup(); diff --git a/package.json b/package.json index fcd5959..fa9b884 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,9 @@ { - "name": "typescript-node-sequelize-boilerplate", - "version": "0.1.0", + "name": "@nabadeep25/create-ts-node-app", + "version": "0.1.3", "description": "", "author": "Nabadeep Thakuria", + "bin": "bin/cli.js", "homepage": "https://github.com/nabadeep25/typescript-node-sequelize-boilerplate", "license": "MIT", "keywords": ["typescript","node","express","sequelize","mysql","postgres","boilerplate","template","typescript node sequelize boilerplate","typescript node","typescript sequelize boilerplate"],