Skip to content
This repository has been archived by the owner on Mar 10, 2022. It is now read-only.

Commit

Permalink
0.0.31: Extending tests, code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
cmtt committed Mar 13, 2016
1 parent cb2174c commit 9f6a2d4
Show file tree
Hide file tree
Showing 19 changed files with 461 additions and 314 deletions.
1 change: 1 addition & 0 deletions .coveralls.yml
@@ -0,0 +1 @@
service_name: travis-pro
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
node_modules
coverage
2 changes: 2 additions & 0 deletions .travis.yml
Expand Up @@ -17,4 +17,6 @@ install:
before_script:
- npm install -g gulp

after_success: 'npm run coveralls'

script: gulp
209 changes: 115 additions & 94 deletions README.md
@@ -1,13 +1,26 @@
# [gulp](http://gulpjs.com)-di

<a href="http://gulpjs.com">
<img align="right" src="http://cmtt.github.io/gulp-di/src/gulp.png">
<img align="right" height="192" src="http://cmtt.github.io/gulp-di/src/gulp.png">
</a>
<p align="left">

<div>
<a href="https://travis-ci.org/cmtt/gulp-di">
<img src="https://travis-ci.org/cmtt/gulp-di.svg?branch=master" alt="Build Status">
<img src="https://img.shields.io/travis/cmtt/gulp-di/master.svg?style=flat-square" alt="Build Status">
</a>
</p>

# [gulp](http://gulpjs.com)-di
<a href="https://www.npmjs.org/package/gulp-di">
<img src="https://img.shields.io/npm/v/gulp-di.svg?style=flat-square" alt="npm version">
</a>
<a href="http://spdx.org/licenses/MIT">
<img src="https://img.shields.io/npm/l/gulp-di.svg?style=flat-square" alt="npm licence">
</a>
<a href="https://coveralls.io/github/cmtt/gulp-di">
<img src="https://img.shields.io/coveralls/cmtt/gulp-di/master.svg?style=flat-square" alt="Code coverage">
</a>
<a href="http://www.ecma-international.org/ecma-262/6.0/">
<img src="https://img.shields.io/badge/ES-2015-F0DB4F.svg?style=flat-square" alt="ECMAScript 2015">
</a>
</div>

gulp-di is a dependency injection framework for the [Gulp](http://gulpjs.com)
streaming build system.
Expand Down Expand Up @@ -64,6 +77,95 @@ module.exports = (gulp, concat) => {
Please read the API notes below for configuring a pattern for packages which
do not start with "gulp-" or "gulp.*".

## Example: Basic refactoring

Instead of declaring all gulp tasks in a single file, gulp-di allows you to
take a more modular approach. You can separate your stream definitions from
configurations while gulp-di handles the majority of your existing calls to
require() for you.

In this example, we will see how we can move a call to gulp.task() to a
separate file.

Additionally, we will see how we can detach a constant (in this case: a glob
matching all files with the "jpg" extension) from the actual task.

The following task

````js
/* gulpfile.js (previous version) */

const gulp = require('gulp');
gulp.task('images', () => {
return gulp.src('./**/*.jpg')
.pipe(gulp.dest('output/'));
});
````

would be re-factored to the file at tasks/images.js as following.

````js
/* tasks/images.js */

module.exports = (gulp, imagesPath) => {
gulp.task('images', () => {
return gulp.src(imagesPath)
.pipe(gulp.dest('output/'));
});
};
````

Notice that the function uses the "imagePath" constant. Such constants can
be defined in your Gulpfile files in order to separate them from the tasks.

Thus, you can now use all of your Functional programming skills with Gulp and
**assemble** your tasks from other declarations and build much more
flexible and re-usable tasks.

gulp-di should help you to reduce your Gulpfile's complexity.
In this example, we will declare additionally the "imagePath" constant which is
being used in our "images" task.

````js
/* gulpfile.js (refactored version) */

const gulp = require('gulp');
let di = require('gulp-di')(gulp);
.tasks('./tasks')
.provide('imagesPath', 'src/**/*.jpg')
.resolve();
````

Additionally, you can now "inject" arbitrary values into your functions, e. g.
run-time configurations, asynchronous methods for usage with Gulp's asynchronous
API, constants etc.

The following example uses constants and modules in order to compose a helper
function.

````js
/* gulpfile.js (refactored version) */

const gulp = require('gulp');
let di = require('gulp-di')(gulp);
.tasks('./tasks')
.provide('sourcePath', 'src') // Provide a string constant.
.module('extensionPath', (sourcePath) => { // Add a module providing a
// helper function to get a
// extension name.

return (extname) => `${sourcePath}/**/*.${extname}`;

})
.task(function (gulp, extensionPath) {
gulp.task('copy-images', function () {
// Copies all *.jpg images from src/ to public/
return gulp.src(extensionPath('jpg')).pipe(gulp.dest('public/'));
});
})
.resolve();
````

## API

### GulpDI(_object_ gulp, _object_ options)
Expand Down Expand Up @@ -312,94 +414,6 @@ function jadeTask(gulp, jade) {
});
}
````
## Example: Basic refactoring

