import { join } from 'path' ; import fs from 'fs' ; import shelljs from 'shelljs' ; import prompts from 'prompts' ; import { assertExists , assertIsNonEmptyString , assertShellResult } from '../../utils' ; import { CreateKeyfile } from '../../utils/create.keyfile' ; import { CommandHandler } from '../..' ; import { ListKeyfiles } from '../../utils/list.keyfiles' ; export default function provideInit ( shell , prompt , filesystem , fortaKeystore , configFilename , listKeyfiles , createKeyfile ) { assertExists ( shell , 'shell' ) ; assertExists ( prompt , 'prompt' ) ; assertExists ( filesystem , 'filesystem' ) ; assertIsNonEmptyString ( fortaKeystore , 'fortaKeystore' ) ; assertIsNonEmptyString ( configFilename , 'configFilename' ) ; assertExists ( listKeyfiles , 'listKeyfiles' ) ; assertExists ( createKeyfile , 'createKeyfile' ) ; return async function init ( cliArgs ) { const files = shell . ls ( ) ; if ( files . length > 0 ) { const { proceed } = await prompt ( { type : 'text' , name : 'proceed' , message : `The current directory is not empty and files could be overwritten. Are you sure you want to initialize? (type 'yes' to proceed)` } ) ; if ( proceed !== 'yes' ) { console . log ( 'aborting initialization' ) ; return ; } } const isTypescript = ! ! cliArgs . typescript ; const isPython = ! ! cliArgs . python ; console . log ( `initializing ${ isPython ? "Python" : isTypescript ? "Typescript" : "Javascript" } Forta Agent...` ) ; const starterProjectPath = `${ join ( __dirname , '..' , '..' , '..' , 'starter-project' ) }` ; const copyProjectResult = shell . cp ( '-r' , [ `${ starterProjectPath }/*` , `${ starterProjectPath }/.*` ] , '.' ) ; assertShellResult ( copyProjectResult , 'error copying starter-project folder' ) ; const copyJsTsPyResult = shell . cp ( '-r' , isPython ? './py/*' : isTypescript ? './ts/*' : './js/*' , '.' ) ; assertShellResult ( copyJsTsPyResult , `error unpacking ${ isPython ? 'py' : isTypescript ? 'ts' : 'js' } folder` ) ; const renameGitignoreResult = shell . mv ( '_gitignore' , '.gitignore' ) ; assertShellResult ( renameGitignoreResult , 'error renaming gitignore file' ) ; const rmResult = shell . rm ( '-rf' , 'js' , 'ts' , 'py' , '.npmignore' ) ; assertShellResult ( rmResult , 'error cleaning up files' ) ; if ( ! filesystem . existsSync ( fortaKeystore ) ) { const createKeystoreResult = shell . mkdir ( fortaKeystore ) ; assertShellResult ( createKeystoreResult , `error creating keystore folder ${ fortaKeystore }` ) ; } if ( ! filesystem . existsSync ( join ( fortaKeystore , configFilename ) ) ) { console . log ( `creating ${ configFilename }...` ) ; const copyConfigResult = shell . cp ( join ( __dirname , configFilename ) , fortaKeystore ) ; assertShellResult ( copyConfigResult , `error creating ${ configFilename }` ) ; } else { console . log ( `found existing ${ configFilename } in ${ fortaKeystore }` ) ; } const keyfiles = listKeyfiles ( ) ; if ( ! keyfiles . length ) { console . log ( 'creating new keyfile...' ) ; const { password } = await prompt ( { type : 'password' , name : 'password' , message : `Enter password to encrypt new keyfile` } ) ; await createKeyfile ( password ) ; } else { console . log ( `found existing keyfile ${ keyfiles [ 0 ] } in ${ fortaKeystore }` ) ; } } ; }