1 | #!/usr/bin/env node
|
2 |
|
3 | 'use strict';
|
4 |
|
5 | const log = require('debug')('pre-git');
|
6 |
|
7 |
|
8 | const Promise = require('bluebird');
|
9 |
|
10 | var child = require('child_process');
|
11 | var label = 'pre-commit';
|
12 |
|
13 | function isForced() {
|
14 | log(label, 'arguments', process.argv);
|
15 | return process.argv.some(function (arg) {
|
16 | return arg === '-f' || arg === '--force';
|
17 | });
|
18 | }
|
19 |
|
20 | function errorMessage(err) {
|
21 | return err instanceof Error ? err.message : err;
|
22 | }
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | function 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 |
|
48 | function printNothingToDo() {
|
49 | console.log('');
|
50 | console.log(label, 'No changes detected, bailing out.');
|
51 | console.log('');
|
52 | }
|
53 |
|
54 | const hasUntrackedFiles = require('pre-git').hasUntrackedFiles;
|
55 | const run = require('pre-git').run;
|
56 | const runTask = run.bind(null, label);
|
57 |
|
58 | console.log('running bin/pre-commit.js script');
|
59 | haveChangesToCommit()
|
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 |
|