Skip to content

Commit

Permalink
fix: fix copyright check script (#344)
Browse files Browse the repository at this point in the history
**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
scottdover authored Jun 19, 2023
1 parent 6409522 commit 3a1e322
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 15 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). If you introduce breaking changes, please group them together in the "Changed" section using the **BREAKING:** prefix.

## [Unreleased]

### Added

- Added support for `npm run copyright:check --fix`. This automatically prepends files with the correct copyright information.

### Fixed

- Fixed an issue with `npm run copyright:check` where some files were not being validated.

## [v1.0.0] - 2023-06-16

### Added
Expand Down
3 changes: 3 additions & 0 deletions client/src/components/ExtensionContext.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright © 2023, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { ExtensionContext } from "vscode";

let context: ExtensionContext;
Expand Down
3 changes: 3 additions & 0 deletions client/src/connection/rest/identities.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright © 2023, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import axios from "axios";

export interface User {
Expand Down
45 changes: 30 additions & 15 deletions tools/check-copyright.mjs
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
);

0 comments on commit 3a1e322

Please sign in to comment.