Signed-off-by: anthony <anthonyaxenov@gmail.com>
This commit is contained in:
Anthony Axenov 2022-01-17 06:48:26 +00:00
parent ec199aa8c2
commit e9f993b396

View File

@ -5,7 +5,7 @@
* Simple Postman Collection Converter
*
* Author: Anthony Axenov (c) 2021
* Version: v1.0
* Version: v1.1
* License: MIT
* Dependecies: php8.0, php-json
*
@ -33,6 +33,12 @@
* Note: this script was quickly written to solve one exact problem in one NDA-project, so it may contain
* stupid errors and (for sure) doesn't cover ALL of possible cases according to collection scheme. So feel
* free to fork and change this script according your needs and to propose your fixes here.
***********************************************************************************************************************
* Version history:
*
* v1.0: initial
* v1.1: improved output directory structure
*
**********************************************************************************************************************/
declare(strict_types = 1);
@ -73,7 +79,7 @@ function out(mixed ...$data): void
*/
function read_collection_file(string $path): object
{
$content = file_get_contents(str_replace('~/', $_SERVER['HOME'], $path));
$content = file_get_contents(str_replace('~/', $_SERVER['HOME'] . '/', $path));
$json = json_decode($content);
return json_last_error() === JSON_ERROR_NONE
? $json
@ -91,7 +97,7 @@ function read_collection_file(string $path): object
*/
function write_request(object $request, ?string $dir_tree = null)
{
$filepath = './files/' . ($dir_tree ? $dir_tree . '/' : '') . $request->name;
$filepath = OUTPUT_DIR . '/' . ($dir_tree ? $dir_tree . '/' : '') . $request->name;
$httpfile = "$filepath.http";
$mdfile = "$filepath.md";
@ -104,7 +110,8 @@ function write_request(object $request, ?string $dir_tree = null)
$httpfile,
'# ' . str_replace("\n", "\n# ", $request->request->description) . "\n\n",
FILE_APPEND
) &&*/ file_put_contents($mdfile, "{$request->request->description}\n\n", FILE_APPEND);
) &&*/
file_put_contents($mdfile, "{$request->request->description}\n\n", FILE_APPEND);
// generating request data
$url = "{$request->request->method} {$request->request->url->raw} HTTP/1.1";
@ -179,9 +186,9 @@ function process_item($item = null)
} else {
$dir_tree[] = $subitem->name;
$path = implode('/', $dir_tree);
!file_exists('./files/' . $path) && mkdir('./files/' . $path);
!file_exists(OUTPUT_DIR . "/$path/") && mkdir(OUTPUT_DIR . "/$path/", recursive: true);
isset($subitem->description) && file_put_contents(
'./files/' . $path . '/README.md',
OUTPUT_DIR . "/$path/README.md",
$subitem->description
);
process_item($subitem->item);
@ -206,13 +213,16 @@ function remove_directory(string $path): void
foreach ($files as $file) {
is_dir($file) ? remove_directory($file) : unlink($file);
}
rmdir($path);
file_exists($path) && rmdir($path);
}
try {
remove_directory('./files');
!file_exists('./files/') && mkdir('./files/');
$json = read_collection_file($argv[1] ?? throw new Exception('ERROR: No collection file specified'));
define('OUTPUT_DIR', './files/' . $json->info->name);
remove_directory(OUTPUT_DIR);
!file_exists(OUTPUT_DIR) && mkdir(OUTPUT_DIR, recursive: true) || throw new Exception(
'ERROR: Cannot create output directory: ' . OUTPUT_DIR
);
process_item($json->item);
} catch (Exception $e) {
out($e->getMessage());