Skip to content

Commit

Permalink
fixing codacy issues
Browse files Browse the repository at this point in the history
  • Loading branch information
cope committed Aug 18, 2018
1 parent c161656 commit f0897ce
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 114 deletions.
5 changes: 2 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* Performs all npm and bower install/update/prune calls in local project
*/

var commander = require("commander");
const commander = require("commander");

commander
.version("0.3.2")
Expand Down Expand Up @@ -83,5 +83,4 @@ if (commander.parallel && (commander.npm !== commander.bower)) {
// console.log("commander.parallel: " + commander.parallel);
// console.log("commander.buffered: " + commander.buffered);

var updatejs = require("./lib/updatejs");
updatejs.update(commander);
require("./lib/updatejs").update(commander);
176 changes: 66 additions & 110 deletions lib/updatejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,177 +2,133 @@

"use strict";

var fs = require("fs");
var _ = require("lodash");
var cmd = require("node-cmd");
var colors = require("colors");
var Promise = require("bluebird");
var exec = require("child_process").exec;
const fs = require("fs");
const _ = require("lodash");
const cmd = require("node-cmd");
const colors = require("colors");
const Promise = require("bluebird");
const exec = require("child_process").exec;

var options = {};
let options = {};

function goto(location, command) {
const goto = (location, command) => {
process.chdir(location);
console.log(("For " + command + ", currently in: " + location).red);
return Promise.resolve(true);
}

function run(command, color) {
return new Promise(function (resolve, reject) {
console.log(color("Running " + command + "..."));
cmd.get(command, function (result) {
result && result.length > 0 && console.log(result);
console.log(color("- " + command + " done."));
return resolve(true);
});
});
}

function seq(promises) {
return Promise
.map(promises, function (promiseFn) {
return promiseFn();
}, {concurrency: 1}); //it will run promises sequentially
}

function build(location, command, color) {
var promises = [
function () {
return goto(location, command);
}
];
options.install && promises.push(function () {
return run(command + " install", color);
});
options.update && promises.push(function () {
return run(command + " update", color);
});
options.prune && promises.push(function () {
return run(command + " prune", color);
};

const run = (command, color) => new Promise((resolve, reject) => {
console.log(color("Running " + command + "..."));
cmd.get(command, (result) => {
result && result.length > 0 && console.log(result);
console.log(color("- " + command + " done."));
return resolve(true);
});
});

return function () {
return seq(promises);
};
}
const seq = (promises) => Promise.map(promises, (promiseFn) => promiseFn(), {concurrency: 1}); //it will run promises sequentially

function params() {
return (options.install ? "i" : "") +
(options.update ? "u" : "") +
(options.prune ? "p" : "") +
(options.recursive ? "r" : "");
}
const build = (location, command, color) => {
let promises = [() => goto(location, command)];
options.install && promises.push(() => run(command + " install", color));
options.update && promises.push(() => run(command + " update", color));
options.prune && promises.push(() => run(command + " prune", color));
return () => seq(promises);
};

const params = () => {
let p = {i: options.install, u: options.update, p: options.prune, r: options.recursive,};
let c = "";
_.forEach(p, (v, k) => c += v ? k : "");
return c;
};

function check(path, file) {
const check = (path, file) => {
try {
var stats = fs.lstatSync(path + "/" + file);
let stats = fs.lstatSync(path + "/" + file);
return stats.isFile();
} catch (err) {
return false;
}
}
};

function isValidFolder(folder) {
return "node_modules" !== folder && "bower_components" !== folder;
}
const isValidFolder = (folder) => "node_modules" !== folder && "bower_components" !== folder;

function find(path, file, rootOnly) {
const find = (path, file, rootOnly) => {
rootOnly = true === rootOnly && !options.recursive;

var ret = [];
let ret = [];
if (check(path, file)) ret.push(path);

var done = rootOnly || (!_.isEmpty(ret) && !options.recursive);
let done = rootOnly || (!_.isEmpty(ret) && !options.recursive);
if (!done) {
var id, child, children = fs.readdirSync(path);
for (id in children) {
if (children.hasOwnProperty(id)) {
child = children[id];
if (isValidFolder(child)) {
if (fs.lstatSync(path + "/" + child).isDirectory()) {
ret = ret.concat(find(path + "/" + child, file));
if (!_.isEmpty(ret) && !options.recursive) break;
}
let child, children = fs.readdirSync(path);
for (child of children) {
if (isValidFolder(child)) {
if (fs.lstatSync(path + "/" + child).isDirectory()) {
ret = ret.concat(find(path + "/" + child, file));
if (!_.isEmpty(ret) && !options.recursive) break;
}
}
}
}
return ret;
}
};

function update(commander) {
const update = (commander) => {
options = commander;

// Build lists of promises for npm:
var npmPromises = [];
let npmPromises = [];
if (options.npm) {
var npmLocations = find(process.cwd(), "package.json", true);

if (!_.isEmpty(npmLocations)) {
_.forEach(npmLocations, function (location) {
npmPromises.push(build(location, "npm", colors.magenta));
});
} else console.log("No package.json found, skipping npm calls.".yellow);
let npmLocations = find(process.cwd(), "package.json", true);
if (!_.isEmpty(npmLocations)) _.forEach(npmLocations, (location) => npmPromises.push(build(location, "npm", colors.magenta)));
else console.log("No package.json found, skipping npm calls.".yellow);
}

// Build lists of promises for bower:
var bowerPromises = [];
let bowerPromises = [];
if (options.bower) {
var bowerLocations = find(process.cwd(), "bower.json");

if (!_.isEmpty(bowerLocations)) {
_.forEach(bowerLocations, function (location) {
bowerPromises.push(build(location, "bower", colors.blue));
});

} else console.log("No bower.json found, skipping bower calls.".yellow);
let bowerLocations = find(process.cwd(), "bower.json");
if (!_.isEmpty(bowerLocations)) _.forEach(bowerLocations, (location) => bowerPromises.push(build(location, "bower", colors.blue)));
else console.log("No bower.json found, skipping bower calls.".yellow);
}

// Parallel processing:
if (options.parallel) {
var parameters = params();
let parameters = params();

// Buffered output for parallel processing:
if (options.buffered) {
console.log("Running all npm calls...".magenta);
exec("updatejs -n" + parameters, function (error, stdout, stderr) {
console.log(stdout.magenta);
});
exec("updatejs -n" + parameters, (error, stdout) => console.log(stdout.magenta));

console.log("Running all bower calls...".blue);
exec("updatejs -b" + parameters, function (error, stdout, stderr) {
console.log(stdout.blue);
});
exec("updatejs -b" + parameters, (error, stdout) => console.log(stdout.blue));

// Real time, non-buffered output for parallel processing:
} else {
var npmChild = exec("updatejs -n" + parameters);
npmChild.stdout.on("data", function (data) {
let npmChild = exec("updatejs -n" + parameters);
npmChild.stdout.on("data", (data) => {
if (data.indexOf("For") >= 0) console.log(data.red);
else if (data.indexOf("Done") >= 0) console.log(data.green);
else console.log(data.magenta);
});

var bowerChild = exec("updatejs -b" + parameters);
bowerChild.stdout.on("data", function (data) {
let bowerChild = exec("updatejs -b" + parameters);
bowerChild.stdout.on("data", (data) => {
if (data.indexOf("For") >= 0) console.log(data.red);
else if (data.indexOf("Done") >= 0) console.log(data.green);
else console.log(data.blue);

});
}
}

// Sequential processing:
else {
var promises = _.concat(npmPromises, bowerPromises);
seq(promises)
.then(function () {
console.log("Done.".green);
});
let promises = _.concat(npmPromises, bowerPromises);
seq(promises).then(() => console.log("Done.".green));
}
}

module.exports = {
update: update
};

module.exports = {update};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "updatejs",
"version": "0.4.1",
"version": "0.4.2",
"description": "Install/Update/Prune NPM and Bower",
"homepage": "https://cope.github.io/updatejs/",
"author": "Predrag Stojadinovic <predrag@stojadinovic.net>",
Expand Down

0 comments on commit f0897ce

Please sign in to comment.