Skip to content

Commit

Permalink
Merge pull request #8 from filiprojek/updates
Browse files Browse the repository at this point in the history
changes:  - norkconfig se generuje lepe a actually ho pouzivam  - pri vytvareni projektu je mozne vybrat si orm (mongoose & sequlize)  - default modely pro db se kopiruji na zaklade parametru db z norkconfigu  - updatnutej ts skeleton  - dropnul jsem support pro js
  • Loading branch information
filiprojek authored Jul 30, 2022
2 parents 3b85bad + d8b8598 commit 564cd09
Show file tree
Hide file tree
Showing 27 changed files with 505 additions and 194 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nork",
"version": "3.0.4",
"version": "3.0.5",
"description": "The best node.js 'framework' :)",
"main": "dist/app.js",
"bin": "dist/app.js",
Expand Down
14 changes: 13 additions & 1 deletion progress-blog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
- https://auth0.com/blog/refresh-tokens-what-are-they-and-when-to-use-them/
- [ ] version of nork control
- [ ] upgrade to newer version system
- [ ] updatnout make files (obzvlast modely a rozlisovat modely podle norkconfigu)
- [ ] vyzadovat aktualni verzi 3.#.# pro make commandy (pouzivaly se jiny predtim soubory)
- [ ] moznost vytvorit projekt bez db


### 11-24-2021
Expand All @@ -19,6 +22,15 @@
- je treba dopsat par types a fixnout zbytek erroru
- zatim netestovana funkcnost

### 1-10.2022
### 1-10-2022
- dodelal jsem create a otestoval ho
- [x] create

### 7-30-2022
- norkconfig se generuje lepe a actually ho pouzivam
- pri vytvareni projektu je mozne vybrat si orm (mongoose & sequlize)
- default modely pro db se kopiruji na zaklade parametru db z norkconfigu
- updatnutej ts skeleton
- dropnul jsem support pro js

- version update: 3.0.5
46 changes: 37 additions & 9 deletions src/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,47 +22,75 @@ export default class Create {
name: 'lang',
choices: [
{ name: 'Typescript', value: 'ts' },
{ name: 'Javascript', value: 'js' },
{ name: 'Javascript - DEPRECATED', value: 'js' },
],
},
{
type: 'list',
message: `Pick the database & ORM you're using:`,
name: 'db',
choices: [
{ name: 'MongoDB - Mongoose', value: { db: 'mongodb', orm: 'mongoose' } },
{ name: 'MySQL - Sequelize', value: { db: 'mysql', orm: 'sequelize' } },
{ name: 'PostgreSQL - Sequelize', value: { db: 'postgresql', orm: 'sequelize' } },
],
},
{
type: 'input',
name: 'author',
message: 'Enter your name:',
},
{
type: 'input',
name: 'email',
message: 'Enter your email:',
},
{
type: 'input',
name: 'website',
message: 'Enter your website:',
},
]
// remove first question if project name is already known
if (projectName) {
questions.shift()
}
if (projectName) questions.shift()

const answers = await inquirer.prompt(questions)
const data: CreateInterface = {
project_name: answers.project_name ? answers.project_name : process.argv[3],
lang: answers.lang,
author: answers.author,
database: answers.db,
website: answers.website,
email: answers.email,
}

// copy skeleton to new project
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)

// copy default db models to new project
if (data.database.orm == 'mongoose') fs.copySync(path.join(__dirname, './skeletons/mongoose-models/'), projectPath + '/src/models')
if (data.database.orm == 'sequelize') fs.copySync(path.join(__dirname, './skeletons/sequelize-models/'), projectPath + '/src/models')

// edit package.json file
const pkgJson = fs.readJsonSync(path.join(projectPath, 'package.json'))
// const pkgJson = require(path.join(projectPath, 'package.json'))

pkgJson.name = data.project_name
pkgJson.author = data.author

pkgJson.author = `${data.author} <${data.email}> (${data.website})`
fs.writeFile(path.join(projectPath, 'package.json'), JSON.stringify(pkgJson, null, 2), err => {
if (err) return global.logError(err.message)
})

