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) {
this.import(url, return new Promise((resolve, reject) => {
// TODO: handle this event this.import(url,
(loadingMsg) => { this.emit('message', loadingMsg) }, // TODO: handle this event
async (error, content, cleanUrl, type, url) => { (loadingMsg) => { this.emit('message', loadingMsg) },
if (error) return cb(error) async (error, content, cleanUrl, type, url) => {
try { if (error) return reject(error)
const provider = await this.call('fileManager', 'getProviderOf', null) try {
const path = targetPath || type + '/' + cleanUrl const provider = await this.call('fileManager', 'getProviderOf', null)
if (provider) provider.addExternal('.deps/' + path, content, url) const path = targetPath || type + '/' + cleanUrl
} catch (err) { if (provider) provider.addExternal('.deps/' + path, content, url)
} catch (err) {
} console.error(err)
cb(null, content) }
}, null) resolve(content)
}, 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)
}) })
} })
})()
return content
} else {
const localhostProvider = await this.call('fileManager', 'getProviderByName', 'localhost')
if (localhostProvider.isConnected()) {
var splitted = /([^/]+)\/(.*)$/g.exec(url)
// try to resolve localhost modules (aka truffle imports) - e.g from the node_modules folder const possiblePaths = ['localhost/installed_contracts/' + url]
this.call('fileManager', 'getProviderByName', 'localhost').then((localhostProvider) => { if (splitted) possiblePaths.push('localhost/installed_contracts/' + splitted[1] + '/contracts/' + splitted[2])
if (localhostProvider.isConnected()) { possiblePaths.push('localhost/node_modules/' + url)
var splitted = /([^/]+)\/(.*)$/g.exec(url) if (splitted) possiblePaths.push('localhost/node_modules/' + splitted[1] + '/contracts/' + splitted[2])
return async.tryEach([
(cb) => { this.resolveAndSave('localhost/installed_contracts/' + url, null).then((result) => cb(null, result)).catch((error) => cb(error.message)) }, for (const path of possiblePaths) {
// eslint-disable-next-line standard/no-callback-literal try {
(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)) } }, const content = await this.resolveAndSave(path, null)
(cb) => { this.resolveAndSave('localhost/node_modules/' + url, null).then((result) => cb(null, result)).catch((error) => cb(error.message)) }, if (content) {
// eslint-disable-next-line standard/no-callback-literal localhostProvider.addNormalizedName(path.replace('localhost/', ''), url)
(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)) } }], return content
(error, result) => { }
if (error) { } catch (e) {}
return this.importExternal(url, targetPath, (error, content) => { }
if (error) return reject(error) return await this.importExternal(url, targetPath)
resolve(content) }
}) return await this.importExternal(url, targetPath)
}
resolve(result)
})
}
this.importExternal(url, targetPath, (error, content) => {
if (error) return reject(error)
resolve(content)
})
})
}).catch(error => {
return reject(error)
})
} }
}).catch(() => { }
// fallback to just resolving the file, it won't be saved in file manager } catch (e) {
return this.importExternal(url, targetPath, (error, content) => { return await this.importExternal(url, targetPath)
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