From 17333e97c696095cb75743f7b965274dd978c59f Mon Sep 17 00:00:00 2001 From: Harminder virk Date: Thu, 28 Nov 2019 12:12:14 +0530 Subject: [PATCH] feat: add prompts to customize project and greet post creation --- index.ts | 60 ++++++++++++++++++++++++++++++++------ package.json | 1 + src/contracts.ts | 3 ++ tasks/createPackageFile.ts | 3 +- tasks/createTslint.ts | 6 +++- tasks/greet.ts | 26 +++++++++++++++++ tasks/index.ts | 2 ++ 7 files changed, 89 insertions(+), 12 deletions(-) create mode 100644 tasks/greet.ts diff --git a/index.ts b/index.ts index 189f098..b7c9309 100644 --- a/index.ts +++ b/index.ts @@ -8,7 +8,8 @@ */ import getops from 'getopts' -import { isAbsolute, join } from 'path' +import { Prompt } from '@poppinss/prompts' +import { isAbsolute, join, basename } from 'path' import { isEmptyDir, logger } from '@adonisjs/sink' import { ensureDirSync, removeSync } from 'fs-extra' import { Application } from '@adonisjs/application/build/standalone' @@ -21,7 +22,11 @@ import { CliState } from './src/contracts' */ export async function runTasks (args: string[]) { const argv = getops(args, { - string: ['boilerplate'], + string: ['boilerplate', 'name'], + boolean: ['tslint'], + default: { + tslint: null, + }, }) /** @@ -32,13 +37,6 @@ export async function runTasks (args: string[]) { return } - let state: CliState = { - boilerplate: argv.boilerplate || 'web', - } - - // tslint:disable-next-line: max-line-length quotemark - console.log(" _ _ _ _ \n / \\ __| | ___ _ __ (_)___ | |___ \n / _ \\ / _` |/ _ \\| '_ \\| / __|_ | / __|\n / ___ \\ (_| | (_) | | | | \\__ \\ |_| \\__ \\\n/_/ \\_\\__,_|\\___/|_| |_|_|___/\\___/|___/\n") - const projectRoot = argv._[0].trim() /** @@ -47,6 +45,50 @@ export async function runTasks (args: string[]) { */ const absPath = isAbsolute(projectRoot) ? projectRoot : join(process.cwd(), projectRoot) + let state: CliState = { + baseName: projectRoot, + boilerplate: argv.boilerplate, + name: argv.name, + tslint: argv.tslint, + } + + /** + * Ask for the project structure + */ + if (!state.boilerplate) { + state.boilerplate = await new Prompt().choice('Select the project structure', [ + { + name: 'api', + message: 'API Server', + }, + { + name: 'web', + message: 'Web Application', + }, + ]) + } + + /** + * Ask for project name. We can fill it inside the `package.json` + * file + */ + if (!state.name) { + state.name = await new Prompt().ask('Enter the project name', { + default: basename(absPath), + }) + } + + /** + * Ask for project name. We can fill it inside the `package.json` + * file + */ + if (state.tslint === null) { + state.tslint = await new Prompt().confirm('Setup tslint?') + } + + // tslint:disable-next-line: max-line-length quotemark + console.log(" _ _ _ _ \n / \\ __| | ___ _ __ (_)___ | |___ \n / _ \\ / _` |/ _ \\| '_ \\| / __|_ | / __|\n / ___ \\ (_| | (_) | | | | \\__ \\ |_| \\__ \\\n/_/ \\_\\__,_|\\___/|_| |_|_|___/\\___/|___/\n") + /** * Ensuring that defined path exists */ diff --git a/package.json b/package.json index 4220331..1a3c820 100644 --- a/package.json +++ b/package.json @@ -67,6 +67,7 @@ "@adonisjs/application": "^1.3.0", "@adonisjs/fold": "^6.2.3", "@adonisjs/sink": "^2.4.0", + "@poppinss/prompts": "^1.0.4", "cli-width": "^2.2.0", "dedent": "^0.7.0", "fs-extra": "^8.1.0", diff --git a/src/contracts.ts b/src/contracts.ts index 262a162..782b83a 100644 --- a/src/contracts.ts +++ b/src/contracts.ts @@ -22,5 +22,8 @@ export type TaskFn = ( * CLI state */ export type CliState = { + baseName: string, boilerplate: 'web' | 'api', + name: string, + tslint: boolean, } diff --git a/tasks/createPackageFile.ts b/tasks/createPackageFile.ts index 4dea319..6a7dc75 100644 --- a/tasks/createPackageFile.ts +++ b/tasks/createPackageFile.ts @@ -8,7 +8,6 @@ */ import ora from 'ora' -import { basename } from 'path' import { PackageFile, logger } from '@adonisjs/sink' import { TaskFn } from '../src/contracts' @@ -22,7 +21,7 @@ import { packages } from '../src/schematics/packages' const task: TaskFn = async (absPath, _app, state) => { const pkg = new PackageFile(absPath) - pkg.set('name', basename(absPath)) + pkg.set('name', state.name) pkg.set('version', '0.0.0') pkg.set('private', true) diff --git a/tasks/createTslint.ts b/tasks/createTslint.ts index 34b1f35..2964dcf 100644 --- a/tasks/createTslint.ts +++ b/tasks/createTslint.ts @@ -13,7 +13,11 @@ import { TaskFn } from '../src/contracts' /** * Creates `tslint.json` file */ -const task: TaskFn = (absPath) => { +const task: TaskFn = (absPath, _app, state) => { + if (!state.tslint) { + return + } + const tslint = new JsonFile(absPath, 'tslint.json') tslint.set('extends', 'adonis-preset-ts/tslint') tslint.set('rules', {}) diff --git a/tasks/greet.ts b/tasks/greet.ts new file mode 100644 index 0000000..196f933 --- /dev/null +++ b/tasks/greet.ts @@ -0,0 +1,26 @@ +/* + * create-adonis-ts-app + * + * (c) Harminder Virk + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. +*/ + +import { kleur } from '@adonisjs/sink' +import { TaskFn } from '../src/contracts' + +/** + * Executes instructions on the installed packages + */ +const task: TaskFn = async (_absPath, _app, state) => { + console.log('') + console.log('🎉 Successfully created the project') + console.log('👉 Execute following commands to get started') + + console.log(` ${kleur.gray('$')} ${kleur.cyan(`cd ${state.name}`)}`) + console.log(` ${kleur.gray('$')} ${kleur.cyan('node ace serve --watch')}`) + console.log('') +} + +export default task diff --git a/tasks/index.ts b/tasks/index.ts index abe6b54..16b2692 100644 --- a/tasks/index.ts +++ b/tasks/index.ts @@ -7,6 +7,7 @@ * file that was distributed with this source code. */ +import greet from './greet' import createRcFile from './createRcFile' import createTsLint from './createTslint' import copyTemplates from './copyTemplates' @@ -30,4 +31,5 @@ export const tasks = [ createEditorConfig, createPackageFile, executeInstructions, + greet, ]