UNPKG

1.97 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3'use strict';
4
5const log = require('debug')('pre-git');
6
7/* jshint -W079 */
8const Promise = require('bluebird');
9
10var child = require('child_process');
11var label = 'pre-commit';
12
13function isForced() {
14 log(label, 'arguments', process.argv);
15 return process.argv.some(function (arg) {
16 return arg === '-f' || arg === '--force';
17 });
18}
19
20function errorMessage(err) {
21 return err instanceof Error ? err.message : err;
22}
23
24// should we exit if there are no changes to commit?
25
26// resolved => there are changes to commit
27// rejected => might be an Error or nothing.
28// if nothing => not changes to commit
29function haveChangesToCommit() {
30 return new Promise(function (resolve, reject) {
31 if (isForced()) {
32 console.log('forcing pre-commit execution');
33 return resolve();
34 }
35
36 child.exec('git status --porcelain', function changes(err, status) {
37 if (err) {
38 console.error(label, 'Failed to check for changes. Cannot run the tests.');
39 console.error(err);
40 return process.exit(1);
41 }
42
43 return status.trim().length ? resolve() : reject();
44 });
45 });
46}
47
48function printNothingToDo() {
49 console.log('');
50 console.log(label, 'No changes detected, bailing out.');
51 console.log('');
52}
53
54const hasUntrackedFiles = require('pre-git').hasUntrackedFiles;
55const run = require('pre-git').run;
56const runTask = run.bind(null, label);
57
58console.log('running bin/pre-commit.js script');
59haveChangesToCommit()
60 .then(hasUntrackedFiles)
61 .then((has) => {
62 if (has) {
63 const message = 'Has untracked files in folder.\n' +
64 'Please delete or ignore them.';
65 return Promise.reject(new Error(message));
66 }
67 })
68 .then(runTask, (err) => {
69 if (err) {
70 console.error(errorMessage(err));
71 process.exit(-1);
72 }
73 printNothingToDo();
74 })
75 .catch((err) => {
76 console.error(label, 'A problem');
77 console.error(errorMessage(err));
78 process.exit(-1);
79 })
80 .done();
81