import * as inquirer from 'inquirer'; import * as fs from 'fs'; const tableJSON: any = []; const insertFormJSON: any[] = []; let temp: any = {}; async function initJSON() { const prompt : any = await inquirer.prompt([{ type: 'input', name: 'name', message: 'Insert name of the file', }]); if (prompt.name == null || prompt.name === undefined || prompt.name === '' || prompt.name === 'none') { initJSON(); } else { return prompt; } } function createTable() { const questions = [ { type: 'input', name: 'headerCell', message: 'What\'s the header cell\'s name? ', }, { type: 'input', name: 'header', message: 'What\'s the header\'s name? ', }, { type: 'list', name: 'pipe', choices: ['currency', 'none'], }, { type: 'input', name: 'pipeVal', message: 'Input pipe value(fill blank if there is none)', }, { type: 'confirm', name: 'askAgain', message: 'Would you like to enter another header?', default: true, }, ]; inquirer.prompt(questions).then(async (answers: any) => { console.log('ans ', answers); let pipe: any = {}; if (answers.pipe !== 'none') { pipe.name = answers.pipe; pipe.value = answers.pipeVal; } else { pipe = undefined; } tableJSON.push({ headerCell: answers.headerCell, header: answers.header, pipe, }); if ((answers.askAgain)) { createTable(); } else { const getFileName = (await initJSON()).name; fs.appendFile(`${getFileName}.json`, JSON.stringify(tableJSON), (err) => { if (err) throw err; console.log('yeay!!'); }); } }); } function createInsertForm() { const questions: any = { formType: [ { type: 'list', name: 'option', message: 'Choose form type', choices: [ 'autocomplete', 'select', 'date', 'textarea', 'fileImage', 'fileVideo', 'input', 'radio' ] } ], autocomplete: [ { type: 'input', name: 'placeholder', message: 'Placeholder value', }, { type: 'input', name: 'formControl', message: 'formControl name', }, { type: 'input', name: 'matAutocomplete', message: 'matAutocomplete value', }, { type: 'input', name: 'displayWith', message: 'displayWith function\'s name', }, { type: 'input', name: 'filteredOptions', message: 'filteredOptions name', }, { type: 'input', name: 'value', message: 'Autocomplete value', }, { type: 'input', name: 'displayValue', message: 'display value', }, ], select: [ { type: 'input', name: 'placeholder', message: 'Placeholder value', }, { type: 'input', name: 'formControlName', message: 'formControlName name', }, { type: 'input', name: 'selectTrigger', message: 'selectTrigger value', }, { type: 'input', name: 'options', message: 'optionList name', }, { type: 'input', name: 'item', message: 'option item variable', }, { type: 'input', name: 'value', message: 'option value', }, { type: 'input', name: 'displayOption', message: 'displayOption value', }, ], date: [ { type: 'input', name: 'placeholder', message: 'placeholder value', }, { type: 'input', name: 'datePicker', message: 'datePicker name', }, { type: 'input', name: 'formControlName', message: 'formControlName name', }, ], textarea: [ { type: 'input', name: 'placeholder', message: 'placeholder value', }, { type: 'input', name: 'formControlName', message: 'formControlName name', }, ], fileImage: [ { type: 'input', name: 'fileClass', message: 'fileClass value', }, { type: 'input', name: 'change', message: 'change fn', }, { type: 'input', name: 'imageFile', message: 'imageFile variable name', }, ], fileVideo: [ { type: 'input', name: 'label', message: 'video label', }, { type: 'input', name: 'fileClass', message: 'fileClass value', }, { type: 'input', name: 'change', message: 'change fn', }, { type: 'input', name: 'videoFile', message: 'videoFile variable name', }, ], input: [ { type: 'input', name: 'placeholder', message: 'placeholder value', }, { type: 'input', name: 'formControlName', message: 'formControlName name', }, { type: 'list', name: 'type', message: 'Choose input type', choices: [ 'text' ] } ], radio: [ { type: 'input', name: 'iterable', message: 'ng for iterable' }, { type: 'input', name: 'variable', message: 'ng for variable' }, { type: 'input', name: 'label', message: 'radio button option label' }, { type: 'input', name: 'formControlName', message: 'formControlName name' } ] }; inquirer.prompt(questions.formType).then( (answers: any) => { temp.formType = answers.option; if (questions[answers.option]) { return inquirer.prompt(questions[answers.option]) } return inquirer.prompt(questions.input) }) .then((opt : any): any => { if (temp.formType === 'autocomplete') { const { placeholder, ...autocomplete} = opt; insertFormJSON.push({ ...temp, placeholder, autocomplete }); } else if (temp.formType === 'select') { const { placeholder } = opt; const { formControlName, selectTrigger, ...option} = opt; insertFormJSON.push({ ...temp, placeholder, select: { formControlName, selectTrigger, option } }) } else if (temp.formType === 'date') { const { placeholder, ...date} = opt; insertFormJSON.push({ ...temp, placeholder, date }); } else if (temp.formType === 'textarea') { const { placeholder, ...textarea} = opt; insertFormJSON.push({ ...temp, placeholder, textarea }); } else if (temp.formType === 'fileImage') { const { fileType, ...image } = opt.file; insertFormJSON.push({ ...temp, file: { fileType, image } }) } else if (temp.formType === 'fileVideo') { const { fileType, ...video } = opt.file; insertFormJSON.push({ ...temp, file: { fileType, video } }) } else if (temp.formType === 'input') { const { placeholder, ...input } = opt; insertFormJSON.push({ ...temp, placeholder, input }) } else if (temp.formType === 'radio') { insertFormJSON.push({ ...temp, radio: { ...opt } }) } return inquirer.prompt([{ type: 'confirm', name: 'askAgain', message: 'Would you like to enter another field?', default: true, }]) }) .then(async (confirm: any) => { if (confirm.askAgain) { createInsertForm(); } else { const getFileName = (await initJSON()).name; fs.appendFile(`${getFileName}.json`, JSON.stringify(insertFormJSON), err => { if (err) throw err; console.log(`yeay!!`); }); } }) .catch(err => console.log(err)); } export function init() { const initQuestion = [{ type: 'list', name: 'option', message: 'What do you want to create? ', choices: [ 'Form', 'Table' ] }]; inquirer.prompt(initQuestion).then((answers: any) => { if (answers.option == 'Table') { createTable(); } else if(answers.option == 'Form') { createInsertForm(); } }); }