From 0ea45f6b2ba4c0ba84c64bedaa6be5acea4d3aca Mon Sep 17 00:00:00 2001 From: Dmitry Filimonov Date: Mon, 21 Dec 2015 07:29:55 +0300 Subject: [PATCH] refactored gulpfile --- .travis.yml | 5 +- gulpfile.babel.js | 224 +---------------------------------------- package.json | 1 + tasks/build.babel.js | 71 +++++++++++++ tasks/bump.babel.js | 24 +++++ tasks/github.babel.js | 42 ++++++++ tasks/website.babel.js | 105 +++++++++++++++++++ 7 files changed, 249 insertions(+), 223 deletions(-) create mode 100644 tasks/build.babel.js create mode 100644 tasks/bump.babel.js create mode 100644 tasks/github.babel.js create mode 100644 tasks/website.babel.js diff --git a/.travis.yml b/.travis.yml index 4cf7429..e461ff9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,8 @@ language: node_js node_js: - "node" -before_install: +before_run: - npm install -g bower gulp - bower install -script: gulp test + - gulp clean +script: gulp travis-ci diff --git a/gulpfile.babel.js b/gulpfile.babel.js index 28d68b9..a425c0e 100644 --- a/gulpfile.babel.js +++ b/gulpfile.babel.js @@ -1,235 +1,17 @@ // generated using generator-gulp-webapp 1.0.3 import gulp from 'gulp'; -import marked from 'marked'; -import highlight from 'highlight.js'; import gulpLoadPlugins from 'gulp-load-plugins'; -import browserSync from 'browser-sync'; +import requireDir from 'require-dir'; import del from 'del'; -import fs from 'fs'; -import childProcess from 'child_process'; const $ = gulpLoadPlugins(); -const reload = browserSync.reload; -const exec = childProcess.exec; -marked.setOptions({ - renderer: new marked.Renderer(), - gfm: true, - tables: true, - breaks: false, - pedantic: false, - sanitize: true, - smartLists: true, - smartypants: false, - highlight: (code, lang) => { - return highlight.highlightAuto(code, [lang]).value; - } -}); - -function markdownFilter(code) { - code = code - .replace(/[\s\S]*(?=#+ Notable Features)/m, '') - .replace(/#+ Copyright[\s\S]*/m, ''); - return marked(code); -} - -function readJson(path) { - return JSON.parse(fs.readFileSync(path, 'utf8')); -} - -function banner() { - var pkg = readJson('./package.json'); - var bower = readJson('./bower.json'); - return ` - /*! - * ${bower.name} - * version: ${pkg.version} - * authors: ${bower.authors} - * license: ${bower.license} - * ${bower.homepage} - */ - `.replace(/\n\s{0,4}/g, '\n').replace('\n', ''); -} - -function bump(type) { - return gulp.src('./package.json') - .pipe($.bump({type: type})) - .pipe(gulp.dest('./')) - .pipe($.callback(() => { gulp.start('build:min'); })); -}; - -gulp.task('images', () => { - return gulp.src('website/images/*') - .pipe(gulp.dest('dist/website/images')) -}); - -gulp.task('styles', () => { - return gulp.src([ - 'src/styles/*.scss', - 'website/styles/*.scss' - ]) - .pipe($.plumber()) - .pipe($.sass.sync({ - outputStyle: 'expanded', - precision: 10, - includePaths: ['.'] - }).on('error', $.util.log)) - .pipe($.autoprefixer({browsers: ['last 1 version']})) - .pipe(gulp.dest('.tmp/styles')) - .pipe(reload({stream: true})); -}); - -gulp.task('scripts', () => { - return gulp.src([ - 'src/scripts/*.coffee', - 'website/scripts/*.coffee' - ]) - .pipe($.include()).on('error', $.util.log) - .pipe($.plumber()) - .pipe($.coffee().on('error', $.util.log)) - .pipe(gulp.dest('.tmp/scripts')) - .pipe(reload({stream: true})); -}); -gulp.task('html', ['styles', 'scripts'], () => { - return gulp.src('website/*.html') - .pipe($.fileInclude({ - prefix: '@@', - basepath: '@file', - filters: { - markdown: markdownFilter - } - })).on('error', $.util.log) - .pipe(gulp.dest('.tmp')) - .pipe(reload({stream: true})); -}); +requireDir('./tasks'); gulp.task('clean', del.bind(null, ['.tmp', '.publish', 'dist'])); -gulp.task('serve', ['html', 'styles', 'scripts'], () => { - browserSync({ - notify: false, - port: 9000, - ghostMode: { - clicks: false, - forms: false, - scroll: false - }, - server: { - baseDir: ['.tmp', 'website'], - routes: { - '/bower_components': 'bower_components', - '/docs': '.tmp/docs.html', - '/tests': '.tmp/tests.html', - '/examples': '.tmp/examples.html' - } - } - }); - - gulp.watch([ - '{src,website}/scripts/**/*.coffee', - 'src/templates/**/*.html', - 'website/**/*.html', - '**/*.md' - ]).on('change', reload); - - gulp.watch('{src,website}/styles/**/*.scss', ['styles']); - gulp.watch('{src,website}/scripts/**/*.coffee', ['scripts']); - gulp.watch('src/templates/**/*.html', ['scripts']); - gulp.watch('website/**/*.html', ['html']); - gulp.watch('**/*.md', ['html']); -}); - -gulp.task('build', ['scripts', 'styles'], () => { - return gulp.src(['.tmp/scripts/daterangepicker.js', '.tmp/styles/daterangepicker.css']) - .pipe($.header(banner())) - .pipe(gulp.dest('dist/')) - .pipe($.size({title: 'build', gzip: true})); -}); - -gulp.task('build:min', ['build'], () => { - return gulp.src(['dist/daterangepicker.js', 'dist/daterangepicker.css']) - .pipe($.if('*.js', $.uglify({preserveComments: 'license'}))) - .pipe($.if('*.css', $.minifyCss({compatibility: '*'}))) - .pipe($.if('*.js', $.extReplace('.min.js'))) - .pipe($.if('*.css', $.extReplace('.min.css'))) - .pipe(gulp.dest('dist/')) - .pipe($.size({title: 'build:min', gzip: true})); -}); - -gulp.task('build:website', ['html', 'scripts', 'styles', 'images'], () => { - const assets = $.useref.assets({searchPath: ['.tmp', 'website', '.']}); - - return gulp.src('.tmp/*.html') - .pipe(assets) - .pipe($.if('*.js', $.uglify({preserveComments: 'license'}))) - .pipe($.if('*.css', $.minifyCss({compatibility: '*'}))) - .pipe(assets.restore()) - .pipe($.useref()) - .pipe(gulp.dest('dist/website')) - .pipe($.size({title: 'build:website', gzip: true})); -}); - -gulp.task('serve:website', ['build:website'], () => { - browserSync({ - notify: false, - port: 9000, - server: { - baseDir: ['dist/website'] - } - }); -}); - -gulp.task('github:pages', ['build:website'], () => { - return gulp.src('./dist/website/**/*') - .pipe($.ghPages()); -}); - -gulp.task('consistency-check', ['build:min'], (cb) => { - exec('git status', function callback(error, stdout, stderr) { - const pendingChanges = stdout.match(/modified:\s*dist/) - if (pendingChanges) { - throw new Error('consistency check failed'); - } else { - cb(); - } - }); -}); - -gulp.task('github:release', ['consistency-check'], () => { - if (!process.env.GITHUB_TOKEN) { - throw new Error('env.GITHUB_TOKEN is empty'); - } - - var manifest = readJson('./package.json'); - const match = manifest.repository.url.split('/').slice(-2) - - return gulp.src([ - 'dist/daterangepicker.js', - 'dist/daterangepicker.css', - 'dist/daterangepicker.min.js', - 'dist/daterangepicker.min.css' - ]) - .pipe($.githubRelease({ - manifest: manifest, - owner: match[0], - repo: match[1] - })); -}); - -gulp.task('bump:major', () => { - return bump('major'); -}); - -gulp.task('bump:minor', () => { - return bump('minor'); -}); - -gulp.task('bump:patch', () => { - return bump('patch'); -}); - -gulp.task('test', ['build:website'], () => { +gulp.task('travis-ci', ['build:website'], () => { return gulp .src('dist/website/tests.html') .pipe($.mochaPhantomjs()); diff --git a/package.json b/package.json index fee715f..70f6f58 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "marked": "^0.3.5", "mocha": "^2.3.4", "opn": "^1.0.1", + "require-dir": "^0.3.0", "wiredep": "^2.2.2" }, "repository": { diff --git a/tasks/build.babel.js b/tasks/build.babel.js new file mode 100644 index 0000000..09c4c31 --- /dev/null +++ b/tasks/build.babel.js @@ -0,0 +1,71 @@ +import gulp from 'gulp'; +import fs from 'fs'; +import gulpLoadPlugins from 'gulp-load-plugins'; +import browserSync from 'browser-sync'; + +const $ = gulpLoadPlugins(); +const reload = browserSync.reload; + + +function readJson(path) { + return JSON.parse(fs.readFileSync(path, 'utf8')); +} + +function banner() { + var pkg = readJson('./package.json'); + var bower = readJson('./bower.json'); + return ` + /*! + * ${bower.name} + * version: ${pkg.version} + * authors: ${bower.authors} + * license: ${bower.license} + * ${bower.homepage} + */ + `.replace(/\n\s{0,4}/g, '\n').replace('\n', ''); +} + +gulp.task('styles', () => { + return gulp.src([ + 'src/styles/*.scss', + 'website/styles/*.scss' + ]) + .pipe($.plumber()) + .pipe($.sass.sync({ + outputStyle: 'expanded', + precision: 10, + includePaths: ['.'] + }).on('error', $.util.log)) + .pipe($.autoprefixer({browsers: ['last 1 version']})) + .pipe(gulp.dest('.tmp/styles')) + .pipe(reload({stream: true})); +}); + +gulp.task('scripts', () => { + return gulp.src([ + 'src/scripts/*.coffee', + 'website/scripts/*.coffee' + ]) + .pipe($.include()).on('error', $.util.log) + .pipe($.plumber()) + .pipe($.coffee().on('error', $.util.log)) + .pipe(gulp.dest('.tmp/scripts')) + .pipe(reload({stream: true})); +}); + +gulp.task('build', ['scripts', 'styles'], () => { + return gulp.src(['.tmp/scripts/daterangepicker.js', '.tmp/styles/daterangepicker.css']) + .pipe($.header(banner())) + .pipe(gulp.dest('dist/')) + .pipe($.size({title: 'build', gzip: true})); +}); + +gulp.task('build:min', ['build'], () => { + return gulp.src(['dist/daterangepicker.js', 'dist/daterangepicker.css']) + .pipe($.if('*.js', $.uglify({preserveComments: 'license'}))) + .pipe($.if('*.css', $.minifyCss({compatibility: '*'}))) + .pipe($.if('*.js', $.extReplace('.min.js'))) + .pipe($.if('*.css', $.extReplace('.min.css'))) + .pipe(gulp.dest('dist/')) + .pipe($.size({title: 'build:min', gzip: true})); +}); diff --git a/tasks/bump.babel.js b/tasks/bump.babel.js new file mode 100644 index 0000000..e3e1dca --- /dev/null +++ b/tasks/bump.babel.js @@ -0,0 +1,24 @@ +import gulp from 'gulp'; +import gulpLoadPlugins from 'gulp-load-plugins'; + +const $ = gulpLoadPlugins(); + + +function bump(type) { + return gulp.src('./package.json') + .pipe($.bump({type: type})) + .pipe(gulp.dest('./')) + .pipe($.callback(() => { gulp.start('build:min'); })); +}; + +gulp.task('bump:major', () => { + return bump('major'); +}); + +gulp.task('bump:minor', () => { + return bump('minor'); +}); + +gulp.task('bump:patch', () => { + return bump('patch'); +}); diff --git a/tasks/github.babel.js b/tasks/github.babel.js new file mode 100644 index 0000000..ea8a37b --- /dev/null +++ b/tasks/github.babel.js @@ -0,0 +1,42 @@ +import gulp from 'gulp'; +import gulpLoadPlugins from 'gulp-load-plugins'; + +const $ = gulpLoadPlugins(); + + +gulp.task('github:pages', ['build:website'], () => { + return gulp.src('./dist/website/**/*') + .pipe($.ghPages()); +}); + +gulp.task('consistency-check', ['build:min'], (cb) => { + exec('git status', function callback(error, stdout, stderr) { + const pendingChanges = stdout.match(/modified:\s*dist/) + if (pendingChanges) { + throw new Error('consistency check failed'); + } else { + cb(); + } + }); +}); + +gulp.task('github:release', ['consistency-check'], () => { + if (!process.env.GITHUB_TOKEN) { + throw new Error('env.GITHUB_TOKEN is empty'); + } + + var manifest = readJson('./package.json'); + const match = manifest.repository.url.split('/').slice(-2) + + return gulp.src([ + 'dist/daterangepicker.js', + 'dist/daterangepicker.css', + 'dist/daterangepicker.min.js', + 'dist/daterangepicker.min.css' + ]) + .pipe($.githubRelease({ + manifest: manifest, + owner: match[0], + repo: match[1] + })); +}); diff --git a/tasks/website.babel.js b/tasks/website.babel.js new file mode 100644 index 0000000..28e9895 --- /dev/null +++ b/tasks/website.babel.js @@ -0,0 +1,105 @@ +import gulp from 'gulp'; +import marked from 'marked'; +import highlight from 'highlight.js'; +import gulpLoadPlugins from 'gulp-load-plugins'; +import browserSync from 'browser-sync'; + +const $ = gulpLoadPlugins(); +const reload = browserSync.reload; + + +marked.setOptions({ + renderer: new marked.Renderer(), + gfm: true, + tables: true, + breaks: false, + pedantic: false, + sanitize: true, + smartLists: true, + smartypants: false, + highlight: (code, lang) => { + return highlight.highlightAuto(code, [lang]).value; + } +}); + +function markdownFilter(code) { + code = code + .replace(/[\s\S]*(?=#+ Notable Features)/m, '') + .replace(/#+ Copyright[\s\S]*/m, ''); + return marked(code); +} + +gulp.task('images', () => { + return gulp.src('website/images/*') + .pipe(gulp.dest('dist/website/images')) +}); + +gulp.task('html', ['styles', 'scripts'], () => { + return gulp.src('website/*.html') + .pipe($.fileInclude({ + prefix: '@@', + basepath: '@file', + filters: { + markdown: markdownFilter + } + })).on('error', $.util.log) + .pipe(gulp.dest('.tmp')) + .pipe(reload({stream: true})); +}); + +gulp.task('serve', ['html', 'styles', 'scripts'], () => { + browserSync({ + notify: false, + port: 9000, + ghostMode: { + clicks: false, + forms: false, + scroll: false + }, + server: { + baseDir: ['.tmp', 'website'], + routes: { + '/bower_components': 'bower_components', + '/docs': '.tmp/docs.html', + '/tests': '.tmp/tests.html', + '/examples': '.tmp/examples.html' + } + } + }); + + gulp.watch([ + '{src,website}/scripts/**/*.coffee', + 'src/templates/**/*.html', + 'website/**/*.html', + '**/*.md' + ]).on('change', reload); + + gulp.watch('{src,website}/styles/**/*.scss', ['styles']); + gulp.watch('{src,website}/scripts/**/*.coffee', ['scripts']); + gulp.watch('src/templates/**/*.html', ['scripts']); + gulp.watch('website/**/*.html', ['html']); + gulp.watch('**/*.md', ['html']); +}); + +gulp.task('build:website', ['html', 'scripts', 'styles', 'images'], () => { + const assets = $.useref.assets({searchPath: ['.tmp', 'website', '.']}); + + return gulp.src('.tmp/*.html') + .pipe(assets) + .pipe($.if('*.js', $.uglify({preserveComments: 'license'}))) + .pipe($.if('*.css', $.minifyCss({compatibility: '*'}))) + .pipe(assets.restore()) + .pipe($.useref()) + .pipe(gulp.dest('dist/website')) + .pipe($.size({title: 'build:website', gzip: true})); +}); + +gulp.task('serve:website', ['build:website'], () => { + browserSync({ + notify: false, + port: 9000, + server: { + baseDir: ['dist/website'] + } + }); +});