Code Monkey home page Code Monkey logo

gulp-patterns's Introduction

gulp-patterns

You've built your JavaScript application but how do you automate testing, code analysis, running it locally or deploying it? These redundant tasks can consume valuable time and resources. Stop working so hard and take advantage of JavaScript task automation using Gulp to streamline these tasks and give you back more time in the day. Studying this repo can help clarify how Gulp works, jump-start task automation with Gulp, find and resolve issues faster, and be a more productive.

Build Status

Codeship Status for johnpapa/gulp-patterns

Requirements

  • Install Node
    • on OSX install home brew and type brew install node
    • on Windows install chocolatey
      • More tips on Windows with node
      • open command prompt as administrator
        • type choco install nodejs
        • type choco install nodejs.install
    • On OSX you can alleviate the need to run as sudo by following these instructions. I highly recommend this step on OSX
  • Open terminal
  • Type npm install -g node-inspector bower gulp tsd

Quick Start

Clone this repo and run the content locally

$ npm install
$ bower install
$ tsd install -r -o --save
$ gulp serve-dev

Tasks

Task Listing

  • gulp help

    Displays all of the available gulp tasks.

Code Analysis

  • gulp vet

    Performs static code analysis on all javascript files. Runs jshint and jscs.

  • gulp vet --verbose

    Displays all files affected and extended information about the code analysis.

  • gulp plato

    Performs code analysis using plato on all javascript files. Plato generates a report in the reports folder.

Testing

  • gulp serve-specs

    Serves and browses to the spec runner html page and runs the unit tests in it. Injects any changes on the fly and re runs the tests. Quick and easy view of tests as an alternative to terminal via gulp test.

  • gulp test

    Runs all unit tests using karma runner, mocha, chai and sinon with phantomjs. Depends on vet task, for code analysis.

  • gulp test --startServers

    Runs all unit tests and midway tests. Cranks up a second node process to run a server for the midway tests to hit a web api.

  • gulp autotest

    Runs a watch to run all unit tests.

  • gulp autotest --startServers

    Runs a watch to run all unit tests and midway tests. Cranks up a second node process to run a server for the midway tests to hit a web api.

Cleaning Up

  • gulp clean

    Remove all files from the build and temp folders

  • gulp clean-images

    Remove all images from the build folder

  • gulp clean-code

    Remove all javascript and html from the build folder

  • gulp clean-fonts

    Remove all fonts from the build folder

  • gulp clean-styles

    Remove all styles from the build folder

Fonts and Images

  • gulp fonts

    Copy all fonts from source to the build folder

  • gulp images

    Copy all images from source to the build folder

Styles

  • gulp styles

    Compile less files to CSS, add vendor prefixes, and copy to the build folder

Bower Files

  • gulp wiredep

    Looks up all bower components' main files and JavaScript source code, then adds them to the index.html.

    The .bowerrc file also runs this as a postinstall task whenever bower install is run.

Angular HTML Templates

  • gulp templatecache

    Create an Angular module that adds all HTML templates to Angular's $templateCache. This pre-fetches all HTML templates saving XHR calls for the HTML.

  • gulp templatecache --verbose

    Displays all files affected by the task.

Serving Development Code

  • gulp serve-dev

    Serves the development code and launches it in a browser. The goal of building for development is to do it as fast as possible, to keep development moving efficiently. This task serves all code from the source folders and compiles less to css in a temp folder.

  • gulp serve-dev --nosync

    Serves the development code without launching the browser.

  • gulp serve-dev --debug

    Launch debugger with node-inspector.

  • gulp serve-dev --debug-brk

    Launch debugger and break on 1st line with node-inspector.

  • gulp serve-dev --stubs

    Serves the development code with the stubs to avoid hitting a real backend

Building Production Code

  • gulp optimize

    Optimize all javascript and styles, move to a build folder, and inject them into the new index.html

  • gulp build

    Copies all fonts, copies images and runs gulp optimize to build the production code to the build folder.

