Skip to content

Commit

Permalink
DQA-8660: Create command toolkit:lint-css (#775)
Browse files Browse the repository at this point in the history
  • Loading branch information
joaocsilva authored May 27, 2024
1 parent 5d244e3 commit 69da5b6
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 18 deletions.
4 changes: 4 additions & 0 deletions config/commands/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ command:
extensions: ${toolkit.lint.php.extensions}
exclude: ${toolkit.lint.php.exclude}
options: ${toolkit.lint.php.options}
lint-css:
options:
config: ${toolkit.lint.css.config}
files: ${toolkit.lint.css.files}
5 changes: 4 additions & 1 deletion config/runner/toolkit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ toolkit:
lint:
eslint:
config: .eslintrc.json
packages: 'eslint-config-drupal eslint-plugin-yml'
packages: 'eslint-config-drupal eslint-plugin-yml stylelint stylelint-config-drupal'
ignores: [ '${toolkit.build.dist.root}/*', '.cache/*', 'vendor/*', 'web/*', 'dist/*' ]
extensions_yaml: [ '.yml', '.yaml' ]
options_yaml: ''
Expand All @@ -102,6 +102,9 @@ toolkit:
extensions: [ 'php', 'module', 'inc', 'theme', 'install' ]
exclude: [ '${toolkit.build.dist.root}/', '.cache/', 'vendor/', 'web/' ]
options: ''
css:
config: .stylelintrc.json
files: '"${toolkit.build.custom-code-folder}/themes/*/css/*.css" "!${toolkit.build.custom-code-folder}/themes/*/css/*.min.css"'
axe-scan:
config: axe-scan.config.json
urls:
Expand Down
55 changes: 42 additions & 13 deletions src/TaskRunner/Commands/LintCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,19 @@ public function getConfigurationFile()
/**
* Setup the ESLint configurations and dependencies.
*
* Check configurations at config/default.yml - 'toolkit.lint.eslint'.
*
* @command toolkit:setup-eslint
*
* @option config The eslint config file.
* @option ignores The patterns to ignore.
* @option drupal-root The drupal root.
* @option packages The npm packages to install.
* @option force If true, the config file will be deleted.
*
* @return int
*/
public function toolkitSetupEslint(array $options = [
'config' => InputOption::VALUE_OPTIONAL,
'config' => InputOption::VALUE_REQUIRED,
'ignores' => InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'drupal-root' => InputOption::VALUE_OPTIONAL,
'packages' => InputOption::VALUE_OPTIONAL,
'drupal-root' => InputOption::VALUE_REQUIRED,
'packages' => InputOption::VALUE_REQUIRED,
'force' => false,
])
{
Expand Down Expand Up @@ -139,8 +135,6 @@ private function generateEslintConfigurations(string $config, array $options)
/**
* Run lint YAML.
*
* Check configurations at config/default.yml - 'toolkit.lint.eslint'.
*
* @command toolkit:lint-yaml
*
* @option config The eslint config file.
Expand All @@ -165,8 +159,6 @@ public function toolkitLintYaml(array $options = [
/**
* Run lint JS.
*
* Check configurations at config/default.yml - 'toolkit.lint.eslint'.
*
* @command toolkit:lint-js
*
* @option config The eslint config file.
Expand Down Expand Up @@ -225,8 +217,6 @@ private function toolkitRunEsLint(string $config, array $extensions, string $opt
/**
* Run lint PHP.
*
* Check configurations at config/default.yml - 'toolkit.lint.php'.
*
* @command toolkit:lint-php
*
* @option exclude The eslint config file.
Expand Down Expand Up @@ -266,4 +256,43 @@ public function toolkitLintPhp(array $options = [
return $result->getExitCode();
}

/**
* Run lint CSS.
*
* @command toolkit:lint-css
*
* @option exclude The stylelint config file.
* @option files The files to check.
*
* @aliases tk-css
*/
public function toolkitLintCss(array $options = [
'config' => InputOption::VALUE_REQUIRED,
'files' => InputOption::VALUE_REQUIRED,
])
{
$tasks = [];

// Make sure eslint is properly installed.
$tasks[] = $this->taskExec($this->getBin('run'))->arg('toolkit:setup-eslint');

// Make sure the stylelint-config-drupal and stylelint are installed.
$tasks[] = $this->taskExecStack()
->exec('npm -v || npm i npm')
->exec('[ -f package.json ] || npm init -y --scope')
->exec('npm list stylelint-config-drupal && npm update stylelint-config-drupal || npm install stylelint-config-drupal -y');

// Generate the config file if missing.
if (!file_exists($options['config'])) {
$data = ['extends' => 'stylelint-config-drupal'];
$tasks[] = $this->taskWriteToFile($options['config'])
->text(json_encode($data, JSON_PRETTY_PRINT));
}

$tasks[] = $this->taskExec($this->getNodeBinPath('stylelint'))
->rawArg($options['files']);

return $this->collectionBuilder()->addTaskList($tasks);
}

}
8 changes: 4 additions & 4 deletions tests/fixtures/commands/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
- contains: |
[Simulator] Simulating Exec('npm ini -y')
[Simulator] Running npm ini -y
[Simulator] Simulating Exec('npm install --save-dev eslint-config-drupal eslint-plugin-yml -y')
[Simulator] Running npm install --save-dev eslint-config-drupal eslint-plugin-yml -y
[Simulator] Simulating Exec('npm install --save-dev eslint-config-drupal eslint-plu ... nt-config-drupal -y')
[Simulator] Running npm install --save-dev eslint-config-drupal eslint-plugin-yml stylelint stylelint-config-drupal -y
[File\Write] Writing to .eslintrc.json.
[Simulator] Simulating File\Write('.prettierignore')
->text('*.yml')
Expand Down Expand Up @@ -34,8 +34,8 @@
[Simulator] Running rm .eslintrc.json
[Simulator] Simulating Exec('npm ini -y')
[Simulator] Running npm ini -y
[Simulator] Simulating Exec('npm install --save-dev eslint-config-drupal eslint-plugin-yml -y')
[Simulator] Running npm install --save-dev eslint-config-drupal eslint-plugin-yml -y
[Simulator] Simulating Exec('npm install --save-dev eslint-config-drupal eslint-plu ... nt-config-drupal -y')
[Simulator] Running npm install --save-dev eslint-config-drupal eslint-plugin-yml stylelint stylelint-config-drupal -y
[Simulator] Simulating File\Write('.prettierignore')
->text('*.yml')
Expand Down

0 comments on commit 69da5b6

Please sign in to comment.