feat: copyright notice year replacement

This commit is contained in:
Rainer Killinger
2022-05-31 14:26:21 +02:00
parent 62c60448aa
commit 17a796e97b
2 changed files with 41 additions and 20 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2021 StApps
* Copyright (C) 2018-2022 Open StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
@@ -100,7 +100,7 @@ checkContributors(projectPath, packageJson);
checkCIConfig(rules, projectPath);
checkCopyrightYears(projectPath, 'src');
checkCopyrightYears(projectPath, 'src', commander.opts().replace);
const indentation = 2;

View File

@@ -1,6 +1,6 @@
/* eslint-disable unicorn/prefer-module */
/*
* Copyright (C) 2018, 2019 StApps
* Copyright (C) 2018-2022 Open StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
@@ -15,7 +15,7 @@
*/
import chalk from 'chalk';
import {execSync} from 'child_process';
import {copyFileSync, existsSync, lstatSync, PathLike, readdirSync, readFileSync} from 'fs';
import {copyFileSync, existsSync, lstatSync, PathLike, readdirSync, readFileSync, writeFileSync} from 'fs';
import path from 'path';
import {satisfies, valid} from 'semver';
import {isDeepStrictEqual} from 'util';
@@ -536,8 +536,13 @@ ${stringify(rules.ciConfig)}`);
*
* @param projectPath Path to project root
* @param checkPathFragment Subordinated directory to examine
* @param replaceFlag Whether or not to replace NYC configuration
*/
export function checkCopyrightYears(projectPath: PathLike, checkPathFragment: PathLike): void {
export function checkCopyrightYears(
projectPath: PathLike,
checkPathFragment: PathLike,
replaceFlag: boolean,
): void {
const fileSystemObjects = readdirSync(path.resolve(projectPath.toString(), checkPathFragment.toString()));
for (const fileSystemObject of fileSystemObjects) {
@@ -548,7 +553,7 @@ export function checkCopyrightYears(projectPath: PathLike, checkPathFragment: Pa
);
const execBuffer = execSync(
`git --git-dir=${projectPath}/.git --work-tree=${projectPath} log --oneline --format='%cI' -- ${fileSystemObjectPath}`,
`git --git-dir=${projectPath}/.git --work-tree=${projectPath} log --date=short --pretty='%ad' -- ${fileSystemObjectPath}`,
);
const seen: number[] = [];
@@ -577,16 +582,14 @@ export function checkCopyrightYears(projectPath: PathLike, checkPathFragment: Pa
continue;
}
const content = readFileSync(fileSystemObjectPath).toString().split('\n');
const content = readFileSync(fileSystemObjectPath).toString();
let copyrightYearsString = '';
for (const line of content) {
const match = line.match(/^ \* Copyright \(C\) ([0-9]{4}-[0-9]{4})|([0-9]{4}) StApps$/m);
const expectedMatchLength = 3;
const match = content.match(/^ \* Copyright \(C\) ([0-9]{4})-?([0-9]{4})?.*StApps\n/im);
const expectedMatchLength = 2;
if (Array.isArray(match) && match.length === expectedMatchLength) {
copyrightYearsString = match[1] ?? match[2];
}
if (Array.isArray(match) && match.length >= expectedMatchLength) {
copyrightYearsString = [match[1], match[2]].filter(year => typeof year !== 'undefined').join('-');
}
if (copyrightYearsString === '') {
@@ -613,16 +616,34 @@ export function checkCopyrightYears(projectPath: PathLike, checkPathFragment: Pa
}
if (copyrightYearNeedsUpdate) {
consoleWarn(
`File '${path.join(
checkPathFragment.toString(),
fileSystemObject,
)}' has to specify '${changedYears.join('-')}' as year(s) in the copyright line.`,
);
if (replaceFlag) {
const correctedContent = content.replace(
/^( \* Copyright \(C\) )(.*)(Open )?(StApps)$/gim,
`$1${changedYears.join('-')} Open StApps`,
);
writeFileSync(fileSystemObjectPath, correctedContent, {mode: fileStats.mode});
consoleWarn(
`Corrected copyright years in '${path.join(
checkPathFragment.toString(),
fileSystemObject,
)}' to ${changedYears.join('-')}'`,
);
} else {
consoleWarn(
`File '${path.join(
checkPathFragment.toString(),
fileSystemObject,
)}' has to specify '${changedYears.join('-')}' as year(s) in the copyright line.`,
);
}
}
}
} else if (fileStats.isDirectory()) {
checkCopyrightYears(projectPath, path.join(checkPathFragment.toString(), fileSystemObject));
checkCopyrightYears(
projectPath,
path.join(checkPathFragment.toString(), fileSystemObject),
replaceFlag,
);
}
}
}