Skip to content

Commit

Permalink
feat: update-all command
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Nov 9, 2018
1 parent e67e65a commit 26364b7
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 8 deletions.
20 changes: 18 additions & 2 deletions bin/dev-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ When <package-name> is not provided then all dependencies of a project at
current working directory are ensured to be linked or installed
up to dev-package installation rules
Options:
--disable-git-pull Do not pull changes from remote
--enable-git-push Push committed changes to remote
--help, -h Show this message
`
],
[
"update-all",
`dev-package v${ meta.version }
Usage: dev-package update-all [-h | --help] [--disable-git-pull] [--enable-git-push]
Ensures all packages in npm packages folder are properly installed and up to date
Options:
--disable-git-pull Do not pull changes from remote
Expand Down Expand Up @@ -71,14 +87,14 @@ if (!command) {
process.exit(1);
}

const supportedCommands = new Set(["install"]);
const supportedCommands = new Set(["install", "update-all"]);

if (!supportedCommands.has(command)) {
process.stderr.write(`${ command } is not a suppported command\n\n${ usage }`);
process.exit(1);
}

require("../lib/private/cli")(packageName, {
require("../lib/private/cli")(command, packageName, {
gitPull: !argv["disable-git-pull"],
gitPush: argv["enable-git-push"]
});
16 changes: 11 additions & 5 deletions lib/private/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@ const log = require("log4").get("dev-package")
, DevPackageError = require("../../dev-package-error")
, resolveUserConfiguration = require("../resolve-user-configuration")
, install = require("../../../install")
, installPackage = require("../../../install-package");
, installPackage = require("../../../install-package")
, updateAll = require("../../../update-all");

module.exports = async (packageName, inputOptions) => {
module.exports = async (command, packageName, inputOptions) => {
cliFooter.shouldAddProgressAnimationPrefix = true;
cliFooter.updateProgress(["resolving user configuration"]);

const userConfiguration = await resolveUserConfiguration();
const installPromise = packageName
? installPackage(packageName, userConfiguration, inputOptions)
: install(process.cwd(), userConfiguration, inputOptions);
const installPromise = (() => {
if (command === "install") {
return packageName
? installPackage(packageName, userConfiguration, inputOptions)
: install(process.cwd(), userConfiguration, inputOptions);
}
if (command === "update-all") return updateAll(userConfiguration, inputOptions);
})();
const installsInProgress = new Map();

const logWordForms = {
Expand Down
28 changes: 28 additions & 0 deletions lib/private/update-all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"use strict";

const { resolve } = require("path")
, readdir = require("fs2/readdir")
, installPackage = require("./install-package");

module.exports = async (userConfiguration, inputOptions, progressData) => {
const { packagesPath, packagesMeta } = userConfiguration;
const { done } = progressData;

const conditionallyUpdatePackage = name => {
if (!packagesMeta[name]) return null; // Not recognized as dev package folder
if (done.has(name)) return null; // Already processed
return installPackage({ name }, userConfiguration, inputOptions, progressData);
};
for (const directoryName of await readdir(packagesPath, { type: { directory: true } })) {
// Intentionally (to avoid npm race conditions) process packages one by one
if (directoryName.startsWith("@")) {
for (const subDirectoryName of await readdir(resolve(packagesPath, directoryName), {
type: { directory: true }
})) {
await conditionallyUpdatePackage(`${ directoryName }/${ subDirectoryName }`);
}
continue;
}
await conditionallyUpdatePackage(directoryName);
}
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
{
"files": [
"lib/private/install-package/index.js",
"lib/private/setup-dependencies/index.js"
"lib/private/setup-dependencies/index.js",
"lib/private/update-all.js"
],
"rules": {
"no-await-in-loop": "off"
Expand Down
16 changes: 16 additions & 0 deletions update-all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"use strict";

const toPlainObject = require("es5-ext/object/normalize-options")
, ee = require("event-emitter")
, unifyEmitters = require("event-emitter/unify")
, ensureConfiguration = require("./lib/private/ensure-user-configuration")
, updateAll = require("./lib/private/update-all");

module.exports = (configuration, options = {}) => {
const progressData = ee({ done: new Set(), ongoingMap: new Map(), externalsMap: new Map() });
const promise = ee(
updateAll(ensureConfiguration(configuration), toPlainObject(options), progressData)
);
unifyEmitters(progressData, promise);
return promise;
};

0 comments on commit 26364b7

Please sign in to comment.