ns-init
Version:
Scaffold a ready-made CLI for Node.js + Express server (JavaScript or TypeScript)
246 lines (167 loc) • 6.88 kB
Markdown
# 🚀 ns-init — Node Server Initializer CLI
*A simple CLI tool to bootstrap **Node.js server projects** with support for **TypeScript** or **JavaScript** templates.
Includes optional **logger** integration with [`node-js-api-response`](https://www.npmjs.com/package/node-js-api-response).*
[](https://www.npmjs.com/package/ns-init)
[](https://www.npmjs.com/package/ns-init)
[](./LICENSE)
## 🛠 Tech Stack
[](https://nodejs.org/)
[](https://expressjs.com/)
[](https://developer.mozilla.org/docs/Web/JavaScript)
[](https://www.typescriptlang.org/)
[](https://www.mongodb.com/)
[](https://mongoosejs.com/)
## 🔗 Related Projects
You can also check out our **Node.js Centralised API Response and Error Handling** utility:
[](https://www.npmjs.com/package/node-js-api-response)
[](https://www.npmjs.com/package/node-js-api-response)
*A lightweight package to standardize API success/error responses and logging in Express.js apps.*
## 📦 Installation
```bash
npm install -g ns-init@latest
````
or run directly with **npx** (no install required):
```bash
npx ns-init my-app
```
## 🆕 Latest Update (v1.0.8)
### 🔹 1. Swagger Documentation
* Automatically sets up Swagger (OpenAPI) config in `src/config/swagger.config.ts`.
* Exposes API docs at:
```
http://localhost:<PORT>/api/v1/api-docs
```
Example in `index.ts`:
```ts
import swaggerUi from "swagger-ui-express";
import {swaggerSpec} from "./config/index.config.ts";
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerSpec));
```
### 🔹 2. Automatic Port Assignment
* If the default port (`process.env.PORT` or `8080`) is already in use,
ns-init will **automatically find the next available port** (`8081`, `8082`, ...).
```ts
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`✅ Server running on http://localhost:${PORT}`);
});
```
### 🔹 3. Bug Fixes & Improvements
* Fixed API response formatting in edge cases.
* Resolved minor CLI bugs during project scaffolding.
## ⚡ Usage
```bash
ns-init [project-name] [options]
```
If no options are passed, the CLI runs in interactive mode (project name, template, logger setup).
## 🛠 Options
| Option | Description |
| ------------ | -------------------------------------------------- |
| `--ts` | Use **TypeScript** template |
| `--js` | Use **JavaScript** template |
| `--log` | Include `node-js-api-response` logger + middleware |setup |
| `-h, --help` | Show help |
## 📋 Examples
### 1. Interactive (recommended)
```bash
npx ns-init
```
### 2. TypeScript
```bash
npx ns-init my-app --ts
npx ns-init my-app --ts --log # with logger
```
### 3. JavaScript
```bash
npx ns-init my-app --js
npx ns-init my-app --js --log # with logger
```
## 📂 Project Structure
Example output for **TypeScript** template:
```
my-app/
├── src/
│ ├── config/
│ │ ├── envConfig.ts
│ │ ├── swagger.config.ts
│ │ └── db.config.ts
│ ├── routes/
│ └── app.ts
│ └── index.ts
├── .env.development
├── .env.production
├── package.json
├── tsconfig.json
└── README.md
```
For **JavaScript** template:
```
my-app/
├── src/
│ ├── config/
│ │ ├── envConfig.js
│ │ ├── swagger.config.js
│ │ └── db.config.js
│ ├── routes/
│ └── app.js
│ └── index.js
├── .env.development
├── .env.production
├── package.json
├── tsconfig.json
└── README.md
```
## 📸 Screenshots / Demo
### 🚀 Interactive CLI Prompt
When you run `ns-init` without arguments, you’ll be guided interactively:
```bash
npx ns-init
```

### ⚡ Direct Command with Options
You can also skip prompts and pass flags directly:
```bash
npx ns-init my-app --ts --log
```

### 📦 Generated Project Structure
After scaffolding, you’ll get a clean project:

## 📝 Logger Integration
If you enable logging with the **`--log`** option, the generated project will include a request/response logger powered by **node-js-api-response**.
Depending on whether you choose **`TypeScript` or `JavaScript`**, the boilerplate will auto-inject the following:
```ts
import useragent from "express-useragent";
import { requestResponseLogger, appLogger } from "node-js-api-response";
const app: Application = express();
app.use(useragent.express());
app.use(requestResponseLogger);
appLogger.info(`✅ Server running on http://localhost:${PORT}`);
```
## 📜 Changelog - v1.0.8
- 🚀 Added Swagger documentation setup
- ⚡ Automatic port assignment if default is busy
- 🐛 Fixed API response formatting edge cases
- 🔧 Minor CLI scaffolding bug fixes
## 🤝 Contributing
Pull requests and feature requests are welcome!
Feel free to open an issue for bugs or suggestions.
## 📜 License
This package is licensed under the MIT License.
This documentation provides a quick overview of the features, installation instructions, and usage examples for each utility. Let me know if you need further clarifications!