1import chalk from 'chalk';
2
3import logger from '../Logger';
4import { Package } from '../Packages';
5import { ActionOptions } from './types';
6import runPackageScriptAsync from './runPackageScriptAsync';
7import checkBuildUniformityAsync from './checkBuildUniformityAsync';
8
9const { green } = chalk;
10
11/**
12 * Runs package checks on given package.
13 */
14export default async function checkPackageAsync(
15  pkg: Package,
16  options: ActionOptions
17): Promise<boolean> {
18  try {
19    if (options.isPlugin) {
20      logger.info(`�� Checking ${green.bold(pkg.packageName)} plugin`);
21    } else {
22      logger.info(`�� Checking ${green.bold(pkg.packageName)} package`);
23    }
24
25    const args = options.isPlugin ? ['plugin'] : [];
26    if (options.build) {
27      await runPackageScriptAsync(pkg, 'clean', args);
28      await runPackageScriptAsync(pkg, 'build', args);
29
30      if (options.uniformityCheck) {
31        await checkBuildUniformityAsync(pkg);
32      }
33    }
34    if (options.test) {
35      const args = ['--watch', 'false', '--passWithNoTests'];
36      if (options.isPlugin) {
37        args.unshift('plugin');
38      }
39      if (process.env.CI) {
40        // Limit to one worker on CIs
41        args.push('--maxWorkers', '1');
42      }
43      await runPackageScriptAsync(pkg, 'test', args);
44    }
45    if (options.lint) {
46      const args = ['--max-warnings', '0'];
47      if (options.isPlugin) {
48        args.unshift('plugin');
49      }
50      if (options.fixLint) {
51        args.push('--fix');
52      }
53      await runPackageScriptAsync(pkg, 'lint', args);
54    }
55    logger.log(`✨ ${green.bold(pkg.packageName)} checks passed`);
56
57    if (!options.isPlugin && pkg.hasPlugin) {
58      return await checkPackageAsync(pkg, { ...options, isPlugin: true });
59    }
60    return true;
61  } catch {
62    // runPackageScriptAsync is intentionally written to handle errors and make it safe to suppress errors in the caller
63    return false;
64  }
65}
66