From c31294324848fe917e1cc5eb7499b10d0771517c Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 18 Feb 2017 22:40:42 -0500 Subject: [PATCH] use saner library to deal with fs --- lib/blockchain.js | 7 +++---- lib/config.js | 20 ++++++++++---------- lib/deploy_tracker.js | 5 ++--- lib/pipeline.js | 7 +++---- lib/plugin.js | 2 +- lib/template_generator.js | 5 ++--- lib/watch.js | 6 ++++-- package.json | 6 ++---- 8 files changed, 27 insertions(+), 31 deletions(-) diff --git a/lib/blockchain.js b/lib/blockchain.js index 5bb67191c..4736779e3 100644 --- a/lib/blockchain.js +++ b/lib/blockchain.js @@ -1,5 +1,4 @@ -var mkdirp = require('mkdirp'); -var wrench = require('wrench'); +var fs = require('fs-extra'); var colors = require('colors'); var GethCommands = require('./geth_commands.js'); var utils = require('./utils.js'); @@ -53,10 +52,10 @@ Blockchain.prototype.initChainAndGetAddress = function() { // ensure datadir exists, bypassing the interactive liabilities prompt. this.datadir = '.embark/development/datadir'; - mkdirp.sync(this.datadir); + fs.mkdirpSync(this.datadir); // 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 result = this.runCommand(this.client.listAccountsCommand()); diff --git a/lib/config.js b/lib/config.js index 532531bc0..d1115ea0b 100644 --- a/lib/config.js +++ b/lib/config.js @@ -1,4 +1,4 @@ -var fs = require('fs'); +var fs = require('fs-extra'); var Plugins = require('./plugins.js'); var utils = require('./utils.js'); @@ -23,7 +23,7 @@ Config.prototype.loadConfigFiles = function(options) { if (options.interceptLogs === undefined) { interceptLogs = true; } - this.embarkConfig = JSON.parse(fs.readFileSync(options.embarkConfig)); + this.embarkConfig = fs.readJSONSync(options.embarkConfig); this.embarkConfig.plugins = this.embarkConfig.plugins || {}; 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() { - var defaultBlockchainConfig = JSON.parse(fs.readFileSync(this.configDir + "blockchain.json"))[this.env]; - this.blockchainConfig = defaultBlockchainConfig; + var defaultBlockchainConfig = fs.readJSONSync(this.configDir + "blockchain.json"); + this.blockchainConfig = defaultBlockchainConfig[this.env]; }; 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); var defaultContractsConfig = configObject['default']; 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); var defaultStorageConfig = configObject['default']; 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); var defaultCommunicationConfig = configObject['default']; var envCommunicationConfig = configObject[this.env]; @@ -135,12 +135,12 @@ Config.prototype.loadChainTrackerFile = function() { //var self = this; var chainTracker; try { - chainTracker = JSON.parse(fs.readFileSync(this.chainsFile)); + chainTracker = fs.readJSONSync(this.chainsFile); } catch(err) { //self.logger.info(this.chainsFile + ' file not found, creating it...'); chainTracker = {}; - fs.writeFileSync(this.chainsFile, '{}'); + fs.writeJSONSync(this.chainsFile, {}); } this.chainTracker = chainTracker; }; @@ -159,7 +159,7 @@ Config.prototype.loadFiles = function(files) { readFiles.push({filename: 'ipfs.js', content: fs.readFileSync(utils.joinPath(__dirname, "/../js/ipfs.js")).toString(), path: utils.joinPath(__dirname, "/../js/ipfs.js")}); // TODO: remove duplicated files if funcitonality is the same for storage and orbit readFiles.push({filename: 'ipfs-api.js', content: fs.readFileSync(utils.joinPath(__dirname, "/../js/ipfs-api.min.js")).toString(), path: utils.joinPath(__dirname, "/../js/ipfs-api.min.js")}); - readFiles.push({filename: 'orbit.js', content: fs.readFileSync(utils.joinPath(__dirname, "/../js/orbit.min.js")).toString(), path: utils.joinPath(__dirname, "/../js/orbit.min.js")}); + readFiles.push({filename: 'orbit.js', content: fs.readFileSync(utils.joinPath(__dirname, "/../js/orbit.min.js")).toString(), path: utils.joinPath(__dirname, "/../js/orbit.min.js")}); readFiles.push({filename: 'embark.js', content: fs.readFileSync(utils.joinPath(__dirname, "/../js/build/embark.bundle.js")).toString(), path: utils.joinPath(__dirname, "/../js/build/embark.bundle.js")}); } }); diff --git a/lib/deploy_tracker.js b/lib/deploy_tracker.js index 9f446a9cd..8a792923d 100644 --- a/lib/deploy_tracker.js +++ b/lib/deploy_tracker.js @@ -1,5 +1,4 @@ -var fs = require('fs'); -var prettyJson = require("json-honey"); +var fs = require('fs-extra'); var DeployTracker = function(options) { this.logger = options.logger; @@ -49,7 +48,7 @@ DeployTracker.prototype.getContract = function(contractName, code, args) { // chainConfig can be an abstract PersistentObject DeployTracker.prototype.save = function() { if (this.chainConfig === false) { return; } - fs.writeFileSync("./chains.json", prettyJson(this.chainConfig)); + fs.writeJSONSync("./chains.json", this.chainConfig); }; module.exports = DeployTracker; diff --git a/lib/pipeline.js b/lib/pipeline.js index 042ada1a4..010f32a11 100644 --- a/lib/pipeline.js +++ b/lib/pipeline.js @@ -1,6 +1,5 @@ /*jshint esversion: 6, loopfunc: true */ -var fs = require('fs'); -var mkdirp = require('mkdirp'); +var fs = require('fs-extra'); var Pipeline = function(options) { this.buildDir = options.buildDir; @@ -49,7 +48,7 @@ Pipeline.prototype.build = function(abi) { var dir = targetFile.split('/').slice(0, -1).join('/'); self.logger.trace("creating dir " + this.buildDir + dir); - mkdirp.sync(this.buildDir + dir); + fs.mkdirpSync(this.buildDir + dir); // if it's a directory if (targetFile.slice(-1) === '/' || targetFile.indexOf('.') === -1) { @@ -64,7 +63,7 @@ Pipeline.prototype.build = function(abi) { filename = filename.replace(targetDir, ''); 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 { var content = contentFiles.map(function(file) { diff --git a/lib/plugin.js b/lib/plugin.js index 574193114..fd7e04723 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -1,5 +1,5 @@ /*jshint esversion: 6, loopfunc: true */ -var fs = require('fs'); +var fs = require('fs-extra'); var utils = require('./utils.js'); // TODO: pass other params like blockchainConfig, contract files, etc.. diff --git a/lib/template_generator.js b/lib/template_generator.js index 8e4448ed2..1befd03a6 100644 --- a/lib/template_generator.js +++ b/lib/template_generator.js @@ -1,5 +1,4 @@ -// TODO: replace with something else more native to node -var wrench = require('wrench'); +var fs = require('fs-extra'); var utils = require('./utils.js'); var TemplateGenerator = function(templateName) { @@ -10,7 +9,7 @@ TemplateGenerator.prototype.generate = function(destinationFolder, name) { var templatePath = utils.joinPath(__dirname, '/../', this.templateName); console.log('Initializing Embark Template....'.green); - wrench.copyDirSyncRecursive(templatePath, destinationFolder + name); + fs.copySync(templatePath, destinationFolder + name); utils.cd(destinationFolder + name); console.log('Installing packages.. this can take a few seconds'.green); diff --git a/lib/watch.js b/lib/watch.js index 9c94e1d84..fa68d056c 100644 --- a/lib/watch.js +++ b/lib/watch.js @@ -1,7 +1,9 @@ /*jshint esversion: 6 */ -var fs = require('fs'); +var fs = require('fs-extra'); var chokidar = require('chokidar'); +// TODO: this should be receiving the config object not re-reading the +// embark.json file var Watch = function(options) { this.logger = options.logger; this.events = {}; @@ -11,7 +13,7 @@ Watch.prototype.start = function() { var self = this; // TODO: should come from the config object instead of reading the file // directly - var embarkConfig = JSON.parse(fs.readFileSync("embark.json")); + var embarkConfig = fs.readJSONSync("embark.json"); this.watchAssets(embarkConfig, function() { self.logger.trace('ready to watch asset changes'); diff --git a/package.json b/package.json index a1e3f67b9..e0cbdbd62 100644 --- a/package.json +++ b/package.json @@ -25,17 +25,15 @@ "colors": "^1.1.2", "commander": "^2.8.1", "finalhandler": "^0.5.0", + "fs-extra": "^2.0.0", "grunt": "^1.0.1", - "json-honey": "^0.4.1", "merge": "^1.2.0", - "mkdirp": "^0.5.1", "request": "^2.75.0", "serve-static": "^1.11.1", "shelljs": "^0.5.0", "solc": "0.4.8", "toposort": "^1.0.0", - "web3": "^0.18.2", - "wrench": "^1.5.8" + "web3": "^0.18.2" }, "author": "Iuri Matias ", "contributors": [],