|
| 1 | +const fs = require('fs') |
1 | 2 | const execProcess = require('./execProcess.js') |
2 | 3 |
|
3 | | -execProcess.result('sh node_modules/squashed/countBranchCommits.sh', function(err, response) { |
4 | | - if (err) { |
5 | | - console.log(err) |
6 | | - } else { |
7 | | - if (response > 1) { |
8 | | - console.log('You need to squash your commits! Do a rebase!') |
| 4 | +let path = './node_modules/squashed/commands/' |
| 5 | +let commitRef = 'HEAD' |
| 6 | + |
| 7 | +// Function which returns a promise so we can chain command execution |
| 8 | +const commandHandler = (command) => { |
| 9 | + return new Promise((resolve, reject) => { |
| 10 | + execProcess.result(`sh ${path}${command}`, (err, response) => { |
| 11 | + if (err) { |
| 12 | + reject(err) |
| 13 | + } else { |
| 14 | + resolve(response) |
| 15 | + } |
| 16 | + }) |
| 17 | + }) |
| 18 | +} |
| 19 | + |
| 20 | +// Updates the exitCode and logs errors form command promise rejections |
| 21 | +const errorHandler = (error) => { |
| 22 | + console.error(error) |
| 23 | + process.exit(1) |
| 24 | +} |
| 25 | + |
| 26 | +// Log squashed messages in blue so they stand out |
| 27 | +const prettyLog = (message) => { |
| 28 | + console.log(`\x1b[34m${message}\x1b[0m`) |
| 29 | +} |
| 30 | + |
| 31 | +// Initialisation and env setup |
| 32 | +const init = () => { |
| 33 | + // if not installed via Node Package Manager adjust path to root (for debugging local builds) |
| 34 | + try { |
| 35 | + (!fs.lstatSync(path).isDirectory()) |
| 36 | + } catch(err) { |
| 37 | + if (err.code === 'ENOENT') { |
| 38 | + path = './commands/' |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // if run inside Travis CI we need to ensure we update the commitRef since we are working in a detached head state |
| 43 | + if (process.env.TRAVIS_PULL_REQUEST_BRANCH) { |
| 44 | + commitRef = process.env.TRAVIS_PULL_REQUEST_SHA |
| 45 | + } |
| 46 | + console.log(`Commit reference: ${commitRef}`) |
| 47 | +} |
| 48 | + |
| 49 | +init() |
| 50 | + |
| 51 | +Promise.all([commandHandler(`getBranchCommitCount.sh ${commitRef}`), commandHandler('getFormattedBranchCommits.sh')]).then( |
| 52 | + ([commitCount, formattedBranchCommits]) => { |
| 53 | + if (commitCount > 1) { |
| 54 | + console.log(` |
| 55 | +${formattedBranchCommits}`) |
| 56 | + prettyLog(` |
| 57 | + You need to squash those ${+commitCount} commits! |
| 58 | + `) |
9 | 59 | process.exit(1) |
10 | 60 | } else { |
11 | | - console.log('Merge away McLoven') |
| 61 | + prettyLog('Merge away McLoven :)') |
12 | 62 | process.exit(0) |
13 | 63 | } |
14 | | - } |
15 | | -}) |
| 64 | + }, errorHandler) |
0 commit comments