Skip to content

Commit

Permalink
feat: add prompts to customize project and greet post creation
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Nov 28, 2019
1 parent 50eaf04 commit 17333e9
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 12 deletions.
60 changes: 51 additions & 9 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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,
},
})

/**
Expand All @@ -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()

/**
Expand All @@ -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
*/
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions src/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ export type TaskFn = (
* CLI state
*/
export type CliState = {
baseName: string,
boilerplate: 'web' | 'api',
name: string,
tslint: boolean,
}
3 changes: 1 addition & 2 deletions tasks/createPackageFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

import ora from 'ora'
import { basename } from 'path'
import { PackageFile, logger } from '@adonisjs/sink'

import { TaskFn } from '../src/contracts'
Expand All @@ -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)

Expand Down
6 changes: 5 additions & 1 deletion tasks/createTslint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', {})
Expand Down
26 changes: 26 additions & 0 deletions tasks/greet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* create-adonis-ts-app
*
* (c) Harminder Virk <[email protected]>
*
* 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
2 changes: 2 additions & 0 deletions tasks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -30,4 +31,5 @@ export const tasks = [
createEditorConfig,
createPackageFile,
executeInstructions,
greet,
]

0 comments on commit 17333e9

Please sign in to comment.