mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-23 01:53:00 +00:00
feat: copyright notice year replacement
This commit is contained in:
@@ -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
|
* 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
|
* under the terms of the GNU General Public License as published by the Free
|
||||||
* Software Foundation, version 3.
|
* Software Foundation, version 3.
|
||||||
@@ -100,7 +100,7 @@ checkContributors(projectPath, packageJson);
|
|||||||
|
|
||||||
checkCIConfig(rules, projectPath);
|
checkCIConfig(rules, projectPath);
|
||||||
|
|
||||||
checkCopyrightYears(projectPath, 'src');
|
checkCopyrightYears(projectPath, 'src', commander.opts().replace);
|
||||||
|
|
||||||
const indentation = 2;
|
const indentation = 2;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/* eslint-disable unicorn/prefer-module */
|
/* 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
|
* 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
|
* under the terms of the GNU General Public License as published by the Free
|
||||||
* Software Foundation, version 3.
|
* Software Foundation, version 3.
|
||||||
@@ -15,7 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
import chalk from 'chalk';
|
import chalk from 'chalk';
|
||||||
import {execSync} from 'child_process';
|
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 path from 'path';
|
||||||
import {satisfies, valid} from 'semver';
|
import {satisfies, valid} from 'semver';
|
||||||
import {isDeepStrictEqual} from 'util';
|
import {isDeepStrictEqual} from 'util';
|
||||||
@@ -536,8 +536,13 @@ ${stringify(rules.ciConfig)}`);
|
|||||||
*
|
*
|
||||||
* @param projectPath Path to project root
|
* @param projectPath Path to project root
|
||||||
* @param checkPathFragment Subordinated directory to examine
|
* @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()));
|
const fileSystemObjects = readdirSync(path.resolve(projectPath.toString(), checkPathFragment.toString()));
|
||||||
|
|
||||||
for (const fileSystemObject of fileSystemObjects) {
|
for (const fileSystemObject of fileSystemObjects) {
|
||||||
@@ -548,7 +553,7 @@ export function checkCopyrightYears(projectPath: PathLike, checkPathFragment: Pa
|
|||||||
);
|
);
|
||||||
|
|
||||||
const execBuffer = execSync(
|
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[] = [];
|
const seen: number[] = [];
|
||||||
@@ -577,16 +582,14 @@ export function checkCopyrightYears(projectPath: PathLike, checkPathFragment: Pa
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const content = readFileSync(fileSystemObjectPath).toString().split('\n');
|
const content = readFileSync(fileSystemObjectPath).toString();
|
||||||
|
|
||||||
let copyrightYearsString = '';
|
let copyrightYearsString = '';
|
||||||
for (const line of content) {
|
const match = content.match(/^ \* Copyright \(C\) ([0-9]{4})-?([0-9]{4})?.*StApps\n/im);
|
||||||
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] ?? match[2];
|
copyrightYearsString = [match[1], match[2]].filter(year => typeof year !== 'undefined').join('-');
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (copyrightYearsString === '') {
|
if (copyrightYearsString === '') {
|
||||||
@@ -613,6 +616,19 @@ export function checkCopyrightYears(projectPath: PathLike, checkPathFragment: Pa
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (copyrightYearNeedsUpdate) {
|
if (copyrightYearNeedsUpdate) {
|
||||||
|
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(
|
consoleWarn(
|
||||||
`File '${path.join(
|
`File '${path.join(
|
||||||
checkPathFragment.toString(),
|
checkPathFragment.toString(),
|
||||||
@@ -621,8 +637,13 @@ export function checkCopyrightYears(projectPath: PathLike, checkPathFragment: Pa
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else if (fileStats.isDirectory()) {
|
} else if (fileStats.isDirectory()) {
|
||||||
checkCopyrightYears(projectPath, path.join(checkPathFragment.toString(), fileSystemObject));
|
checkCopyrightYears(
|
||||||
|
projectPath,
|
||||||
|
path.join(checkPathFragment.toString(), fileSystemObject),
|
||||||
|
replaceFlag,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user