forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckCopyrightHeaders.js
executable file
·150 lines (132 loc) · 3.04 KB
/
checkCopyrightHeaders.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
const fs = require('fs');
const {execSync} = require('child_process');
const {isBinaryFileSync} = require('isbinaryfile');
const getFileContents = path => fs.readFileSync(path, {encoding: 'utf-8'});
const isDirectory = path => fs.lstatSync(path).isDirectory();
const createRegExp = pattern => new RegExp(pattern);
// Important: this patterns must be in sync with internal Facebook tools
const GENERIC_IGNORED_EXTENSIONS = [
'lock',
'patch',
'exe',
'bin',
'cfg',
'config',
'conf',
'html',
'md',
'markdown',
'opam',
'osm',
'descr',
'rst',
'json',
'key',
'ini',
'plist',
'snap',
'svg',
'txt',
'xcodeproj',
'xcscheme',
'xml',
'yaml',
'yml',
'textile',
'tsv',
'csv',
'pem',
'csr',
'der',
'crt',
'cert',
'cer',
'p7b',
'iml',
'org',
'podspec',
'modulemap',
'pch',
'lproj',
'xcworkspace',
'storyboard',
'tvml',
'xib',
'pbxproj',
'xcworkspacedata',
'xccheckout',
'xcsettings',
'strings',
'ipynb',
'htm',
'toml',
].map(extension => createRegExp(`\.${extension}$`));
const GENERIC_IGNORED_PATTERNS = [
'(^|/)\\.[^/]+(/|$)',
'third[_\\-. ]party/',
'^node[_\\-. ]modules/',
'gradlew\\.bat$',
'gradlew$',
'gradle/wrapper/',
'.idea/',
'__init__\\.py$',
'^Setup.hs$',
'^(readme|README|Readme)\\..*$',
'Cargo\\.toml$',
'^Cartfile.*$',
'^.*\\.xcodeproj/$',
'^.*\\.xcworkspace/$',
'^.*\\.lproj/$',
'^.*\\.bundle/$',
'^MANIFEST\\.in$',
].map(createRegExp);
const CUSTOM_IGNORED_PATTERNS = [
'\\.(example|map)$',
'^examples/.*',
'^flow-typed/.*',
'^packages/expect/src/jasmineUtils\\.ts$',
'^packages/jest-config/src/vendor/jsonlint\\.js$',
'^packages/jest-diff/src/cleanupSemantic\\.ts$',
].map(createRegExp);
const IGNORED_PATTERNS = [
...GENERIC_IGNORED_EXTENSIONS,
...GENERIC_IGNORED_PATTERNS,
...CUSTOM_IGNORED_PATTERNS,
];
const INCLUDED_PATTERNS = [
// Any file with an extension
/\.[^/]+$/,
];
const COPYRIGHT_HEADER =
'Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.';
function needsCopyrightHeader(file) {
const contents = getFileContents(file);
return contents.trim().length > 0 && !contents.includes(COPYRIGHT_HEADER);
}
function check() {
const allFiles = execSync('git ls-files', {encoding: 'utf-8'})
.trim()
.split('\n');
const invalidFiles = allFiles.filter(
file =>
INCLUDED_PATTERNS.some(pattern => pattern.test(file)) &&
!IGNORED_PATTERNS.some(pattern => pattern.test(file)) &&
!isDirectory(file) &&
!isBinaryFileSync(file) &&
needsCopyrightHeader(file)
);
if (invalidFiles.length > 0) {
console.log(`Facebook copyright header check failed for the following files:
${invalidFiles.join('\n ')}
Please include the header or blacklist the files in \`scripts/checkCopyrightHeaders.js\``);
process.exit(1);
}
}
check();