1import chalk from 'chalk';
2
3import logger from '../Logger';
4import { Package } from '../Packages';
5import checkUniformityAsync from './checkUniformityAsync';
6import runPackageScriptAsync from './runPackageScriptAsync';
7import { ActionOptions } from './types';
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      if (pkg.scripts.bundle) {
30        await runPackageScriptAsync(pkg, 'bundle', args);
31      }
32
33      if (options.uniformityCheck) {
34        await checkUniformityAsync(pkg, './build');
35        if (pkg.scripts.bundle) {
36          await checkUniformityAsync(pkg, '*bundle');
37        }
38      }
39    }
40    if (options.test) {
41      const args = ['--watch', 'false', '--passWithNoTests'];
42      if (options.isPlugin) {
43        args.unshift('plugin');
44      }
45      if (process.env.CI) {
46        // Limit to one worker on CIs
47        args.push('--maxWorkers', '1');
48      }
49      await runPackageScriptAsync(pkg, 'test', args);
50    }
51    if (options.lint) {
52      const args = ['--max-warnings', '0'];
53      if (options.isPlugin) {
54        args.unshift('plugin');
55      }
56      if (options.fixLint) {
57        args.push('--fix');
58      }
59      await runPackageScriptAsync(pkg, 'lint', args);
60    }
61    logger.log(`✨ ${green.bold(pkg.packageName)} checks passed`);
62
63    if (!options.isPlugin && pkg.hasPlugin) {
64      return await checkPackageAsync(pkg, { ...options, isPlugin: true });
65    }
66    return true;
67  } catch {
68    // runPackageScriptAsync is intentionally written to handle errors and make it safe to suppress errors in the caller
69    return false;
70  }
71}
72