Skip to content

Commit cfd15bc

Browse files
authored
Take CSS in from STDIN
1 parent 1824a72 commit cfd15bc

File tree

1 file changed

+42
-19
lines changed

1 file changed

+42
-19
lines changed

scripts/php-css-lint

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,50 @@ function printUsage(){
3232
'------------------------------------------------------------'.PHP_EOL.
3333
'Lint a CSS file : '.PHP_EOL.
3434
basename(__DIR__).'/'.basename(__FILE__).' css_file_path_to_lint.css'.PHP_EOL.
35+
'Lint a CSS string : '.PHP_EOL.
36+
basename(__DIR__).'/'.basename(__FILE__).' ".test { color: red; }"'.PHP_EOL.
3537
'------------------------------------------------------------'.PHP_EOL.PHP_EOL;
3638
}
3739

3840
function printError($sError){
3941
echo "\033[31m/!\ Error : ".$sError."\033[0m".PHP_EOL.PHP_EOL;
4042
}
4143

44+
function lintFile($sFilePath){
45+
echo '# Lint file "'.$sFilePath.'"...'.PHP_EOL;
46+
47+
$oCssLinter = new \CssLint\Linter();
48+
if($oCssLinter->lintFile($sFilePath)){
49+
echo "\033[32m => File \"".$sFilePath."\" is valid\033[0m".PHP_EOL.PHP_EOL;
50+
exit(0);
51+
}
52+
53+
echo "\033[31m => File \"".$sFilePath."\" is not valid :\033[0m".PHP_EOL.PHP_EOL;
54+
displayLinterErrors($oCssLinter->getErrors());
55+
exit(1);
56+
}
57+
58+
function lintString($sString){
59+
echo '# Lint css string...'.PHP_EOL;
60+
61+
$oCssLinter = new \CssLint\Linter();
62+
if($oCssLinter->lintString($sString)){
63+
echo "\033[32m => Css string is valid\033[0m".PHP_EOL.PHP_EOL;
64+
exit(0);
65+
}
66+
67+
echo "\033[31m => Css string is not valid :\033[0m".PHP_EOL.PHP_EOL;
68+
displayLinterErrors($oCssLinter->getErrors());
69+
exit(1);
70+
}
71+
72+
function displayLinterErrors($aErrors){
73+
foreach($aErrors as $sError){
74+
echo "\033[31m - ".$sError."\033[0m".PHP_EOL;
75+
}
76+
echo PHP_EOL;
77+
}
78+
4279
if(empty($_SERVER['argv']) || count($_SERVER['argv']) === 1){
4380
printUsage();
4481
exit(1);
@@ -50,28 +87,14 @@ if(count($_SERVER['argv']) > 2){
5087
exit(1);
5188
}
5289

53-
$sFilePath = $_SERVER['argv'][1];
54-
if(!file_exists($sFilePath)){
55-
printError('File "'.$sFilePath.'" does not exist');
56-
exit(1);
90+
$sFilePathOrCssString = $_SERVER['argv'][1];
91+
if(!file_exists($sFilePathOrCssString)){
92+
lintString($sFilePath);
5793
}
5894

95+
$sFilePath = $sFilePathOrCssString;
5996
if(!is_readable($sFilePath)){
6097
printError('File "'.$sFilePath.'" is not readable');
6198
exit(1);
6299
}
63-
64-
echo '# Lint file "'.$sFilePath.'"...'.PHP_EOL;
65-
66-
$oCssLinter = new \CssLint\Linter();
67-
if($oCssLinter->lintFile($sFilePath)){
68-
echo "\033[32m => File \"".$sFilePath."\" is valid\033[0m".PHP_EOL.PHP_EOL;
69-
exit(0);
70-
}
71-
72-
echo "\033[31m => File \"".$sFilePath."\" is not valid :\033[0m".PHP_EOL.PHP_EOL;
73-
foreach($oCssLinter->getErrors() as $sError){
74-
echo "\033[31m - ".$sError."\033[0m".PHP_EOL;
75-
}
76-
echo PHP_EOL;
77-
exit(0);
100+
lintFile($sFilePath);

0 commit comments

Comments
 (0)