-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse-css.php
51 lines (41 loc) · 1.33 KB
/
parse-css.php
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
51
<?php
/**
* Google Chrome Coverage Export Parser PHP
*
* Export a coverage report from Google Chrome:
* Developer Tools > 3-dot menu > More tools > Coverage
*
* Download the coverage report JSON file by clicking the "Export..." icon on
* the Coverage panel toolbar
*
* @usage
* php coverage-export-parser.php Coverage-YYYYMMDDTHHIISS.json /wp-content/themes/theme-directory/style.css output.css
*/
$json_string = $argv[1] ?: 'Coverage-YYYYMMDDTHHIISS.json';
$target_url = $argv[2] ?: '/wp-content/themes/theme-directory/style.css';
$output_filename = $argv[3] ?: 'output.css';
$jsondata = file_get_contents(sprintf('%s/%s', dirname(__FILE__), $json_string));
$obj = json_decode($jsondata, true);
$output_file = '';
foreach ($obj as $arr) {
/** Match Coverage item source url with $target_url */
if (strpos($arr['url'], $target_url) !== false) {
foreach ($arr['ranges'] as $name => $value) {
$length = $value['end'] - $value['start'];
$output_file .= substr($arr['text'], $value['start'], $length) . PHP_EOL;
}
break;
}
}
if ($output_file) {
echo PHP_EOL;
/** Echo CSS to terminal */
echo $output_file;
/** Output CSS to file */
file_put_contents($output_filename, $output_file);
/** Open CSS file in VS Code */
exec(sprintf('code %s', $output_filename));
}
else {
echo 'No output' . PHP_EOL;
}