import fs from 'fs' import { snakeCase } from 'snake-case' import { InitState } from './types' export const writeConfigFile = async ( { serviceName, serviceDescription, port, privateStatus, domains, apiKey, authRotation, prisma, sql, repository, deployment, routing, }: InitState, serviceNamePath: string, ) => { const fileContents: string = `{ "apiKey": "${apiKey}", "authRotation": "${authRotation}", "deployment": "${deployment}", "domains": "${domains}", "port": ${port}, "prisma": ${prisma}, "private": ${privateStatus}, "repository": "${repository}", "routing": "${routing}", "serviceDescription": "${serviceDescription}", "serviceName": "${serviceName}", "sql": ${sql} }` fs.writeFile(`${serviceNamePath}/wappi.json`, fileContents, 'utf-8', (err3) => { if (err3) console.info(err3) }) } export const writeDotEnvExampleFile = async ( { prisma, sql, }: InitState, serviceNamePath: string, ) => { let sqlConnectionStrings: string = '' if (sql) { sqlConnectionStrings = ` # DATABASE CONNECTION DETAILS - Add Database Connection Values Here # Core database Connection names DB_HOSTNAME='edit this' # Change this Locally DB_USER='edit this' # Change this Locally DB_PASSWORD='edit this' # Change this Locally DB_NAME='edit this' # Change this Locally # Or add for another service DB_HOSTNAME_SERVICE='edit this' # Change this Locally DB_USER_SERVICE='edit this' # Change this Locally DB_PASSWORD_SERVICE='edit this' # Change this Locally DB_NAME_SERVICE='edit this' # Change this Locally ` } let prismaConnectionString: string = '' if (prisma) { prismaConnectionString = ` # Prisma Variables DATABASE_URL='mysql://username:password@hostname:port/dbname' ` } const frontendPaths: string = `# Internal Frontend Project Paths. # This is the path that the built types files will be automatically copied over to # when you run wappi locally. Allowing the types files to always stay in sync. # ## Only for Local Environment ## # FRONTEND_PROJECT_PATHS=[''] ` const fileContents: string = `# ENVIRONMENT - Either: 'localhost' | 'dev' | 'stage' | 'prod' - Rebuild when you change this ENVIRONMENT='localhost' # Show Logs is for development or bug fixing. Recommended: localhost = TRUE; prod = false # Showing logs will display the body data. So it's a security risk to show on production SHOW_LOGS=true # AUTH KEY SECRETS - Default FallBack - You should set these to rotate using AWS Secrets Manager AUTH_KEY_CURRENT='asdf' AUTH_KEY_PREVIOUS='asdf' # Silence the dotenv messaging. DOTENV_CONFIG_QUIET=true ${frontendPaths}${sqlConnectionStrings}${prismaConnectionString} # Add Custom Service Environment Values Here ` fs.writeFile(`${serviceNamePath}/.env.example`, fileContents, 'utf-8', (err3) => { if (err3) console.info(err3) }) fs.writeFile(`${serviceNamePath}/.env`, fileContents, 'utf-8', (err3) => { if (err3) console.info(err3) }) } export const updatePackageJson = async ( state: InitState, serviceNamePath: string, ) => { const { port, serviceName, serviceDescription } = state if (port === 3030) return const filePath = `${serviceNamePath}/package.json` fs.readFile( filePath, 'utf-8', (err, contents) => { if (err) { console.info(err) return } let newContents = contents const portRegEx = /3030/g newContents = newContents.replace(portRegEx, `${port}`) const serviceNameRegEx = /service_name/g const snakeCaseServiceName = snakeCase(serviceName) newContents = newContents.replace(serviceNameRegEx, snakeCaseServiceName) const serviceDescriptionRegEx = /service_description/g newContents = newContents.replace(serviceDescriptionRegEx, serviceDescription) // eslint-disable-next-line no-console console.log('Updated package.json') fs.writeFile(filePath, newContents, 'utf-8', (err2) => { if (err2) console.info(err2) }) }, ) } export const copyGitIgnore = async ( serviceNamePath: string, filesPath: string, ) => { const filePath = `${filesPath}/Git/gitignore.txt` fs.readFile( filePath, 'utf-8', (err, contents) => { if (err) { console.info(err) return } fs.writeFile(`${serviceNamePath}/.gitignore`, contents, 'utf-8', (err2) => { if (err2) console.info(err2) }) }, ) }