Serving Production Code

  • gulp serve-build

    Serve the optimized code from the build folder and launch it in a browser.

  • gulp serve-build --nosync

    Serve the optimized code from the build folder and manually launch the browser.

  • gulp serve-build --debug

    Launch debugger with node-inspector.

  • gulp serve-build --debug-brk

    Launch debugger and break on 1st line with node-inspector.

gulp-patterns's People

Contributors

dciccale avatar energeticpixels avatar johnpapa avatar turkingj avatar wardbell avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gulp-patterns's Issues

Gulp-useref has been updated with a new API

There as been an update to gulp-useref (v3) which changes the API breaking the gulp file in this project. The use of $.useref.assets({searchPath: './'}) is now incorrect; resulting in the error TypeError: $.useref.assets is not a function

dashboard template testing

Hi,

I was currently looking into how gulp-patterns tests, and everything seems fine except when for example we want to test a main layout, like dashboard. (so testing the layout, not the controller)

    el = angular.element(
        '<div ng-include="app/dashboard/dashboard.html" ng-controller="Dashboard as vm"></div>');

    scope = $rootScope;
    var compiledEl = $compile(el)(scope);

    // The spec examines changes to these template parts
    searchElement = compiledEl.find('.dashboard .searchBtn'); // the button to click

What I don't understand is why the compiledEl has no include at all.

This project creates/generates the templates.js file, which includes all layout templates. so the $templateCache is populated. The karma seems to be configured to work with that.

but still, I cannot get the simple dashboard to work.

Any suggestion ?

Error: Cannot find module 'utf8'

Following instructions on the ReadMe doc for the installation process and came up with the error below. The weird thing is that I did the same process yesterday with no problems. Trying it again to see if it happens will keep the issue updated.

Error: Cannot find module 'utf8'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object. (......../gulp-patterns/node_modules/browser-sync/node_modules/socket.io/node_modules/engine.io/node_modules/engine.io-parser/lib/index.js:5:12)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)

Error in plugin 'gulp-inject'

Hello
I'm starting with angular and when starting the server to check if all is well, I have this error
C: \ angular> gulp
[14:12:09] Using gulpfile C: \ angular\ gulpfile.js
[14:12:09] Starting 'server' ...
[14:12:09] Finished 'server' after 24 ms
[14:12:09] Starting 'inject' ...
[14:12:09] 'inject' errored after 17 ms
[14:12:09] Error in plugin 'Gulp-inject'
message:
There is no read option. Did you mean to Provide for gulp.src Perhaps it?
[14:12:09] Server started http: // localhost: 3000
[14:12:09] LiveReload started on port 35729

I am using this version [email protected]

Someone could help me. Thanks for your time

Running gulp commands is changing source files

Great project - I am finding it very useful to learn gulp best practices.

It seems that running gulp commands is changing source files. For example:

  1. gulp serve-dev is changing src/client/index.html.
  2. gulp serve-specs is changing src/client/specs.html.

Is this intentional?

running fonts function 2 times can fail, add a wait will help

When you use clean-fonts in the fonts task and you run the fonts task twice,it will fail 1 time.

image

If you run them separate , so first gulp task clean-fonts and then fonts,it works always.

It seems there is some problem with the creation of the 4.4.0 subdirectory data in the pipeline.

When you put a wait before the copy it works for me, you can put a wait of 50 instead of 1500,but with 1500 you see the directories dissapear and re-appear in visual studio code.

I think the bug is that the copy starts for the fonts in the directory and subdirectories while the deletion and creation of the subdirectories is still not done yet.

gulp.task('fonts', ['clean-fonts'], function () {

log('Copying fonts');

return gulp

.src(config.root + config.fonts)

.pipe($.wait(1500))

.pipe(gulp.dest(config.build + 'fonts'));

});

DeleteShare

should have called dataservice.getCustomer fails

Test fails for:
it('with id ' + id, function() {
expect(dataservice.getCustomer).to.have.been.calledWith(id);
});
getCustomer() doesn't appear to get the .withArgs(id)

should have called dataservice.getCustomer
1 time

with id 1017109

