Commit 3273dfdf authored by yann300's avatar yann300 Committed by Aniket

make resolveAndSave async and make sure node_modules path import are normalized

parent 9f9d1f4d
...@@ -88,21 +88,23 @@ export class CompilerImports extends Plugin { ...@@ -88,21 +88,23 @@ export class CompilerImports extends Plugin {
} }
} }
importExternal (url, targetPath, cb) { importExternal (url, targetPath) {
return new Promise((resolve, reject) => {
this.import(url, this.import(url,
// TODO: handle this event // TODO: handle this event
(loadingMsg) => { this.emit('message', loadingMsg) }, (loadingMsg) => { this.emit('message', loadingMsg) },
async (error, content, cleanUrl, type, url) => { async (error, content, cleanUrl, type, url) => {
if (error) return cb(error) if (error) return reject(error)
try { try {
const provider = await this.call('fileManager', 'getProviderOf', null) const provider = await this.call('fileManager', 'getProviderOf', null)
const path = targetPath || type + '/' + cleanUrl const path = targetPath || type + '/' + cleanUrl
if (provider) provider.addExternal('.deps/' + path, content, url) if (provider) provider.addExternal('.deps/' + path, content, url)
} catch (err) { } catch (err) {
console.error(err)
} }
cb(null, content) resolve(content)
}, null) }, null)
})
} }
/** /**
...@@ -115,66 +117,58 @@ export class CompilerImports extends Plugin { ...@@ -115,66 +117,58 @@ export class CompilerImports extends Plugin {
* @param {String} targetPath - (optional) internal path where the content should be saved to * @param {String} targetPath - (optional) internal path where the content should be saved to
* @returns {Promise} - string content * @returns {Promise} - string content
*/ */
resolveAndSave (url, targetPath) { async resolveAndSave (url, targetPath) {
return new Promise((resolve, reject) => { if (url.indexOf('remix_tests.sol') !== -1) return remixTests.assertLibCode
if (url.indexOf('remix_tests.sol') !== -1) resolve(remixTests.assertLibCode) try {
this.call('fileManager', 'getProviderOf', url).then((provider) => { const provider = await this.call('fileManager', 'getProviderOf', url)
if (provider) { if (provider) {
if (provider.type === 'localhost' && !provider.isConnected()) { if (provider.type === 'localhost' && !provider.isConnected()) {
return reject(new Error(`file provider ${provider.type} not available while trying to resolve ${url}`)) throw new Error(`file provider ${provider.type} not available while trying to resolve ${url}`)
} }
provider.exists(url).then(exist => { const exist = await provider.exists(url)
/* /*
if the path is absolute and the file does not exist, we can stop here if the path is absolute and the file does not exist, we can stop here
Doesn't make sense to try to resolve "localhost/node_modules/localhost/node_modules/<path>" and we'll end in an infinite loop. Doesn't make sense to try to resolve "localhost/node_modules/localhost/node_modules/<path>" and we'll end in an infinite loop.
*/ */
if (!exist && url.startsWith('browser/')) return reject(new Error(`not found ${url}`)) if (!exist && url.startsWith('browser/')) throw new Error(`not found ${url}`)
if (!exist && url.startsWith('localhost/')) return reject(new Error(`not found ${url}`)) if (!exist && url.startsWith('localhost/')) throw new Error(`not found ${url}`)
if (exist) { if (exist) {
return provider.get(url, (error, content) => { const content = await (() => {
return new Promise((resolve, reject) => {
provider.get(url, (error, content) => {
if (error) return reject(error) if (error) return reject(error)
resolve(content) resolve(content)
}) })
} })
})()
// try to resolve localhost modules (aka truffle imports) - e.g from the node_modules folder return content
this.call('fileManager', 'getProviderByName', 'localhost').then((localhostProvider) => { } else {
const localhostProvider = await this.call('fileManager', 'getProviderByName', 'localhost')
if (localhostProvider.isConnected()) { if (localhostProvider.isConnected()) {
var splitted = /([^/]+)\/(.*)$/g.exec(url) var splitted = /([^/]+)\/(.*)$/g.exec(url)
return async.tryEach([
(cb) => { this.resolveAndSave('localhost/installed_contracts/' + url, null).then((result) => cb(null, result)).catch((error) => cb(error.message)) }, const possiblePaths = ['localhost/installed_contracts/' + url]
// eslint-disable-next-line standard/no-callback-literal if (splitted) possiblePaths.push('localhost/installed_contracts/' + splitted[1] + '/contracts/' + splitted[2])
(cb) => { if (!splitted) { cb('URL not parseable: ' + url) } else { this.resolveAndSave('localhost/installed_contracts/' + splitted[1] + '/contracts/' + splitted[2], null).then((result) => cb(null, result)).catch((error) => cb(error.message)) } }, possiblePaths.push('localhost/node_modules/' + url)
(cb) => { this.resolveAndSave('localhost/node_modules/' + url, null).then((result) => cb(null, result)).catch((error) => cb(error.message)) }, if (splitted) possiblePaths.push('localhost/node_modules/' + splitted[1] + '/contracts/' + splitted[2])
// eslint-disable-next-line standard/no-callback-literal
(cb) => { if (!splitted) { cb('URL not parseable: ' + url) } else { this.resolveAndSave('localhost/node_modules/' + splitted[1] + '/contracts/' + splitted[2], null).then((result) => cb(null, result)).catch((error) => cb(error.message)) } }], for (const path of possiblePaths) {
(error, result) => { try {
if (error) { const content = await this.resolveAndSave(path, null)
return this.importExternal(url, targetPath, (error, content) => { if (content) {
if (error) return reject(error) localhostProvider.addNormalizedName(path.replace('localhost/', ''), url)
resolve(content) return content
})
} }
resolve(result) } catch (e) {}
})
} }
this.importExternal(url, targetPath, (error, content) => { return await this.importExternal(url, targetPath)
if (error) return reject(error) }
resolve(content) return await this.importExternal(url, targetPath)
}) }
}) }
}).catch(error => { } catch (e) {
return reject(error) return await this.importExternal(url, targetPath)
})
} }
}).catch(() => {
// fallback to just resolving the file, it won't be saved in file manager
return this.importExternal(url, targetPath, (error, content) => {
if (error) return reject(error)
resolve(content)
})
})
})
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment