import fs from 'fs' import { getConfigFile } from '../helpers' import { CreateFunctionState } 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 validatePascalCase = (string: string): boolean | string => { if (!string) return 'Enter A Function Name - Cannot be empty..' const PASCAL_REGEX = /^[A-Z][a-z]+([A-Z][a-z]+)*$/ if (PASCAL_REGEX.test(string)) return true return 'Use PascalCase For The Function Name' } export const copyExistingEndpoint = async ({ namespace, functionName, latestVersion, version, }: CreateFunctionState) => { const previousEndpointPath = `./API/${namespace}/${functionName}/${latestVersion}` const newEndpointPath = `./API/${namespace}/${functionName}/${version}` try { fs.cpSync(previousEndpointPath, newEndpointPath, { recursive: true }) } catch (error) { console.info('Copy Error', error) } } export const authOrNoAuthFiles = async ( { authenticated, endpointPath, }: CreateFunctionState, ) => { const { prisma } = await getConfigFile() const templateMainPath = `${__dirname}/Templates/Files/FunctionName_Options/Auth` // NoAuth is default let typesPath = `${templateMainPath}/types_NoAuth.ts` let successDataPath = `${templateMainPath}/successData_NoAuth.ts` let failDataPath = `${templateMainPath}/failData_NoAuth.ts` let readmePath = `${templateMainPath}/README_NoAuth.md` let indexPath = `${templateMainPath}/index_NoAuth.ts` if (prisma) { indexPath = `${templateMainPath}/index_NoAuth_Prisma.ts` } if (authenticated) { typesPath = `${templateMainPath}/types_Auth.ts` successDataPath = `${templateMainPath}/successData_Auth.ts` failDataPath = `${templateMainPath}/failData_Auth.ts` readmePath = `${templateMainPath}/README_Auth.md` indexPath = `${templateMainPath}/index_Auth.ts` if (prisma) { indexPath = `${templateMainPath}/index_Auth_Prisma.ts` } } try { fs.cpSync(typesPath, `${endpointPath}/types.ts`, { recursive: true }) fs.cpSync(successDataPath, `${endpointPath}/_data/successData.ts`, { recursive: true }) fs.cpSync(failDataPath, `${endpointPath}/_data/failData.ts`, { recursive: true }) fs.cpSync(indexPath, `${endpointPath}/index.ts`, { recursive: true }) fs.cpSync(readmePath, `${endpointPath}/README.md`, { recursive: true }) } catch (error) { console.info('Copy Error', error) } } export const createNewEndpointFromTemplate = async ({ endpointPath, }: CreateFunctionState) => { const previousEndpointPath = `${__dirname}/Templates/FunctionName` try { fs.cpSync(previousEndpointPath, endpointPath, { recursive: true }) } catch (error) { console.info('Copy Error', error) } } export const updateFileFunctionName = async ( { versionCopy, namespace, functionName, version, latestVersion, }: CreateFunctionState, filePath: string, ) => fs.readFile( filePath, 'utf-8', (err, contents) => { if (err) { console.info(err) return } let oldFunctionName = 'FunctionName' const newFunctionName = `${namespace}_${functionName}_${underscoreVersion(version)}` if (versionCopy) { oldFunctionName = `${namespace}_${functionName}_${underscoreVersion(latestVersion)}` } const regEx = new RegExp(oldFunctionName, 'g') const replaced = contents.replace(regEx, newFunctionName) fs.writeFile(filePath, replaced, 'utf-8', (err2) => { if (err2) console.info(err2) }) }, ) export const updateFileFunctionNames = async (state: CreateFunctionState) => { const indexFilePath = `${state.endpointPath}/index.ts` await updateFileFunctionName(state, indexFilePath) const readmePath = `${state.endpointPath}/README.md` await updateFileFunctionName(state, readmePath) const testFilePath = `${state.endpointPath}/index.spec.ts` await updateFileFunctionName(state, testFilePath) const failDataPath = `${state.endpointPath}/_data/failData.ts` await updateFileFunctionName(state, failDataPath) } export const updateIndexFile = async ({ namespace, functionName, version, authenticated, external, publicAPI, }: CreateFunctionState) => { 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,` let replaced = contents if (external) { const endpointText = '// NewEndpointGoesHere' const newEndpointReplacementExternal = `${newFunctionName} ${endpointText}` const regEx1 = new RegExp(endpointText, 'g') replaced = replaced.replace(regEx1, newEndpointReplacementExternal) } if (authenticated) { const loginRequiredEndpointText = '// NewLoginRequiredGoesHere' const newEndpointReplacementAuthenticated = `${newFunctionName} ${loginRequiredEndpointText}` const regEx2 = new RegExp(loginRequiredEndpointText, 'g') replaced = replaced.replace(regEx2, newEndpointReplacementAuthenticated) } if (publicAPI) { const publicApiEndpointUsingApiKeyText = '// NewPublicApiEndpointGoesHere' const newEndpointReplacementPublicApi = `${newFunctionName} ${publicApiEndpointUsingApiKeyText}` const regEx2 = new RegExp(publicApiEndpointUsingApiKeyText, 'g') replaced = replaced.replace(regEx2, newEndpointReplacementPublicApi) } fs.writeFile(filePath, replaced, 'utf-8', (err3) => { if (err3) console.info(err3) }) }, ) }