All files / executors/stylelint check.ts

60.86% Statements 14/23
50% Branches 9/18
100% Functions 1/1
60.86% Lines 14/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50                            4x   4x 1x     3x 3x 3x 3x 1x   2x 2x       2x   2x                   2x       2x    
import { type ExecutorContext, getProjects, logger } from '@nx/devkit';
import { execFileSync } from 'child_process';
import { FsTree } from 'nx/src/generators/tree';
 
import type { IExecutorOptions } from './schema';
import { findScssFiles } from './utils/find-scss-files.util';
 
/**
 * Execute stylelint checks.
 * @param options executor options
 * @param context executor context
 * @returns execution result
 */
export default async function check(options: IExecutorOptions, context: ExecutorContext): Promise<{ success: boolean }> {
  const projectName = context.projectName;
 
  if (typeof projectName === 'undefined') {
    throw new Error('Project name is not defined.');
  }
 
  const tree = new FsTree(context.root, false);
  const projects = getProjects(tree);
  const project = projects.get(projectName);
  if (typeof project === 'undefined') {
    throw new Error('Project does not exist.');
  }
  const src = project.sourceRoot;
  Iif (typeof src === 'undefined') {
    throw new Error('Project root does not exist.');
  }
 
  const files = findScssFiles(tree, src);
 
  Iif (files.stdout.length > 0) {
    const input = files.stdout.split(' ');
    logger.log('files', input);
    const cmd = 'npx';
    const config = options.config !== '' ? `--config ${options.config}` : '';
    const defaultArgs = ['stylelint', ...input, config, '--custom-syntax', options.customSyntax ?? 'postcss-scss'];
    const args = options.dryRun === true ? defaultArgs : defaultArgs.concat(['--fix']);
    execFileSync(cmd, args, { stdio: 'inherit', cwd: process.cwd(), env: process.env, shell: true });
  }
 
  Iif (files.stdout.length === 0 && files.stderr.length === 0) {
    logger.info(`${src} does not contain scss files.`);
  }
 
  return { success: files.stderr === '' };
}