Compare commits

..

No commits in common. "c5f928dc47f0def1fe242b66741aefaf00694f8b" and "9ed0ddf79b48232991a1d0e0f2c82a3378437750" have entirely different histories.

2 changed files with 13 additions and 54 deletions

View File

@ -10,17 +10,8 @@ use PmConverter\Exceptions\{
DirectoryIsNotWriteableException, DirectoryIsNotWriteableException,
DirectoryNotExistsException}; DirectoryNotExistsException};
/**
* Helper class to work with files and directories
*/
class FileSystem class FileSystem
{ {
/**
* Normalizes a given path
*
* @param string $path
* @return string
*/
public static function normalizePath(string $path): string public static function normalizePath(string $path): string
{ {
$path = str_replace('~', $_SERVER['HOME'], $path); $path = str_replace('~', $_SERVER['HOME'], $path);
@ -28,8 +19,6 @@ class FileSystem
} }
/** /**
* Recursively creates a new directory by given path
*
* @param string $path * @param string $path
* @return string * @return string
* @throws CannotCreateDirectoryException * @throws CannotCreateDirectoryException
@ -49,8 +38,6 @@ class FileSystem
} }
/** /**
* Recursively removes a given directory
*
* @param string $path * @param string $path
* @return void * @return void
* @throws DirectoryIsNotReadableException * @throws DirectoryIsNotReadableException
@ -90,10 +77,11 @@ class FileSystem
} }
/** /**
* Returns content of given directory path
*
* @param string $path * @param string $path
* @return array * @return array
* @throws DirectoryIsNotReadableException
* @throws DirectoryIsNotWriteableException
* @throws DirectoryNotExistsException
*/ */
public static function dirContents(string $path): array public static function dirContents(string $path): array
{ {
@ -104,22 +92,4 @@ class FileSystem
} }
return $records; return $records;
} }
/**
* Checks if a given file is a valid collection json file
*
* @param string $path
* @return bool
*/
public static function isCollectionFile(string $path): bool
{
$path = static::normalizePath($path);
return !empty($path = trim($path))
&& str_ends_with($path, '.postman_collection.json')
&& file_exists($path)
&& is_readable($path)
&& ($json = json_decode(file_get_contents($path), true))
&& json_last_error() === JSON_ERROR_NONE
&& isset($json['collection']['info']['name']);
}
} }

View File

@ -22,7 +22,7 @@ class Processor
/** /**
* Converter version * Converter version
*/ */
public const VERSION = '1.1.1'; public const VERSION = '1.1.0';
/** /**
* @var string[] Paths to collection files * @var string[] Paths to collection files
@ -92,58 +92,47 @@ class Processor
switch ($arg) { switch ($arg) {
case '-f': case '-f':
case '--file': case '--file':
$rawpath = $this->argv[$idx + 1]; $path = FileSystem::normalizePath($this->argv[$idx + 1]);
$normpath = FileSystem::normalizePath($rawpath); if (empty($path) || !str_ends_with($path, '.json') || !file_exists($path) || !is_readable($path)) {
if (!FileSystem::isCollectionFile($normpath)) { throw new InvalidArgumentException('a valid json-file path is expected for -f (--file)');
throw new InvalidArgumentException(
sprintf("this is not a valid collection file:%s\t%s %s", PHP_EOL, $arg, $rawpath)
);
} }
$this->collectionPaths[] = $this->argv[$idx + 1]; $this->collectionPaths[] = $this->argv[$idx + 1];
break; break;
case '-o': case '-o':
case '--output': case '--output':
if (empty($this->argv[$idx + 1])) { if (empty($this->argv[$idx + 1])) {
throw new InvalidArgumentException('-o is required'); throw new InvalidArgumentException('-o expected');
} }
$this->outputPath = $this->argv[$idx + 1]; $this->outputPath = $this->argv[$idx + 1];
break; break;
case '-d': case '-d':
case '--dir': case '--dir':
if (empty($this->argv[$idx + 1])) { if (empty($this->argv[$idx + 1])) {
throw new InvalidArgumentException('a directory path is expected for -d (--dir)'); throw new InvalidArgumentException('a directory path is expected for -d (--dir)');
} }
$normpath = $this->argv[$idx + 1]; $path = $this->argv[$idx + 1];
$files = array_filter( $files = array_filter(
FileSystem::dirContents($normpath), FileSystem::dirContents($path),
static fn($filename) => FileSystem::isCollectionFile($filename) static fn($filename) => str_ends_with($filename, '.json')
); );
$this->collectionPaths = array_unique(array_merge($this?->collectionPaths ?? [], $files)); $this->collectionPaths = array_unique(array_merge($this?->collectionPaths ?? [], $files));
break; break;
case '-p': case '-p':
case '--preserve': case '--preserve':
$this->preserveOutput = true; $this->preserveOutput = true;
break; break;
case '--http': case '--http':
$this->formats[ConvertFormat::Http->name] = ConvertFormat::Http; $this->formats[ConvertFormat::Http->name] = ConvertFormat::Http;
break; break;
case '--curl': case '--curl':
$this->formats[ConvertFormat::Curl->name] = ConvertFormat::Curl; $this->formats[ConvertFormat::Curl->name] = ConvertFormat::Curl;
break; break;
case '--wget': case '--wget':
$this->formats[ConvertFormat::Wget->name] = ConvertFormat::Wget; $this->formats[ConvertFormat::Wget->name] = ConvertFormat::Wget;
break; break;
case '-v': case '-v':
case '--version': case '--version':
die(implode(PHP_EOL, $this->version()) . PHP_EOL); die(implode(PHP_EOL, $this->version()) . PHP_EOL);
case '-h': case '-h':
case '--help': case '--help':
die(implode(PHP_EOL, $this->usage()) . PHP_EOL); die(implode(PHP_EOL, $this->usage()) . PHP_EOL);
@ -211,11 +200,10 @@ class Processor
$this->initConverters(); $this->initConverters();
$this->initCollections(); $this->initCollections();
$count = count($this->collections); $count = count($this->collections);
$current = 0; $current = 1;
$success = 0; $success = 0;
print(implode(PHP_EOL, array_merge($this->version(), $this->copyright())) . PHP_EOL . PHP_EOL); print(implode(PHP_EOL, array_merge($this->version(), $this->copyright())) . PHP_EOL . PHP_EOL);
foreach ($this->collections as $collectionName => $collection) { foreach ($this->collections as $collectionName => $collection) {
++$current;
printf("Converting '%s' (%d/%d):%s", $collectionName, $current, $count, PHP_EOL); printf("Converting '%s' (%d/%d):%s", $collectionName, $current, $count, PHP_EOL);
foreach ($this->converters as $type => $exporter) { foreach ($this->converters as $type => $exporter) {
printf(' > %s', strtolower($type)); printf(' > %s', strtolower($type));
@ -224,6 +212,7 @@ class Processor
printf(' - OK: %s%s', $exporter->getOutputPath(), PHP_EOL); printf(' - OK: %s%s', $exporter->getOutputPath(), PHP_EOL);
} }
print(PHP_EOL); print(PHP_EOL);
++$current;
++$success; ++$success;
} }
$this->printStats($success, $current); $this->printStats($success, $current);