Skip to content

Commit

Permalink
Add missing license headers + verify:source script.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrause committed Jan 3, 2025
1 parent e755c89 commit 30b21b6
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 8 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ jobs:
cache: 'npm'
- run: npm ci
#- run: npm run build --if-present
- run: npm run verify verify:build
- run: npm test
77 changes: 69 additions & 8 deletions scripts/verify.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@

import { dedent } from 'ts-dedent';
import { parseArgs } from 'node:util';
import * as pathUtil from 'node:path';
import { fileURLToPath } from 'node:url';
import { type PathLike, createReadStream } from 'node:fs';
import * as fs from 'node:fs/promises';
import { exec } from 'node:child_process';
import { AsyncLocalStorage } from 'node:async_hooks';
Expand Down Expand Up @@ -33,15 +35,44 @@ type ScriptArgs = {
// Common
//

const getCurrentGitBranch = () => new Promise<string>((resolve, reject) => {
return exec('git rev-parse --abbrev-ref HEAD', (err, stdout, stderr) => {
if (err) {
reject(`Failed to determine current git branch: ${err}`);
} else if (typeof stdout === 'string') {
resolve(stdout.trim());
// Return a relative path to the given absolute path, relative to the current CWD
const rel = (absolutePath: string): string => {
return pathUtil.relative(process.cwd(), absolutePath);
};

const readFirstNBytes = async (path: PathLike, n: number): Promise<Buffer> => {
const chunks = [];
for await (let chunk of createReadStream(path, { start: 0, end: n-1 })) {
chunks.push(chunk);
}
return Buffer.concat(chunks);
};

const readSourceFiles = async (rootPath: string) => {
const paths = await fs.readdir(rootPath, { recursive: true });

const files: Array<string> = [];
for await (const pathRel of paths) {
const path = pathUtil.resolve(pathUtil.join(rootPath, pathRel));
const stat = await fs.lstat(path);
if (!stat.isFile()) { continue; }

const fileName = pathUtil.basename(path);
const fileExt = pathUtil.extname(fileName);
const fileExtFull = fileExt === '' ? '' : '.' + fileName.split('.').slice(1).join('.');
const isSourceFile = [
['.js', '.jsx', '.ts', '.tsx', '.css', '.scss'].includes(fileExt),
!['.d.ts'].includes(fileExtFull),
!path.includes('node_modules'),
].every(Boolean);

if (isSourceFile) {
files.push(pathUtil.resolve(path));
}
});
});
}

return files;
};

const readDistCss = async () => {
const path = fileURLToPath(new URL('../dist/lib.css', import.meta.url));
Expand All @@ -53,6 +84,34 @@ const readDistCss = async () => {
// Commands
//

export const runVerifySource = async (args: ScriptArgs) => {
const { logger } = getServices();

const rootPath = fileURLToPath(new URL('..', import.meta.url));

const filePaths = [
pathUtil.resolve('./plopfile.ts'),
...await readSourceFiles(pathUtil.join(rootPath, 'src')),
];

for (const filePath of filePaths) {
const shouldIgnore = [
filePath.startsWith(pathUtil.join(rootPath, 'src/styling/lib')),
filePath.startsWith(pathUtil.join(rootPath, 'src/styling/generated')),
filePath === pathUtil.join(rootPath, 'src/assets/icons/_icons.ts'),
].some(Boolean);

if (shouldIgnore) { continue; }

const firstChunk = (await readFirstNBytes(filePath, 100)).toString();
const hasLicenseHeader = firstChunk.includes('Copyright (c) Fortanix');

if (!hasLicenseHeader) {
logger.error(`Missing license header in ${filePath}`);
}
}
};

export const runVerifyBuild = async (args: ScriptArgs) => {
const { logger } = getServices();

Expand All @@ -79,6 +138,7 @@ const printUsage = () => {
Usage: verify.ts <cmd> <...args>
Commands:
- verify:source
- verify:build
`);
};
Expand Down Expand Up @@ -111,6 +171,7 @@ export const run = async (argsRaw: Array<string>): Promise<void> => {

const argsForCommand: ScriptArgs = { ...args, positionals: args.positionals.slice(1) };
switch (command) {
case 'verify:source': await runVerifySource(argsForCommand); break;
case 'verify:build': await runVerifyBuild(argsForCommand); break;
default:
logger.error(`Unknown command '${command}'\n`);
Expand Down
3 changes: 3 additions & 0 deletions src/components/forms/controls/Switch/Switch.module.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/* Copyright (c) Fortanix, Inc.
|* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
|* the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */

@use '../../../../styling/defs.scss' as bk;

Expand Down
3 changes: 3 additions & 0 deletions src/components/forms/controls/Switch/Switch.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/* Copyright (c) Fortanix, Inc.
|* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
|* the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import type { Meta, StoryObj } from '@storybook/react';
import * as React from 'react';
Expand Down
3 changes: 3 additions & 0 deletions src/components/forms/controls/Switch/Switch.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/* Copyright (c) Fortanix, Inc.
|* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
|* the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { classNames as cx, type ComponentProps } from '../../../../util/componentUtil.ts';
import * as React from 'react';
Expand Down
3 changes: 3 additions & 0 deletions src/layouts/util/Scroller.module.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/* Copyright (c) Fortanix, Inc.
|* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
|* the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */

@layer baklava.overrides {
.bk-util-scroller {
Expand Down
3 changes: 3 additions & 0 deletions src/layouts/util/Scroller.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/* Copyright (c) Fortanix, Inc.
|* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
|* the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { classNames as cx } from '../../util/componentUtil.ts';

Expand Down
3 changes: 3 additions & 0 deletions src/styling/global/reset.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/* Copyright (c) Fortanix, Inc.
|* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
|* the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */

@use 'sass:meta';

Expand Down
3 changes: 3 additions & 0 deletions src/styling/layers.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/* Copyright (c) Fortanix, Inc.
|* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
|* the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* Cascade layer ordering */

Expand Down

0 comments on commit 30b21b6

Please sign in to comment.