forked from jenkinsci/blueocean-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre-commit.js
executable file
·48 lines (37 loc) · 1.51 KB
/
pre-commit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#! /usr/bin/env node
'use strict';
const child_process = require('child_process');
const os = require('os');
const fs = require('fs');
// --[ Helpers ]------------------------------------------------------------------------------
function execSync(command, ...args) {
let result = child_process.spawnSync(command, args || [], { encoding: 'utf8' });
// Docs say errors are thrown, but apparently not
if (result.error) {
throw new Error(JSON.stringify(result.error, null, 4));
} else if (result.status !== 0) {
throw new Error(result.stderr);
}
return result.stdout;
}
// --[ Main ]---------------------------------------------------------------------------------
try {
// Get staged files from git
const stagedFiles = execSync('git', 'diff', '--name-only', '--staged').split(os.EOL);
// Prune out any whitespace lines or deleted files (which show up in --staged)
const filteredFiles = stagedFiles.filter(filePath => filePath && fs.existsSync(filePath));
if (filteredFiles.length === 0) {
console.log('No staged files to format.');
return;
}
// Reformat files
const prettyResult = execSync('node', 'bin/pretty.js', '--fix-show-changed', ...filteredFiles);
const formattedFiles = prettyResult.split(os.EOL).filter(filePath => !!filePath);
// Re-stage formatted files
const gitOutput = execSync('git', 'add', ...formattedFiles);
console.log(gitOutput);
} catch (err) {
console.error(err);
process.exitCode = -1;
return;
}