Skip to content

Instantly share code, notes, and snippets.

@dmytroZherebko
Created February 21, 2018 20:49
Show Gist options
  • Save dmytroZherebko/8f8466334c73f8443a16ae3799805f65 to your computer and use it in GitHub Desktop.
Save dmytroZherebko/8f8466334c73f8443a16ae3799805f65 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const _ = require('lodash')
const {parseChanges} = require('../shared/get-changed-files')
const {getStagedChanges} = require('../shared/utils')
const {projectPath} = require('../constants')
const messageReg = /[^\]^\[]+$/i
const prefixes = _.keys(parseChanges(getStagedChanges()))
.sort()
.map(el => `[${el}]`)
.join('')
const gitMsgPath = path.join(projectPath, '.git/COMMIT_EDITMSG')
const commitMsg = fs.readFileSync(gitMsgPath, {
encoding: 'utf8',
})
const parsedCommitMsg = commitMsg
.match(messageReg)[0]
.replace('\n', ' ')
.trimLeft()
fs.writeFileSync(`./.git/COMMIT_EDITMSG`, `${prefixes} ${parsedCommitMsg}`, {
encoding: 'utf-8',
})
// get changed files
const fs = require('fs')
const path = require('path')
const shelljs = require('shelljs')
function currentBranch() {
return (
shelljs.exec('git rev-parse --abbrev-ref HEAD', {
silent: true,
}) + ''
)
}
function parse(str) {
return str
.trim()
.split(/[\n]/)
.map(el => {
return el.split(/[:]/).map(el => el.trim())
})
}
function getRepos(type) {
const filePath = path.resolve(`./${type}/.reference`)
const components = fs.readFileSync(filePath, {
encoding: 'utf-8',
})
return parse(components)
}
function getStagedChanges() {
return shelljs.exec('git diff --name-only --cached', {silent: true})
}
const promisifyCommand = exec => (command, args = {}) => {
console.log('Running command: ', command)
return new Promise((res, rej) => {
exec(command, {...args, async: true}, (code, out, err) => {
return code == 0 ? res(out) : rej(err)
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment