Comparing version 0.2.2 to 0.4.0
110
index.js
@@ -1,2 +0,2 @@ | ||
var exec = require('child_process').exec; | ||
var childProcess = require('child_process'); | ||
@@ -20,2 +20,3 @@ module.exports = pushDir; | ||
var cleanup = opts.cleanup === undefined ? false : opts.cleanup; | ||
var verbose = opts.verbose; | ||
@@ -32,11 +33,11 @@ Promise.resolve() | ||
return overwriteLocal ? | ||
overwriteLocalBranch(local) : Promise.reject(reason); | ||
overwriteLocalBranch(local, verbose) : Promise.reject(reason); | ||
}) | ||
.then(checkoutOrphanBranch.bind(null, directory, local)) | ||
.then(addDir.bind(null, directory)) | ||
.then(commitDir.bind(null, directory, message)) | ||
.then(pushDirToRemote.bind(null, remote, remoteBranch)) | ||
.then(resetBranch.bind(null, originalBranch, detachedHead)) | ||
.then(cleanup ? deleteLocalBranch.bind(null, local) : null) | ||
.then(checkoutOrphanBranch.bind(null, directory, local, verbose)) | ||
.then(addDir.bind(null, directory, verbose)) | ||
.then(commitDir.bind(null, directory, message, verbose)) | ||
.then(pushDirToRemote.bind(null, remote, remoteBranch, verbose)) | ||
.then(resetBranch.bind(null, originalBranch, detachedHead, verbose)) | ||
.then(cleanup ? deleteLocalBranch.bind(null, local, verbose) : null) | ||
.catch(handleError); | ||
@@ -51,5 +52,5 @@ | ||
function overwriteLocalBranch(local) { | ||
function overwriteLocalBranch(local, verbose) { | ||
console.log('will overwite local branch...'); | ||
return deleteLocalBranch(local); | ||
return deleteLocalBranch(local, verbose); | ||
} | ||
@@ -68,7 +69,9 @@ | ||
*/ | ||
function getCurrentBranch() { | ||
function getCurrentBranch(verbose) { | ||
return Promise.resolve() | ||
.then(execCmd.bind(null, | ||
'git symbolic-ref HEAD -q', | ||
'problem getting current branch' | ||
'git', | ||
['symbolic-ref', 'HEAD', '-q'], | ||
'problem getting current branch', | ||
verbose | ||
)) | ||
@@ -83,42 +86,54 @@ .catch(function() { | ||
function resetBranch(branch, detach) { | ||
var detached = detach ? '--detach ' : ''; | ||
function resetBranch(branch, detach, verbose) { | ||
var detached = detach ? '--detach' : false; | ||
return execCmd( | ||
'git checkout -f ' + detached + branch, | ||
'problem resetting branch' | ||
'git', | ||
['checkout', '-f', detached, branch].filter(Boolean), | ||
'problem resetting branch', | ||
verbose | ||
); | ||
} | ||
function addDir(directory) { | ||
function addDir(directory, verbose) { | ||
return execCmd( | ||
'git --work-tree ' + directory + ' add --all', | ||
'problem adding directory to local branch' | ||
'git', | ||
['--work-tree', directory, 'add', '--all'], | ||
'problem adding directory to local branch', | ||
verbose | ||
); | ||
} | ||
function commitDir(directory, message) { | ||
function commitDir(directory, message, verbose) { | ||
return execCmd( | ||
'git --work-tree ' + directory + ' commit -m "' + message + '"', | ||
'problem committing directory to local branch' | ||
'git', | ||
['--work-tree', directory, 'commit', '-m', '"' + message + '"'], | ||
'problem committing directory to local branch', | ||
verbose | ||
); | ||
} | ||
function pushDirToRemote(remote, remoteBranch) { | ||
function pushDirToRemote(remote, remoteBranch, verbose) { | ||
return execCmd( | ||
'git push ' + remote + ' HEAD:' + remoteBranch + ' --force', | ||
'problem pushing local branch to remote' | ||
'git', | ||
['push', remote, 'HEAD:' + remoteBranch, '--force'], | ||
'problem pushing local branch to remote', | ||
verbose | ||
); | ||
} | ||
function checkoutOrphanBranch(directory, branch) { | ||
function checkoutOrphanBranch(directory, branch, verbose) { | ||
return execCmd( | ||
'git --work-tree ' + directory + ' checkout --orphan ' + branch, | ||
'problem creating local orphan branch' | ||
'git', | ||
['--work-tree', directory, 'checkout', '--orphan', branch], | ||
'problem creating local orphan branch', | ||
verbose | ||
); | ||
} | ||
function deleteLocalBranch(branch) { | ||
function deleteLocalBranch(branch, verbose) { | ||
return execCmd( | ||
'git branch -D ' + branch, | ||
'problem deleting local branch' | ||
'git', | ||
['branch', '-D', branch], | ||
'problem deleting local branch', | ||
verbose | ||
); | ||
@@ -149,6 +164,8 @@ } | ||
function getLastCommitHash() { | ||
function getLastCommitHash(verbose) { | ||
return execCmd( | ||
'git rev-parse --short HEAD', | ||
'problem getting last commit hash' | ||
'git', | ||
['rev-parse', '--short', 'HEAD'], | ||
'problem getting last commit hash', | ||
verbose | ||
); | ||
@@ -166,7 +183,22 @@ } | ||
function execCmd(cmd, errMessage) { | ||
function execCmd(cmd, args, errMessage, verbose) { | ||
return new Promise(function(resolve, reject) { | ||
exec(cmd, function(error, stdout, stderr) { | ||
error ? reject(errMessage) : resolve(stdout); | ||
verbose ? console.log(cmd, args.join(' ')) : null; | ||
const proc = childProcess.spawn(cmd, args, { stdio: 'pipe' }); | ||
const stdoutChunks = []; | ||
proc.stdout.on('data', function(data) { | ||
stdoutChunks.push(data); | ||
verbose ? process.stdout.write(data) : null; | ||
}); | ||
proc.stderr.on('data', function(data) { | ||
verbose ? process.stderr.write(data) : null; | ||
}); | ||
proc.on('close', function(code) { | ||
verbose ? console.log('\n') : null; | ||
if (code !== 0) reject(errMessage); | ||
else resolve(Buffer.concat(stdoutChunks).toString()); | ||
}); | ||
}); | ||
@@ -177,3 +209,3 @@ } | ||
return new Promise(function(resolve, reject) { | ||
exec(cmd, function(error, stdout, stderr) { | ||
childProcess.exec(cmd, function(error, stdout, stderr) { | ||
(error || stdout.length || stderr.length) ? reject(errMessage) : resolve(); | ||
@@ -180,0 +212,0 @@ }); |
{ | ||
"name": "push-dir", | ||
"version": "0.2.2", | ||
"version": "0.4.0", | ||
"description": "Push a directory to a remote branch", | ||
@@ -5,0 +5,0 @@ "keywords": [], |
@@ -42,15 +42,18 @@ # push-dir | ||
--cleanup | ||
whether to delete the local branch after creating | ||
Whether to delete the local branch after creating | ||
--local-branch-name | ||
force the name of the local branch that is pushed to the remote branch | ||
Force the name of the local branch that is pushed to the remote branch | ||
--allow-unclean | ||
whether to attempt push even if git unclean | ||
Whether to attempt push even if git unclean | ||
--overwrite-local | ||
whether to override a local branch of the same name, if exists | ||
Whether to override a local branch of the same name, if exists | ||
--force | ||
alias for both --allow-unclean and --overwrite-local | ||
Alias for both --allow-unclean and --overwrite-local | ||
--verbose | ||
Display stdout and stderr from internal commands | ||
``` | ||
@@ -57,0 +60,0 @@ |
var test = require('tape'); | ||
var exec = require('child_process').exec; | ||
test('test abort - staged changes', function (t) { | ||
@@ -59,2 +58,7 @@ var cmds = fixtureTestCommands('test-abort-staged-changes.sh'); | ||
test('test works - verbose', function (t) { | ||
var cmds = fixtureTestCommands('test-works-verbose.sh'); | ||
exec(cmds, shouldWork.bind(null, t)); | ||
t.plan(1); | ||
}); | ||
@@ -61,0 +65,0 @@ function fixtureTestCommands(fixture) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
280
66
57174
29