refactor: update copyright year calculation

This commit is contained in:
Rainer Killinger
2021-02-22 10:23:39 +01:00
parent a87969813f
commit f13a9a2036

View File

@@ -348,6 +348,7 @@ export function checkNeededFiles(rules: Rules, path: string, replaceFlag: boolea
if (unignoresDocs) { if (unignoresDocs) {
consoleWarn(`'.npmignore' contains '!docs' and thus the package will contain the documentation. consoleWarn(`'.npmignore' contains '!docs' and thus the package will contain the documentation.
Consider creating a CI job to publish those files, rather than committing this folder to the npm repo.
Please double check that this is desired behavior since the docs can become huge: Please double check that this is desired behavior since the docs can become huge:
https://gitlab.com/openstapps/configuration/issues/11`); https://gitlab.com/openstapps/configuration/issues/11`);
@@ -439,7 +440,7 @@ export function checkScripts(rules: Rules, packageJson: PackageJSON, replaceFlag
consoleInfo(`Added '${scriptName}' script to 'package.json'.`); consoleInfo(`Added '${scriptName}' script to 'package.json'.`);
} else if (typeof scriptToCheck === 'string' && scriptToCheck !== rules.scripts[scriptName]) { } else if (typeof scriptToCheck === 'string' && scriptToCheck !== rules.scripts[scriptName]) {
consoleWarn(`Script '${scriptName}' in 'package.json' should be: consoleWarn(`Script '${scriptName}' in 'package.json' should be:
'${rules.scripts[scriptName].replace('\n', '\\n')}'.`); \"${rules.scripts[scriptName].replace('\n', '\\n')}\" .`);
} }
} }
@@ -543,7 +544,7 @@ export function checkCopyrightYears(path: PathLike, subDir: PathLike): void {
const execBuffer = execSync(`git --git-dir=${path}/.git --work-tree=${path} log --oneline --format='%cI' -- ${fileSystemObjectPath}`); const execBuffer = execSync(`git --git-dir=${path}/.git --work-tree=${path} log --oneline --format='%cI' -- ${fileSystemObjectPath}`);
const seen: number[] = []; const seen: number[] = [];
const changedYears = execBuffer let changedYears = execBuffer
.toString() .toString()
.split('\n') .split('\n')
.map((date) => parseInt(date.split('-')[0], 10)) .map((date) => parseInt(date.split('-')[0], 10))
@@ -558,6 +559,8 @@ export function checkCopyrightYears(path: PathLike, subDir: PathLike): void {
return true; return true;
}) })
.sort(); .sort();
changedYears = [changedYears[0], changedYears[changedYears.length -1]];
changedYears = [...new Set(changedYears)].sort();
const fileStats = lstatSync(fileSystemObjectPath); const fileStats = lstatSync(fileSystemObjectPath);
@@ -572,48 +575,37 @@ export function checkCopyrightYears(path: PathLike, subDir: PathLike): void {
let copyrightYearsString = ''; let copyrightYearsString = '';
for (const line of content) { for (const line of content) {
const match = line.match(/^ \* Copyright \(C\) ([0-9\-,\s]*) StApps$/); const match = line.match(/^ \* Copyright \(C\) ([0-9]{4}-[0-9]{4})|([0-9]{4}) StApps$/m);
const expectedMatchLength = 2; const expectedMatchLength = 3;
if (Array.isArray(match) && match.length === expectedMatchLength) { if (Array.isArray(match) && match.length === expectedMatchLength) {
copyrightYearsString = match[1]; // tslint:disable-next-line:no-magic-numbers
copyrightYearsString = match[1] ?? match[2];
} }
} }
if (copyrightYearsString === '') { if (copyrightYearsString === '') {
consoleWarn(`Copyright line for file '${fileSystemObjectPath}' could not be found!`); consoleWarn(`Copyright line for file '${fileSystemObjectPath}' could not be found!`);
} else { } else {
const copyrightYearsWithIntervals = copyrightYearsString.split(',') const copyrightYearsWithIntervals = copyrightYearsString.split('-')
.map((year) => year.trim()); .map((year) => year.trim());
const copyrightYears: number[] = []; const copyrightYears: number[] = copyrightYearsWithIntervals.map(year => parseInt(year, 10))
.sort();
for (const copyrightYear of copyrightYearsWithIntervals) { let copyrightYearNeedsUpdate = false;
if (copyrightYear.indexOf('-') >= 0) { if (typeof copyrightYears[0] !== 'undefined' && typeof changedYears[0] !== 'undefined') {
const [startString, endString] = copyrightYear.split('-'); copyrightYearNeedsUpdate = copyrightYears[0] !== changedYears[0];
const start = parseInt(startString.trim(), 10); }
const end = parseInt(endString.trim(), 10); if (typeof copyrightYears[1] !== 'undefined' && typeof changedYears[1] !== 'undefined' && !copyrightYearNeedsUpdate) {
for (let year = start; year <= end; year++) { copyrightYearNeedsUpdate = copyrightYears[1] !== changedYears[1];
copyrightYears.push(year); }
} if (copyrightYears.length !== changedYears.length) {
} else { copyrightYearNeedsUpdate = true;
copyrightYears.push(parseInt(copyrightYear, 10));
}
} }
for (const copyrightYear of copyrightYears) { if (copyrightYearNeedsUpdate) {
const idx = changedYears.indexOf(copyrightYear);
if (idx >= 0) {
changedYears.splice(idx, 1);
} else {
// tslint:disable-next-line:max-line-length
consoleWarn(`File '${join(subDir.toString(), fileSystemObject)}' wrongly states '${copyrightYear}' as year in the copyright line.`);
}
}
if (changedYears.length > 0) {
// tslint:disable-next-line:max-line-length // tslint:disable-next-line:max-line-length
consoleWarn(`File '${join(subDir.toString(), fileSystemObject)}' is missing '${changedYears.join(', ')}' as year(s) in the copyright line.`); consoleWarn(`File '${join(subDir.toString(), fileSystemObject)}' has to specify '${changedYears.join('-')}' as year(s) in the copyright line.`);
} }
} }
} else if (fileStats.isDirectory()) { } else if (fileStats.isDirectory()) {