From 7fb3f323f14e8ceefdf5f7cacdf0d83dcae6b586 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Mon, 24 Aug 2015 22:25:02 +0200 Subject: [PATCH] gulp: gulp-my-tasks, npm module for running my gulp tasks In this way I should be able to use the same tasks in any AngularJS project (if it follows my patterns) --- gulp/gulp-my-tasks/index.js | 3 + gulp/gulp-my-tasks/package.json | 58 ++++ gulp/gulp-my-tasks/tasks/.htmlhintrc | 14 + gulp/gulp-my-tasks/tasks/.jscsrc | 79 +++++ gulp/gulp-my-tasks/tasks/.jshintrc | 69 ++++ gulp/gulp-my-tasks/tasks/favicon.ico | Bin 0 -> 318 bytes gulp/gulp-my-tasks/tasks/karma.conf.js | 105 +++++++ gulp/gulp-my-tasks/tasks/server.config.js | 12 + gulp/gulp-my-tasks/tasks/server.js | 50 +++ gulp/gulp-my-tasks/tasks/tasks.config.js | 99 ++++++ gulp/gulp-my-tasks/tasks/tasks.js | 505 ++++++++++++++++++++++++++++++ 11 files changed, 994 insertions(+) create mode 100644 gulp/gulp-my-tasks/index.js create mode 100644 gulp/gulp-my-tasks/package.json create mode 100644 gulp/gulp-my-tasks/tasks/.htmlhintrc create mode 100644 gulp/gulp-my-tasks/tasks/.jscsrc create mode 100644 gulp/gulp-my-tasks/tasks/.jshintrc create mode 100644 gulp/gulp-my-tasks/tasks/favicon.ico create mode 100644 gulp/gulp-my-tasks/tasks/karma.conf.js create mode 100644 gulp/gulp-my-tasks/tasks/server.config.js create mode 100644 gulp/gulp-my-tasks/tasks/server.js create mode 100644 gulp/gulp-my-tasks/tasks/tasks.config.js create mode 100644 gulp/gulp-my-tasks/tasks/tasks.js diff --git a/gulp/gulp-my-tasks/index.js b/gulp/gulp-my-tasks/index.js new file mode 100644 index 0000000..97958dd --- /dev/null +++ b/gulp/gulp-my-tasks/index.js @@ -0,0 +1,3 @@ +module.exports = function(gulp, config){ + require(__dirname + '/tasks/tasks.js')(gulp, config) +}; diff --git a/gulp/gulp-my-tasks/package.json b/gulp/gulp-my-tasks/package.json new file mode 100644 index 0000000..16cbd2b --- /dev/null +++ b/gulp/gulp-my-tasks/package.json @@ -0,0 +1,58 @@ +{ + "name": "gulp-my-tasks", + "version": "0.0.1", + "description": "Gulp tasks for AngularJS projects", + "author": { + "name": "Gustavo Martin Morcuende", + "email": "noemail@noemail.invalid", + "url": "http://gumartinm.name" + }, + "homepage": "http://gumartinm.name", + "license": "Apache-2.0", + "main" : "index.js", + "dependencies": { + "del": "~1.2.1", + "express": "~4.13.3", + "express-http-proxy": "~0.6.0", + "extend": "~3.0.0", + "gulp-angular-filesort": "~1.1.1", + "gulp-angular-templatecache": "~1.7.0", + "gulp-batch": "~1.0.5", + "gulp-bytediff": "~1.0.0", + "gulp-filter": "~3.0.0", + "gulp-header": "~1.2.2", + "gulp-htmlhint": "~0.3.0", + "gulp-if": "~1.2.5", + "gulp-inject": "~1.5.0", + "gulp-jscs": "~2.0.0", + "gulp-jshint": "~1.11.2", + "gulp-load-plugins": "~1.0.0-rc.1", + "gulp-minify-css": "~1.2.0", + "gulp-minify-html": "~1.0.4", + "gulp-ng-annotate": "~1.1.0", + "gulp-ngdocs": "~0.2.13", + "gulp-nodemon": "~2.0.3", + "gulp-order": "~1.1.1", + "gulp-print": "~1.1.0", + "gulp-rev": "~5.1.0", + "gulp-rev-replace": "~0.4.2", + "gulp-task-listing": "~1.0.1", + "gulp-uglify": "~1.2.0", + "gulp-useref": "~1.3.0", + "gulp-util": "~3.0.6", + "gulp-watch": "~4.3.5", + "jshint-stylish": "~2.0.1", + "karma": "~0.13.9", + "karma-coverage": "~0.5.0", + "karma-jasmine": "~0.3.6", + "karma-junit-reporter": "~0.3.3", + "karma-phantomjs-launcher": "~0.2.1", + "morgan": "~1.6.1", + "serve-favicon": "~2.3.0", + "wiredep": "~3.0.0-beta", + "yargs": "~3.19.0" + }, + "devDependencies": { + "gulp": "~3.9.0" + } +} diff --git a/gulp/gulp-my-tasks/tasks/.htmlhintrc b/gulp/gulp-my-tasks/tasks/.htmlhintrc new file mode 100644 index 0000000..dc75d5f --- /dev/null +++ b/gulp/gulp-my-tasks/tasks/.htmlhintrc @@ -0,0 +1,14 @@ +{ + "tagname-lowercase": true, + "attr-lowercase": true, + "attr-value-double-quotes": true, + "attr-value-not-empty": false, + "attr-no-duplication" : true, + "doctype-first": true, + "tag-pair": true, + "tag-self-close": false, + "spec-char-escape": true, + "id-unique": true, + "src-not-empty": true, + "head-script-disabled": false +} \ No newline at end of file diff --git a/gulp/gulp-my-tasks/tasks/.jscsrc b/gulp/gulp-my-tasks/tasks/.jscsrc new file mode 100644 index 0000000..194fb59 --- /dev/null +++ b/gulp/gulp-my-tasks/tasks/.jscsrc @@ -0,0 +1,79 @@ +{ + "excludeFiles": ["node_modules/**", "bower_components/**"], + + "requireCurlyBraces": [ + "if", + "else", + "for", + "while", + "do", + "try", + "catch" + ], + "requireOperatorBeforeLineBreak": true, + "requireCamelCaseOrUpperCaseIdentifiers": true, + "maximumLineLength": { + "value": 120, + "allowComments": true, + "allowRegex": true + }, + "validateIndentation": 2, + "validateQuoteMarks": "'", + + "disallowMultipleLineStrings": true, + "disallowMixedSpacesAndTabs": true, + "disallowTrailingWhitespace": true, + "disallowSpaceAfterPrefixUnaryOperators": true, + "disallowMultipleVarDecl": null, + + "requireSpaceAfterKeywords": [ + "if", + "else", + "for", + "while", + "do", + "switch", + "return", + "try", + "catch" + ], + "requireSpaceBeforeBinaryOperators": [ + "=", "+=", "-=", "*=", "/=", "%=", "<<=", ">>=", ">>>=", + "&=", "|=", "^=", "+=", + + "+", "-", "*", "/", "%", "<<", ">>", ">>>", "&", + "|", "^", "&&", "||", "===", "==", ">=", + "<=", "<", ">", "!=", "!==" + ], + "requireSpaceAfterBinaryOperators": true, + "requireSpacesInConditionalExpression": true, + "requireSpaceBeforeBlockStatements": true, + "requireLineFeedAtFileEnd": true, + "disallowSpacesInsideObjectBrackets": "all", + "disallowSpacesInsideArrayBrackets": "all", + "disallowSpacesInsideParentheses": true, + + "jsDoc": { + "checkAnnotations": { + "preset": "jsdoc3", + "extra": { + "ngdoc": true + } + }, + "checkParamNames": true, + "requireParamTypes": true, + "checkReturnTypes": true, + "checkTypes": true + }, + + "disallowMultipleLineBreaks": true, + + "disallowCommaBeforeLineBreak": null, + "disallowDanglingUnderscores": null, + "disallowEmptyBlocks": null, + "disallowTrailingComma": null, + "requireCommaBeforeLineBreak": null, + "requireDotNotation": null, + "requireMultipleVarDecl": null, + "requireParenthesesAroundIIFE": true +} diff --git a/gulp/gulp-my-tasks/tasks/.jshintrc b/gulp/gulp-my-tasks/tasks/.jshintrc new file mode 100644 index 0000000..2e382d2 --- /dev/null +++ b/gulp/gulp-my-tasks/tasks/.jshintrc @@ -0,0 +1,69 @@ +{ + "bitwise": true, + "camelcase": true, + "curly": true, + "eqeqeq": true, + "es3": false, + "forin": true, + "freeze": true, + "immed": true, + "indent": 2, + "latedef": "nofunc", + "newcap": true, + "noarg": true, + "noempty": true, + "nonbsp": true, + "nonew": true, + "plusplus": false, + "quotmark": "single", + "undef": true, + "unused": false, + "strict": false, + "maxparams": 10, + "maxdepth": 5, + "maxstatements": 40, + "maxcomplexity": 8, + "maxlen": 120, + + "asi": false, + "boss": false, + "debug": false, + "eqnull": true, + "esnext": false, + "evil": false, + "expr": false, + "funcscope": false, + "globalstrict": false, + "iterator": false, + "lastsemic": false, + "laxbreak": false, + "laxcomma": false, + "loopfunc": true, + "maxerr": 9999, + "moz": false, + "multistr": false, + "notypeof": false, + "proto": false, + "scripturl": false, + "shadow": false, + "sub": true, + "supernew": false, + "validthis": false, + "noyield": false, + + "browser": true, + "node": true, + + "globals": { + "angular": false, + /* Jasmine */ + "describe" : false, + "expect" : false, + "inject" : false, + "it" : false, + "before" : false, + "beforeEach" : false, + "after" : false, + "afterEach" : false + } +} diff --git a/gulp/gulp-my-tasks/tasks/favicon.ico b/gulp/gulp-my-tasks/tasks/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..b8e4f5fd039c7b2a90507fc023720ed7584c22ef GIT binary patch literal 318 zcmZQzU<5(|0RbS%!l1#(z#zuJz@P!d0zj+)#2|4HXaJKC0wf0pUFRY6&HoGx4}o~v z0|th=y$lRzelaj)%wb@d_=&+`(|-n;r$D()Zy{=tNI^kC2?-#9M2nY~7XrEBB`z-R zK)S?x_wFq~fs$?IW!dh6;@;`TMrvDubaGmfQmSCd?j);Kt4t-*(v%l4Ff5(1SlEhD zP>@xSYt;gvW&uH - <%= pkg.description %>', + ' * @author <%= pkg.author.name %>', + ' * @email <%= pkg.author.email %>', + ' * @url <%= pkg.author.url %>', + ' * @version <%= pkg.version %>', + ' * @link <%= pkg.homepage %>', + ' * @license <%= pkg.license %>', + ' */', + '' + ].join('\n'); + return plugins.header(banner, {pkg: pkg}); + } + + }); + + /** + * @ngdoc function + * + * @description + * Cleans up files in distribution directory. + * + * @returns {undefined} + */ + gulp.task('clean', function(done) { + + log('*** Cleans up directories ***'); + + del.sync([config.build.directory + '**/*', config.temp]); + done(); + }); + + /** + * @ngdoc function + * + * @description + * Creates $templateCache from html templates + * + * @returns {stream} + */ + gulp.task('templatecache', ['clean'], function() { + + log('*** Creating AngularJS $templateCache ***'); + + return gulp + .src(config.html) + .pipe(plugins.if(args.verbose, plugins.bytediff.start())) + .pipe(plugins.minifyHtml({ + empty: true, + spare: true, + quotes: true + })) + .pipe(plugins.if(args.verbose, plugins.bytediff.stop(byteDiffFormat))) + .pipe(plugins.angularTemplatecache(config.templateFile, { + module: 'app.core', + root: 'app/', + standalone: false + } + )) + .pipe(gulp.dest(config.temp)); + }); + + /** + * @ngdoc function + * + * @description + * Generate documentation + * + * @returns {stream} + */ + gulp.task('ngdocs', function() { + var pkg = require($.path.join(process.cwd(), 'package.json')); + var gulpDocs = require('gulp-ngdocs'); + var options = { + html5Mode: true, + startPage: '/api/app', + title: pkg.description + }; + return gulpDocs.sections({ + api: { + glob:['src/**/*.js', '!src/**/*.spec.js'], + api: true, + title: 'API Documentation' + }}) + .pipe(gulpDocs.process(options)) + .pipe(gulp.dest('./docs')); + }); + + /** + * @ngdoc function + * + * @description + * Inject files in a sorted sequence at a specified inject label. + * + * @param {Array|string} source Source files (glob patterns) + * @param {string=} label The label name to be used by gulp-inject. + * @returns {stream} The stream. + */ + function inject(source, label) { + var options = {relative: false}; + if (label) { + options.name = 'inject:' + label; + } + + return plugins.inject( + gulp.src(source) + .pipe(plugins.angularFilesort()), options); + } + + /** + * @ngdoc function + * + * @description + * Runs HTTP server. + * + * @param {string=} [environment='development'] development or production environments. + * @returns {stream} The stream + */ + function server(environment) { + var nodeOptions = { + script: __dirname + '/server.js', + env: { + 'NODE_ENV': environment ? environment : 'development', + 'VERBOSE': args.verbose ? true : false + }, + verbose: args.verbose ? true : false, + watch: [__dirname + '/server.js'], + delay: 250 // 250ms + }; + + return plugins.nodemon(nodeOptions) + .on('restart', function(ev) { + log('HTTP server restarted'); + log('files changed:\n' + ev); + }) + .on('start', function () { + log('HTTP server started'); + synchronization(environment); + }) + .on('crash', function () { + log('HTTP server crashed'); + }) + .on('exit', function () { + log('HTTP server exited'); + }); + } + + /** + * @ngdoc function + * + * @description + * Tests runner, karma launcher. + * + * @param {boolean} singleRun True means run once and end (continuous integration), or keep running (development) + * @param {function} done Callback to fire when karma is done + * @returns {undefined} + */ + function startTests(singleRun, done) { + var wiredep = require('wiredep'); + var bowerFiles = wiredep(config.wiredepKarmaOptions)['js']; + var excludeFiles = []; + var Server = require('karma').Server; + var karma = new Server({ + files: [].concat( + bowerFiles, + process.cwd() + '/src/showcase/app/**/*.module.js', + process.cwd() + '/src/showcase/app/**/*.js' + ), + configFile: __dirname + '/karma.conf.js', + exclude: excludeFiles, + singleRun: singleRun + }, doneKarma); + + karma.start(); + + function doneKarma(result) { + log('Karma completed'); + log('Karma: tests exited with code ' + result); + done(); + } + } + + /** + * @ngdoc function + * + * @description + * Log messages to CLI. + * + * @param {string} message The message to be shown. + * @returns {undefined} + */ + function log(message) { + plugins.util.log(plugins.util.colors.blue(message)); + } + + /** + * @ngdoc function + * + * @description + * Format data generated by the gulp-bytediff plugin. + * + * @param {Object} data The data created by gulp-bytediff plugin. + * @returns {string} + */ + function byteDiffFormat(data) { + var difference = (data.savings > 0) ? ' smaller.' : ' larger.'; + return data.fileName + ' is ' + data.percent + '%' + difference; + } + + /** + * @ngdoc function + * + * @description + * Files synchronization. + * + * @param {string=} [environment='development'] development or production environments. + * @returns {undefined} + */ + function synchronization(environment) { + + log('Files synchronization'); + + var jsFiles = args.stubs ? [].concat(config.jsFilesWithoutSpecs, config.jsFilesStubs) : config.jsFilesWithoutSpecs; + + switch (environment) { + case 'production': + jsFiles = jsFiles.concat(config.html); + plugins.watch(jsFiles, { + name: 'Files synchronization', + verbose: true, + readDelay: 250 + }, plugins.batch(function (events, done) { + // TODO: gulp.start going to be deprecated in gulp 4.0.0 version. Alternatives? + gulp.start('build', done); + })); + break; + case 'ngdocs': + plugins.watch(jsFiles, { + name: 'Files synchronization', + verbose: true, + readDelay: 250 + }, plugins.batch(function (events, done) { + // TODO: gulp.start going to be deprecated in gulp 4.0.0 version. Alternatives? + gulp.start('ngdocs', done); + })); + break; + default: + plugins.watch(jsFiles, { + name: 'Files synchronization', + verbose: true, + readDelay: 250 + }, plugins.batch(function (events, done) { + // TODO: gulp.start going to be deprecated in gulp 4.0.0 version. Alternatives? + gulp.start('wireup', done); + })); + break; + } + } +}; -- 2.1.4