-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support glob paths + support positional file arguments in CLI
- Loading branch information
Showing
6 changed files
with
129 additions
and
45 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,65 @@ | ||
module minijson.cli; | ||
|
||
import minijson.lib : minifyFiles, minifyString; | ||
import minijson.lib : minifyFiles, minifyStrings; | ||
import argparse; | ||
|
||
import std.getopt : getopt, defaultGetoptPrinter, GetoptResult; | ||
@(Command("minijson") | ||
.Description(`minijson: minify json files with support for comments | ||
/** Print help */ | ||
void printHelp(GetoptResult optResult) @trusted | ||
{ | ||
return defaultGetoptPrinter(`minijson: minify json files with support for comments | ||
minijson --file file1.json --file file2.json | ||
minijson --file file1_with_comment.json --file file2_with_comment.json --comment | ||
# Minify the specified files | ||
minijson ./dist/**/*.json ./build/a.json | ||
# Minify the specified files (supports comments) | ||
minijson --comment file1_with_comment.json file2_with_comment.json | ||
# Minify the specified json string | ||
minijson --str '{"some_json": "string_here"}' | ||
minijson --string '{"some_json": "string_here"}' | ||
minijson --string '{"some_json": "string_here"} //comment' --comment | ||
# Minify the specified json string (supports comments) | ||
minijson --comment --str '{"some_json": "string_here"} //comment' | ||
More information at https://github.com/aminya/minijson | ||
`, optResult.options); | ||
`) | ||
) | ||
struct Options | ||
{ | ||
@TrailingArguments string[] files; | ||
bool comment = false; | ||
string[] str; | ||
// (Deprecated) A list of files to minify (for backwards compatiblitity with getopt) | ||
string[] file; | ||
} | ||
|
||
void main(string[] args) @trusted | ||
int actualMain(Options opts) @trusted | ||
{ | ||
string[] files; | ||
string jsonString; | ||
bool hasComment = false; | ||
try | ||
{ | ||
// minify the given files | ||
if (opts.files.length > 0 || opts.file.length > 0) | ||
{ | ||
const auto files = opts.files ~ opts.file; | ||
minifyFiles(files, opts.comment); | ||
} | ||
|
||
auto optResult = getopt(args, "file", "an array of files to minify", &files, "string", | ||
"a json string to minify", &jsonString, "comment", "a flag to support comments in json", &hasComment); | ||
// minify the given string and print to stdout | ||
if (opts.str) | ||
{ | ||
import std.algorithm : each; | ||
import std.stdio : writeln; | ||
|
||
if (optResult.helpWanted || (!files && !jsonString)) | ||
{ | ||
return printHelp(optResult); | ||
} | ||
minifyStrings(opts.str, opts.comment).each!writeln; | ||
|
||
// minify the given files | ||
if (files) | ||
{ | ||
minifyFiles(files, hasComment); | ||
} | ||
} | ||
|
||
// minify the given string and print to stdout | ||
if (jsonString) | ||
catch (Exception e) | ||
{ | ||
import std : write; | ||
import std.stdio : stderr; | ||
|
||
write(minifyString(jsonString, hasComment)); | ||
stderr.writeln("Error: ", e.msg); | ||
return 1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
mixin CLI!Options.main!((args) { return actualMain(args); }); |
This file contains 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
This file contains 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