UNPKG

expressjs-gen-javascript

Version:

a powerful tool to create expressjs projects from CLI

70 lines (63 loc) 1.49 kB
import arg from 'arg'; import inquirer from 'inquirer'; import { createProject } from './main'; const path = require('path'); function parseArgumentsIntoOptions(rawArgs) { const args = arg( { '--git': Boolean, '--yes': Boolean, '--install': Boolean, '-g': '--git', '-y': '--yes', '-i': '--install', }, { argv: rawArgs.slice(2), } ); return { skipPrompts: args['--yes'] || false, git: args['--git'] || false, template: args._[0], runInstall: args['--install'] || false, }; } async function promptForMissingOptions(options) { const defaultTemplate = 'express-mongo'; if (options.skipPrompts) { return { ...options, template: options.template || defaultTemplate, }; } const questions = []; if (!options.template) { questions.push({ type: 'list', name: 'template', message: 'Choose your template', choices: ['express-mongo'], default: defaultTemplate, }); } if (!options.git) { questions.push({ type: 'confirm', name: 'git', message: 'Initialize a git repository?', default: false, }); } const answers = await inquirer.prompt(questions); return { ...options, template: options.template || answers.template, git: options.git || answers.git, }; } export async function cli(args) { let options = parseArgumentsIntoOptions(args); options = await promptForMissingOptions(options); await createProject(options); }