// create norkconfig.json
const norkconfig: any = { ...data }
norkconfig['version'] = require('../package.json').version
fs.writeFileSync(path.join(projectPath, 'norkconfig.json'), JSON.stringify(norkconfig, null, 2))

console.log(colors.yellow('Project settings'))
console.log(colors.yellow('------------------'))
console.log(pad(colors.gray('Project name: '), 30), data.project_name)
console.log(pad(colors.gray('Author: '), 30), data.author)
console.log(pad(colors.gray('Author: '), 30), pkgJson.author)
console.log(pad(colors.gray('Language: '), 30), global.langToLanguage(String(data.lang)))
console.log(pad(colors.gray('Database: '), 30), global.dbToDatabase(String(data.database.db)))

return global.logSuccess(`Project ${data.project_name} created successfully!`)
}
Expand Down
13 changes: 13 additions & 0 deletions src/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,17 @@ export default class Global {
return 'Unknown language'
}
}

static dbToDatabase(lang: string): string {
switch (lang) {
case 'mongodb':
return 'MongoDB'
case 'postgresql':
return 'PostgreSQL'
case 'mysql':
return 'MySQL'
default:
return 'Unknown database'
}
}
}
8 changes: 8 additions & 0 deletions src/interfaces/CreateInterface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
interface database {
db: string
orm: string
}

export interface Create {
project_name: string
author: string
lang: string
database: database
email: string
website: string
}
2 changes: 1 addition & 1 deletion src/skeletons/express-ts/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Logs
logs
#logs
*.log
npm-debug.log*
yarn-debug.log*
Expand Down
33 changes: 14 additions & 19 deletions src/skeletons/express-ts/.prettierrc
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
{
"arrowParens": "avoid",
"bracketSpacing": true,
"endOfLine": "lf",
"htmlWhitespaceSensitivity": "css",
"insertPragma": false,
"jsxBracketSameLine": true,
"jsxSingleQuote": true,
"printWidth": 200,
"proseWrap": "preserve",
"quoteProps": "as-needed",
"requirePragma": false,
"semi": false,
"singleQuote": true,
"tabWidth": 4,
"trailingComma": "all",
"useTabs": true,
"vueIndentScriptAndStyle": true,
"parser": "typescript"
}
"tabWidth": 4,
"useTabs": true,
"singleQuote": true,
"semi": false,
"trailingComma": "none",
"jsxSingleQuote": true,
"jsxBracketSameLine": true,
"printWidth": 200,
"bracketSpacing": true,
"vueIndentScriptAndStyle": true,
"arrowParens": "always",
"bracketSameLine": false,
"endOfLine": "lf"
}
5 changes: 0 additions & 5 deletions src/skeletons/express-ts/jest.config.js

This file was deleted.

3 changes: 2 additions & 1 deletion src/skeletons/express-ts/norkconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"lang": "ts",
"db": ""
"db": "",
"orm": ""
}
38 changes: 21 additions & 17 deletions src/skeletons/express-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,33 @@
"name": "project-name",
"version": "1.0.0",
"description": "",
"main": "app.js",
"main": "dist/server.js",
"private": "true",
"keywords": [],
"author": "",
"repository": "github:username/repo",
"license": "ISC",
"scripts": {
"start": "node -r tsconfig-paths/register -r ts-node/register dist/server.js",
"start:dev": "npx nodemon src/server.ts",
"dev": "nodemon src/server.ts",
"start": "node dist/server.js",
"start:dev": "nodemon src/server.ts",
"test": "jest",
"clean": "rimraf dist/*",
"copy-assets": "ts-node src/utils/copyAssets",
"copy-assets": "npx ts-node src/utils/copyAssets",
"tsc": "tsc -p .",
"build": "npm-run-all clean tsc copy-assets"
"build": "npm-run-all clean tsc copy-assets",
"format": "npx prettier --write ."
},
"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",
"express-validator": "^6.14.0",
"express-validator": "^6.14.2",
"fs-extra": "^10.0.0",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.12.3",
"morgan": "^1.10.0",
"pg": "^8.7.1",
"pg-hstore": "^2.3.4",
"sequelize": "^6.15.0"
Expand All @@ -38,7 +39,7 @@
"@types/ejs": "^3.0.6",
"@types/express": "^4.17.11",
"@types/fs-extra": "^9.0.12",
"@types/jest": "^27.0.1",
"@types/jest": "^27.5.2",
"@types/jsonwebtoken": "^8.5.8",
"@types/mongoose": "^5.10.5",
"@types/morgan": "^1.9.2",
Expand All @@ -48,11 +49,15 @@
"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"
"ts-jest": "^27.1.5",
"ts-node": "^10.8.1",
"typescript": "^4.2.4",
"morgan": "^1.10.0"
},
"jest": {
"preset": "ts-jest",
"testEnvironment": "node"
},
"nodemonConfig": {
"ignore": [
"**/*.test.ts",
Expand All @@ -63,7 +68,6 @@
"watch": [
"src"
],
"exec": "node -r tsconfig-paths/register -r ts-node/register ./src/server.ts",
"ext": "ts, js"
}
}
13 changes: 11 additions & 2 deletions src/skeletons/express-ts/src/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
APP_PORT = 6060
APP_HOSTNAME = 'localhost'
APP_HOST = 'http://localhost:8080' # frontend url
CORS_WHITELIST = http://172.15.46.21:8080;http://192.168.0.1:8080

