import fs from 'fs' import { DeleteFunctionState } from './types' export const underscoreVersion = (version: string) => version.replace(/\./g, '_') export const getFunctionVersions: any = (namespace: string, functionName: string) => { console.info('namespace', namespace) return fs.readdirSync(`./API/${namespace}/${functionName}`, { withFileTypes: true }).filter((dirent) => dirent.isDirectory()) .map((dirent) => dirent.name) } export const deleteEndpoint = async ({ namespace, functionName, version, }: DeleteFunctionState) => { const path = `./API/${namespace}/${functionName}/${version}` return fs.rmSync(path, { recursive: true, force: true }) } export const deleteNamespace = async ({ namespace, }: DeleteFunctionState) => { const path = `./API/${namespace}` return fs.rmSync(path, { recursive: true, force: true }) } export const updateIndexFile = async ({ namespace, functionName, version, }: DeleteFunctionState) => { const filePath = './API/index.ts' return fs.readFile( filePath, 'utf-8', (err, contents) => { if (err) { console.info(err) return } const newFunctionName = ` ${namespace}_${functionName}_${underscoreVersion(version)}: true,` const regEx = new RegExp(newFunctionName, 'g') const replaced = contents.replace(regEx, '') fs.writeFile(filePath, replaced, 'utf-8', (err2) => { if (err2) console.info(err2) }) }, ) }