import path = require ( "path" ) ; import fs = require ( "fs" ) ; import * as presenter from './presenter' ; import * as workspace from './workspace' ; import { QuickPickItem , window } from 'vscode' ; import * as errors from './errors' ; import { waitForProcessInTerminal } from "./utils" ; import { Feedback } from "./feedback" ; import { glob } from "glob" ; export async function runContractSnippet ( folder ) { let metadata = workspace . getMetadataObjectByFolder ( folder ) ; let pattern = `${ folder }/**/*snippets.sh` ; let snippetsFiles = glob . sync ( pattern , { } ) ; if ( ! snippetsFiles . length ) { throw new errors . MyError ( { Message : `No *snippets.sh file found.` } ) ; } let allSnippets = [ ] ; snippetsFiles . forEach ( file => { let names = getSnippetsNames ( file ) ; let snippets = names . map ( name => new Snippet ( file , name ) ) ; allSnippets . push ( ... snippets ) ; } ) ; let snippetChoice = await presenter . askChoiceTyped ( allSnippets ) ; if ( ! snippetChoice ) { return ; } let terminalName = `Elrond snippets: ${ metadata . ProjectName }` ; let command = `source ${ snippetChoice . file } && ${ snippetChoice . name }` ; await runInTerminal ( terminalName , command , metadata ) ; Feedback . info ( `Snippet "${ snippetChoice }" has been executed. Check output in Terminal.` ) ; } function getSnippetsNames ( file ) { let content = fs . readFileSync ( file , { encoding : "utf8" } ) ; const found = content . match ( /([a-zA-Z_{1}][a-zA-Z0-9_]+)(?=\()/g ) ; return found ; } async function runInTerminal ( terminalName , command , metadata ) { let envTestnet = path . join ( metadata . ProjectPath , "testnet" ) ; let envWallets = path . join ( metadata . ProjectPath , "wallets" ) ; let envUsers = path . join ( envWallets , "users" ) ; let terminal = window . terminals . find ( item => item . name == terminalName ) ; if ( ! terminal ) { let env = { PROJECT : metadata . ProjectPath , PROJECT_NAME : metadata . ProjectName , TESTNET : envTestnet , WALLETS : envWallets , USERS : envUsers } ; terminal = window . createTerminal ( { name : terminalName , env : env , cwd : metadata . ProjectPath } ) ; } terminal . sendText ( command ) ; terminal . show ( false ) ; await waitForProcessInTerminal ( terminal ) ; } export class Snippet implements QuickPickItem { readonly file : string ; readonly name : string ; readonly label : string ; readonly description ? : string ; readonly detail ? : string ; readonly picked ? : boolean ; readonly alwaysShow ? : boolean ; constructor ( file , name ) { this . file = file ; this . name = name ; let fileLabel = path . basename ( file ) . replace ( ".snippets.sh" , "" ) ; this . label = `${ fileLabel }: ${ name }` ; } toString ( ) { return this . label ; } }