Skip to content

Commit

Permalink
feat: Use yarn package manager when available
Browse files Browse the repository at this point in the history
updtr is now using the yarn package manager when a `yarn.lock` file exists in `process.cwd()`.
  • Loading branch information
axe312ger authored and jhnns committed Mar 31, 2017
1 parent 4e955f7 commit bcca7ce
Show file tree
Hide file tree
Showing 7 changed files with 871 additions and 23 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ node_js:
- "0.12"
- "0.10"

install:
- npm install

script:
- npm test

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

Based on `npm outdated`, **updtr** installs the latest version and runs `npm test` for each dependency. If the test succeeds, **updtr** saves the new version number to your `package.json`. If the test fails, however, **updtr** rolls back its changes.

Additionally, it will use `yarn` instead of `npm` when a `yarn.lock` file is present in your project.

Made by [Peerigon](https://peerigon.com/?pk_campaign=gh-os&pk_kwd=updtr).

![updtr](assets/updtr.gif)
Expand All @@ -34,7 +36,7 @@ npm install -g updtr
-h, --help output usage information
-V, --version output the version number
-R, --reporter <reporter> choose reporter: default, shy
-R, --reporter <reporter> choose reporter: default, shy, simple
-w, --wanted updates to wanted version specified in package.json instead of the modules latest version
-t, --test <test> change the command for the tests
-e, --exclude <exclude> exclude modules comma seperated, e.g. updtr --exclude module1,module2
Expand Down
6 changes: 3 additions & 3 deletions lib/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

var semver = require("semver");

function isGitDependency(info) {
return info.latest === "git";
function isExoticDependency(info) {
return ["git", "exotic"].indexOf(info.latest) !== -1;
}

function isAlreadyInstalled(info) {
Expand All @@ -27,7 +27,7 @@ function isCurrentGreaterThanUpdateTo(info) {

function filter(config) {
return function (info) {
return isGitDependency(info) === false &&
return isExoticDependency(info) === false &&
isAlreadyInstalled(info) === false &&
isUnstable(info) === false &&
isExcluded(info, config.exclude) === false &&
Expand Down
61 changes: 43 additions & 18 deletions lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ var async = require("async");
var childProcess = require("child_process");
var filter = require("./filter");
var EventEmitter = require("./EventEmitter.js");
var fs = require("fs");
var path = require("path");

var defaultTestCmd = "npm test";
var defaultTestCmdNpm = "npm test";
var defaultTestCmdYarn = "yarn test";

function run(config, done) {
var emitter = new EventEmitter();
var testCmd;
var reporter;
var cwd;

function exec(cmd, cb) {
childProcess.exec(cmd, { maxBuffer: Infinity, encoding: "utf8", cwd: config.cwd }, cb);
Expand All @@ -22,25 +24,28 @@ function run(config, done) {
done(err || null);
}

if (typeof config.cwd !== "string") {
throw new Error("Cannot run updtr: cwd missing");
}

sanitizeConfig(config);

if (config.registry && config.useYarn) {
throw new Error("`yarn add` does not support custom registries yet. Please use a .npmrc file to achieve this.");
}

reporter = config.reporter;
cwd = config.cwd;
testCmd = config.testCmd;

if (typeof cwd !== "string") {
throw new Error("Cannot run updtr: cwd missing");
}

reporter(emitter);

emitter.emit("init", {
cwd: config.cwd
});
exec("npm outdated --json --long --depth=0", function (err, stdout) {
exec(config.useYarn ? "yarn outdated --json --flat" : "npm outdated --json --long --depth=0", function (err, stdout) {
var missing;
var outdated;
var infos;
var infos = [];
var tasks;

function createTask(info, index) {
Expand All @@ -50,19 +55,21 @@ function run(config, done) {
total: tasks.length,
info: info,
testCmd: testCmd,
installCmd: "npm i" + (config.registry ? (" --registry " + config.registry) : "")
installCmd: (config.useYarn ? "yarn add" : "npm i") + (config.registry ? (" --registry " + config.registry) : "")
};
var testStdout;
var installCmd = event.installCmd + " " + info.name + "@" + info.updateTo + " " + info.saveCmd;
var installCmd;

if (config.saveExact) {
installCmd += " --save-exact";
if (!config.useYarn && config.saveExact) {
event.installCmd += " --save-exact";
}

installCmd = event.installCmd + " " + info.name + "@" + info.updateTo + " " + info.saveCmd;

emitter.emit("updating", event);

async.series({
deleteOldVersion: async.apply(exec, "npm remove " + info.name + " " + info.saveCmd),
deleteOldVersion: async.apply(exec, (config.useYarn ? "yarn remove " : "npm remove ") + info.name + " " + info.saveCmd),
installNewVersion: async.apply(exec, installCmd),
emitTestingEvent: function (done) {
emitter.emit("testing", event);
Expand Down Expand Up @@ -113,7 +120,22 @@ function run(config, done) {
}

outdated = JSON.parse(stdout);
infos = Object.keys(outdated)
if (config.useYarn) {
if ("data" in outdated && outdated.data.body instanceof Array) {
infos = outdated.data.body.map(function (info) {
return {
name: info[0],
current: info[1],
saveCmd: info[4] === "devDependencies" ? "--dev" : "",
type: info[4],
wanted: info[2],
latest: info[3],
updateTo: config.wanted ? info[2] : info[3]
};
});
}
} else {
infos = Object.keys(outdated)
.map(function (moduleName) {
var info = outdated[moduleName];

Expand All @@ -122,8 +144,10 @@ function run(config, done) {
info.updateTo = config.wanted ? info.wanted : info.latest;

return info;
})
.filter(filter(config));
});
}

infos = infos.filter(filter(config));

missing = modulesMissing(infos);
if (missing.length > 0) {
Expand Down Expand Up @@ -166,8 +190,9 @@ function modulesMissing(infos) {
}

function sanitizeConfig(config) {
config.useYarn = fs.existsSync(path.resolve(config.cwd, "yarn.lock"));
config.reporter = config.reporter || Function.prototype;
config.testCmd = config.testCmd || defaultTestCmd;
config.testCmd = config.testCmd || (config.useYarn ? defaultTestCmdYarn : defaultTestCmdNpm);
config.exclude = config.exclude && config.exclude.split(",").map(function (name) {
return name.trim();
}) || [];
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"eslint-plugin-jsdoc": "^2.3.1",
"istanbul": "^0.4.3",
"mocha": "^2.3.3",
"rewire": "^2.5.2",
"sinon": "^1.17.4",
"sinon-chai": "^2.8.0"
},
Expand Down
Loading

0 comments on commit bcca7ce

Please sign in to comment.