diff --git a/package-lock.json b/package-lock.json index a37c5e7..8fcc7a8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,8 +11,10 @@ "dependencies": { "colors": "^1.4.0", "commander": "^8.1.0", + "fs-extra": "^10.0.0", "inquirer": "^8.1.2", "leven": "^4.0.0", + "ncp": "^2.0.0", "pad": "^3.2.0" }, "bin": { @@ -200,6 +202,24 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/fs-extra": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-10.0.0.tgz", + "integrity": "sha512-C5owb14u9eJwizKGdchcDUQeFtlSHHthBk8pbX9Vc1PFZrLombudjDnNns88aYslCyF6IY5SUw3Roz6xShcEIQ==", + "dependencies": { + "graceful-fs": "^4.2.0", + "jsonfile": "^6.0.1", + "universalify": "^2.0.0" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/graceful-fs": { + "version": "4.2.8", + "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.8.tgz", + "integrity": "sha512-qkIilPUYcNhJpd33n0GBXTB1MMPp14TxEsEs0pTrsSVucApsYzW5V+Q8Qxhik6KU3evy+qkAAowTByymK0avdg==" + }, "node_modules/iconv-lite": { "version": "0.4.24", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", @@ -432,6 +452,17 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/jsonfile": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.1.0.tgz", + "integrity": "sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==", + "dependencies": { + "universalify": "^2.0.0" + }, + "optionalDependencies": { + "graceful-fs": "^4.1.6" + } + }, "node_modules/leven": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/leven/-/leven-4.0.0.tgz", @@ -453,6 +484,14 @@ "resolved": "https://registry.npmjs.org/mute-stream/-/mute-stream-0.0.8.tgz", "integrity": "sha512-nnbWWOkoWyUsTjKrhgD0dcz22mdkSnpYqbEjIm2nhwhuxlSkpywJmBo8h0ZqJdkp73mb90SssHkN4rsRaBAfAA==" }, + "node_modules/ncp": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ncp/-/ncp-2.0.0.tgz", + "integrity": "sha1-GVoh1sRuNh0vsSgbo4uR6d9727M=", + "bin": { + "ncp": "bin/ncp" + } + }, "node_modules/os-tmpdir": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/os-tmpdir/-/os-tmpdir-1.0.2.tgz", @@ -583,6 +622,14 @@ "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.1.0.tgz", "integrity": "sha512-hcVC3wYEziELGGmEEXue7D75zbwIIVUMWAVbHItGPx0ziyXxrOMQx4rQEVEV45Ut/1IotuEvwqPopzIOkDMf0A==" }, + "node_modules/universalify": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz", + "integrity": "sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==", + "engines": { + "node": ">= 10.0.0" + } + }, "node_modules/util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", @@ -708,6 +755,21 @@ "escape-string-regexp": "^1.0.5" } }, + "fs-extra": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-10.0.0.tgz", + "integrity": "sha512-C5owb14u9eJwizKGdchcDUQeFtlSHHthBk8pbX9Vc1PFZrLombudjDnNns88aYslCyF6IY5SUw3Roz6xShcEIQ==", + "requires": { + "graceful-fs": "^4.2.0", + "jsonfile": "^6.0.1", + "universalify": "^2.0.0" + } + }, + "graceful-fs": { + "version": "4.2.8", + "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.8.tgz", + "integrity": "sha512-qkIilPUYcNhJpd33n0GBXTB1MMPp14TxEsEs0pTrsSVucApsYzW5V+Q8Qxhik6KU3evy+qkAAowTByymK0avdg==" + }, "iconv-lite": { "version": "0.4.24", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", @@ -862,6 +924,15 @@ "resolved": "https://registry.npmjs.org/is-unicode-supported/-/is-unicode-supported-0.1.0.tgz", "integrity": "sha512-knxG2q4UC3u8stRGyAVJCOdxFmv5DZiRcdlIaAQXAbSfJya+OhopNotLQrstBhququ4ZpuKbDc/8S6mgXgPFPw==" }, + "jsonfile": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.1.0.tgz", + "integrity": "sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==", + "requires": { + "graceful-fs": "^4.1.6", + "universalify": "^2.0.0" + } + }, "leven": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/leven/-/leven-4.0.0.tgz", @@ -877,6 +948,11 @@ "resolved": "https://registry.npmjs.org/mute-stream/-/mute-stream-0.0.8.tgz", "integrity": "sha512-nnbWWOkoWyUsTjKrhgD0dcz22mdkSnpYqbEjIm2nhwhuxlSkpywJmBo8h0ZqJdkp73mb90SssHkN4rsRaBAfAA==" }, + "ncp": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ncp/-/ncp-2.0.0.tgz", + "integrity": "sha1-GVoh1sRuNh0vsSgbo4uR6d9727M=" + }, "os-tmpdir": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/os-tmpdir/-/os-tmpdir-1.0.2.tgz", @@ -972,6 +1048,11 @@ "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.1.0.tgz", "integrity": "sha512-hcVC3wYEziELGGmEEXue7D75zbwIIVUMWAVbHItGPx0ziyXxrOMQx4rQEVEV45Ut/1IotuEvwqPopzIOkDMf0A==" }, + "universalify": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz", + "integrity": "sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==" + }, "util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", diff --git a/package.json b/package.json index af1a518..5f663e3 100644 --- a/package.json +++ b/package.json @@ -18,8 +18,10 @@ "dependencies": { "colors": "^1.4.0", "commander": "^8.1.0", + "fs-extra": "^10.0.0", "inquirer": "^8.1.2", "leven": "^4.0.0", + "ncp": "^2.0.0", "pad": "^3.2.0" }, "repository": { diff --git a/src/app.js b/src/app.js index 094810c..fdcd798 100644 --- a/src/app.js +++ b/src/app.js @@ -1,50 +1,86 @@ #!/usr/bin/env node const inquirer = require('inquirer') const path = require('path') - -const main = async () => { - if (process.argv[ 2 ] == 'create') - { - const data = {} - 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:', - }, - ] - - if (process.argv[ 3 ]) { - questions.shift() - } - - let answers = await inquirer.prompt(questions) - data.project_name = answers.project_name ? answers.project_name : process.argv[3] - data.lang = answers.lang - data.author = answers.author - - console.log(data) - } - - if (process.argv[ 2 ] == 'make') - { - const component = process.argv[ 3 ] - +const fs = require('fs-extra') +const colors = require('colors') +const pad = require('pad') + +const langToLanguage = lang => { + switch (lang) { + case 'js': + return 'Javascript' + break + case 'ts': + return 'Typescript' + break + default: + return 'Unknown language' } } -main() +;(async () => { + if (process.argv[2] == 'create') { + // get info about new project + const data = { lang: 'ts' } + 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 (process.argv[3]) { + questions.shift() + } + + let answers = await inquirer.prompt(questions) + data.project_name = answers.project_name ? answers.project_name : process.argv[3] + data.lang = answers.lang + data.author = answers.author + + // copy skeleton to new project + fs.copySync(path.join(__dirname, './skeletons/express-' + data.lang), process.cwd()) + + // edit package.json file + const pkgJson = require(path.join(process.cwd(), 'package.json')) + + pkgJson.name = data.project_name + pkgJson.author = data.author + + fs.writeFile(path.join(process.cwd(), 'package.json'), JSON.stringify(pkgJson, null, 2), err => { + if (err) return console.log(err) + }) + + 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), langToLanguage(data.lang)) + + console.log(colors.cyan(`Project ${ data.project_name } created successfully!`)) + return true + } + + if (process.argv[2] == 'make') { + const component = process.argv[3] + } + if (!process.argv[2]) { + // Help + console.log('Help coming soon!') + } +})() diff --git a/src/make-files/express-ts/controller.ts b/src/make-files/express-ts/controller.ts index e69de29..1cfd054 100644 --- a/src/make-files/express-ts/controller.ts +++ b/src/make-files/express-ts/controller.ts @@ -0,0 +1,10 @@ +import { Request, Response } from 'express' + +const root_get = (req: Request, res: Response) => { + res.render('home') + return true +} + +module.exports = { + root_get, +} diff --git a/src/make-files/express-ts/middleware.ts b/src/make-files/express-ts/middleware.ts index e69de29..7a9027b 100644 --- a/src/make-files/express-ts/middleware.ts +++ b/src/make-files/express-ts/middleware.ts @@ -0,0 +1,10 @@ +import { Router, Request, Response, NextFunction } from 'express' + +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 new file mode 100644 index 0000000..877524f --- /dev/null +++ b/src/make-files/express-ts/model.js @@ -0,0 +1,17 @@ +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 deleted file mode 100644 index e69de29..0000000 diff --git a/src/make-files/express-ts/routes.ts b/src/make-files/express-ts/routes.ts index e69de29..9d0925b 100644 --- a/src/make-files/express-ts/routes.ts +++ b/src/make-files/express-ts/routes.ts @@ -0,0 +1,8 @@ +const { Router } = require('express') +const rootController = require('../controllers/rootController') + +const router = Router() + +router.get('/', rootController.root_get) + +module.exports = router diff --git a/src/make-files/express-ts/test.js b/src/make-files/express-ts/test.js new file mode 100644 index 0000000..7949993 --- /dev/null +++ b/src/make-files/express-ts/test.js @@ -0,0 +1,9 @@ +const { getReq, getRes } = require('./modules/reqRes.module.js') +const { root_get } = require('../controllers/rootController.ts') + +test('Home page render test', () => { + const req = getReq() + const res = getRes() + + expect(root_get(req, res)).toBe(true) +}) diff --git a/src/make-files/express-ts/test.ts b/src/make-files/express-ts/test.ts deleted file mode 100644 index e69de29..0000000 diff --git a/src/make-files/express-ts/view.ejs b/src/make-files/express-ts/view.ejs new file mode 100644 index 0000000..faf15cc --- /dev/null +++ b/src/make-files/express-ts/view.ejs @@ -0,0 +1,15 @@ + + + + + + + New Project + + + + + + + + \ No newline at end of file diff --git a/src/make-files/express-ts/view.ts b/src/make-files/express-ts/view.ts deleted file mode 100644 index e69de29..0000000