AssertionError: expected getCustomer to have been called with arguments 1017109
getCustomer(undefined) => {
$$state: {
pending: undefined,
processScheduled: false,
status: 1,
value: {
city: "Albany",
firstName: "Black",
id: 1017109,
lastName: "Widow",
state: "NY",
thumbnail: "colleen_papa.jpg",
zip: "12205"
}
}
}
at Context. (src/client/app/customers/customer-detail.controller.spec.js:38:66)
expect(dataservice.getCustomer).to.have.been.calledWith(id);

[...]
beforeEach(function() {
sinon.stub(dataservice, 'getCustomer')
.returns($q.when(mockData.blackWidow))
.withArgs(id);
[...]
describe('after activate', function() {
[...]
it('with id ' + id, function() {
expect(dataservice.getCustomer).to.have.been.calledWith(id);
});
[...]

Circular dependency with bower

Ok, after playing around with some systems and some people, a problem was highlighted.
0) "npm install gulp bower -g"

  1. "bower install" calls "gulp wiredep"
  2. "npm install" will call "bower install".
  3. "gulp " requires step 2.

.bowerrc
{
"directory": "bower_components",
"scripts": {
"postinstall": "gulp wiredep"
}
}

npm package.json
"scripts": {
"init": "npm install",
"install": "bower install",
"start": "node src/server/app.js",
"test": "gulp test"
},

So, i think for projects that have existent node_modules and bower_components, the problem is not really visible.

For a fresh project checkout, the circular dependency will show up.
"gulp wiredep"
although convinient, seem to me a bit harmful.

That target anyways is called for "gulp build" or any variation of "gulp serve-". So it won't have side-effects to remove "gulp wiredep" from the bower .bowerrc file.

del 2.x has no callback function

// del 1.x
del(patterns, [options], callback)

// del 2.x
del(patterns, [options])

So, clean function should upgrade like:

// with del 1.x
function clean(path, done) {
    log('Cleaning: ' + $.util.colors.blue(path));
    del(path, done);
}

// with del 2.x
function clean(path) {
    return del(config.distribution.scripts);
}

clean functions do no longer work due to change in nodes del plugin

Description from this (http://stackoverflow.com/questions/32770896/nodes-del-command-callback-not-firing) Stackoverflow question:
"The callback on the del function is not firing. The del function is running, file are deleted, I see no error messages. If I call the callback manually it executes, so looks like the function is in tact. So I am wondering what would cause del not to want to execute the callback."

This seems to be the solution:
"The [del] API now returns a promise instead of accepting a callback function."
https://github.com/sindresorhus/del/releases/tag/v2.0.0

'Cannot GET /' on http://localhost:3000/

Please help me out fixing this. After gulp serve-build completes Firefox launches with this error message. It seems Firefox doesn't get connected. There is no 'browser connected' output in the console.

Production Deployment

Hey John - Great course - entertaining and educational as always. One question - what's the deployment strategy? running gulp build (or it's variants) - only outputs the client-side code to the build folder. What's the process/strategy for deploying the server-side code as well?

What is bardjs?

Can you please explain the purpose of bardjs? It seems to me that following two are equivalent (one using bard and another without):

beforeEach(inject(function($controller, $q, $rootScope, 'dataservice') { }

beforeEach(function() {
    bard.inject('$controller', '$q', '$rootScope', 'dataservice');
});

Installation requires gulp-less

I cloned the repository and followed the instructions, but when I got to the "gulp serve-dev" step, I received the following error:

/Users/rchawdry/tmp/gulptest/gulp-patterns/node_modules/gulp-less/index.js:68
}).done(undefined, cb);
^
TypeError: undefined is not a function
at DestroyableTransform._transform (/Users/rchawdry/tmp/gulptest/gulp-patterns/node_modules/gulp-less/index.js:68:8)
at DestroyableTransform.Transform._read (/Users/rchawdry/tmp/gulptest/gulp-patterns/node_modules/gulp-less/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:184:10)
at DestroyableTransform.Transform._write (/Users/rchawdry/tmp/gulptest/gulp-patterns/node_modules/gulp-less/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:172:12)
at doWrite (/Users/rchawdry/tmp/gulptest/gulp-patterns/node_modules/gulp-less/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:237:10)
at writeOrBuffer (/Users/rchawdry/tmp/gulptest/gulp-patterns/node_modules/gulp-less/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:227:5)
at DestroyableTransform.Writable.write (/Users/rchawdry/tmp/gulptest/gulp-patterns/node_modules/gulp-less/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:194:11)
at write (/Users/rchawdry/tmp/gulptest/gulp-patterns/node_modules/gulp-plumber/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:623:24)
at flow (/Users/rchawdry/tmp/gulptest/gulp-patterns/node_modules/gulp-plumber/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:632:7)
at DestroyableTransform.pipeOnReadable (/Users/rchawdry/tmp/gulptest/gulp-patterns/node_modules/gulp-plumber/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:664:5)
at DestroyableTransform.emit (events.js:129:20)

Installing gulp-less via "npm install gulp-less" fixed the issue. Wasn't sure if this needed to be added to the package.json to make sure its downloaded before use.

wiredep broken when targeting bootstrap 3.3.5

Just an FYI, the latest version of bootstrap breaks the wiredep functionality and does not properly inject the bootstap.css file when you get to the "Removing Scripts and Styles From the Main HTML" section.

The fix for me was to simply target bootstrap 3.3.2 directly. Removing the ~ from the bower.json file entry and running bower install again forced 3.3.2.

Awesome course!

I think i found a bug in the inject function

John I believe i found a bug in the inject/order functions.
The read option {read: false} is given to the gulp-inject function instead of the gulp.src.
So i got this error because of it:
[14:35:17] Wiring the bower dependencies into the html
[14:35:17] 'wiredep' errored after 51 ms
[14:35:17] Error in plugin 'gulp-inject'
Message:
There is no read option. Did you mean to provide it for gulp.src perhaps?

Rg,
Stefan

doubts on the build process

Hi, there,

I'm trying to migrate a Gulp process to include Typescript and I'm stumbling upon several issues I'd be more than happy to discuss with its team, but on its issues page, they just redirect discussions to stackoverflow; weel, I posted my question there, but I'm not having much feedback. Since this is an important source for many developers, I hope my questions about the build process of an Angular application will be heard:

  1. why use Bower? all bower packages yoman bloated into the initial build process exist in Npm; I don't like using more than one single tool for the same job;
  2. why use an automatic plugin loader? it's easy, too easy, to forget plugins used for tests on local environment; having them available to the Gulp process feels like magic (or voodoo, pick yours);
  3. why automatically binding the automatically loaded plugins on their names on "gulp.config.js"? one very nice feature of the Gulp streams is to allow composition in a very natural way, but using this pattern just make it not possible and turns Gulp almost into ... Grunt;
  4. why not use javascript-aware tool to make the bundle (like browserify, webpack, ...)? bundling all javascript files together requires for the developer to know all the subtle dependencies among libraries -- I already had a hard to spot problem when jQuery was loaded after Angular and the pseudo selector ":first" would yield nothing, without any compilation or run-time error;
  5. why use such plugins as "gulp-inject" or "usemin"? I see Angular applications as javascript that happen to have HTML views, instead of HTML pages that happen to have javascript behavior; that way, I just have exactly two 'script' tags, one for my code, one for vendor's and the same for css; I believe file transversing should follow the language specifications (JS/TS and CSS) and produce deterministic outputs that could be statically named on the application loader (index.html).
  6. why include image optimization into the application build process?

Please consider these questions as very sincere -- of course, it's possible to spot some preferences, due to many headaches I had before but, utterly -- I want to understand the whys, as there are many, many ways to solve the same problem.

Integration of RequireJS

Is it possible to integrate this particular test app with requireJS, so it would be more scalable ?

Thanks

Mikhail

production build failing while trying to inject file into index

I am evaluating the possibility to use your gulp-patterns as a starting point for several web dev projects we have for this fiscal year and next. The build on the development side works just fine. But when I attempt to build for production I am running into node error directly or during gulp-inject 1 file into index.html (its using gulp-ng-annotate (ng-annotate-main.js at line 330). Node is having issues with Cannot assign to read only property '$methodName' of false.

I have double checked to make sure that I am using the same version of ng-annotate that you are in your project. Got any ideas??
Tony

gulp-patterns_nodeerror

Localization support

Hi John,

I have done some related-work recently for localization support.

  • It was quite nice implemented using (https://angular-gettext.rocketeer.be/).
  • Extended gulpfile.js/gulpconfig.js: added a task for extracting translations and a task for compiling the translations.
  • Added a /localization/ directory containing: template.pot and locale like en_US.po, etc.

There was a minor problem with making translations dynamically available, since they need a 1 time compilation. So I generated them anyway in the /build/ directory, and make the server recognize them.

I could provide the changes I did, even if they are not optimal.

Cheers,
Igor

dependent task is not called

when called templatecache clean-code is called but not template cache.

gulp.task('templatecache', ['clean-code'], function () {
log('Creating AngularJs templateCache');
return gulp.src(config.htmltemplates)
.pipe($.minifyHtml({ empty: true }))
.pipe($.angularTemplatecache(config.templateCache.files, config.templateCache.options))
.pipe(gulp.dest(config.temp));
});

image

Following this guide produces an error

When I go through the following steps:
$ npm install
$ bower install
$ tsd install -r -o --save
$ gulp serve-dev

The final step results in the following error:
gulp-patterns] Watching files...
About to crank up node
PORT=7203
NODE_ENV=dev
** DEV **
events.js:85
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at exports._errnoException (util.js:746:11)
at Server._listen2 (net.js:1129:14)
at listen (net.js:1155:10)
at Server.listen (net.js:1240:5)
at EventEmitter.listen (/##########/gulpTest/node_modules/express/lib/application.js:617:24)
at Object. (/##########/gulpTest/src/server/app.js:63:5)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
[10:15:38] *** nodemon crashed: script crashed for some reason
[10:15:38] [nodemon] app crashed - waiting for file changes before starting...

NOTE: I replaced the path that was displayed in the output with ##########.

Routing issue after the build process

I have the problem with going to the specific page of the application after the build was created and app was moved to the web server.

Example: App was moved to the server www.testserver.com to the folder angular. I've changed the base href attribute in the index.html file after the build:

We are trying to access it using http://www.testserver.com/angular/customers for example but getting 404 and route is not being hit at all even though all js files were loaded.

Is that a known issue ?

BTW If I try access the page using the pound sign reference, the app at least tries to load the correct route.

http://www.testserver.com/angular/#/customers

Thanks

Mikhail

Target/config profiles support.

Hi John,

I have done some related-work recently where we target different deployments.

The basic requirement was configuration based on 3 profiles (production, integration/test, local) that included:

  • configuration of 1 or many REST endpoints
  • the root base the application it self. (contained usually in the index.html)

The gulp-config works well for project structure/resources related things but it works less good when deployment-related configuration is needed.

What I did was:

  • created a /profile/ directory containing a json file per "profile target"
    {
    "end-point-url" : ''http://...."
    "app-root-base" : "/test/"
    }
  • Extend the gulpfile.js/gulp.config.js files to add a profile variable so that we can call:
    gulp build --profile production
    in the case a /profile/production.json file exists, then that configuration would be used.
  • There would be some constants/configuration js file that has some @@constants_ids that would be replaced when matched.
  • Process the JS for constant replacement
  • Text-replace the to whatever is declared under "app-root-base"

The challenge was of course how to keep the configuration working for both:
1)the "production" processed/optimized code/application. (easy case since it just go through the regular gulp stream)
2)the local dev non-optimized. (difficult case)

The reason why #2 is difficult is because the resources js/html are not processed by gulp, therefore no text-replace nor constant js can be generated that would be recognized. I don't like my solution since @@constants_ids has default value in case it does not get replaced.
=> Effectively making /profile/localdev.json a difficult case.
Also, one cannot cleanly update index.html, otherwise, it would be check-in depending on the profile you used on the gulp call. (this is less of a problem, as the root "/" is good enough)

Perhaps there would be a more cleaver way to achieve it ?

(I could provide the changes I did, even if they are clearly ugly.)

Cheers

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.