mirror of https://github.com/embarklabs/embark.git
refacotr how we handle files already parsed
This commit is contained in:
parent
abc89b2015
commit
9bf06aebce
|
@ -15,11 +15,16 @@ class File {
|
|||
}
|
||||
|
||||
parseFileForImport(content, isHttpContract, callback) {
|
||||
const self = this;
|
||||
if (typeof isHttpContract === 'function') {
|
||||
callback = isHttpContract;
|
||||
isHttpContract = false;
|
||||
}
|
||||
const self = this;
|
||||
if (self.parsedImports) {
|
||||
// We already parsed this file
|
||||
return callback();
|
||||
}
|
||||
self.parsedImports = true;
|
||||
if (self.filename.indexOf('.sol') < 0) {
|
||||
// Only supported in Solidity
|
||||
return callback();
|
||||
|
@ -98,20 +103,15 @@ class File {
|
|||
content = fs.readFileSync(this.path).toString();
|
||||
} else if (this.type === File.types.custom) {
|
||||
return this.resolver((theContent) => {
|
||||
if (!this.parsedImports) {
|
||||
this.parsedImports = true;
|
||||
return this.parseFileForImport(content, () => {
|
||||
this.parseFileForImport(content, () => {
|
||||
callback(theContent);
|
||||
});
|
||||
}
|
||||
callback(theContent);
|
||||
});
|
||||
} else if (this.type === File.types.http) {
|
||||
return this.downloadFile(this.filename, this.path, (content) => {
|
||||
if (!content) {
|
||||
return callback(content);
|
||||
}
|
||||
this.parsedImports = true;
|
||||
this.path = this.filename;
|
||||
this.type = File.types.dapp_file;
|
||||
callback(content);
|
||||
|
@ -119,14 +119,10 @@ class File {
|
|||
} else {
|
||||
throw new Error("unknown file: " + this.filename);
|
||||
}
|
||||
if (!this.parsedImports) {
|
||||
this.parsedImports = true;
|
||||
return this.parseFileForImport(content, () => {
|
||||
callback(content);
|
||||
});
|
||||
}
|
||||
callback(content);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
22
test/file.js
22
test/file.js
|
@ -59,5 +59,27 @@ describe('embark.File', function () {
|
|||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should find all the imports but only once if called twice', function (done) {
|
||||
const contract = fs.readFileSync('./test/contracts/contract_with_http_import.sol').toString();
|
||||
const file = new File({filename: '.embark/contracts/embark-framework/embark/master/test_app/app/contracts/simple_storage.sol',
|
||||
path: 'https://raw.githubusercontent.com/embark-framework/embark/develop/test_apps/test_app/app/contracts/simple_storage.sol'});
|
||||
const downloadFileStub = sinon.stub(file, 'downloadFile')
|
||||
.callsFake((path, url, cb) => {
|
||||
cb();
|
||||
});
|
||||
|
||||
file.parseFileForImport(contract, () => {
|
||||
// Parse again
|
||||
file.parseFileForImport(contract, () => {
|
||||
assert.strictEqual(downloadFileStub.callCount, 1);
|
||||
assert.strictEqual(downloadFileStub.firstCall.args[0],
|
||||
'.embark/contracts/embark-framework/embark/develop/test_apps/contracts_app/contracts/contract_args.sol');
|
||||
assert.strictEqual(downloadFileStub.firstCall.args[1],
|
||||
'https://raw.githubusercontent.com/embark-framework/embark/develop/test_apps/contracts_app/contracts/contract_args.sol');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue