Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: run license script on pre-commit for staged files #136

Merged
merged 6 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lefthook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pre-commit:
parallel: true
commands:
generate-license:
run: pnpm run license "--files={staged_files}"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.g. for multiple changed files, this will run

pnpm zx scripts/license.mjs "--files=packages/design-system/src/utils/css.ts packages/design-system/src/utils/events.ts"

stage_fixed: true
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@changesets/cli": "2.27.9",
"@ls-lint/ls-lint": "2.3.0-beta.1",
"@swc/core": "1.7.36",
"lefthook": "1.9.3",
"rimraf": "5.0.10",
"syncpack": "13.0.0",
"taze": "0.17.2",
Expand Down
100 changes: 100 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 36 additions & 18 deletions scripts/license.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* governing permissions and limitations under the License.
*/

import { fs, glob, path } from 'zx';
import { fs, glob, path, argv } from 'zx';

const HEADER = `Copyright ${new Date().getFullYear()} Hypergiant Galactic Systems Inc. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -46,28 +46,46 @@ const COMMENT_STYLES = {
'.mdx': HTML_COMMENT_STYLE,
};

const files = await glob(['**/*.{js,ts,tsx,mjs,mdx,md,css}'], {
ignore: [
'**/node_modules/**',
'**/dist/**',
'**/README.md',
'**/LICENSE.md',
'**/CHANGELOG.md',
'**/.github/**/*.md',
],
});
function getFormattedHeader(fileExtension) {
const style = COMMENT_STYLES[fileExtension];
return `${style.start}${HEADER.split('\n').join(`\n${style.middle}`)}${style.end}`.replace(
/\s+\n/g,
'\n',
);
}

const filesToParse = argv.files?.split(' ');
const files = await glob(
filesToParse && filesToParse.length > 0
? filesToParse
: ['**/*.{js,ts,tsx,mjs,mdx,md,css}'],
{
ignore: [
'**/node_modules/**',
'**/dist/**',
'**/README.md',
'**/LICENSE.md',
'**/CHANGELOG.md',
'**/.github/**/*.md',
'**/*.yml',
orteth01 marked this conversation as resolved.
Show resolved Hide resolved
],
},
);

for (const file of files) {
const style = COMMENT_STYLES[path.extname(file)];
const header = getFormattedHeader(path.extname(file));

let contents = fs.readFileSync(file, 'utf8');
let header =
style.start + HEADER.split('\n').join(`\n${style.middle}`) + style.end;
if (!/Copyright \d+ Hypergiant/.test(contents)) {
const interpreterDirective = contents.match(/^#!.*$/m)?.[0];

header = header.replace(/\s+\n/g, '\n');
if (interpreterDirective) {
contents = contents.replace(interpreterDirective, '').trimStart();
contents = `${interpreterDirective}\n\n${header}\n${contents}`;
} else {
contents = `${header}\n${contents}`;
}

if (!/Copyright \d+ Hypergiant/.test(contents)) {
// TODO: check for hashbang for things like zx scripts
contents = `${header}\n${contents}`;
fs.writeFileSync(file, contents);
}
}
Loading