Created
July 29, 2016 13:28
-
-
Save roelofr/b4180594ca61767556d2f33496ff4bbc to your computer and use it in GitHub Desktop.
PHP project config organizer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env php | |
| <?php | |
| /** | |
| * This script will organize JSON files. Initially only by alphabetical order but for certain files | |
| * standards exist, which will be maintained. | |
| * | |
| * @author Roelof Roos <[email protected]> | |
| * @license GPL-3.0 | |
| * @see https://gist.github.com/roelofr/b4180594ca61767556d2f33496ff4bbc | |
| * @see https://gist.github.com/roelofr | |
| */ | |
| $file = $argv[1]; | |
| if ($file[0] === '/') { | |
| $path = $file; | |
| } else { | |
| $path = __DIR__ . '/' . $file; | |
| } | |
| if (!file_exists($path) || !is_file($path) || !is_readable($path)) { | |
| echo "File \"{$file}\" does not exist or is unreadable.\n"; | |
| echo "Checked {$path}\n"; | |
| exit(1); | |
| } | |
| $text = file_get_contents($path); | |
| $data = json_decode($text, true); | |
| if (json_last_error() !== JSON_ERROR_NONE) { | |
| echo "File \"{$file}\" cannot be parsed: "; | |
| echo json_last_error_msg() . PHP_EOL; | |
| echo "Checked {$path}\n"; | |
| exit(1); | |
| } | |
| ksort($data); | |
| // Lets change up some elements if we need to. | |
| $fileName = basename($path); | |
| // Change 'warn', 'error' and 'off' in yaml to 1, 2 and 0 respectively. | |
| if (preg_match('/^\.eslintrc(?:\.(?:js|yaml|yml|json))?$/', $fileName)) { | |
| $formatErrorToNumber = function ($text) { | |
| switch ($text) { | |
| case "error": | |
| return 2; | |
| case "warn": | |
| case "warning": | |
| return 1; | |
| case "off": | |
| return 0; | |
| default: | |
| return $text; | |
| } | |
| }; | |
| if (array_key_exists('rules', $data)) { | |
| foreach ($data['rules'] as $name => $config) { | |
| if (is_string($config)) { | |
| $data['rules'][$name] = formatToNumber($config); | |
| } elseif (is_array($config)) { | |
| $data['rules'][$name][0] = formatToNumber($config[0]); | |
| } | |
| } | |
| } | |
| } | |
| if ($fileName === 'package.json') { | |
| // Auto-sort keywords | |
| if (array_key_exists('keywords', $data)) { | |
| sort($data['keywords']); | |
| } | |
| // Meta info as sorted on https://docs.npmjs.com/files/package.json | |
| $meta = [ | |
| ['name', 'my-package-name'], | |
| ['version', '0.1.0'], | |
| ['description', ''], | |
| 'keywords', | |
| 'homepage', | |
| 'bugs', | |
| ['license', 'UNLICENSED'], | |
| 'author', | |
| 'contributors', | |
| 'files', | |
| 'main', | |
| 'bin', | |
| 'man', | |
| 'directories', | |
| 'repository', | |
| 'scripts', | |
| 'config', | |
| 'dependencies', | |
| 'devDependencies', | |
| 'peerDependencies', | |
| 'bundledDependencies', | |
| 'optionalDependencies', | |
| 'engines', | |
| 'engineStrict', | |
| 'preferGlobal', | |
| 'private', | |
| 'publishConfig' | |
| ]; | |
| $officialData = []; | |
| foreach ($meta as $metaEntry) { | |
| if (is_array($metaEntry)) { | |
| $default = $metaEntry[1]; | |
| $name = $metaEntry[0]; | |
| } else { | |
| $name = $metaEntry; | |
| $default = null; | |
| } | |
| if (array_key_exists($name, $data)) { | |
| $officialData[$name] = $data[$name]; | |
| unset($data[$name]); | |
| } elseif ($default !== null) { | |
| $officialData[$name] = $default; | |
| } | |
| } | |
| // And re-add them at the beginning. | |
| $data = $officialData + $data; | |
| } | |
| file_put_contents($path, json_encode($data, JSON_PRETTY_PRINT)); | |
| echo "Updated {$file}.\n"; | |
| exit(0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment