implement the download function

This commit is contained in:
Jonathan Rainville 2018-04-17 16:34:37 -04:00
parent cf14b3784f
commit 2568ec1aec
5 changed files with 354 additions and 333 deletions

View File

@ -6,7 +6,8 @@
},
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module"
"sourceType": "module",
"ecmaVersion": 2017
},
"rules": {
"accessor-pairs": "error",

View File

@ -180,9 +180,27 @@ Config.prototype.getExternalContractUrl = function (contract) {
return url;
};
Config.prototype.loadContractOnTheWeb = function (contract) {
const url = this.getExternalContractUrl(contract);
request(url).pipe(fs.createWriteStream('doodle.png'));
Config.prototype.loadContractOnTheWeb = function (directory, contract) {
return new Promise((resolve, reject) => {
fs.mkdirp(directory, (err) => {
if (err) {
return reject(new Error(err));
}
const url = this.getExternalContractUrl(contract);
const localFile = utils.joinPath(process.cwd(), `${directory}/${path.basename(url)}`);
request(url)
.on('response', function (response) {
if (response.statusCode !== 200) {
return reject(new Error('Getting file returned code ' + response.statusCode));
}
resolve(localFile);
})
.on('error', function (err) {
reject(new Error(err));
})
.pipe(fs.createWriteStream(localFile));
});
});
};
Config.prototype.loadExternalContractsFiles = function() {

View File

@ -7,6 +7,10 @@ function mkdirpSync() {
return fs.mkdirpSync.apply(fs.mkdirpSync, arguments);
}
function mkdirp() {
return fs.mkdirp.apply(fs.mkdirp, arguments);
}
function copySync() {
return fs.copySync.apply(fs.copySync, arguments);
}
@ -54,8 +58,13 @@ function dappPath() {
return utils.joinPath(utils.pwd(), ...arguments);
}
function createWriteStream() {
return fs.createWriteStream.apply(fs.createWriteStream, arguments);
}
module.exports = {
mkdirpSync: mkdirpSync,
mkdirp,
copySync: copySync,
readFileSync: readFileSync,
appendFileSync: appendFileSync,
@ -65,5 +74,6 @@ module.exports = {
existsSync: existsSync,
removeSync: removeSync,
embarkPath: embarkPath,
dappPath: dappPath
dappPath: dappPath,
createWriteStream
};

637
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -3,6 +3,7 @@ const Config = require('../lib/core/config.js');
const Plugins = require('../lib/core/plugins.js');
const assert = require('assert');
const TestLogger = require('../lib/tests/test_logger.js');
const path = require('path');
describe('embark.Config', function () {
let config = new Config({
@ -128,4 +129,14 @@ describe('embark.Config', function () {
});
});
describe('#loadContractOnTheWeb', function () {
it('should get the right url for a https://github file', async function () {
const filePath = await config.loadContractOnTheWeb(
'test_apps/test_app/.embark/contracts',
{file: 'https://github.com/embark-framework/embark/blob/master/test_app/app/contracts/simple_storage.sol'}
);
assert.strictEqual(filePath,
path.normalize('C:/dev/embark/test_apps/test_app/.embark/contracts/simple_storage.sol'));
});
});
});