Skip to content

Commit

Permalink
CB-4911 feat: use node for license check
Browse files Browse the repository at this point in the history
  • Loading branch information
Wroud committed Apr 9, 2024
1 parent 2f42363 commit 7319c10
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 37 deletions.
4 changes: 1 addition & 3 deletions .vscode/license.code-snippets
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
" *",
" * Licensed under the Apache License, Version 2.0.",
" * you may not use this file except in compliance with the License.",
" */",
"",
""
" */"
]
}
}
11 changes: 1 addition & 10 deletions webapp/.husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,10 +1 @@
#!/usr/bin/env sh
source ./webapp/scripts/license.sh

validate_missed_license "/*
* CloudBeaver - Cloud Database Manager
* Copyright (C) 2020-2024 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/" "^.+\.(ts|tsx|css|scss)$"
node ./webapp/scripts/license-check.mjs ./.vscode/license.code-snippets license
65 changes: 65 additions & 0 deletions webapp/scripts/license-check.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { exec, execSync } from 'child_process';
import fs from 'fs';
import { resolve } from 'path';
import { createInterface } from 'readline';

const snippetPath = resolve(process.argv[2]);
const snippetName = process.argv[3];
const snippets = JSON.parse(fs.readFileSync(snippetPath, 'utf8'));
const licenseSnippet = snippets[snippetName];

const scopes = licenseSnippet.scope.split(',');
const license = licenseSnippet.body;
const extensions = new Set();

for (const scope of scopes) {
switch (scope.trim()) {
case 'typescript':
extensions.add('.ts');
break;
case 'typescriptreact':
extensions.add('.tsx');
break;
case 'javascript':
extensions.add('.js');
break;
case 'javascriptreact':
extensions.add('.jsx');
break;
case 'css':
extensions.add('.css');
break;
case 'scss':
extensions.add('.scss');
break;
}
}

const output = exec('git diff --cached --name-only --diff-filter=ACMR');
const rl = createInterface(output.stdout);
const invalidFiles = [];

for await (const line of rl) {
const extension = line.slice(line.lastIndexOf('.'));
if (extensions.has(extension)) {
const fileRl = createInterface(fs.createReadStream(line));

let fileLineIndex = 0;
for await (const fileLine of fileRl) {
if (fileLine !== license[fileLineIndex]) {
invalidFiles.push(line);
break;
}
fileLineIndex++;
if (fileLineIndex === license.length) {
break;
}
}
}
}

if (invalidFiles.length > 0) {
execSync('git restore --staged ' + invalidFiles.join(' '));
process.stdout.write('Found files without license header. Please add license to all unstaged files.');
process.exit(1);
}
24 changes: 0 additions & 24 deletions webapp/scripts/license.sh

This file was deleted.

0 comments on commit 7319c10

Please sign in to comment.