Rewrited to TypeScript, better file structure

This commit is contained in:
Filip Rojek 2022-01-12 23:15:24 +01:00
parent 5eafee03b2
commit c988ef64c4
13 changed files with 112 additions and 55 deletions

View File

@ -1,6 +1,6 @@
{
"name": "nork",
"version": "2.0.0.1",
"version": "3.0.0",
"description": "The best node.js 'framework' :)",
"main": "dist/app.js",
"bin": "dist/app.js",

View File

@ -8,3 +8,10 @@
- [x] setup
- [x] make
- [ ] create
- delam na tom
- je treba dopsat par types a fixnout zbytek erroru
- zatim netestovana funkcnost
### 1-10.2022
- dodelal jsem create a otestoval ho
- [x] create

View File

@ -1,6 +0,0 @@
import { Request, Response } from 'express'
export const root_get = (req: Request, res: Response) => {
res.render('home')
return true
}

69
src/create.ts Normal file
View File

@ -0,0 +1,69 @@
import pad from 'pad'
import colors from 'colors'
import fs from 'fs-extra'
import path from 'path'
import global from './global'
import { Create as CreateInterface } from './interfaces/CreateInterface'
import inquirer from 'inquirer'
export default class Create {
static async project(projectName: string | boolean = false) {
// get info about new project
let projectPath
const questions = [
{
type: 'input',
name: 'project_name',
message: 'Enter project name:',
},
{
type: 'list',
message: `Pick the technology you're using:`,
name: 'lang',
choices: [
{ name: 'Typescript', value: 'ts' },
{ name: 'Javascript', value: 'js' },
],
},
{
type: 'input',
name: 'author',
message: 'Enter your name:',
},
]
// remove first question if project name is already known
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,
}
// 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)
// 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
fs.writeFile(path.join(projectPath, 'package.json'), JSON.stringify(pkgJson, null, 2), err => {
if (err) return global.logError(err.message)
})
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('Language: '), 30), global.langToLanguage(String(data.lang)))
return global.logSuccess(`Project ${data.project_name} created successfully!`)
}
}

View File

@ -13,4 +13,15 @@ export default class Global {
console.log(colors.bgYellow.red(errorMsg))
return
}
static langToLanguage(lang: string): string {
switch (lang) {
case 'js':
return 'Javascript'
case 'ts':
return 'Typescript'
default:
return 'Unknown language'
}
}
}

View File

@ -0,0 +1,5 @@
export interface Create {
project_name: string
author: string
lang: string
}

View File

@ -1,8 +0,0 @@
import { Router, Request, Response, NextFunction } from 'express'
export const router = Router()
router.use((req: Request, res: Response, next: NextFunction) => {
console.log('Hi :)')
next()
})

View File

@ -1,15 +0,0 @@
import { Schema, model } from 'mongoose'
const modelSchema = new Schema<any>(
{
title: {
type: String,
required: true,
},
},
{
timestamps: true,
},
)
export default model('ModelName', modelSchema)

View File

@ -2,6 +2,7 @@ import Help from './help'
import Version from './version'
import Setup from './setup'
import Make from './make'
import Create from './create'
export default class Routes {
static router(): string {
@ -35,6 +36,13 @@ export default class Routes {
}
}
if (process.argv[2] == 'create') {
if (process.argv[4] != 'test') {
Create.project(process.argv[3])
}
return `create ${process.argv[3]}`
}
console.log(Help.logHelp())
return 'all help'
}

View File

@ -1,6 +0,0 @@
import { Router } from 'express'
import * as rootController from '@/controllers/rootController'
export const router = Router()
router.get('/', rootController.root_get)

View File

@ -1,3 +0,0 @@
export function helloWorld() {
console.log('hello world')
}

View File

@ -56,3 +56,13 @@ describe('should return make', () => {
})
}
})
describe('should return create', () => {
it('return create testProject', () => {
process.argv[2] = 'create'
process.argv[3] = 'testProject'
process.argv[4] = 'test'
const routes = Routes.router()
assert.equal(routes, 'create testProject')
})
})

View File

@ -1,15 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>New Project</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Open+Sans&display=swap" rel="stylesheet">
</head>
<body>
</body>
</html>