Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support stdin for hook #13

Merged
merged 13 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions bin/hooks/post-rewrite
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,23 @@ var path = require('path')
, name = path.basename(__filename)
, project = scripts();

const args = process.argv.slice(2);
const readline = require('readline').createInterface({
input: process.stdin,
});

readline.question('', data => {
const arr = data.split(' ')
args.push(...arr)
readline.close()
});

project.run(name, process.argv.slice(2), function(err) {
if (err) {
console.error('Failed to exec ' + name + ' hook script')
process.exit(err.code);
}
setImmediate(() => {
project.run(name, args, function(err) {
if (err) {
console.error('Failed to exec ' + name + ' hook script')
process.exit(err.code);
}
});
});

22 changes: 17 additions & 5 deletions bin/hooks/pre-push
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,23 @@ var path = require('path')
, name = path.basename(__filename)
, project = scripts();

const args = process.argv.slice(2);
const readline = require('readline').createInterface({
input: process.stdin,
});

readline.question('', data => {
const arr = data.split(' ')
args.push(...arr)
readline.close()
});

project.run(name, process.argv.slice(2), function(err) {
if (err) {
console.error('Failed to exec ' + name + ' hook script')
process.exit(err.code);
}
setImmediate(() => {
project.run(name, args, function(err) {
if (err) {
console.error('Failed to exec ' + name + ' hook script')
process.exit(err.code);
}
});
});

22 changes: 17 additions & 5 deletions bin/hooks/pre-receive
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,23 @@ var path = require('path')
, name = path.basename(__filename)
, project = scripts();

const args = process.argv.slice(2);
const readline = require('readline').createInterface({
input: process.stdin,
});

readline.question('', data => {
const arr = data.split(' ')
args.push(...arr)
readline.close()
});

project.run(name, process.argv.slice(2), function(err) {
if (err) {
console.error('Failed to exec ' + name + ' hook script')
process.exit(err.code);
}
setImmediate(() => {
project.run(name, args, function(err) {
if (err) {
console.error('Failed to exec ' + name + ' hook script')
process.exit(err.code);
}
});
});

2 changes: 1 addition & 1 deletion lib/git-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ GitScripts.prototype.run = function(name) {
if (!cmd) return callback();

var conf = {stdio: 'inherit', customFds: [0, 1, 2]}
, proc = spawn('sh', ['-c', cmd].concat(args), conf);
, proc = spawn('sh', ['-c', cmd + ' $*'].concat(...args), conf);

proc.on('close', function(code) {
var err;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "git-scripts",
"version": "0.4.3",
"version": "0.5.3",
"description": "Git hooks integration for Node.js projects",
"main": "index.js",
"scripts": {
Expand Down
24 changes: 23 additions & 1 deletion scripts/pre-push.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ const shell = require('shelljs')
const NO_HOOK_VAR = 'NO_HOOK'
const INNER_PRE_HOOK = 'CHATIE_INNER_PRE_HOOK'

const argv = process.argv.slice(2)
const remoteName = argv[0] || ''
const remoteUrl = argv[1] || ''
const localBranch = argv[2] || ''
const localCommit = argv[3] || ''
const remoteBranch = argv[4] || ''
const remoteCommit = argv[5] || ''

if (localCommit.match(/^0+$/)) {
// delete remote branch
process.exit(0)
}

if (process.env[NO_HOOK_VAR]) {
// user set NO_HOOK=1 to prevent this hook works
process.exit(0)
Expand All @@ -24,10 +37,19 @@ if (process.env[INNER_PRE_HOOK]) {
process.exit(0)
}

const packageVersion = require('../package.json').version
const lastCommitMsg = shell.exec('git log --pretty=format:"%s" HEAD^0 -1', {silent: true}).stdout

if (packageVersion === lastCommitMsg) {
process.exit(0)
}

shell.rm('-f', 'package-lock.json')
shell.exec('npm version patch --no-package-lock').code === 0 || process.exit(1)
process.env[INNER_PRE_HOOK] = '1'
shell.exec('git push').code === 0 || process.exit(1)

const cmd = ['git push', remoteName, remoteBranch ? localBranch + ':' + remoteBranch : ''].join(' ')
shell.exec(cmd).code === 0 || process.exit(1)

console.info(String.raw`
____ _ _ ____ _
Expand Down