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