@@ -3,56 +3,46 @@ import "@babel/polyfill"
33import * as ts from "typescript" ;
44import { createClient } from '@remixproject/plugin-iframe'
55import { PluginClient } from '@remixproject/plugin'
6- import * as ethersJs from 'ethers' // eslint-disable-line
7- import multihash from 'multihashes'
8- import * as web3Js from 'web3'
96import Web3 from 'web3'
107import { waffleChai } from "@ethereum-waffle/chai" ;
11- import * as starknet from 'starknet'
12- import * as zokratesJs from 'zokrates-js' ;
13- import * as circomlibjs from 'circomlibjs' ;
14- const snarkjs = require ( 'snarkjs' ) ;
15- import * as zkkitIncrementalMerkleTree from '@zk-kit/incremental-merkle-tree' ;
16- import * as semaphoreProtocolProof from '@semaphore-protocol/proof'
17- // import * as semaphoreProtocolContracts from '@semaphore-protocol/contracts'
18- import * as semaphoreProtocolGroup from '@semaphore-protocol/group'
19- import * as semaphoreProtocolIdentity from '@semaphore-protocol/identity'
208import './runWithMocha'
219import * as path from 'path'
2210import * as hhEtherMethods from './hardhat-ethers/methods'
11+ import { yarnLoader , yarnContext } from './yarn/yarn-loader'
2312const chai = require ( 'chai' )
2413chai . use ( waffleChai )
2514
26- window . starknet = starknet
2715window . chai = chai
28- window . ethers = ethersJs
29- window . multihashes = multihash
30- window [ 'zokrates-js' ] = zokratesJs
31- window [ 'snarkjs' ] = snarkjs
32- window [ 'circomlibjs' ] = circomlibjs
33- window [ '@zk-kit/incremental-merkle-tree' ] = zkkitIncrementalMerkleTree
34-
35- window [ '@semaphore-protocol/proof' ] = semaphoreProtocolProof
36- // window['@semaphore-protocol/contracts'] = semaphoreProtocolContracts
37- window [ '@semaphore-protocol/group' ] = semaphoreProtocolGroup
38- window [ '@semaphore-protocol/identity' ] = semaphoreProtocolIdentity
3916
4017const scriptReturns = { } // keep track of modules exported values
4118const fileContents = { } // keep track of file content
42- window . require = ( module ) => {
43- if ( module === 'web3' ) return web3Js
44- if ( window [ module ] ) return window [ module ] // library
45- else if ( ( module . endsWith ( '.json' ) || module . endsWith ( '.abi' ) ) && window . __execPath__ && fileContents [ window . __execPath__ ] ) return JSON . parse ( fileContents [ window . __execPath__ ] [ module ] )
46- else if ( window . __execPath__ && scriptReturns [ window . __execPath__ ] ) return scriptReturns [ window . __execPath__ ] [ module ] // module exported values
47- else throw new Error ( `${ module } module require is not supported by Remix IDE` )
19+ window . require = ( module ) => {
20+ if ( ( module . endsWith ( '.json' ) || module . endsWith ( '.abi' ) ) && window . __execPath__ && fileContents [ window . __execPath__ ] ) return JSON . parse ( fileContents [ window . __execPath__ ] [ module ] )
21+ else if ( window . __execPath__ && scriptReturns [ window . __execPath__ ] ) {
22+ let returns = scriptReturns [ window . __execPath__ ] [ module ] || scriptReturns [ window . __execPath__ ] [ module + '.js' ] // module exported values
23+ if ( module === 'ethers' ) {
24+ // Support hardhat-ethers, See: https://hardhat.org/plugins/nomiclabs-hardhat-ethers.html
25+ returns . provider = new ethers . providers . Web3Provider ( window . web3Provider )
26+ for ( const method in hhEtherMethods ) Object . defineProperty ( returns , method , { value : hhEtherMethods [ method ] } )
27+ }
28+ return returns
29+ } else {
30+ throw new Error ( `${ module } module require is not supported by Remix IDE` )
31+ }
4832}
4933
5034class CodeExecutor extends PluginClient {
35+ onActivation ( ) {
36+ this . on ( 'filePanel' , 'setWorkspace' , ( workspace ) => {
37+ console . log ( 'opening a new workspace in script runner ' , workspace )
38+ yarnLoader ( this )
39+ } )
40+ }
5141 async execute ( script , filePath ) {
5242 filePath = filePath || 'scripts/script.ts'
5343 const paths = filePath . split ( '/' )
5444 paths . pop ( )
55- const fromPath = paths . join ( '/' ) // get current execcution context path
45+ const fromPath = paths . join ( '/' ) // get current execution context path
5646 if ( script ) {
5747 try {
5848 script = ts . transpileModule ( script , { moduleName : filePath , filePath,
@@ -76,16 +66,22 @@ class CodeExecutor extends PluginClient {
7666 if ( ! scriptReturns [ fromPath ] ) scriptReturns [ fromPath ] = { }
7767 if ( ! fileContents [ fromPath ] ) fileContents [ fromPath ] = { }
7868 const { returns, content } = await this . executeFile ( absolutePath )
69+
7970 scriptReturns [ fromPath ] [ file ] = returns
8071 fileContents [ fromPath ] [ file ] = content
8172 }
8273
8374 // execute the script
84- script = `const exports = {};
85- const module = { exports: {} }
75+ script = `let exports = {};
76+ let module = { exports: {} }
8677 window.__execPath__ = "${ fromPath } "
8778 ${ script } ;
88- return exports || module.exports`
79+ if (Object.keys(exports).length) {
80+ return exports
81+ } else {
82+ return module.exports
83+ }
84+ `
8985 const returns = ( new Function ( script ) ) ( )
9086 if ( mocha . suite && ( ( mocha . suite . suites && mocha . suite . suites . length ) || ( mocha . suite . tests && mocha . suite . tests . length ) ) ) {
9187 console . log ( `RUNS ${ filePath } ....` )
@@ -100,18 +96,61 @@ class CodeExecutor extends PluginClient {
10096 }
10197 }
10298
99+ async _resolveRemixFileSystem ( fileName ) {
100+ if ( await this . call ( 'fileManager' , 'exists' , fileName ) ) return { content : await this . call ( 'fileManager' , 'readFile' , fileName ) }
101+ if ( await this . call ( 'fileManager' , 'exists' , fileName + '.ts' ) ) return { content : await this . call ( 'fileManager' , 'readFile' , fileName + '.ts' ) }
102+ if ( await this . call ( 'fileManager' , 'exists' , fileName + '.js' ) ) return { content : await this . call ( 'fileManager' , 'readFile' , fileName + '.js' ) }
103+ return { content : null }
104+ }
105+
106+ async _resolveYarnFileSystem ( fileName ) {
107+ let path = fileName . startsWith ( '/app/node_modules/' ) ? fileName : `/app/node_modules/${ fileName } `
108+ let content
109+ let stat
110+ try {
111+ stat = yarnContext . memfs . statSync ( path )
112+ } catch ( e ) { }
113+
114+ if ( stat && stat . isDirectory ( ) ) {
115+ const json = JSON . parse ( yarnContext . memfs . readFileSync ( `/app/node_modules/${ fileName } /package.json` , 'utf8' ) )
116+ const path = `/app/node_modules/${ fileName } /${ json . main } `
117+ content = yarnContext . memfs . readFileSync ( path , 'utf8' )
118+ return { content, path }
119+ }
120+
121+ try {
122+ stat = yarnContext . memfs . statSync ( path )
123+ } catch ( e ) { }
124+
125+ if ( stat && stat . isFile ( ) ) {
126+ content = yarnContext . memfs . readFileSync ( path , 'utf8' )
127+ return { content, path }
128+ }
129+
130+ if ( ! path . endsWith ( '.js' ) ) path = path + '.js'
131+ try {
132+ stat = yarnContext . memfs . statSync ( path )
133+ } catch ( e ) { }
134+
135+ if ( stat && stat . isFile ( ) ) {
136+ content = yarnContext . memfs . readFileSync ( path , 'utf8' )
137+ return { content, path }
138+ }
139+ return { content : null }
140+ }
141+
103142 async _resolveFile ( fileName ) {
104- if ( await this . call ( 'fileManager' , 'exists' , fileName ) ) return await this . call ( 'fileManager' , 'readFile' , fileName )
105- if ( await this . call ( 'fileManager' , 'exists' , fileName + '.ts' ) ) return await this . call ( 'fileManager' , 'readFile' , fileName + '.ts' )
106- if ( await this . call ( 'fileManager' , 'exists' , fileName + '.js' ) ) return await this . call ( 'fileManager' , 'readFile' , fileName + '.js' )
143+ const { content , path } = this . _resolveRemixFileSystem ( fileName )
144+ if ( content ) return { content , path }
145+ return this . _resolveYarnFileSystem ( fileName )
107146 }
108147
109148 async executeFile ( fileName ) {
110149 try {
111150 if ( require ( fileName ) ) return require ( fileName )
112151 } catch ( e ) { }
113- const content = await this . _resolveFile ( fileName )
114- const returns = await this . execute ( content , fileName )
152+ const { content, path } = await this . _resolveFile ( fileName )
153+ const returns = await this . execute ( content , path || fileName )
115154 return { returns, content}
116155 }
117156}
@@ -131,36 +170,30 @@ window.ethereum = web3Provider
131170
132171window . web3 = new Web3 ( window . web3Provider )
133172
134- // Support hardhat-ethers, See: https://hardhat.org/plugins/nomiclabs-hardhat-ethers.html
135- const { ethers } = ethersJs
136- ethers . provider = new ethers . providers . Web3Provider ( window . web3Provider )
137- window . hardhat = { ethers }
138- for ( const method in hhEtherMethods ) Object . defineProperty ( window . hardhat . ethers , method , { value : hhEtherMethods [ method ] } )
139-
140173console . logInternal = console . log
141174console . log = function ( ) {
142175 window . remix . emit ( 'log' , {
143- data : Array . from ( arguments ) . map ( ( el ) => JSON . parse ( JSON . stringify ( el ) ) )
176+ data : Array . from ( arguments ) . map ( ( el ) => el ? JSON . parse ( JSON . stringify ( el ) ) : 'undefined' )
144177 } )
145178 }
146179
147180console . infoInternal = console . info
148181console . info = function ( ) {
149182 window . remix . emit ( 'info' , {
150- data : Array . from ( arguments ) . map ( ( el ) => JSON . parse ( JSON . stringify ( el ) ) )
183+ data : Array . from ( arguments ) . map ( ( el ) => el ? JSON . parse ( JSON . stringify ( el ) ) : 'undefined' )
151184 } )
152185}
153186
154187console . warnInternal = console . warn
155188console . warn = function ( ) {
156189 window . remix . emit ( 'warn' , {
157- data : Array . from ( arguments ) . map ( ( el ) => JSON . parse ( JSON . stringify ( el ) ) )
190+ data : Array . from ( arguments ) . map ( ( el ) => el ? JSON . parse ( JSON . stringify ( el ) ) : 'undefined' )
158191 } )
159192}
160193
161194console . errorInternal = console . error
162195console . error = function ( ) {
163196 window . remix . emit ( 'error' , {
164- data : Array . from ( arguments ) . map ( ( el ) => JSON . parse ( JSON . stringify ( el ) ) )
197+ data : Array . from ( arguments ) . map ( ( el ) => el ? JSON . parse ( JSON . stringify ( el ) ) : 'undefined' )
165198 } )
166199}
0 commit comments