import { execSync } from 'child_process'; import { findImports } from './find-imports.js'; import { getModules } from './get-modules.js'; const E2E_PATH = 'test/cypress/integration'; const FINDED_PATHS = ['src', E2E_PATH]; function getGitDiff(options) { const TARGET_BRANCH = options[2] || 'dev'; execSync(`git fetch origin ${TARGET_BRANCH}`, { encoding: 'utf-8', }); const diff = execSync(`git diff --name-only origin/${TARGET_BRANCH}`, { encoding: 'utf-8', }); return diff.split('\n'); } async function getChangedModules() { let changedModules = new Set(); const changes = getGitDiff(process.argv); for (const change of changes) { if (!change) continue; if (!FINDED_PATHS.some((prefix) => change.startsWith(prefix))) return ''; const changedArray = [ ...changedModules, ...new Set(getModules(await findImports(change))), ]; if (change.startsWith(E2E_PATH)) changedArray.push(change); changedModules = new Set(changedArray); } return cleanSpecs(changedModules).join('\n'); } getChangedModules() .then((modules) => console.log(modules)) // is return .catch((e) => { console.error(e); process.exit(1); }); function cleanSpecs(changedModules) { let specifics = []; const modules = []; for (const changed of changedModules) { if (changed.endsWith('*.spec.js')) { modules.push(changed); continue; } specifics.push(changed); } specifics = specifics.filter( (spec) => !modules.some((module) => spec.startsWith(module.split('**')[0])), ); return [...modules, ...specifics]; }