Files
cloudbeaver/webapp/scripts/license-check.mjs
T
2a610c3f50 Cb 4911 add license headers with git hooks (#2504)
* adds husky with license script

* fix: license can pass file not fitted to provided FILES_PATH pattern

* license: notify user for updated files with new license header

* moves husky to frontend directory

* adds check for unstaged files with license

* change pre commit to pre push

* test: remove license from value panel

* Revert "test: remove license from value panel"

This reverts commit b690b75c6b.

* adds postinstall

* test: remove license from value panel [2]

* Revert "test: remove license from value panel [2]"

This reverts commit ea86fd3c57.

* sets back precommit

* CB-4911 validate missed license

* CB-4911 fix linter

* CB-4911 check license line by line

* CB-4911 feat: use node for license check

---------

Co-authored-by: s.teleshev <s.teleshev@mu.se>
Co-authored-by: Aleksei Potsetsuev <wrouds@gmail.com>
2024-04-10 14:08:20 +03:00

66 lines
1.7 KiB
JavaScript

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);
}