diff --git a/package.json b/package.json index a6a58d3..cbba65d 100644 --- a/package.json +++ b/package.json @@ -1,32 +1,32 @@ { - "name": "nork", - "version": "1.1.6", - "description": "The best node.js 'framework' :)", - "main": "src/app.js", - "bin": "src/app.js", - "scripts": { - "start": "node src/app.js" - }, - "keywords": [ - "node", - "framework", - "express", - "mvc" - ], - "author": "Filip Rojek", - "license": "MIT", - "dependencies": { - "colors": "^1.4.0", - "fs-extra": "^10.0.0", - "inquirer": "^8.1.2", - "pad": "^3.2.0" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/filiprojek/nork.git" - }, - "bugs": { - "url": "https://github.com/filiprojek/nork/issues" - }, - "homepage": "https://github.com/filiprojek/nork/blob/master/README.md" -} + "name": "nork", + "version": "1.2.0", + "description": "The best node.js 'framework' :)", + "main": "src/app.js", + "bin": "src/app.js", + "scripts": { + "start": "node src/app.js" + }, + "keywords": [ + "node", + "framework", + "express", + "mvc" + ], + "author": "Filip Rojek", + "license": "MIT", + "dependencies": { + "colors": "^1.4.0", + "fs-extra": "^10.0.0", + "inquirer": "^8.1.2", + "pad": "^3.2.0" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/filiprojek/nork.git" + }, + "bugs": { + "url": "https://github.com/filiprojek/nork/issues" + }, + "homepage": "https://github.com/filiprojek/nork/blob/master/README.md" +} \ No newline at end of file diff --git a/src/app.js b/src/app.js index b557ddf..284dafa 100644 --- a/src/app.js +++ b/src/app.js @@ -59,9 +59,11 @@ const logHelp = (specific = false, command = false) => { console.log(pad(' create [app-name]', spc), 'create a new project') console.log(pad(' create [app-name] -i', spc), 'create a new project in current directory') console.log(pad(' make controller [name]', spc), 'create a new controller') + console.log(pad(' make interface [name]', spc), 'create a new interface') console.log(pad(' make middleware [name]', spc), 'create a new middleware') console.log(pad(' make model [name]', spc), 'create a new model') console.log(pad(' make route [name]', spc), 'create a new route') + console.log(pad(' make service [name]', spc), 'create a new service') console.log(pad(' make test [name]', spc), 'create a new test') console.log(pad(' make view [name]', spc), 'create a new view') console.log(pad(' setup', spc), 'set up an existing project for nork') @@ -112,7 +114,7 @@ const logHelp = (specific = false, command = false) => { data.author = answers.author // copy skeleton to new project - process.argv.includes('-i') ? projectPath = process.cwd() : projectPath = path.join(process.cwd(), data.project_name) + process.argv.includes('-i') ? (projectPath = process.cwd()) : (projectPath = path.join(process.cwd(), data.project_name)) fs.copySync(path.join(__dirname, './skeletons/express-' + data.lang), projectPath) // edit package.json file @@ -140,7 +142,7 @@ const logHelp = (specific = false, command = false) => { const component = process.argv[3] const norkcfg = require(path.join(process.cwd(), 'norkconfig.json')) - let tsComponents = ['controller', 'middleware', 'route'] + let tsComponents = ['controller', 'middleware', 'route', 'service'] if (tsComponents.includes(component)) { let src = path.join(__dirname, './make-files/express-' + norkcfg.lang + '/' + component + '.' + norkcfg.lang) @@ -182,6 +184,17 @@ const logHelp = (specific = false, command = false) => { let src = path.join(__dirname, './make-files/express-' + norkcfg.lang + '/' + component + '.js') let dest = path.join(process.cwd(), './src/' + component + 's' + '/' + process.argv[4] + '.test.js') + try { + fs.copySync(src, dest, { overwrite: false, errorOnExist: true }) + } catch (err) { + return logError(err.message) + } + return logSuccess() + } + if (component == 'interface') { + let src = path.join(__dirname, './make-files/express-' + norkcfg.lang + '/' + component + '.ts') + let dest = path.join(process.cwd(), './src/' + component + 's' + '/' + process.argv[4] + '.ts') + try { fs.copySync(src, dest, { overwrite: false, errorOnExist: true }) } catch (err) { diff --git a/src/make-files/express-ts/controller.ts b/src/make-files/express-ts/controller.ts index 1cfd054..4a94a0d 100644 --- a/src/make-files/express-ts/controller.ts +++ b/src/make-files/express-ts/controller.ts @@ -1,10 +1,6 @@ import { Request, Response } from 'express' -const root_get = (req: Request, res: Response) => { +export const root_get = (req: Request, res: Response) => { res.render('home') return true } - -module.exports = { - root_get, -} diff --git a/src/make-files/express-ts/interface.ts b/src/make-files/express-ts/interface.ts new file mode 100644 index 0000000..727a759 --- /dev/null +++ b/src/make-files/express-ts/interface.ts @@ -0,0 +1,4 @@ +export interface interfaceName { + hello: string + world: boolean | null | undefined +} diff --git a/src/make-files/express-ts/middleware.ts b/src/make-files/express-ts/middleware.ts index 7a9027b..32accc2 100644 --- a/src/make-files/express-ts/middleware.ts +++ b/src/make-files/express-ts/middleware.ts @@ -1,10 +1,8 @@ import { Router, Request, Response, NextFunction } from 'express' -const router = Router() +export const router = Router() router.use((req: Request, res: Response, next: NextFunction) => { console.log('Hi :)') next() }) - -module.exports = router diff --git a/src/make-files/express-ts/model.js b/src/make-files/express-ts/model.js deleted file mode 100644 index 877524f..0000000 --- a/src/make-files/express-ts/model.js +++ /dev/null @@ -1,17 +0,0 @@ -const mongoose = require('mongoose') -const Schema = mongoose.Schema - -const modelSchema = new Schema( - { - title: { - type: String, - required: true, - }, - }, - { - timestamps: true, - }, -) - -const ModelName = mongoose.model('ModelName', modelSchema) -module.exports = ModelName diff --git a/src/make-files/express-ts/model.ts b/src/make-files/express-ts/model.ts new file mode 100644 index 0000000..cc22d18 --- /dev/null +++ b/src/make-files/express-ts/model.ts @@ -0,0 +1,15 @@ +import { Schema, model } from 'mongoose' + +const modelSchema = new Schema( + { + title: { + type: String, + required: true, + }, + }, + { + timestamps: true, + }, +) + +export default model('ModelName', modelSchema) diff --git a/src/make-files/express-ts/route.ts b/src/make-files/express-ts/route.ts index e78b69b..7374556 100644 --- a/src/make-files/express-ts/route.ts +++ b/src/make-files/express-ts/route.ts @@ -1,8 +1,6 @@ import { Router } from 'express' -const rootController = require('../controllers/rootController') +import * as rootController from '@/controllers/rootController' -const router = Router() +export const router = Router() router.get('/', rootController.root_get) - -module.exports = router diff --git a/src/make-files/express-ts/service.ts b/src/make-files/express-ts/service.ts new file mode 100644 index 0000000..ccb388e --- /dev/null +++ b/src/make-files/express-ts/service.ts @@ -0,0 +1,3 @@ +export function helloWorld() { + console.log('hello world') +} diff --git a/src/skeletons/express-ts/package.json b/src/skeletons/express-ts/package.json index e3c16b3..4ae4d96 100644 --- a/src/skeletons/express-ts/package.json +++ b/src/skeletons/express-ts/package.json @@ -1,45 +1,62 @@ { - "name": "project-name", - "version": "1.0.0", - "description": "", - "main": "app.js", - "scripts": { - "start": "node dist/app.js", - "dev": "nodemon src/app.ts", - "test": "jest", - "clean": "rimraf dist/*", - "copy-assets": "ts-node src/utils/copyAssets", - "tsc": "tsc -p .", - "build": "npm-run-all clean tsc copy-assets" - }, - "keywords": [], - "author": "", - "license": "ISC", - "dependencies": { - "cors": "^2.8.5", - "dotenv": "^8.2.0", - "ejs": "^3.1.6", - "express": "^4.17.1", - "mongoose": "^5.12.3", - "morgan": "^1.10.0", - "fs-extra": "^10.0.0" - }, - "devDependencies": { - "@types/cors": "^2.8.10", - "@types/ejs": "^3.0.6", - "@types/express": "^4.17.11", - "@types/fs-extra": "^9.0.12", - "@types/jest": "^27.0.1", - "@types/mongoose": "^5.10.5", - "@types/morgan": "^1.9.2", - "@types/node": "^14.14.41", - "@types/shelljs": "^0.8.9", - "jest": "^27.0.6", - "npm-run-all": "^4.1.5", - "rimraf": "^3.0.2", - "shelljs": "^0.8.4", - "ts-jest": "^27.0.5", - "ts-node": "^9.1.1", - "typescript": "^4.2.4" - } -} + "name": "project-name", + "version": "1.0.0", + "description": "", + "main": "app.js", + "scripts": { + "start": "node -r tsconfig-paths/register -r ts-node/register dist/server.js", + "dev": "nodemon src/server.ts", + "test": "jest", + "clean": "rimraf dist/*", + "copy-assets": "ts-node src/utils/copyAssets", + "tsc": "tsc -p .", + "build": "npm-run-all clean tsc copy-assets" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "colors": "^1.4.0", + "cookie-parser": "^1.4.5", + "cors": "^2.8.5", + "dotenv": "^8.2.0", + "ejs": "^3.1.6", + "express": "^4.17.1", + "fs-extra": "^10.0.0", + "mongoose": "^5.12.3", + "morgan": "^1.10.0" + }, + "devDependencies": { + "@types/cookie-parser": "^1.4.2", + "@types/cors": "^2.8.10", + "@types/ejs": "^3.0.6", + "@types/express": "^4.17.11", + "@types/fs-extra": "^9.0.12", + "@types/jest": "^27.0.1", + "@types/mongoose": "^5.10.5", + "@types/morgan": "^1.9.2", + "@types/node": "^14.14.41", + "@types/shelljs": "^0.8.9", + "jest": "^27.0.6", + "npm-run-all": "^4.1.5", + "rimraf": "^3.0.2", + "shelljs": "^0.8.4", + "ts-jest": "^27.0.5", + "ts-node": "^9.1.1", + "tsconfig-paths": "^3.11.0", + "typescript": "^4.2.4" + }, + "nodemonConfig": { + "ignore": [ + "**/*.test.ts", + "**/*.spec.ts", + ".git", + "node_modules" + ], + "watch": [ + "src" + ], + "exec": "node -r tsconfig-paths/register -r ts-node/register ./src/server.ts", + "ext": "ts, js" + } +} \ No newline at end of file diff --git a/src/skeletons/express-ts/src/app.ts b/src/skeletons/express-ts/src/app.ts index 2ea4ee0..f190a11 100644 --- a/src/skeletons/express-ts/src/app.ts +++ b/src/skeletons/express-ts/src/app.ts @@ -1,43 +1,36 @@ import express from 'express' import morgan from 'morgan' -import mongoose from 'mongoose' import path from 'path' import cors from 'cors' +import cookieParser from 'cookie-parser' +import { router as routes } from '@/routes' +import { router as middlewares } from '@/middlewares' -const config = require('./utils/environment') -const routes = require('./routes') -const middlewares = require('./middlewares') +const corsWhitelist = ['http://localhost:8080', 'http://localhost:6060'] +const corsOptions = { + origin: function (origin: any, callback: any) { + if (!origin || corsWhitelist.indexOf(origin) !== -1) { + callback(null, true) + } else { + callback(new Error('Not allowed by CORS')) + } + }, + optionsSuccessStatus: 200, + credentials: true, +} -const port: Number = config.APP_PORT || 8080 -const app = express() - -// MongoDB -const dbURI: string = config.DB_URI -mongoose - .connect(dbURI, { - useNewUrlParser: true, - useUnifiedTopology: true, - useCreateIndex: true, - }) - .then(result => { - console.log('connected to db') - app.listen(port, () => { - console.log(`Server is listening on http://localhost:${port}`) - }) - }) - .catch(err => { - console.log(err) - }) +export const app = express() // Middlewares app.use(middlewares) app.set('view engine', 'ejs') app.set('views', path.join(__dirname, 'views')) -app.use(cors()) +app.use(cors(corsOptions)) app.use(morgan('dev')) app.use(express.urlencoded({ extended: true })) app.use(express.json()) app.use(express.static(path.join(__dirname, 'public'))) +app.use(cookieParser()) // Routes app.use(routes) diff --git a/src/skeletons/express-ts/src/controllers/rootController.ts b/src/skeletons/express-ts/src/controllers/rootController.ts index 1cfd054..4a94a0d 100644 --- a/src/skeletons/express-ts/src/controllers/rootController.ts +++ b/src/skeletons/express-ts/src/controllers/rootController.ts @@ -1,10 +1,6 @@ import { Request, Response } from 'express' -const root_get = (req: Request, res: Response) => { +export const root_get = (req: Request, res: Response) => { res.render('home') return true } - -module.exports = { - root_get, -} diff --git a/src/skeletons/express-ts/src/interfaces/globalInterface.ts b/src/skeletons/express-ts/src/interfaces/globalInterface.ts new file mode 100644 index 0000000..4f4e882 --- /dev/null +++ b/src/skeletons/express-ts/src/interfaces/globalInterface.ts @@ -0,0 +1,4 @@ +export interface ErrType { + code: number + message: string +} diff --git a/src/skeletons/express-ts/src/middlewares/index.ts b/src/skeletons/express-ts/src/middlewares/index.ts index 640b880..3ac1fdb 100644 --- a/src/skeletons/express-ts/src/middlewares/index.ts +++ b/src/skeletons/express-ts/src/middlewares/index.ts @@ -1,8 +1,6 @@ import { Router } from 'express' -const sayHiMiddleware = require('./sayHiMiddleware') +import { router as sayHiMiddleware } from '@/middlewares/sayHiMiddleware' -const router = Router() +export const router = Router() router.use(sayHiMiddleware) - -module.exports = router diff --git a/src/skeletons/express-ts/src/middlewares/sayHiMiddleware.ts b/src/skeletons/express-ts/src/middlewares/sayHiMiddleware.ts index f0ba714..bc3a6e1 100644 --- a/src/skeletons/express-ts/src/middlewares/sayHiMiddleware.ts +++ b/src/skeletons/express-ts/src/middlewares/sayHiMiddleware.ts @@ -1,11 +1,9 @@ import { Router, Request, Response, NextFunction } from 'express' -const router = Router() +export const router = Router() router.use((req: Request, res: Response, next: NextFunction) => { console.log('Hi :)') next() }) - -module.exports = router diff --git a/src/skeletons/express-ts/src/routes/index.ts b/src/skeletons/express-ts/src/routes/index.ts index 93ad010..fec3481 100644 --- a/src/skeletons/express-ts/src/routes/index.ts +++ b/src/skeletons/express-ts/src/routes/index.ts @@ -1,13 +1,11 @@ -import { Router } from 'express' -const rootRoutes = require('./rootRoutes') +import { Request, Response, Router } from 'express' +import { router as rootRoutes } from './rootRoutes' -const router = Router() +export const router = Router() router.use(rootRoutes) // 404 -router.use((req, res) => { +router.use((req: Request, res: Response) => { res.status(404).send('E404') }) - -module.exports = router diff --git a/src/skeletons/express-ts/src/routes/rootRoutes.ts b/src/skeletons/express-ts/src/routes/rootRoutes.ts index e78b69b..7374556 100644 --- a/src/skeletons/express-ts/src/routes/rootRoutes.ts +++ b/src/skeletons/express-ts/src/routes/rootRoutes.ts @@ -1,8 +1,6 @@ import { Router } from 'express' -const rootController = require('../controllers/rootController') +import * as rootController from '@/controllers/rootController' -const router = Router() +export const router = Router() router.get('/', rootController.root_get) - -module.exports = router diff --git a/src/skeletons/express-ts/src/server.ts b/src/skeletons/express-ts/src/server.ts new file mode 100644 index 0000000..7943293 --- /dev/null +++ b/src/skeletons/express-ts/src/server.ts @@ -0,0 +1,24 @@ +import mongoose from 'mongoose' +import { app } from '@/app' +import config from '@/utils/environment' +import { Err, Succ } from '@/services/globalService' + +const port: Number = config.APP_PORT || 8080 + +// MongoDB +const dbURI: string = config.DB_URI +mongoose + .connect(dbURI, { + useNewUrlParser: true, + useUnifiedTopology: true, + useCreateIndex: true, + }) + .then(() => { + new Succ(200, 'connected to db') + app.listen(port, () => { + new Succ(200, `Server is listening on http://localhost:${port}`) + }) + }) + .catch((err: any) => { + new Err(500, err) + }) diff --git a/src/skeletons/express-ts/src/services/globalService.ts b/src/skeletons/express-ts/src/services/globalService.ts new file mode 100644 index 0000000..9464eed --- /dev/null +++ b/src/skeletons/express-ts/src/services/globalService.ts @@ -0,0 +1,40 @@ +import colors from 'colors' +import { ErrType } from '@/interfaces/globalInterface' + +export class Err implements ErrType { + code: number + message: string + + constructor(code: number, message: string) { + this.code = code + this.message = message + this.drop() + } + + drop() { + console.log(colors.bgRed(`${this.code}`) + colors.bgBlack.red(` ${this.message}`)) + return { + code: this.code, + message: this.message, + } + } +} + +export class Succ { + code: number + message: string + + constructor(code: number, message: string) { + this.code = code + this.message = message + this.drop() + } + + drop() { + console.log(colors.bgGreen.black(`${this.code}`) + colors.green.bgBlack(` ${this.message}`)) + return { + code: this.code, + message: this.message, + } + } +} diff --git a/src/skeletons/express-ts/src/services/rootService.ts b/src/skeletons/express-ts/src/services/rootService.ts new file mode 100644 index 0000000..75ca5d8 --- /dev/null +++ b/src/skeletons/express-ts/src/services/rootService.ts @@ -0,0 +1,3 @@ +export const helloWorld = () => { + console.log('hello world') +} diff --git a/src/skeletons/express-ts/src/utils/copyAssets.ts b/src/skeletons/express-ts/src/utils/copyAssets.ts index 52c7555..658c4c5 100644 --- a/src/skeletons/express-ts/src/utils/copyAssets.ts +++ b/src/skeletons/express-ts/src/utils/copyAssets.ts @@ -3,4 +3,5 @@ import * as shell from 'shelljs' // Copy all the view templates shell.cp('-R', 'src/views', 'dist/') shell.cp('-R', 'src/public', 'dist/') +shell.cp('-R', 'src/models', 'dist/') shell.cp('-u', 'src/.env', 'dist/') diff --git a/src/skeletons/express-ts/src/utils/environment.ts b/src/skeletons/express-ts/src/utils/environment.ts index c53d8cb..6280b67 100644 --- a/src/skeletons/express-ts/src/utils/environment.ts +++ b/src/skeletons/express-ts/src/utils/environment.ts @@ -1,7 +1,7 @@ import path from 'path' require('dotenv').config({ path: path.join(__dirname, '../.env') }) -module.exports = { - APP_PORT: process.env.APP_PORT, - DB_URI: process.env.DB_URI, +export default { + APP_PORT: Number(process.env.APP_PORT), + DB_URI: String(process.env.DB_URI), } diff --git a/src/skeletons/express-ts/tsconfig.json b/src/skeletons/express-ts/tsconfig.json index d052944..8f4eb08 100644 --- a/src/skeletons/express-ts/tsconfig.json +++ b/src/skeletons/express-ts/tsconfig.json @@ -14,7 +14,7 @@ // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ // "sourceMap": true, /* Generates corresponding '.map' file. */ // "outFile": "./", /* Concatenate and emit output to single file. */ - "outDir": "./dist", /* Redirect output structure to the directory. */ + "outDir": "./dist", /* Redirect output structure to the directory. */ "rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ // "composite": true, /* Enable project compilation */ // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ @@ -44,8 +44,22 @@ /* Module Resolution Options */ // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ - // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ - // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ + "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ + "paths": { + "@/*": ["src/","src/*"], + "@dist/*": ["dist/*"], + "@controllers/*": ["src/controllers/"], + "@interfaces/*": ["src/interfaces/*"], + "@middlewares/*": ["src/middlewares/*"], + "@models/*": ["src/models/*"], + "@public/*": ["src/public/*"], + "@routes/*": ["src/routes/*"], + "@services/*": ["src/services/*"], + "@test/*": ["src/test/*"], + "@utils/*": ["src/utils/*"], + "@validators/*": ["src/validators/*"], + "@views/*": ["src/views/*"], + }, // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ // "typeRoots": [], /* List of folders to include type definitions from. */ // "types": [], /* Type declaration files to be included in compilation. */