From f13a9a20360fab5cd26a771b607c53b15fab97b0 Mon Sep 17 00:00:00 2001 From: Rainer Killinger Date: Mon, 22 Feb 2021 10:23:39 +0100 Subject: [PATCH] refactor: update copyright year calculation --- src/common.ts | 54 ++++++++++++++++++++++----------------------------- 1 file changed, 23 insertions(+), 31 deletions(-) diff --git a/src/common.ts b/src/common.ts index bae0c2a0..f8ff23f1 100644 --- a/src/common.ts +++ b/src/common.ts @@ -348,6 +348,7 @@ export function checkNeededFiles(rules: Rules, path: string, replaceFlag: boolea if (unignoresDocs) { 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: 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'.`); } else if (typeof scriptToCheck === 'string' && scriptToCheck !== rules.scripts[scriptName]) { 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 seen: number[] = []; - const changedYears = execBuffer + let changedYears = execBuffer .toString() .split('\n') .map((date) => parseInt(date.split('-')[0], 10)) @@ -558,6 +559,8 @@ export function checkCopyrightYears(path: PathLike, subDir: PathLike): void { return true; }) .sort(); + changedYears = [changedYears[0], changedYears[changedYears.length -1]]; + changedYears = [...new Set(changedYears)].sort(); const fileStats = lstatSync(fileSystemObjectPath); @@ -572,48 +575,37 @@ export function checkCopyrightYears(path: PathLike, subDir: PathLike): void { let copyrightYearsString = ''; for (const line of content) { - const match = line.match(/^ \* Copyright \(C\) ([0-9\-,\s]*) StApps$/); - const expectedMatchLength = 2; + const match = line.match(/^ \* Copyright \(C\) ([0-9]{4}-[0-9]{4})|([0-9]{4}) StApps$/m); + const expectedMatchLength = 3; if (Array.isArray(match) && match.length === expectedMatchLength) { - copyrightYearsString = match[1]; + // tslint:disable-next-line:no-magic-numbers + copyrightYearsString = match[1] ?? match[2]; } } if (copyrightYearsString === '') { consoleWarn(`Copyright line for file '${fileSystemObjectPath}' could not be found!`); } else { - const copyrightYearsWithIntervals = copyrightYearsString.split(',') + const copyrightYearsWithIntervals = copyrightYearsString.split('-') .map((year) => year.trim()); - const copyrightYears: number[] = []; + const copyrightYears: number[] = copyrightYearsWithIntervals.map(year => parseInt(year, 10)) + .sort(); - for (const copyrightYear of copyrightYearsWithIntervals) { - if (copyrightYear.indexOf('-') >= 0) { - const [startString, endString] = copyrightYear.split('-'); - const start = parseInt(startString.trim(), 10); - const end = parseInt(endString.trim(), 10); - for (let year = start; year <= end; year++) { - copyrightYears.push(year); - } - } else { - copyrightYears.push(parseInt(copyrightYear, 10)); - } + let copyrightYearNeedsUpdate = false; + if (typeof copyrightYears[0] !== 'undefined' && typeof changedYears[0] !== 'undefined') { + copyrightYearNeedsUpdate = copyrightYears[0] !== changedYears[0]; + } + if (typeof copyrightYears[1] !== 'undefined' && typeof changedYears[1] !== 'undefined' && !copyrightYearNeedsUpdate) { + copyrightYearNeedsUpdate = copyrightYears[1] !== changedYears[1]; + } + if (copyrightYears.length !== changedYears.length) { + copyrightYearNeedsUpdate = true; } - for (const copyrightYear of copyrightYears) { - 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) { + if (copyrightYearNeedsUpdate) { // 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()) {