浏览代码

v0.2.0 - a bit more option parsing; fix run with empty JSON options; added more excluded files; changed excluded parsing; a bit more error messages; added PHP hashbang and made file executable

master
父节点
当前提交
2d3db39b3d
共有 1 个文件被更改,包括 21 次插入21 次删除
  1. +21
    -21
      dir2json.php

+ 21
- 21
dir2json.php 查看文件

@@ -18,18 +18,18 @@ $DIR2JSON = '0.2.0';
function dir2json($dir) function dir2json($dir)
{ {
$dirList = []; $dirList = [];
$excludes = [
".",
"..",
"Thumb.db",
"Thumbs.db",
".DS_Store",
".DS_Store?",
".Spotlight-V100",
".Trashes",
"ehthumbs.db",
];
$excludes = [
".",
"..",
"Thumb.db",
"Thumbs.db",
".DS_Store",
".DS_Store?",
".Spotlight-V100",
".Trashes",
"ehthumbs.db",
];


if($handler = opendir($dir)) if($handler = opendir($dir))
{ {
@@ -38,7 +38,7 @@ function dir2json($dir)
if (!in_array($content, $excludes)) if (!in_array($content, $excludes))
{ {
if(is_file($dir."/".$content)) $dirList[] = $content; if(is_file($dir."/".$content)) $dirList[] = $content;
else if(is_dir($dir."/".$content)) $dirList[$content] = dir2json($dir."/".$content);
else if(is_dir($dir."/".$content)) $dirList[$content] = dir2json($dir."/".$content);
} }
} }
closedir($handler); closedir($handler);
@@ -49,7 +49,7 @@ function dir2json($dir)
// Long and short help opts // Long and short help opts
if ($argv[1] === "-h" || $argv[1] === "--help") if ($argv[1] === "-h" || $argv[1] === "--help")
{ {
echo <<<EOT
echo <<<EOT
------------------------------------------------------ ------------------------------------------------------
dir2json - v$DIR2JSON dir2json - v$DIR2JSON


@@ -57,7 +57,7 @@ by Ryan & Tavinus, 2015-2018
http://www.ryadel.com/ http://www.ryadel.com/
https://github.com/Darkseal/dir2json https://github.com/Darkseal/dir2json
------------------------------------------------------ ------------------------------------------------------
USAGE (from CLI): USAGE (from CLI):
> ./dir2json.php <targetFolder> <outputFile> [JSON_OPTIONS] > ./dir2json.php <targetFolder> <outputFile> [JSON_OPTIONS]


@@ -66,7 +66,7 @@ EXAMPLE:


HELP: HELP:
> ./dir2json.php -h > ./dir2json.php -h
JSON_OPTIONS is a bitmask consisting of: JSON_OPTIONS is a bitmask consisting of:
JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK,
JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_PRESERVE_ZERO_FRACTION, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_PRESERVE_ZERO_FRACTION,
@@ -92,21 +92,21 @@ $jsonOptions = empty($jsonOptions) ? 0 : constant($jsonOptions);


// If we have a folder to read // If we have a folder to read
if (!is_dir($targetFolder)) { if (!is_dir($targetFolder)) {
echo "Cannot open folder $targetFolder\n";
exit(2);
echo "Cannot open folder $targetFolder\n";
exit(2);
} }


// If we have and output file name // If we have and output file name
if (empty($outputFile)) { if (empty($outputFile)) {
echo "Need a valid output file name (empty)\n"; echo "Need a valid output file name (empty)\n";
exit(3);
exit(3);
} }


$arr = dir2json($targetFolder); $arr = dir2json($targetFolder);
$json = json_encode($arr, $jsonOptions); $json = json_encode($arr, $jsonOptions);
if (!file_put_contents($outputFile, $json)) { if (!file_put_contents($outputFile, $json)) {
echo "Could not save output file: $outputFile\n";
exit(4);
echo "Could not save output file: $outputFile\n";
exit(4);
} }


exit(0); exit(0);


正在加载...
取消
保存