Skip to content

Commit

Permalink
feat: enable git push option
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Nov 9, 2018
1 parent ab1fc26 commit 2e91ec7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 22 deletions.
13 changes: 10 additions & 3 deletions bin/dev-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ process.on("unhandledRejection", reason => { throw reason; });

require("log4-nodejs")({ defaultNamespace: "dev-package" });

const meta = require("../package")
, argv = require("minimist")(process.argv.slice(2), { boolean: "skip-git-update" });
const meta = require("../package");

const argv = require("minimist")(process.argv.slice(2), {
boolean: ["enable-git-push", "skip-git-update"]
});

const usage = `dev-package v${ meta.version } - Install dev package
Expand All @@ -21,6 +24,7 @@ where <command> is one of:
Options:
--skip-git-update Do not 'git pull' on update
--enable-git-push Push committed changes to remote
--version, -v Display version
--help, -h Show this message
Expand Down Expand Up @@ -55,4 +59,7 @@ if (!packageName) {
process.exit(1);
}

require("../lib/private/cli")(packageName, { skipGitUpdate: argv["skip-git-update"] });
require("../lib/private/cli")(packageName, {
gitPush: argv["enable-git-push"],
skipGitUpdate: argv["skip-git-update"]
});
54 changes: 35 additions & 19 deletions lib/setup-repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,49 @@ const isObject = require("es5-ext/object/is-object")
module.exports = async (path, repoUrl, options = {}) => {
if (!isObject(options)) options = {};
if (await isDirectory(path)) {
if (options.skipGitUpdate) return false;
// Confirm directory is clean
let { stdout: localStatus } = await runProgram("git", ["status", "--porcelain"], {
cwd: path
});
if (localStatus) {
throw new DevPackageError(
`Cannot proceed with update, repository at ${ path } is not clean:\n${
localStatus.split("\n").map(line => ` ${ line }`).join("\n")
}`
);
const tryPull = !options.skipGitUpdate, tryPush = options.gitPush;

if (!tryPull && !tryPush) return false;

if (tryPull) {
// Confirm directory is clean
const { stdout: localStatus } = await runProgram("git", ["status", "--porcelain"], {
cwd: path
});
if (localStatus) {
throw new DevPackageError(
`Cannot proceed with update, repository at ${ path } is not clean:\n${
localStatus.split("\n").map(line => ` ${ line }`).join("\n")
}`
);
}

// Update
log.info("update repository %s", path);
await runProgram("git", ["remote", "update"], {
cwd: path,
logger: log.levelRoot.get("git:remote")
});
}

// Update
log.info("update repository %s", path);
await runProgram("git", ["remote", "update"], {
cwd: path,
logger: log.levelRoot.get("git:remote")
});
const { stdout: localStatus } = await runProgram("git", ["status"], { cwd: path });

({ stdout: localStatus } = await runProgram("git", ["status"], { cwd: path }));
if (localStatus.includes("Your branch is behind")) {
if (
tryPull &&
(localStatus.includes("Your branch is behind") || localStatus.includes("have diverged"))
) {
await runProgram("git", ["merge", "FETCH_HEAD"], {
cwd: path,
logger: log.levelRoot.get("git:merge")
});
}

if (
tryPush &&
(localStatus.includes("Your branch is ahead") || localStatus.includes("have diverged"))
) {
await runProgram("git", ["push"], { cwd: path, logger: log.levelRoot.get("git:push") });
}
return false;
}
log.info("clone repository %s from %s", path, repoUrl);
Expand Down

0 comments on commit 2e91ec7

Please sign in to comment.