Skip to content

Commit af90966

Browse files
committed
cmd: add --bundle to explicitly merge/concatenate input files, clarifies previous decision that depended on the output name to end in a slash
1 parent 0e65f72 commit af90966

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

cmd/minify/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ sudo apt-get install minify
6262

6363
Options:
6464
-a, --all Minify all files, including hidden files and files in hidden directories
65+
-b, --bundle Bundle files by concatenation into a single file
6566
--cpuprofile string Export CPU profile
6667
--css-precision int Number of significant digits to preserve in numbers, 0 is all (default 0)
6768
-h, --help Show usage

cmd/minify/bash_completion

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ _minify_complete()
66

77
cur_word="${COMP_WORDS[COMP_CWORD]}"
88
prev_word="${COMP_WORDS[COMP_CWORD-1]}"
9-
flags="-a --all -l --list --match --mime -o --output -r --recursive --type --url -v --verbose --version -w --watch --css-precision --html-keep-conditional-comments --html-keep-default-attrvals --html-keep-document-tags --html-keep-end-tags --html-keep-quotes --html-keep-whitespace --json-precision --svg-precision -s --sync --xml-keep-whitespace --cpuprofile --memprofile"
9+
flags="-a --all --bundle --cpuprofile -l --list --match --memprofile --mime -o --output -r --recursive --type --url -v --verbose --version -w --watch --css-precision --html-keep-conditional-comments --html-keep-default-attrvals --html-keep-document-tags --html-keep-end-tags --html-keep-quotes --html-keep-whitespace --json-precision --svg-precision -s --sync --xml-keep-whitespace"
1010
mimes="text/css text/html text/javascript application/javascript application/json image/svg+xml text/xml application/xml"
1111
types="css html js json svg xml"
1212

cmd/minify/main.go

+20-11
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ var (
5353
version bool
5454
watch bool
5555
sync bool
56+
bundle bool
5657
)
5758

5859
type Task struct {
@@ -120,6 +121,7 @@ func run() int {
120121
flag.BoolVarP(&verbose, "verbose", "v", false, "Verbose")
121122
flag.BoolVarP(&watch, "watch", "w", false, "Watch files and minify upon changes")
122123
flag.BoolVarP(&sync, "sync", "s", false, "Copy all files to destination directory and minify when filetype matches")
124+
flag.BoolVarP(&bundle, "bundle", "b", false, "Bundle files by concatenation into a single file")
123125
flag.BoolVarP(&version, "version", "", false, "Version")
124126

125127
flag.StringVar(&siteurl, "url", "", "URL of file to enable URL minification")
@@ -144,14 +146,14 @@ func run() int {
144146
fmt.Printf("Try 'minify --help' for more information\n")
145147
return 1
146148
}
147-
rawInputs := flag.Args()
148-
useStdin := len(rawInputs) == 0
149+
inputs := flag.Args()
150+
useStdin := len(inputs) == 0
149151

150152
Error = log.New(os.Stderr, "ERROR: ", 0)
151153
if verbose {
152-
Info = log.New(os.Stderr, "INFO: ", 0)
154+
Info = log.New(os.Stderr, "", 0)
153155
} else {
154-
Info = log.New(ioutil.Discard, "INFO: ", 0)
156+
Info = log.New(ioutil.Discard, "", 0)
155157
}
156158

157159
if help {
@@ -259,13 +261,14 @@ func run() int {
259261
// set output, empty means stdout, ending in slash means a directory, otherwise a file
260262
dirDst := false
261263
if output != "" {
264+
if !bundle && 1 < len(inputs) && output[len(output)-1] != '/' {
265+
output += "/"
266+
}
262267
output = sanitizePath(output)
263-
if output[len(output)-1] == '/' {
264-
dirDst = true
265-
if err := os.MkdirAll(output, 0777); err != nil {
266-
Error.Println(err)
267-
return 1
268-
}
268+
dirDst = output[len(output)-1] == '/'
269+
if dirDst && bundle {
270+
Error.Println("--bundle requires destination to be stdout or a file")
271+
return 1
269272
}
270273
}
271274
if !dirDst && (sync || watch) {
@@ -291,6 +294,12 @@ func run() int {
291294
Info.Println("minify from stdin")
292295
}
293296
}
297+
if dirDst {
298+
if err := os.MkdirAll(output, 0777); err != nil {
299+
Error.Println(err)
300+
return 1
301+
}
302+
}
294303

295304
var tasks []Task
296305
var roots []string
@@ -303,7 +312,7 @@ func run() int {
303312
tasks = append(tasks, task)
304313
roots = append(roots, "")
305314
} else {
306-
tasks, roots, err = createTasks(rawInputs, output)
315+
tasks, roots, err = createTasks(inputs, output)
307316
if err != nil {
308317
Error.Println(err)
309318
return 1

0 commit comments

Comments
 (0)