forked from gulpjs/gulp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request gulpjs#629 from superlukas/patch-1
Sexier option parsing example
- Loading branch information
Showing
1 changed file
with
12 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,30 @@ | ||
# Pass arguments from the command line | ||
|
||
```js | ||
// npm install --save-dev gulp yargs gulp-if gulp-uglify | ||
// npm install --save-dev gulp gulp-if gulp-uglify minimist | ||
|
||
var args = require('yargs').argv; | ||
var gulp = require('gulp'); | ||
var gulpif = require('gulp-if'); | ||
var uglify = require('gulp-uglify'); | ||
|
||
var isProduction = args.type === 'production'; | ||
var minimist = require('minimist'); | ||
|
||
var knownOptions = { | ||
string: 'env', | ||
default: { env: process.env.NODE_ENV || 'production' } | ||
}; | ||
|
||
var options = minimist(process.argv.slice(2), knownOptions); | ||
|
||
gulp.task('scripts', function() { | ||
return gulp.src('**/*.js') | ||
.pipe(gulpif(isProduction, uglify())) // only minify in production | ||
.pipe(gulpif(options.env === 'production', uglify())) // only minify in production | ||
.pipe(gulp.dest('dist')); | ||
}); | ||
``` | ||
|
||
The run gulp with: | ||
Then run gulp with: | ||
|
||
```sh | ||
$ gulp scripts --type production | ||
$ gulp scripts --env development | ||
``` |