mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-16 09:36:44 +00:00
ci: Fix prettier auto-formatting (no-changelog) (#7063)
This commit is contained in:
committed by
GitHub
parent
fa3d7070b0
commit
a693b29134
104
.github/scripts/check-tests.mjs
vendored
104
.github/scripts/check-tests.mjs
vendored
@@ -2,90 +2,100 @@ import fs from 'fs';
|
||||
import path from 'path';
|
||||
import util from 'util';
|
||||
import { exec } from 'child_process';
|
||||
import { glob } from "glob";
|
||||
import { glob } from 'glob';
|
||||
import ts from 'typescript';
|
||||
|
||||
const readFileAsync = util.promisify(fs.readFile);
|
||||
const execAsync = util.promisify(exec);
|
||||
|
||||
const filterAsync = async (asyncPredicate, arr) => {
|
||||
const filterResults = await Promise.all(arr.map(async item => ({
|
||||
item,
|
||||
shouldKeep: await asyncPredicate(item)
|
||||
})));
|
||||
const filterResults = await Promise.all(
|
||||
arr.map(async (item) => ({
|
||||
item,
|
||||
shouldKeep: await asyncPredicate(item),
|
||||
})),
|
||||
);
|
||||
|
||||
return filterResults.filter(({shouldKeep}) => shouldKeep).map(({item}) => item);
|
||||
}
|
||||
return filterResults.filter(({ shouldKeep }) => shouldKeep).map(({ item }) => item);
|
||||
};
|
||||
|
||||
const isAbstractClass = (node) => {
|
||||
if (ts.isClassDeclaration(node)) {
|
||||
return node.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.AbstractKeyword) || false;
|
||||
return (
|
||||
node.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.AbstractKeyword) || false
|
||||
);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
const isAbstractMethod = (node) => {
|
||||
return ts.isMethodDeclaration(node) && Boolean(node.modifiers?.find((modifier) => modifier.kind === ts.SyntaxKind.AbstractKeyword));
|
||||
}
|
||||
};
|
||||
|
||||
const isAbstractMethod = (node) => {
|
||||
return (
|
||||
ts.isMethodDeclaration(node) &&
|
||||
Boolean(node.modifiers?.find((modifier) => modifier.kind === ts.SyntaxKind.AbstractKeyword))
|
||||
);
|
||||
};
|
||||
|
||||
// Function to check if a file has a function declaration, function expression, object method or class
|
||||
const hasFunctionOrClass = async filePath => {
|
||||
const hasFunctionOrClass = async (filePath) => {
|
||||
const fileContent = await readFileAsync(filePath, 'utf-8');
|
||||
const sourceFile = ts.createSourceFile(filePath, fileContent, ts.ScriptTarget.Latest, true);
|
||||
|
||||
let hasFunctionOrClass = false;
|
||||
const visit = node => {
|
||||
const visit = (node) => {
|
||||
if (
|
||||
ts.isFunctionDeclaration(node)
|
||||
|| ts.isFunctionExpression(node)
|
||||
|| ts.isArrowFunction(node)
|
||||
|| (ts.isMethodDeclaration(node) && !isAbstractMethod(node))
|
||||
|| (ts.isClassDeclaration(node) && !isAbstractClass(node))
|
||||
ts.isFunctionDeclaration(node) ||
|
||||
ts.isFunctionExpression(node) ||
|
||||
ts.isArrowFunction(node) ||
|
||||
(ts.isMethodDeclaration(node) && !isAbstractMethod(node)) ||
|
||||
(ts.isClassDeclaration(node) && !isAbstractClass(node))
|
||||
) {
|
||||
hasFunctionOrClass = true;
|
||||
}
|
||||
node.forEachChild(visit);
|
||||
}
|
||||
};
|
||||
|
||||
visit(sourceFile);
|
||||
|
||||
return hasFunctionOrClass;
|
||||
}
|
||||
};
|
||||
|
||||
const main = async () => {
|
||||
// Run a git command to get a list of all changed files in the branch (branch has to be up to date with master)
|
||||
const changedFiles = await execAsync(
|
||||
'git diff --name-only --diff-filter=d origin/master..HEAD',
|
||||
).then(({ stdout }) => stdout.trim().split('\n').filter(Boolean));
|
||||
|
||||
// Run a git command to get a list of all changed files in the branch (branch has to be up to date with master)
|
||||
const changedFiles = await execAsync('git diff --name-only --diff-filter=d origin/master..HEAD')
|
||||
.then(({stdout}) => stdout.trim().split('\n').filter(Boolean));
|
||||
|
||||
// Get all .spec.ts and .test.ts files from the packages
|
||||
// Get all .spec.ts and .test.ts files from the packages
|
||||
const specAndTestTsFiles = await glob('packages/*/**/{test,__tests__}/**/*.{spec,test}.ts');
|
||||
const specAndTestTsFilesNames = specAndTestTsFiles.map(file => path.parse(file).name.replace(/\.(test|spec)/, ''));
|
||||
|
||||
// Filter out the .ts and .vue files from the changed files
|
||||
const changedVueFiles = changedFiles.filter(file => file.endsWith('.vue'));
|
||||
// .ts files with any kind of function declaration or class and not in any of the test folders
|
||||
const changedTsFilesWithFunction = await filterAsync(
|
||||
async filePath =>
|
||||
filePath.endsWith('.ts') &&
|
||||
!(await glob('packages/*/**/{test,__tests__}/*.ts')).includes(filePath) &&
|
||||
await hasFunctionOrClass(filePath),
|
||||
changedFiles
|
||||
const specAndTestTsFilesNames = specAndTestTsFiles.map((file) =>
|
||||
path.parse(file).name.replace(/\.(test|spec)/, ''),
|
||||
);
|
||||
|
||||
// For each .ts or .vue file, check if there's a corresponding .test.ts or .spec.ts file in the repository
|
||||
const missingTests = changedVueFiles.concat(changedTsFilesWithFunction).reduce((filesList, nextFile) => {
|
||||
const fileName = path.parse(nextFile).name;
|
||||
// Filter out the .ts and .vue files from the changed files
|
||||
const changedVueFiles = changedFiles.filter((file) => file.endsWith('.vue'));
|
||||
// .ts files with any kind of function declaration or class and not in any of the test folders
|
||||
const changedTsFilesWithFunction = await filterAsync(
|
||||
async (filePath) =>
|
||||
filePath.endsWith('.ts') &&
|
||||
!(await glob('packages/*/**/{test,__tests__}/*.ts')).includes(filePath) &&
|
||||
(await hasFunctionOrClass(filePath)),
|
||||
changedFiles,
|
||||
);
|
||||
|
||||
if (!specAndTestTsFilesNames.includes(fileName)) {
|
||||
filesList.push(nextFile);
|
||||
}
|
||||
// For each .ts or .vue file, check if there's a corresponding .test.ts or .spec.ts file in the repository
|
||||
const missingTests = changedVueFiles
|
||||
.concat(changedTsFilesWithFunction)
|
||||
.reduce((filesList, nextFile) => {
|
||||
const fileName = path.parse(nextFile).name;
|
||||
|
||||
return filesList;
|
||||
}, []);
|
||||
if (!specAndTestTsFilesNames.includes(fileName)) {
|
||||
filesList.push(nextFile);
|
||||
}
|
||||
|
||||
if(missingTests.length) {
|
||||
return filesList;
|
||||
}, []);
|
||||
|
||||
if (missingTests.length) {
|
||||
console.error(`Missing tests for:\n${missingTests.join('\n')}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user