Skip to content

Commit

Permalink
feat: general install command
Browse files Browse the repository at this point in the history
It's to install project dependencies according to dev-package rules
  • Loading branch information
medikoo committed Nov 7, 2018
1 parent 542f951 commit b8dcbc1
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 17 deletions.
43 changes: 31 additions & 12 deletions bin/dev-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,50 @@ const argv = require("minimist")(process.argv.slice(2), {
boolean: ["disable-git-pull", "enable-git-push"]
});

const usage = `dev-package v${ meta.version } - Install dev package
const usage = `dev-package v${ meta.version }
Usage: dev-package <command>
Usage: dev-package [-h | --help] <command> [<args>]
where <command> is one of:
install
dev-package <command> -h quick help on <command>
Options:
--help, -h Show this message
`;

const commandUsage = new Map([
[
"install",
`dev-package v${ meta.version }
Usage: dev-package install [-h | --help] [--disable-git-pull] [--enable-git-push] [<package-name>]
When <package-name> is provided, it is ensured it's installed and is up to date,
as located in npm packages folder
(there are no updates made to eventual project at current working directory)
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
--version, -v Display version
--help, -h Show this message
`;
`
]
]);

const [command, packageName] = argv._;

if (argv.h || argv.help) {
process.stdout.write(usage);
process.stdout.write(commandUsage.get(command) || usage);
return;
}

Expand All @@ -40,8 +66,6 @@ if (argv.v || argv.version) {
return;
}

const [command, packageName] = argv._;

if (!command) {
process.stderr.write(`Provide command name to install\n\n${ usage }`);
process.exit(1);
Expand All @@ -54,11 +78,6 @@ if (!supportedCommands.has(command)) {
process.exit(1);
}

if (!packageName) {
process.stderr.write(`Provide package name to install\n\n${ usage }`);
process.exit(1);
}

require("../lib/private/cli")(packageName, {
gitPull: !argv["disable-git-pull"],
gitPush: argv["enable-git-push"]
Expand Down
19 changes: 19 additions & 0 deletions install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"use strict";

const toPlainObject = require("es5-ext/object/normalize-options")
, ensureString = require("es5-ext/object/validate-stringifiable-value")
, ee = require("event-emitter")
, unifyEmitters = require("event-emitter/unify")
, { resolve } = require("path")
, ensureConfiguration = require("./lib/private/ensure-user-configuration")
, install = require("./lib/private/install");

module.exports = (path, configuration, options = {}) => {
path = resolve(ensureString(path));
const progressData = ee({ done: new Set(), ongoingMap: new Map(), externalsMap: new Map() });
const promise = ee(
install({ path }, ensureConfiguration(configuration), toPlainObject(options), progressData)
);
unifyEmitters(progressData, promise);
return promise;
};
9 changes: 5 additions & 4 deletions lib/private/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ const log = require("log4").get("dev-package")
, cliFooter = require("cli-progress-footer")()
, DevPackageError = require("../../dev-package-error")
, resolveUserConfiguration = require("../resolve-user-configuration")
, install = require("../../../install")
, installPackage = require("../../../install-package");

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

const installPromise = installPackage(
packageName, await resolveUserConfiguration(), inputOptions
);

const userConfiguration = await resolveUserConfiguration();
const installPromise = packageName
? installPackage(packageName, userConfiguration, inputOptions)
: install(process.cwd(), userConfiguration, inputOptions);
const installsInProgress = new Map();

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

const wait = require("timers-ext/promise/sleep")
, cleanupNpmInstall = require("./cleanup-npm-install")
, getPackageJson = require("./get-package-json")
, removeNonDirectDependencies = require("./remove-non-direct-dependencies")
, setupDependencies = require("./setup-dependencies");

module.exports = async (packageContext, userConfiguration, inputOptions, progressData) => {
const { path } = packageContext;
const { hooks } = userConfiguration;
const packageJson = (packageContext.packageJson = getPackageJson(path));
if (!packageJson) return;
const name = (packageContext.name = packageJson.name);

// Ensure to emit "start" event in next event loop
await wait();
progressData.emit("start", { name, type: "update" });

// Cleanup outcome of eventual previous npm crashes
await cleanupNpmInstall(packageContext);

await setupDependencies(packageContext, userConfiguration, inputOptions, progressData);

if (hooks.afterPackageInstall) {
await hooks.afterPackageInstall(packageContext, userConfiguration, inputOptions);
}

// Cleanup unexpected dependencies from node_modules
await removeNonDirectDependencies(packageContext, userConfiguration);

progressData.emit("end", { name });
};
3 changes: 2 additions & 1 deletion lib/private/setup-dependencies/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ const setupOptionalDependencies = async (

module.exports = async (packageContext, userConfiguration, inputOptions, progressData) => {
const { path } = packageContext;
const packageJson = (packageContext.packageJson = getPackageJson(path));
if (!packageContext.packageJson) packageContext.packageJson = getPackageJson(path);
const { packageJson } = packageContext;
if (!packageJson) return;

await setupRequiredDependencies(packageContext, userConfiguration, inputOptions, progressData);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"minimist": "^1.2",
"semver": "^5.6",
"tar": "^4.4.6",
"timers-ext": "^0.1.7",
"split": "^1.0.1"
},
"devDependencies": {
Expand Down

0 comments on commit b8dcbc1

Please sign in to comment.