├── .travis.yml ├── .gitignore ├── package.json ├── README.md ├── LICENSE ├── test ├── fixtures │ └── package.json └── test.js ├── index.js └── gulpfile.js /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '0.11' 4 | - '0.10' 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # Compiled binary addons (http://nodejs.org/api/addons.html) 20 | build/Release 21 | 22 | # Dependency directory 23 | # Commenting this out is preferred by some people, see 24 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- 25 | node_modules 26 | 27 | # Users Environment Variables 28 | .lock-wscript 29 | test/tmp 30 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gulp-npm-script-sync", 3 | "version": "1.1.0", 4 | "description": "gulp-npm-script-sync ====================", 5 | "main": "index.js", 6 | "directories": { 7 | "test": "test" 8 | }, 9 | "scripts": { 10 | "test": "mocha test/test.js" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/austinpray/gulp-npm-script-sync.git" 15 | }, 16 | "author": "Austin Pray ", 17 | "license": "MIT", 18 | "bugs": { 19 | "url": "https://github.com/austinpray/gulp-npm-script-sync/issues" 20 | }, 21 | "homepage": "https://github.com/austinpray/gulp-npm-script-sync", 22 | "devDependencies": { 23 | "chai": "*", 24 | "gulp": "*", 25 | "mkdirp": "^0.5.0", 26 | "mocha": "*" 27 | }, 28 | "dependencies": { 29 | "detect-indent": "^3.0.0" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | gulp-npm-script-sync [![Build Status](https://travis-ci.org/austinpray/gulp-npm-script-sync.svg?branch=master)](https://travis-ci.org/austinpray/gulp-npm-script-sync) 2 | ==================== 3 | 4 | Syncs all your gulp tasks into the scripts section of your package.json. For instance: `gulp watch` can be run as `npm run watch`. npm will use the local gulp inside your `node_modules` folder, so there is no need to install gulp globally to run the tasks. [inspired by this stackoverflow question.](http://stackoverflow.com/a/27166589/1585343) 5 | 6 | ## install 7 | 8 | ``` 9 | npm install --save-dev gulp-npm-script-sync 10 | ``` 11 | 12 | ## usage 13 | 14 | ```javascript 15 | // gulpfile.js 16 | var gulp = require('gulp'); 17 | var sync = require('gulp-npm-script-sync'); 18 | 19 | // your gulpfile contents 20 | 21 | sync(gulp); 22 | ``` 23 | 24 | Using the configurations: 25 | ```javascript 26 | // gulpfile.js 27 | var gulp = require('gulp'); 28 | var sync = require('gulp-npm-script-sync'); 29 | 30 | // your gulpfile contents 31 | 32 | sync(gulp, { 33 | path: 'path/to/package.json', 34 | excluded: ['task1', 'task2'] 35 | }); 36 | ``` 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Austin Pray 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /test/fixtures/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "asset-builder", 3 | "version": "0.0.1", 4 | "description": "asset-builder =============", 5 | "main": "lib/index.js", 6 | "scripts": { 7 | "test": "mocha test", 8 | "coveralls": "./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec test && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/austinpray/asset-builder.git" 13 | }, 14 | "author": "Austin Pray ", 15 | "license": "MIT", 16 | "bugs": { 17 | "url": "https://github.com/austinpray/asset-builder/issues" 18 | }, 19 | "homepage": "https://github.com/austinpray/asset-builder", 20 | "devDependencies": { 21 | "bower": "*", 22 | "chai": "*", 23 | "coveralls": "^2.11.2", 24 | "istanbul": "^0.3.2", 25 | "mkdirp": "^0.5.0", 26 | "mocha": "*", 27 | "q": "^1.1.2" 28 | }, 29 | "dependencies": { 30 | "lodash": "^2.4.1", 31 | "main-bower-files": "^2.4.0", 32 | "object-path": "^0.8.0", 33 | "traverse": "^0.6.6", 34 | "wiredep": "^2.2.0" 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'); 2 | var detectIndent = require('detect-indent'); 3 | 4 | module.exports = function (gulp, config) { 5 | config = config || {}; 6 | if (typeof gulp === 'undefined') { 7 | throw new Error("Gulp Undefined"); 8 | } 9 | if (config.hasOwnProperty('excluded') && !Array.isArray(config.excluded)) { 10 | throw new Error('The excluded should be an array'); 11 | } 12 | 13 | var file = fs.readFileSync(config.path || 'package.json', 'utf-8'); 14 | var pkg = JSON.parse(file); 15 | var indent = detectIndent(file).indent || ' '; 16 | if (typeof gulp.tasks === 'object') { 17 | var tasks = gulp.tasks; 18 | } else if (typeof gulp.registry === 'function') { 19 | var tasks = gulp.registry().tasks(); 20 | } else { 21 | throw new Error('Could not find gulp tasks'); 22 | } 23 | 24 | pkg.scripts = pkg.scripts || {}; 25 | 26 | Object.keys(tasks).forEach(function (t) { 27 | if (!config.excluded || config.excluded.indexOf(tasks[t].name) === -1) { 28 | pkg.scripts[t] = 'gulp ' + tasks[t].name; 29 | } 30 | }); 31 | 32 | fs.writeFileSync(config.path || 'package.json', JSON.stringify(pkg, null, indent) + '\n'); 33 | 34 | }; -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | /* jshint node: true */ 2 | /* global describe, it, beforeEach, afterEach */ 3 | 'use strict'; 4 | 5 | var assert = require('chai').assert; 6 | var a = require('assert'); 7 | var fs = require('fs'); 8 | var mkdirp = require('mkdirp'); 9 | var gulp = require('gulp'); 10 | 11 | var sync = require('../index'); 12 | 13 | function addTasks(done) { 14 | mkdirp('test/tmp/', function () { 15 | fs.writeFileSync('test/tmp/package.json', fs.readFileSync('test/fixtures/package.json')); 16 | done(); 17 | }); 18 | var fn = function () {}; 19 | gulp.task('foo', fn); 20 | gulp.task('bar', fn); 21 | gulp.task('baz', fn); 22 | gulp.task('excluded', fn); 23 | } 24 | function removeTasks() { 25 | gulp.reset(); 26 | } 27 | 28 | 29 | describe('Adding properties to package.json', function(){ 30 | beforeEach(addTasks); 31 | afterEach(removeTasks); 32 | it('should succeed if gulp is defined', function () { 33 | a.doesNotThrow(function () {sync(gulp, { path: 'test/tmp/package.json' }); }, Error); 34 | }); 35 | it('should add the gulp tasks to scripts', function () { 36 | sync(gulp, { path: 'test/tmp/package.json' }); 37 | var f = JSON.parse(fs.readFileSync('test/tmp/package.json')); 38 | assert.equal(f.scripts.foo, 'gulp foo'); 39 | assert.equal(f.scripts.bar, 'gulp bar'); 40 | assert.equal(f.scripts.baz, 'gulp baz'); 41 | assert.equal(f.scripts.excluded, 'gulp excluded'); 42 | }); 43 | it('should exclude the gulp tasks from scripts', function () { 44 | sync(gulp, { 45 | path: 'test/tmp/package.json', 46 | excluded: ['excluded'] 47 | }); 48 | var f = JSON.parse(fs.readFileSync('test/tmp/package.json')); 49 | assert.equal(f.scripts.foo, 'gulp foo'); 50 | assert.equal(f.scripts.bar, 'gulp bar'); 51 | assert.equal(f.scripts.baz, 'gulp baz'); 52 | assert.isUndefined(f.scripts.excluded, 'excluded should be undefined'); 53 | }); 54 | }); 55 | 56 | describe('error handling', function () { 57 | it('should error if gulp is undefined', function () { 58 | a.throws(function () {sync();}, Error, "Error thrown"); 59 | }); 60 | it('should error if config.excludes is not an array', function () { 61 | a.throws(function () {sync(gulp, {excluded: ''});}, Error, "Error thrown"); 62 | }); 63 | }); 64 | -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | /*global $:true*/ 2 | var gulp = require('gulp'); 3 | 4 | var $ = require('gulp-load-plugins')(); 5 | 6 | var paths = { 7 | scripts: [ 8 | 'assets/src/js/**/*' 9 | ], 10 | jshint: [ 11 | 'bower.json', 12 | 'gulpfile.js', 13 | 'assets/src/js/**/*' 14 | ], 15 | less: 'assets/src/less/main.less', 16 | editorStyle: 'assets/src/less/editor-style.less' 17 | }; 18 | 19 | gulp.task('less:dev', function() { 20 | return gulp.src(paths.less) 21 | .pipe($.plumber()) 22 | .pipe($.sourcemaps.init()) 23 | .pipe($.less()).on('error', function(err) { 24 | console.warn(err.message); 25 | }) 26 | .pipe($.autoprefixer('last 2 versions', 'ie 8', 'ie 9', 'android 2.3', 'android 4', 'opera 12')) 27 | .pipe($.sourcemaps.write()) 28 | .pipe($.rename('./main.css')) 29 | .pipe(gulp.dest('assets/dist/css')) 30 | .pipe($.livereload({ auto: false })); 31 | }); 32 | 33 | gulp.task('less:build', function() { 34 | return gulp.src(paths.less) 35 | .pipe($.plumber()) 36 | .pipe($.less()).on('error', function(err) { 37 | console.warn(err.message); 38 | }) 39 | .pipe($.autoprefixer('last 2 versions', 'ie 9', 'android 2.3', 'android 4', 'opera 12')) 40 | .pipe($.rename('./main.min.css')) 41 | .pipe($.minifyCss()) 42 | .pipe(gulp.dest('assets/dist/css')); 43 | }); 44 | 45 | gulp.task('less:editorStyle', function() { 46 | return gulp.src(paths.editorStyle) 47 | .pipe($.plumber()) 48 | .pipe($.less()).on('error', function(err) { 49 | console.warn(err.message); 50 | }) 51 | .pipe($.autoprefixer('last 2 versions', 'ie 9', 'android 2.3', 'android 4', 'opera 12')) 52 | .pipe($.rename('./editor-style.css')) 53 | .pipe(gulp.dest('assets/dist/css')); 54 | }); 55 | 56 | gulp.task('jshint', function() { 57 | return gulp.src(paths.jshint) 58 | .pipe($.jshint()) 59 | .pipe($.jshint.reporter('jshint-stylish')) 60 | .pipe($.jshint.reporter('fail')); 61 | }); 62 | 63 | gulp.task('js:dev', ['jshint'], function() { 64 | return gulp.src(require('main-bower-files')().concat(paths.scripts)) 65 | .pipe($.filter('**/*.js')) 66 | .pipe($.concat('./scripts.js')) 67 | .pipe(gulp.dest('assets/dist/js')) 68 | .pipe($.livereload({ auto: false })); 69 | }); 70 | 71 | gulp.task('js:build', ['jshint'], function() { 72 | return gulp.src(require('main-bower-files')().concat(paths.scripts)) 73 | .pipe($.filter('**/*.js')) 74 | .pipe($.concat('./scripts.min.js')) 75 | .pipe($.uglify()) 76 | .pipe(gulp.dest('assets/dist/js')); 77 | }); 78 | 79 | gulp.task('copy:fonts', function() { 80 | return gulp.src(require('main-bower-files')().concat('asset/src/fonts/**/*')) 81 | .pipe($.filter('**/*.{eot,svg,ttf,woff}')) 82 | .pipe(gulp.dest('assets/dist/fonts')); 83 | }); 84 | 85 | gulp.task('copy:jquery', function() { 86 | return gulp.src(['bower_components/jquery/dist/jquery.min.js']) 87 | .pipe($.rename('jquery-1.11.1.min.js')) 88 | .pipe(gulp.dest('assets/dist/js')); 89 | }); 90 | 91 | gulp.task('copy:modernizr', function() { 92 | return gulp.src(['bower_components/modernizr/modernizr.js']) 93 | .pipe($.uglify()) 94 | .pipe($.rename('modernizr.min.js')) 95 | .pipe(gulp.dest('assets/dist/js')); 96 | }); 97 | 98 | gulp.task('images', function() { 99 | return gulp.src('assets/src/img/**/*') 100 | .pipe($.imagemin({ 101 | progressive: true, 102 | interlaced: true 103 | })) 104 | .pipe(gulp.dest('assets/dist/img')); 105 | }); 106 | 107 | gulp.task('version', function() { 108 | return gulp.src(['assets/dist/css/main.min.css', 'assets/dist/js/scripts.min.js'], { base: 'assets/dist' }) 109 | .pipe(gulp.dest('assets/dist')) 110 | .pipe($.rev()) 111 | .pipe(gulp.dest('assets/dist')) 112 | .pipe($.rev.manifest()) 113 | .pipe(gulp.dest('assets/dist')); 114 | }); 115 | 116 | gulp.task('clean', function() { 117 | return gulp.src(['assets/dist/css/main.min*', 'assets/dist/js/scripts.min*'], { read: false }) 118 | .pipe($.clean()); 119 | }); 120 | 121 | gulp.task('watch', function() { 122 | $.livereload.listen(); 123 | gulp.watch(['assets/src/less/**/*', 'bower.json'], ['less:dev']); 124 | gulp.watch(['assets/src/js/**/*', 'bower.json'], ['jshint', 'js:dev']); 125 | gulp.watch('**/*.php').on('change', function(file) { 126 | $.livereload.changed(file.path); 127 | }); 128 | }); 129 | 130 | gulp.task('default', ['less:dev', 'less:editorStyle', 'jshint', 'js:dev', 'copy:fonts', 'images']); 131 | gulp.task('dev', ['default']); 132 | gulp.task('build', ['clean', 'less:build', 'less:editorStyle', 'js:build', 'copy:fonts', 'copy:jquery', 'copy:modernizr', 'images', 'version']); 133 | --------------------------------------------------------------------------------