use saner library to deal with fs

This commit is contained in:
Iuri Matias 2017-02-18 22:40:42 -05:00
parent 3f3a129276
commit c312943248
8 changed files with 27 additions and 31 deletions

View File

@ -1,5 +1,4 @@
var mkdirp = require('mkdirp'); var fs = require('fs-extra');
var wrench = require('wrench');
var colors = require('colors'); var colors = require('colors');
var GethCommands = require('./geth_commands.js'); var GethCommands = require('./geth_commands.js');
var utils = require('./utils.js'); var utils = require('./utils.js');
@ -53,10 +52,10 @@ Blockchain.prototype.initChainAndGetAddress = function() {
// ensure datadir exists, bypassing the interactive liabilities prompt. // ensure datadir exists, bypassing the interactive liabilities prompt.
this.datadir = '.embark/development/datadir'; this.datadir = '.embark/development/datadir';
mkdirp.sync(this.datadir); fs.mkdirpSync(this.datadir);
// copy mining script // copy mining script
wrench.copyDirSyncRecursive(utils.joinPath(__dirname, "/../js"), ".embark/development/js", {forceDelete: true}); fs.copySync(utils.joinPath(__dirname, "/../js"), ".embark/development/js", {overwrite: true});
// check if an account already exists, create one if not, return address // check if an account already exists, create one if not, return address
result = this.runCommand(this.client.listAccountsCommand()); result = this.runCommand(this.client.listAccountsCommand());

View File

@ -1,4 +1,4 @@
var fs = require('fs'); var fs = require('fs-extra');
var Plugins = require('./plugins.js'); var Plugins = require('./plugins.js');
var utils = require('./utils.js'); var utils = require('./utils.js');
@ -23,7 +23,7 @@ Config.prototype.loadConfigFiles = function(options) {
if (options.interceptLogs === undefined) { if (options.interceptLogs === undefined) {
interceptLogs = true; interceptLogs = true;
} }
this.embarkConfig = JSON.parse(fs.readFileSync(options.embarkConfig)); this.embarkConfig = fs.readJSONSync(options.embarkConfig);
this.embarkConfig.plugins = this.embarkConfig.plugins || {}; this.embarkConfig.plugins = this.embarkConfig.plugins || {};
this.plugins = new Plugins({plugins: this.embarkConfig.plugins, logger: this.logger, interceptLogs: interceptLogs}); this.plugins = new Plugins({plugins: this.embarkConfig.plugins, logger: this.logger, interceptLogs: interceptLogs});
@ -49,8 +49,8 @@ Config.prototype.reloadConfig = function() {
}; };
Config.prototype.loadBlockchainConfigFile = function() { Config.prototype.loadBlockchainConfigFile = function() {
var defaultBlockchainConfig = JSON.parse(fs.readFileSync(this.configDir + "blockchain.json"))[this.env]; var defaultBlockchainConfig = fs.readJSONSync(this.configDir + "blockchain.json");
this.blockchainConfig = defaultBlockchainConfig; this.blockchainConfig = defaultBlockchainConfig[this.env];
}; };
Config.prototype.loadContractsConfigFile = function() { Config.prototype.loadContractsConfigFile = function() {
@ -66,7 +66,7 @@ Config.prototype.loadContractsConfigFile = function() {
}); });
} }
var contractsConfig = JSON.parse(fs.readFileSync(this.configDir + "contracts.json")); var contractsConfig = fs.readJSONSync(this.configDir + "contracts.json");
configObject = utils.recursiveMerge(configObject, contractsConfig); configObject = utils.recursiveMerge(configObject, contractsConfig);
var defaultContractsConfig = configObject['default']; var defaultContractsConfig = configObject['default'];
var envContractsConfig = configObject[this.env]; var envContractsConfig = configObject[this.env];
@ -88,7 +88,7 @@ Config.prototype.loadStorageConfigFile = function() {
// }); // });
//} //}
var storageConfig = JSON.parse(fs.readFileSync(this.configDir + "storage.json")); var storageConfig = fs.readJSONSync(this.configDir + "storage.json");
configObject = utils.recursiveMerge(configObject, storageConfig); configObject = utils.recursiveMerge(configObject, storageConfig);
var defaultStorageConfig = configObject['default']; var defaultStorageConfig = configObject['default'];
var envStorageConfig = configObject[this.env]; var envStorageConfig = configObject[this.env];
@ -109,7 +109,7 @@ Config.prototype.loadCommunicationConfigFile = function() {
// }); // });
//} //}
var communicationConfig = JSON.parse(fs.readFileSync(this.configDir + "communication.json")); var communicationConfig = fs.readJSONSync(this.configDir + "communication.json");
configObject = utils.recursiveMerge(configObject, communicationConfig); configObject = utils.recursiveMerge(configObject, communicationConfig);
var defaultCommunicationConfig = configObject['default']; var defaultCommunicationConfig = configObject['default'];
var envCommunicationConfig = configObject[this.env]; var envCommunicationConfig = configObject[this.env];
@ -135,12 +135,12 @@ Config.prototype.loadChainTrackerFile = function() {
//var self = this; //var self = this;
var chainTracker; var chainTracker;
try { try {
chainTracker = JSON.parse(fs.readFileSync(this.chainsFile)); chainTracker = fs.readJSONSync(this.chainsFile);
} }
catch(err) { catch(err) {
//self.logger.info(this.chainsFile + ' file not found, creating it...'); //self.logger.info(this.chainsFile + ' file not found, creating it...');
chainTracker = {}; chainTracker = {};
fs.writeFileSync(this.chainsFile, '{}'); fs.writeJSONSync(this.chainsFile, {});
} }
this.chainTracker = chainTracker; this.chainTracker = chainTracker;
}; };

View File

@ -1,5 +1,4 @@
var fs = require('fs'); var fs = require('fs-extra');
var prettyJson = require("json-honey");
var DeployTracker = function(options) { var DeployTracker = function(options) {
this.logger = options.logger; this.logger = options.logger;
@ -49,7 +48,7 @@ DeployTracker.prototype.getContract = function(contractName, code, args) {
// chainConfig can be an abstract PersistentObject // chainConfig can be an abstract PersistentObject
DeployTracker.prototype.save = function() { DeployTracker.prototype.save = function() {
if (this.chainConfig === false) { return; } if (this.chainConfig === false) { return; }
fs.writeFileSync("./chains.json", prettyJson(this.chainConfig)); fs.writeJSONSync("./chains.json", this.chainConfig);
}; };
module.exports = DeployTracker; module.exports = DeployTracker;

View File

@ -1,6 +1,5 @@
/*jshint esversion: 6, loopfunc: true */ /*jshint esversion: 6, loopfunc: true */
var fs = require('fs'); var fs = require('fs-extra');
var mkdirp = require('mkdirp');
var Pipeline = function(options) { var Pipeline = function(options) {
this.buildDir = options.buildDir; this.buildDir = options.buildDir;
@ -49,7 +48,7 @@ Pipeline.prototype.build = function(abi) {
var dir = targetFile.split('/').slice(0, -1).join('/'); var dir = targetFile.split('/').slice(0, -1).join('/');
self.logger.trace("creating dir " + this.buildDir + dir); self.logger.trace("creating dir " + this.buildDir + dir);
mkdirp.sync(this.buildDir + dir); fs.mkdirpSync(this.buildDir + dir);
// if it's a directory // if it's a directory
if (targetFile.slice(-1) === '/' || targetFile.indexOf('.') === -1) { if (targetFile.slice(-1) === '/' || targetFile.indexOf('.') === -1) {
@ -64,7 +63,7 @@ Pipeline.prototype.build = function(abi) {
filename = filename.replace(targetDir, ''); filename = filename.replace(targetDir, '');
self.logger.info("writing file " + (self.buildDir + targetDir + filename).bold.dim); self.logger.info("writing file " + (self.buildDir + targetDir + filename).bold.dim);
fs.writeFileSync(self.buildDir + targetDir + filename, fs.readFileSync(file.path)); fs.copySync(self.buildDir + targetDir + filename, file.path, {overwrite: true});
}); });
} else { } else {
var content = contentFiles.map(function(file) { var content = contentFiles.map(function(file) {

View File

@ -1,5 +1,5 @@
/*jshint esversion: 6, loopfunc: true */ /*jshint esversion: 6, loopfunc: true */
var fs = require('fs'); var fs = require('fs-extra');
var utils = require('./utils.js'); var utils = require('./utils.js');
// TODO: pass other params like blockchainConfig, contract files, etc.. // TODO: pass other params like blockchainConfig, contract files, etc..

View File

@ -1,5 +1,4 @@
// TODO: replace with something else more native to node var fs = require('fs-extra');
var wrench = require('wrench');
var utils = require('./utils.js'); var utils = require('./utils.js');
var TemplateGenerator = function(templateName) { var TemplateGenerator = function(templateName) {
@ -10,7 +9,7 @@ TemplateGenerator.prototype.generate = function(destinationFolder, name) {
var templatePath = utils.joinPath(__dirname, '/../', this.templateName); var templatePath = utils.joinPath(__dirname, '/../', this.templateName);
console.log('Initializing Embark Template....'.green); console.log('Initializing Embark Template....'.green);
wrench.copyDirSyncRecursive(templatePath, destinationFolder + name); fs.copySync(templatePath, destinationFolder + name);
utils.cd(destinationFolder + name); utils.cd(destinationFolder + name);
console.log('Installing packages.. this can take a few seconds'.green); console.log('Installing packages.. this can take a few seconds'.green);

View File

@ -1,7 +1,9 @@
/*jshint esversion: 6 */ /*jshint esversion: 6 */
var fs = require('fs'); var fs = require('fs-extra');
var chokidar = require('chokidar'); var chokidar = require('chokidar');
// TODO: this should be receiving the config object not re-reading the
// embark.json file
var Watch = function(options) { var Watch = function(options) {
this.logger = options.logger; this.logger = options.logger;
this.events = {}; this.events = {};
@ -11,7 +13,7 @@ Watch.prototype.start = function() {
var self = this; var self = this;
// TODO: should come from the config object instead of reading the file // TODO: should come from the config object instead of reading the file
// directly // directly
var embarkConfig = JSON.parse(fs.readFileSync("embark.json")); var embarkConfig = fs.readJSONSync("embark.json");
this.watchAssets(embarkConfig, function() { this.watchAssets(embarkConfig, function() {
self.logger.trace('ready to watch asset changes'); self.logger.trace('ready to watch asset changes');

View File

@ -25,17 +25,15 @@
"colors": "^1.1.2", "colors": "^1.1.2",
"commander": "^2.8.1", "commander": "^2.8.1",
"finalhandler": "^0.5.0", "finalhandler": "^0.5.0",
"fs-extra": "^2.0.0",
"grunt": "^1.0.1", "grunt": "^1.0.1",
"json-honey": "^0.4.1",
"merge": "^1.2.0", "merge": "^1.2.0",
"mkdirp": "^0.5.1",
"request": "^2.75.0", "request": "^2.75.0",
"serve-static": "^1.11.1", "serve-static": "^1.11.1",
"shelljs": "^0.5.0", "shelljs": "^0.5.0",
"solc": "0.4.8", "solc": "0.4.8",
"toposort": "^1.0.0", "toposort": "^1.0.0",
"web3": "^0.18.2", "web3": "^0.18.2"
"wrench": "^1.5.8"
}, },
"author": "Iuri Matias <iuri.matias@gmail.com>", "author": "Iuri Matias <iuri.matias@gmail.com>",
"contributors": [], "contributors": [],