From c988ef64c457fb53d23cb529bbe238b0b550d31b Mon Sep 17 00:00:00 2001 From: Filip Rojek Date: Wed, 12 Jan 2022 23:15:24 +0100 Subject: [PATCH] Rewrited to TypeScript, better file structure --- package.json | 2 +- progress-blog.md | 9 +++- src/controllers/test.ts | 6 --- src/create.ts | 69 +++++++++++++++++++++++++++++++ src/global.ts | 11 +++++ src/interfaces/CreateInterface.ts | 5 +++ src/middlewares/test.ts | 8 ---- src/models/test.ts | 15 ------- src/routes.ts | 8 ++++ src/routes/test.ts | 6 --- src/services/test.ts | 3 -- src/tests/router.test.ts | 10 +++++ src/views/test.ejs | 15 ------- 13 files changed, 112 insertions(+), 55 deletions(-) delete mode 100644 src/controllers/test.ts create mode 100644 src/create.ts create mode 100644 src/interfaces/CreateInterface.ts delete mode 100644 src/middlewares/test.ts delete mode 100644 src/models/test.ts delete mode 100644 src/routes/test.ts delete mode 100644 src/services/test.ts delete mode 100644 src/views/test.ejs diff --git a/package.json b/package.json index 37eba83..31b61a4 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/progress-blog.md b/progress-blog.md index f9f3454..8ca11e5 100644 --- a/progress-blog.md +++ b/progress-blog.md @@ -7,4 +7,11 @@ - [x] --version - [x] setup - [x] make - - [ ] create \ No newline at end of file + - [ ] 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 diff --git a/src/controllers/test.ts b/src/controllers/test.ts deleted file mode 100644 index 4a94a0d..0000000 --- a/src/controllers/test.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Request, Response } from 'express' - -export const root_get = (req: Request, res: Response) => { - res.render('home') - return true -} diff --git a/src/create.ts b/src/create.ts new file mode 100644 index 0000000..f56e2fa --- /dev/null +++ b/src/create.ts @@ -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!`) + } +} diff --git a/src/global.ts b/src/global.ts index 9739a7a..1c32e4f 100644 --- a/src/global.ts +++ b/src/global.ts @@ -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' + } + } } diff --git a/src/interfaces/CreateInterface.ts b/src/interfaces/CreateInterface.ts new file mode 100644 index 0000000..b4a5d25 --- /dev/null +++ b/src/interfaces/CreateInterface.ts @@ -0,0 +1,5 @@ +export interface Create { + project_name: string + author: string + lang: string +} diff --git a/src/middlewares/test.ts b/src/middlewares/test.ts deleted file mode 100644 index 32accc2..0000000 --- a/src/middlewares/test.ts +++ /dev/null @@ -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() -}) diff --git a/src/models/test.ts b/src/models/test.ts deleted file mode 100644 index cc22d18..0000000 --- a/src/models/test.ts +++ /dev/null @@ -1,15 +0,0 @@ -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/routes.ts b/src/routes.ts index eb73382..8eb3a28 100644 --- a/src/routes.ts +++ b/src/routes.ts @@ -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' } diff --git a/src/routes/test.ts b/src/routes/test.ts deleted file mode 100644 index 7374556..0000000 --- a/src/routes/test.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Router } from 'express' -import * as rootController from '@/controllers/rootController' - -export const router = Router() - -router.get('/', rootController.root_get) diff --git a/src/services/test.ts b/src/services/test.ts deleted file mode 100644 index ccb388e..0000000 --- a/src/services/test.ts +++ /dev/null @@ -1,3 +0,0 @@ -export function helloWorld() { - console.log('hello world') -} diff --git a/src/tests/router.test.ts b/src/tests/router.test.ts index f5f8340..b64fe28 100644 --- a/src/tests/router.test.ts +++ b/src/tests/router.test.ts @@ -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') + }) +}) diff --git a/src/views/test.ejs b/src/views/test.ejs deleted file mode 100644 index faf15cc..0000000 --- a/src/views/test.ejs +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - New Project - - - - - - - - \ No newline at end of file