| @@ 23-125 (lines=103) @@ | ||
| 20 | /** |
|
| 21 | * @author Beñat Espiña <[email protected]> |
|
| 22 | */ |
|
| 23 | final class EsLint implements Checker |
|
| 24 | { |
|
| 25 | use FileFinder; |
|
| 26 | use ToolAvailability; |
|
| 27 | ||
| 28 | public static function check(array $files = [], array $parameters = null) |
|
| 29 | { |
|
| 30 | self::isAvailable('eslint'); |
|
| 31 | self::file($parameters); |
|
| 32 | ||
| 33 | $excludes = []; |
|
| 34 | if (true === array_key_exists('eslint_exclude', $parameters)) { |
|
| 35 | foreach ($parameters['eslint_exclude'] as $key => $exclude) { |
|
| 36 | $excludes[$key] = $parameters['eslint_path'] . '/' . $exclude; |
|
| 37 | } |
|
| 38 | } |
|
| 39 | ||
| 40 | $errors = []; |
|
| 41 | foreach ($files as $file) { |
|
| 42 | if (false === self::exist($file, $parameters['eslint_path'], 'js') || in_array($file, $excludes, true)) { |
|
| 43 | continue; |
|
| 44 | } |
|
| 45 | ||
| 46 | $process = new Process( |
|
| 47 | sprintf('eslint %s -c %s/.eslintrc.js', $file, self::location($parameters)), |
|
| 48 | $parameters['root_directory'] |
|
| 49 | ); |
|
| 50 | $process->run(); |
|
| 51 | if (!$process->isSuccessful()) { |
|
| 52 | $errors[] = new Error( |
|
| 53 | $file, |
|
| 54 | sprintf('<error>%s</error>', trim($process->getErrorOutput())), |
|
| 55 | sprintf('<error>%s</error>', trim($process->getOutput())) |
|
| 56 | ); |
|
| 57 | } |
|
| 58 | } |
|
| 59 | ||
| 60 | return $errors; |
|
| 61 | } |
|
| 62 | ||
| 63 | public static function file($parameters) |
|
| 64 | { |
|
| 65 | $jsContent = file_get_contents(__DIR__ . '/../.eslintrc.js.dist'); |
|
| 66 | ||
| 67 | $arrayContent = self::extractContent($jsContent); |
|
| 68 | foreach ($parameters['eslint_rules'] as $ruleType => $rules) { |
|
| 69 | if (!is_array($rules)) { |
|
| 70 | $arrayContent[$ruleType] = $rules; |
|
| 71 | continue; |
|
| 72 | } |
|
| 73 | ||
| 74 | if (self::isAssociativeArray($rules)) { |
|
| 75 | foreach ($rules as $name => $rule) { |
|
| 76 | $arrayContent[$ruleType][$name] = $rule; |
|
| 77 | } |
|
| 78 | continue; |
|
| 79 | } |
|
| 80 | ||
| 81 | foreach ($rules as $rule) { |
|
| 82 | if (in_array($rule, $arrayContent[$ruleType], true)) { |
|
| 83 | continue; |
|
| 84 | } |
|
| 85 | $arrayContent[$ruleType][] = $rule; |
|
| 86 | } |
|
| 87 | } |
|
| 88 | ||
| 89 | $location = self::location($parameters) . '/.eslintrc.js'; |
|
| 90 | $fileSystem = new Filesystem(); |
|
| 91 | ||
| 92 | try { |
|
| 93 | $fileSystem->remove($location); |
|
| 94 | $fileSystem->touch($location); |
|
| 95 | file_put_contents($location, self::buildEslintJsFile($arrayContent)); |
|
| 96 | } catch (\Exception $exception) { |
|
| 97 | echo sprintf("Something wrong happens during the creating process: \n%s\n", $exception->getMessage()); |
|
| 98 | } |
|
| 99 | } |
|
| 100 | ||
| 101 | private static function extractContent($jsFileContent) |
|
| 102 | { |
|
| 103 | $position = mb_strpos($jsFileContent, 'module.exports = '); |
|
| 104 | $position = $position + 17; |
|
| 105 | $json = mb_substr($jsFileContent, $position); |
|
| 106 | $json = rtrim(trim($json), ';'); |
|
| 107 | ||
| 108 | return json_decode($json, true); |
|
| 109 | } |
|
| 110 | ||
| 111 | private static function buildEslintJsFile(array $content) |
|
| 112 | { |
|
| 113 | return sprintf('module.exports = %s;', str_replace('\/', '/', json_encode($content))); |
|
| 114 | } |
|
| 115 | ||
| 116 | private static function isAssociativeArray(array $array) |
|
| 117 | { |
|
| 118 | return [] !== $array && array_keys($array) !== range(0, count($array) - 1); |
|
| 119 | } |
|
| 120 | ||
| 121 | private static function location($parameters) |
|
| 122 | { |
|
| 123 | return $parameters['root_directory'] . '/' . $parameters['eslint_file_location']; |
|
| 124 | } |
|
| 125 | } |
|
| 126 | ||
| @@ 23-127 (lines=105) @@ | ||
| 20 | /** |
|
| 21 | * @author Beñat Espiña <[email protected]> |
|
| 22 | */ |
|
| 23 | final class Stylelint implements Checker |
|
| 24 | { |
|
| 25 | use FileFinder; |
|
| 26 | use ToolAvailability; |
|
| 27 | ||
| 28 | public static function check(array $files = [], array $parameters = null) |
|
| 29 | { |
|
| 30 | self::isAvailable('stylelint'); |
|
| 31 | self::file($parameters); |
|
| 32 | ||
| 33 | $excludes = []; |
|
| 34 | if (true === array_key_exists('stylelint_exclude', $parameters)) { |
|
| 35 | foreach ($parameters['stylelint_exclude'] as $key => $exclude) { |
|
| 36 | $excludes[$key] = $parameters['stylelint_path'] . '/' . $exclude; |
|
| 37 | } |
|
| 38 | } |
|
| 39 | ||
| 40 | $errors = []; |
|
| 41 | foreach ($files as $file) { |
|
| 42 | if (false === self::exist($file, $parameters['stylelint_path'], 'scss') |
|
| 43 | || in_array($file, $excludes, true) |
|
| 44 | ) { |
|
| 45 | continue; |
|
| 46 | } |
|
| 47 | ||
| 48 | $process = new Process( |
|
| 49 | sprintf('stylelint %s -c %s/.stylelintrc.js', $file, self::location($parameters)), |
|
| 50 | $parameters['root_directory'] |
|
| 51 | ); |
|
| 52 | $process->run(); |
|
| 53 | if (!$process->isSuccessful()) { |
|
| 54 | $errors[] = new Error( |
|
| 55 | $file, |
|
| 56 | sprintf('<error>%s</error>', trim($process->getErrorOutput())), |
|
| 57 | sprintf('<error>%s</error>', trim($process->getOutput())) |
|
| 58 | ); |
|
| 59 | } |
|
| 60 | } |
|
| 61 | ||
| 62 | return $errors; |
|
| 63 | } |
|
| 64 | ||
| 65 | public static function file($parameters) |
|
| 66 | { |
|
| 67 | $jsContent = file_get_contents(__DIR__ . '/../.stylelintrc.js.dist'); |
|
| 68 | ||
| 69 | $arrayContent = self::extractContent($jsContent); |
|
| 70 | foreach ($parameters['stylelint_rules'] as $ruleType => $rules) { |
|
| 71 | if (!is_array($rules)) { |
|
| 72 | $arrayContent[$ruleType] = $rules; |
|
| 73 | continue; |
|
| 74 | } |
|
| 75 | ||
| 76 | if (self::isAssociativeArray($rules)) { |
|
| 77 | foreach ($rules as $name => $rule) { |
|
| 78 | $arrayContent[$ruleType][$name] = $rule; |
|
| 79 | } |
|
| 80 | continue; |
|
| 81 | } |
|
| 82 | ||
| 83 | foreach ($rules as $rule) { |
|
| 84 | if (in_array($rule, $arrayContent[$ruleType], true)) { |
|
| 85 | continue; |
|
| 86 | } |
|
| 87 | $arrayContent[$ruleType][] = $rule; |
|
| 88 | } |
|
| 89 | } |
|
| 90 | ||
| 91 | $location = self::location($parameters) . '/.stylelintrc.js'; |
|
| 92 | $fileSystem = new Filesystem(); |
|
| 93 | ||
| 94 | try { |
|
| 95 | $fileSystem->remove($location); |
|
| 96 | $fileSystem->touch($location); |
|
| 97 | file_put_contents($location, self::buildStylelintJsFile($arrayContent)); |
|
| 98 | } catch (\Exception $exception) { |
|
| 99 | echo sprintf("Something wrong happens during the creating process: \n%s\n", $exception->getMessage()); |
|
| 100 | } |
|
| 101 | } |
|
| 102 | ||
| 103 | private static function extractContent($jsFileContent) |
|
| 104 | { |
|
| 105 | $position = mb_strpos($jsFileContent, 'module.exports = '); |
|
| 106 | $position = $position + 17; |
|
| 107 | $json = mb_substr($jsFileContent, $position); |
|
| 108 | $json = rtrim(trim($json), ';'); |
|
| 109 | ||
| 110 | return json_decode($json, true); |
|
| 111 | } |
|
| 112 | ||
| 113 | private static function buildStylelintJsFile(array $content) |
|
| 114 | { |
|
| 115 | return sprintf('module.exports = %s;', str_replace('\/', '/', json_encode($content))); |
|
| 116 | } |
|
| 117 | ||
| 118 | private static function isAssociativeArray(array $array) |
|
| 119 | { |
|
| 120 | return [] !== $array && array_keys($array) !== range(0, count($array) - 1); |
|
| 121 | } |
|
| 122 | ||
| 123 | private static function location($parameters) |
|
| 124 | { |
|
| 125 | return $parameters['root_directory'] . '/' . $parameters['stylelint_file_location']; |
|
| 126 | } |
|
| 127 | } |
|
| 128 | ||