-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix copyright check script (#344)
**Summary** This updates our copyright check script to run on all _valid_ files, instead of those added in a recent commit. You can run this check manually via `npm run copyright:check`. This is also ran via github actions. Additionally, this adds a fix flag (`npm run copyright:check --fix`) that will automatically prepend copyright information to files that are missing it. **Testing** - [x] Make sure github actions fails build (on first commit) - [x] Make sure github actions passed on next build (after running `--fix`) - [x] Make sure copyright appears on files
- Loading branch information
1 parent
6409522
commit 3a1e322
Showing
4 changed files
with
46 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,49 @@ | ||
import { exec } from "child_process"; | ||
import { readFileSync } from "fs"; | ||
import { readFileSync, writeFileSync } from "fs"; | ||
import glob from "glob"; | ||
|
||
// These files will not be checked for copyright information | ||
const filesToIgnore = [ | ||
"**/dist/**", | ||
"**/node_modules/**", | ||
"**/out/**", | ||
"**/test/**", | ||
"*.config.js", | ||
"*.test.tsx?", | ||
"tools/**", | ||
]; | ||
|
||
const INCLUDED_FILE_TYPES = /.*\.(mjs|js|ts|tsx|jsx)$/; | ||
const EXCLUDED_FILE_TYPES = /.*(\.test\.tsx?|check-copyright\.mjs)$/; | ||
const COPYRIGHT_REGEX = /^\/\/ Copyright © ([0-9-\s]+), SAS Institute/; | ||
const COPYRIGHT_TEMPLATE = `// Copyright © ${new Date().getFullYear()}, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
const processChanges = (changedFiles) => { | ||
const filesToCheck = changedFiles | ||
.map((file) => file.trim()) | ||
.filter( | ||
(file) => | ||
INCLUDED_FILE_TYPES.test(file) && !EXCLUDED_FILE_TYPES.test(file) | ||
); | ||
`; | ||
|
||
const processChanges = (filesToCheck, fix = false) => { | ||
let invalidFiles = []; | ||
filesToCheck.map((file) => { | ||
const fileContents = readFileSync(file); | ||
if (!COPYRIGHT_REGEX.test(fileContents.toString())) { | ||
invalidFiles.push(file); | ||
if (fix) { | ||
writeFileSync(file, `${COPYRIGHT_TEMPLATE}${fileContents}`); | ||
} | ||
} | ||
}); | ||
|
||
if (invalidFiles.length > 0) { | ||
console.log("The following files are missing copyright information"); | ||
console.log( | ||
fix | ||
? "The following files have been updated with copyright information" | ||
: "The following files are missing copyright information" | ||
); | ||
console.log(invalidFiles.map((file) => `- ${file}`).join("\n")); | ||
process.exit(1); | ||
} | ||
}; | ||
|
||
await exec( | ||
`git diff origin/HEAD --diff-filter=A --name-only`, | ||
async (error, stdout, stderr) => processChanges(stdout.split("\n")) | ||
await processChanges( | ||
glob.sync("**/*.{mjs,js,ts,tsx,jsx}", { | ||
ignore: filesToIgnore, | ||
}), | ||
process.env.npm_config_fix || false | ||
); |