# Timezone
TZ = 'Europe/Prague'

CORS_WHITELIST = http://172.15.46.21:8080;http://192.168.0.1:8080
JWT_SECRET = ''

# MongoDB
Expand All @@ -14,4 +17,10 @@ DB_PORT = 5432
DB_HOST = '127.0.0.1'
DB_USERNAME = ''
DB_PASSWORD = ''
DB_DATABASE = ''
DB_DATABASE = ''

# SMTP
SMTP_HOST = ''
SMTP_USER = ''
SMTP_PASS = ''
SMTP_FROM = ''
14 changes: 10 additions & 4 deletions src/skeletons/express-ts/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ import morgan from 'morgan'
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'
import { router as routes } from './routes'
import { router as middlewares } from './middlewares'
import env from './config/environment'

const corsWhitelist = ['http://localhost:8080', 'http://localhost:6060']
export let corsWhitelist: Array<string>
if (env.CORS_WHITELIST != 'undefined') {
corsWhitelist = [...['http://localhost:8080', 'http://localhost:6040'], ...env.CORS_WHITELIST.split(';')]
} else {
corsWhitelist = ['http://localhost:8080', 'http://localhost:6040']
}
const corsOptions = {
origin: function (origin: any, callback: any) {
if (!origin || corsWhitelist.indexOf(origin) !== -1) {
Expand All @@ -16,7 +22,7 @@ const corsOptions = {
}
},
optionsSuccessStatus: 200,
credentials: true,
credentials: true
}

export const app = express()
Expand Down
20 changes: 10 additions & 10 deletions src/skeletons/express-ts/src/config/database.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import mongoose from 'mongoose'
import config from '@/utils/environment'
import { Err, Succ } from '@/services/globalService'
import db from '@/config/postgres.config'
import env from './environment'
import { Err, Succ } from '../services/globalService'
import db from './sequelize.config'

// MongoDB
const dbURI: string = config.DB_URI
const dbURI: string = env.DB_URI
function connect() {
if (!config.NORK.db) {
if (!env.NORK.database) {
new Err(500, 'no database is in norkcfg.json')
return false
}

if (config.NORK.db == 'mongodb') {
if (env.NORK.database.orm == 'mongoose') {
mongoose
.connect(dbURI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useCreateIndex: true
})
.then(() => {
new Succ(200, 'connected to db')
Expand All @@ -28,7 +28,7 @@ function connect() {
})
}

if (config.NORK.db == 'postgresql') {
if (env.NORK.database.orm == 'sequelize') {
db.sync()
.then(() => {
new Succ(200, 'connected to db')
Expand All @@ -40,8 +40,8 @@ function connect() {
})
}

if (config.NORK.db.length > 0) {
new Err(500, `unsupported database ${config.NORK.db}`)
if (env.NORK.database.db.length > 0) {
new Err(500, `unsupported database ${env.NORK.database.db}`)
return false
}
}
Expand Down
Loading

0 comments on commit 564cd09

Please sign in to comment.