forked from angular/tsickle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_format.js
59 lines (51 loc) · 1.61 KB
/
check_format.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
49
50
51
52
53
54
55
56
57
58
59
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
const childProcess = require('child_process');
const fs = require('fs');
const glob = require('glob');
const prettier = require('prettier');
const CLANG_FORMAT = 'node_modules/.bin/clang-format';
/** Returns the clang-format formatted text of an input file. */
function clangFormat(path, text) {
return childProcess.execFileSync(CLANG_FORMAT, [path], {encoding: 'utf8'});
}
/** Returns the prettier formatted text of an input file. */
function prettierFormat(path, text) {
return prettier.format(text, {filepath: path});
}
/**
* Checks that a path is formatted.
* @return new text of file if it needs formatting or null otherwise.
*/
function checkFile(path) {
const formatter = path.match(/\.md$/) ? prettierFormat : clangFormat;
const source = fs.readFileSync(path, 'utf8');
const formatted = formatter(path, source);
if (source !== formatted) {
return formatted;
}
return null;
}
function main(args) {
const fix = args[0] === '--fix';
const sourceFiles = glob.sync('{*.md,*.js,src/**/*.[jt]s,test/**/*.[jt]s}');
for (const path of sourceFiles) {
const newText = checkFile(path);
if (newText != null) {
if (fix) {
fs.writeFileSync(path, newText);
console.log(`wrote ${path}`);
} else {
console.error(`${path} not formatted; run\n\tnode check_format.js --fix`);
return 1;
}
}
}
return 0;
}
process.exitCode = main(process.argv.slice(2));