import { prompt } from 'enquirer' ; import Handlebars from 'handlebars' ; import fs from 'fs' ; import path from 'path' ; import process from 'process' ; export enum Platform { CLOUD_FUNCTIONS = 'Cloud Functions' , CLOUD_RUN = 'Cloud Run' } export interface ProgramOptions { programName : string ; exportName ? : string ; description : string ; fileLocation : string ; platform : Platform ; } export function checkValidity ( opts ) { const validName = /[^-A-Za-z_]+/ ; const botName = opts . programName ; if ( validName . test ( botName ) ) { console . log ( 'Only letters, hyphens, and underscores are permitted in the bot name. Please try again.' ) ; return false ; } if ( ! opts . programName ) { console . log ( 'You forgot to name your program. Please try again.' ) ; return false ; } const relativePath = path . join ( __dirname , '..' , '..' , '..' ) ; if ( ! opts . fileLocation ) { opts . fileLocation = path . join ( relativePath , opts . programName ) ; } if ( fs . existsSync ( path . join ( opts . fileLocation ) ) ) { console . log ( 'Your program name and location is not unique. Please rename.' ) ; return false ; } opts . programName = opts . programName . toLowerCase ( ) ; opts . exportName = opts . programName . replace ( /-/g , '_' ) ; return true ; } export async function collectUserInput ( ) { let isValid = false ; let input ; while ( ! isValid ) { input = await prompt ( [ { type : 'input' , name : 'programName' , message : 'What is the name of the program?' , } , { type : 'input' , name : 'description' , message : 'What is the description of the program?' , } , { type : 'input' , name : 'fileLocation' , message : `This package will be saved in /packages/yourProgramName unless you specify another location and directory name here relative to ${ process . cwd ( ) } : ` , } , { type : 'select' , name : 'platform' , message : 'Select a platform' , choices : Object . values ( Platform ) , } , ] ) ; isValid = checkValidity ( input ) ; } return input ! ; } param templatePath param options export function creatingBotFiles ( options ) { console . log ( `Creating new folder ${ options . fileLocation }` ) ; fs . mkdirSync ( options . fileLocation , { recursive : true } ) ; const mkDir = options . fileLocation ; const handlebarsFileRegex = /.hbs$/ ; const [ platformKey ] = Object . entries ( Platform ) . find ( ( [ , value ] ) => value === options . platform ) ! ; const handlebarsOptions = { ... options , platform : { [ platformKey ] : true } } ; const readAllFiles = function ( dirNameRead , dirNameWrite ) { console . log ( `copying from ${ dirNameRead } to ${ dirNameWrite }...` ) ; const files = fs . readdirSync ( dirNameRead ) ; files . forEach ( file => { const fileName = file . toString ( ) ; const isHandlebarsFile = handlebarsFileRegex . test ( fileName ) ; let fileNameResult = fileName ; if ( isHandlebarsFile ) { const fileNameTemplate = Handlebars . compile ( fileName ) ; fileNameResult = fileNameTemplate ( handlebarsOptions ) . replace ( handlebarsFileRegex , '' ) ; } const readName = path . join ( dirNameRead , file ) ; const writeName = path . join ( dirNameWrite , fileNameResult ) ; if ( fs . statSync ( readName ) . isDirectory ( ) ) { fs . mkdirSync ( writeName ) ; console . log ( writeName + ' generated' ) ; readAllFiles ( readName , writeName ) ; } else { if ( isHandlebarsFile ) { const fileContents = fs . readFileSync ( readName ) ; const template = Handlebars . compile ( fileContents . toString ( ) ) ; fs . writeFileSync ( writeName , template ( handlebarsOptions ) ) ; } else { fs . copyFileSync ( readName , writeName ) ; } console . log ( writeName + ' generated' ) ; } } ) ; } ; const templatePath = path . join ( __dirname , '..' , '..' , 'templates' ) ; readAllFiles ( templatePath , mkDir ) ; }