xref: /expo/tools/src/commands/CheckPackages.ts (revision a272999e)
1import { Command } from '@expo/commander';
2import chalk from 'chalk';
3
4import logger from '../Logger';
5import checkPackageAsync from '../check-packages/checkPackageAsync';
6import getPackagesToCheckAsync from '../check-packages/getPackagesToCheckAsync';
7import { ActionOptions } from '../check-packages/types';
8
9const { green, magenta, yellow } = chalk;
10
11export default (program: Command) => {
12  program
13    .command('check-packages [packageNames...]')
14    .alias('check', 'cp')
15    .option(
16      '-s, --since <commit>',
17      'Reference to the commit since which you want to run incremental checks. Defaults to HEAD of the main branch.',
18      'main'
19    )
20    .option('-a, --all', 'Whether to check all packages and ignore `--since` option.', false)
21    .option('--no-build', 'Whether to skip `yarn build` check.', false)
22    .option('--no-test', 'Whether to skip `yarn test` check.', false)
23    .option('--no-lint', 'Whether to skip `yarn lint` check.', false)
24    .option('--fix-lint', 'Whether to run `yarn lint --fix` instead of `yarn lint`.', false)
25    .option(
26      '--no-uniformity-check',
27      'Whether to check the uniformity of committed and generated build files.',
28      false
29    )
30    .description('Checks if packages build successfully and their tests pass.')
31    .asyncAction(main);
32};
33
34async function main(packageNames: string[], options: ActionOptions): Promise<void> {
35  options.packageNames = packageNames;
36
37  const packages = await getPackagesToCheckAsync(options);
38  const failedPackages: string[] = [];
39  let passCount = 0;
40
41  for (const pkg of packages) {
42    if (await checkPackageAsync(pkg, options)) {
43      passCount++;
44    } else {
45      failedPackages.push(pkg.packageName);
46    }
47    logger.log();
48  }
49
50  const failureCount = failedPackages.length;
51
52  if (failureCount !== 0) {
53    logger.log(
54      `${green(`�� ${passCount} packages passed`)},`,
55      `${magenta(`${failureCount} ${failureCount === 1 ? 'package' : 'packages'} failed:`)}`,
56      failedPackages.map((failedPackage) => yellow(failedPackage)).join(', ')
57    );
58    process.exit(1);
59    return;
60  }
61  logger.success('�� All packages passed.');
62}
63