import {CliCommandInterface} from '../../interfaces/cli-command.interface'; import * as inquirer from "inquirer"; import {FormDataInterface} from "./form-data.interface"; import * as shell from "shelljs"; import {ProjectConfiguration} from "../../shared/project/project-configuration"; import fs = require('fs'); import path = require("path"); import appRoot = require('app-root-path'); export const newCommand: CliCommandInterface = { name: 'new', description: 'Create a new Project', _call(data?: any): boolean { const formPrompt = inquirer.prompt([ { name: 'name', type: 'input', message: 'Choose a name for the template:', validate(answer: string): boolean | string { if (!answer) { return 'Please provide a name'; } if (/[^a-zA-Z0-9\-_]/.test(answer)) { return 'The name can only consist of letters & numbers including - and _'; } return true; } }, { name: 'path', type: 'input', message: 'Choose the location for the template', default: (answers: FormDataInterface) => process.cwd() + '/' + answers.name, validate(answer: string): boolean | string { // TODO validate path // TODO ensure directory is empty or does not exist at all return !!answer || 'Please enter a path'; } }, { name: 'initTemplate', type: 'confirm', message: 'Would you like to start with a basic template?' } ]).then((answers: FormDataInterface) => { shell.mkdir(answers.path); const projectConfig: ProjectConfiguration = ProjectConfiguration.create(answers.name); fs.writeFile(path.resolve(answers.path, ProjectConfiguration.configurationFileName), projectConfig.serialize(), () => { }); if (answers.initTemplate) { // Copy contents of pdf-bunnies template to the newly created bunny-project shell.cp('-R', path.resolve(appRoot.path, 'etc/template/*'), answers.path); } }); return true; } };