import prompts from 'prompts' import colors from 'colors' import { deleteEndpoint, deleteNamespace, getFunctionVersions, updateIndexFile, } from './helpers' import { DeleteFunctionState } from './types' import { checkIfInitShouldRun, getDirectories, getNamespaceDirectories } from '../helpers' export const DeleteFunction = async () => { const state: DeleteFunctionState = { namespace: '', functionName: '', version: '1.0.0', endpointPath: '', removeNamespace: false, } let directories: any[] = [] if (checkIfInitShouldRun()) { directories = await getDirectories() } if (directories.length === 0) { console.info(`\n ${colors.red.bold('No endpoints to delete')}`) process.exit(1) } const namespace = await prompts({ type: 'select', name: 'answer', message: 'Select a Namespace', choices: [ ...directories, ], }) state.namespace = namespace.answer let functionNames = [] if (checkIfInitShouldRun()) { functionNames = await getNamespaceDirectories(namespace.answer) } const functionName = await prompts([ { type: 'select', name: 'answer', message: 'Select one of the following functions:', choices: [ ...functionNames, ], }, ]) state.functionName = functionName.answer const currentVersions = getFunctionVersions(state.namespace, state.functionName) console.info('Current Versions') const versions = currentVersions.map((dirname: any) => ({ title: dirname, value: dirname, })) const version = await prompts([ { type: 'select', name: 'answer', message: 'Select one of the following versions:', choices: [ ...versions, ], }, ]) state.version = version.answer if (versions.length === 1) { const removeNamespace = await prompts([ { type: 'confirm', name: 'answer', message: 'Would you like to delete the Namespace?', initial: true, }, ]) state.removeNamespace = removeNamespace.answer } console.info(`\n ${colors.yellow.bold('Check The Infomation Below')}`) console.info(` ${colors.grey.underline('Namespace: ')}${colors.green(state.namespace)} ${colors.grey.underline('FunctionName: ')}${colors.green(state.functionName)} ${colors.grey.underline('Version: ')}${colors.green(state.version)} `) state.endpointPath = `./API/${state.namespace}/${state.functionName}/${state.version}` const confirmation = await prompts({ type: 'text', name: 'answer', message: 'Type DELETE to confirm', }) if (confirmation.answer !== 'DELETE') { console.info(colors.yellow('No action taken!')) console.info(colors.yellow('Why? You didn\'t type in DELETE')) process.exit(1) } await deleteEndpoint(state) if (state.removeNamespace) { await deleteNamespace(state) } console.info(colors.green('Deleteing the endpoint...')) await updateIndexFile(state) return confirmation } export default DeleteFunction