import chalk from 'chalk';
import { COMPONENTS_DIR } from './meta';
import { readdir, stat, unlink } from 'fs/promises';
import { compileTemplateFile } from './lib';
const gitlabCiYmlFileName = '.gitlab-ci.yml';
const packageJsonFileName = 'package.json';
const checkGitlabCiYmlForCustomExist = async (componentName) => {
  return await stat(`${COMPONENTS_DIR}/customs/${componentName}/${gitlabCiYmlFileName}`)
    .then(statInfo => statInfo.isFile())
    .catch(() => {
    return false;
  });
};
const checkPackageJsonForCustomExist = async (componentName) => {
  return await stat(`${COMPONENTS_DIR}/customs/${componentName}/${packageJsonFileName}`)
    .then(statInfo => statInfo.isFile())
    .catch(() => {
    return false;
  });
};
const checkGitlabCiYmlForSharedExist = async (componentName) => {
  return await stat(`${COMPONENTS_DIR}/shared/${componentName}/${gitlabCiYmlFileName}`)
    .then(statInfo => statInfo.isFile())
    .catch(() => {
    return false;
  });
};
const checkPackageJsonForSharedExist = async (componentName) => {
  return await stat(`${COMPONENTS_DIR}/shared/${componentName}/${packageJsonFileName}`)
    .then(statInfo => statInfo.isFile())
    .catch(() => {
    return false;
  });
};
const regenerateGitlabCiYmlForCustom = async (componentName, gitalYmlExistsForCustom) => {
  if (gitalYmlExistsForCustom) {
    await unlink(`${COMPONENTS_DIR}/customs/${componentName}/${gitlabCiYmlFileName}`);
    console.log(`File for ${componentName} ${chalk.green('successfully')} deleted`);
  }
  await compileTemplateFile('/customs/' + componentName, gitlabCiYmlFileName);
  console.log(`File for ${componentName} ${chalk.green('successfully')} regenerated`);
};
const regeneratePackageJsonForCustom = async (componentName, packageJsonExistsForCustom) => {
  if (packageJsonExistsForCustom) {
    await unlink(`${COMPONENTS_DIR}/customs/${componentName}/${packageJsonFileName}`);
    console.log(`File for ${componentName} ${chalk.green('successfully')} deleted`);
  }
  await compileTemplateFile('/customs/' + componentName, packageJsonFileName);
  console.log(`File for ${componentName} ${chalk.green('successfully')} regenerated`);
};
const regenerateGitlabCiYmlForShared = async (componentName, gitalYmlExistsForShared) => {
  if (gitalYmlExistsForShared) {
    await unlink(`${COMPONENTS_DIR}/shared/${componentName}/${gitlabCiYmlFileName}`);
    console.log(`File for ${componentName} ${chalk.green('successfully')} deleted`);
  }
  await compileTemplateFile('/shared/' + componentName, gitlabCiYmlFileName);
  console.log(`File for ${componentName} ${chalk.green('successfully')} regenerated`);
};
const regeneratePackageJsonForShared = async (componentName, packageJsonExistsForShared) => {
  if (packageJsonExistsForShared) {
    await unlink(`${COMPONENTS_DIR}/shared/${componentName}/${packageJsonFileName}`);
    console.log(`File for ${componentName} ${chalk.green('successfully')} deleted`);
  }
  await compileTemplateFile('/shared/' + componentName, packageJsonFileName);
  console.log(`File for ${componentName} ${chalk.green('successfully')} regenerated`);
};
const main = async () => {
  console.log(`This script will ${chalk.green('generate')} or ${chalk.yellow('re-generate')} the ${chalk.magenta(gitlabCiYmlFileName)} file for each stencil component based on the template located in the template folder`);
  const directoriesShared = await readdir(COMPONENTS_DIR + '/shared');
  const directoriesCustom = await readdir(COMPONENTS_DIR + '/customs');
  console.log('Components count:', directoriesCustom.length);
  for (const directory of directoriesCustom) {
    const gitalYmlExistsForCustom = await checkGitlabCiYmlForCustomExist(directory);
    const packageJsonExistsForCustom = await checkPackageJsonForCustomExist(directory);
    await regenerateGitlabCiYmlForCustom(directory, gitalYmlExistsForCustom);
    await regeneratePackageJsonForCustom(directory, packageJsonExistsForCustom);
  }
  console.log('Components count:', directoriesShared.length);
  for (const directory of directoriesShared) {
    const gitalYmlExistsForShared = await checkGitlabCiYmlForSharedExist(directory);
    const packageJsonExistsForShared = await checkPackageJsonForSharedExist(directory);
    await regenerateGitlabCiYmlForShared(directory, gitalYmlExistsForShared);
    await regeneratePackageJsonForShared(directory, packageJsonExistsForShared);
  }
};
main();
