const fs = require('fs');
const path = require('path');
const {spawnSync} = require('child_process');

const backendRoot = path.resolve(__dirname, '..');
const files = [];

function collectJsFiles(directory) {
  for (const entry of fs.readdirSync(directory, {withFileTypes: true})) {
    const fullPath = path.join(directory, entry.name);
    if (entry.isDirectory()) {
      if (entry.name !== 'node_modules') {
        collectJsFiles(fullPath);
      }
      continue;
    }

    if (entry.isFile() && entry.name.endsWith('.js')) {
      files.push(fullPath);
    }
  }
}

collectJsFiles(backendRoot);

for (const file of files) {
  const result = spawnSync(process.execPath, ['--check', file], {stdio: 'inherit'});
  if (result.status !== 0) {
    process.exit(result.status);
  }
}

console.log(`Checked ${files.length} backend JavaScript files.`);
