Skip to content

Commit e85193e

Browse files
committed
make copyrightheader script suitable for any file type; support .sql
1 parent 58d35d6 commit e85193e

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

lib/bin/check-file-headers.js

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,26 @@
99

1010
const fs = require('fs');
1111

12-
const header = `// Copyright 2017 ODK Central Developers
13-
// See the NOTICE file at the top-level directory of this distribution and at
14-
// https://github.com/getodk/central-backend/blob/master/NOTICE.
15-
// This file is part of ODK Central. It is subject to the license terms in
16-
// the LICENSE file found in the top-level directory of this distribution and at
17-
// https://www.apache.org/licenses/LICENSE-2.0. No part of ODK Central,
18-
// including this file, may be copied, modified, propagated, or distributed
19-
// except according to the terms contained in the LICENSE file.
12+
const header = `Copyright 2017 ODK Central Developers
13+
See the NOTICE file at the top-level directory of this distribution and at
14+
https://github.com/getodk/central-backend/blob/master/NOTICE.
15+
This file is part of ODK Central. It is subject to the license terms in
16+
the LICENSE file found in the top-level directory of this distribution and at
17+
https://www.apache.org/licenses/LICENSE-2.0. No part of ODK Central,
18+
including this file, may be copied, modified, propagated, or distributed
19+
except according to the terms contained in the LICENSE file.
2020
`;
21-
const thisYearHeader = header.replace('2017', new Date().getFullYear());
21+
const thisYearsCopyright = `Copyright ${new Date().getFullYear()}`;
22+
const thisYearHeader = header.replace('Copyright 2017', thisYearsCopyright);
23+
const headerCommentPrefixByFileExt = { js: '//', sql: '--' };
24+
const headerByFiletype = Object.fromEntries(
25+
Object.entries(headerCommentPrefixByFileExt)
26+
.map(([ext, commentPrefix]) => [ext, thisYearHeader.split('\n')
27+
.map(line => (line.length ? `${commentPrefix} ${line}` : '')).join('\n')])
28+
);
29+
const headerableFileTypes = Object.keys(headerCommentPrefixByFileExt);
30+
const isHeaderableFile = (filename) => headerableFileTypes.some(ext => filename.endsWith(`.${ext}`));
31+
2232

2333
const skip = [
2434
'test/',
@@ -35,7 +45,7 @@ process.stdin.on('data', (data) => {
3545
process.stdin.on('end', () => {
3646
const files = stdin
3747
.split('\n')
38-
.filter(f => f.endsWith('.js'))
48+
.filter(f => isHeaderableFile(f))
3949
.filter(f => !skip.some(skipEntry => {
4050
if (skipEntry.endsWith('.js')) return f === skipEntry;
4151
if (skipEntry.endsWith('/')) return f.startsWith(skipEntry);
@@ -48,13 +58,14 @@ process.stdin.on('end', () => {
4858

4959
for (const entryPath of files) {
5060
const contents = fs.readFileSync(entryPath).toString();
51-
const withConsistentYear = contents.replace(/(Copyright 20)\d\d/, '$117');
52-
if (!withConsistentYear.startsWith(header)) {
61+
const appropriateHeader = headerByFiletype[entryPath.split('.').reverse()[0]];
62+
const withConsistentYear = contents.replace(/Copyright 20\d\d/, thisYearsCopyright);
63+
if (!withConsistentYear.startsWith(appropriateHeader)) {
5364
console.error(entryPath);
5465
missing = true;
5566

5667
if (mode === 'fix') {
57-
fs.writeFileSync(entryPath, `${thisYearHeader}\n${contents}`);
68+
fs.writeFileSync(entryPath, `${appropriateHeader}\n${contents}`);
5869
}
5970
}
6071
}
@@ -64,7 +75,7 @@ process.stdin.on('end', () => {
6475
console.error('\nThe files above have been changed to include the required header.');
6576
} else {
6677
console.error('\nThe files above do not have the expected file header.');
67-
console.error('\nPlease re-run this script with the --fix flag, or add the following header manually:');
78+
console.error(`\nPlease re-run this script with the --fix flag, or add the following header manually,\nline-commenting each line with leading ${Object.values(headerCommentPrefixByFileExt).map(el => `"${el} "`).join(' or ')} as appropriate for the file type:`);
6879
console.error(`\n${thisYearHeader}`);
6980
process.exit(1);
7081
}

0 commit comments

Comments
 (0)