Instead of declaring all gulp tasks in a single file, gulp-di allows you to
take a more modular approach. You can separate your stream definitions from
configurations while gulp-di handles the majority of your existing calls to
require() for you.

In this example, we will see how we can move a call to gulp.task() to a
separate file.

Additionally, we will see how we can detach a constant (in this case: a glob
matching all files with the "jpg" extension) from the actual task.

The following task

````js
/* gulpfile.js (previous version) */

const gulp = require('gulp');
gulp.task('images', () => {
return gulp.src('./**/*.jpg')
.pipe(gulp.dest('output/'));
});
````

would be re-factored to the file at tasks/images.js as following.

````js
/* tasks/images.js */

module.exports = (gulp, imagesPath) => {
gulp.task('images', () => {
return gulp.src(imagesPath)
.pipe(gulp.dest('output/'));
});
};
````

Notice that the function uses the "imagePath" constant. Such constants can
be defined in your Gulpfile files in order to separate them from the tasks.

Thus, you can now use all of your Functional programming skills with Gulp and
**assemble** your tasks from other declarations and build much more
flexible and re-usable tasks.

gulp-di should help you to reduce your Gulpfile's complexity.
In this example, we will declare additionally the "imagePath" constant which is
being used in our "images" task.

````js
/* gulpfile.js (refactored version) */

const gulp = require('gulp');
let di = require('gulp-di')(gulp);
.tasks('./tasks')
.provide('imagesPath', 'src/**/*.jpg')
.resolve();
````

Additionally, you can now "inject" arbitrary values into your functions, e. g.
run-time configurations, asynchronous methods for usage with Gulp's asynchronous
API, constants etc.

The following example uses constants and modules in order to compose a helper
function.

````js
/* gulpfile.js (refactored version) */

const gulp = require('gulp');
let di = require('gulp-di')(gulp);
.tasks('./tasks')
.provide('sourcePath', 'src') // Provide a string constant.
.module('extensionPath', (sourcePath) => { // Add a module providing a
// helper function to get a
// extension name.

return (extname) => `${sourcePath}/**/*.${extname}`;

})
.task(function (gulp, extensionPath) {
gulp.task('copy-images', function () {
// Copies all *.jpg images from src/ to public/
return gulp.src(extensionPath('jpg')).pipe(gulp.dest('public/'));
});
})
.resolve();
````

## FAQ

Expand Down Expand Up @@ -466,6 +480,13 @@ let di = require('gulp-di')(gulp, {

## Changelog

0.0.31 - 03/13/2016

- options.argv for "runningTasks" test
- options.parentDir
- updating documentation
- extending test suite, adding code coverage report

0.0.3 - 03/12/2016

- ES2015 rewrite
Expand Down
7 changes: 5 additions & 2 deletions contrib/help.js
@@ -1,7 +1,7 @@
'use strict';
/*jshint -W089 */

module.exports = function HelpTask (gulp, Package, log, chalk) {
module.exports = function HelpTask (gulp) {

/**
* Modifies the gulp.task() function and builds a list of all tasks.
Expand All @@ -27,6 +27,10 @@ module.exports = function HelpTask (gulp, Package, log, chalk) {
* experimental.
*/

let Package = this.byId('Package', true) || {};
let chalk = this.byId('chalk', true);
let log = this.byId('log', true) || console.log.bind(console);

const parseFn = require('parse-function');
const util = require('util');
const extractComments = require('extract-comments');
Expand Down Expand Up @@ -85,7 +89,6 @@ module.exports = function HelpTask (gulp, Package, log, chalk) {
};
}


if (entry) {
entry.deps = deps;
if (DEBUG) {
Expand Down
9 changes: 5 additions & 4 deletions contrib/running-tasks.js
@@ -1,8 +1,10 @@
'use strict';

module.exports = function RunningTasks (gulp, gutil, log) {
module.exports = function RunningTasks (gulp) {

const DEBUG = this.options.DEBUG;
let log = this.byId('log', true) || console.log.bind(console);
let gutil = this.byId('gutil', true) || { env : { _ : process.argv }};

/**
* Adds a function returning an array of strings, containing all current
Expand All @@ -25,11 +27,10 @@ module.exports = function RunningTasks (gulp, gutil, log) {
log('Adding runningTasks helper...');
}

this.provide('runningTasks', function () {
this.provide('runningTasks', () => {
let tasks = [];
let args = gutil.env._;
let args = this.options.argv || gutil.env._;
let taskNames = Object.keys(gulp.tasks);

// Filter all available task names using gutil.env._

let cliTasks = taskNames.filter(function (name) {
Expand Down
1 change: 0 additions & 1 deletion contrib/standard-task.js
Expand Up @@ -74,7 +74,6 @@ function standardTask (name, src, dest) {
});
`;
let taskFn = eval(`function ${fnName}Task (gulp) { ${body} }; ${fnName}Task;`);

return taskFn;
}

Expand Down

0 comments on commit 9f6a2d4

Please sign in to comment.