|
| 1 | +import gulp from 'gulp' |
| 2 | +import gutil from 'gulp-util' |
| 3 | +import loadPlugins from 'gulp-load-plugins' |
| 4 | +import {Instrumenter} from 'isparta' |
| 5 | +import del from 'del' |
| 6 | +import globule from 'globule' |
| 7 | +import seq from 'run-sequence' |
| 8 | +import fsp from 'fs-promise' |
| 9 | +import stableStringify from 'json-stable-stringify' |
| 10 | +import path from 'path' |
| 11 | +import marshal from './test/lib/marshal' |
| 12 | + |
| 13 | +const $ = loadPlugins() |
| 14 | + |
| 15 | +const plumb = () => $.plumber({ |
| 16 | + errorHandler: $.notify.onError('<%= error.message %>') |
| 17 | +}) |
| 18 | + |
| 19 | +const test = (strict = false) => { |
| 20 | + return gulp.src(['test/lib/setup.js', 'test/integration/**/*.js'], {read: false}) |
| 21 | + .pipe($.if(!strict, plumb())) |
| 22 | + .pipe($.mocha({reporter: 'spec'})) |
| 23 | +} |
| 24 | + |
| 25 | +gulp.task('clean', () => del('lib')) |
| 26 | + |
| 27 | +gulp.task('transpile', () => { |
| 28 | + return gulp.src('src/**/*.js') |
| 29 | + .pipe(plumb()) |
| 30 | + .pipe($.sourcemaps.init()) |
| 31 | + .pipe($.babel()) |
| 32 | + .pipe($.sourcemaps.write()) |
| 33 | + .pipe(gulp.dest('lib')) |
| 34 | +}) |
| 35 | + |
| 36 | +gulp.task('lint', () => { |
| 37 | + return gulp.src('src/**/*.js') |
| 38 | + .pipe(plumb()) |
| 39 | + .pipe($.standard()) |
| 40 | + .pipe($.standard.reporter('default', { |
| 41 | + breakOnError: false |
| 42 | + })) |
| 43 | +}) |
| 44 | + |
| 45 | +gulp.task('build', (cb) => seq('lint', 'test', 'transpile', cb)) |
| 46 | + |
| 47 | +gulp.task('cleanbuild', (cb) => seq('clean', 'build', cb)) |
| 48 | + |
| 49 | +gulp.task('pre-coverage', () => { |
| 50 | + return gulp.src('src/**/*.js') |
| 51 | + .pipe($.istanbul({instrumenter: Instrumenter})) |
| 52 | + .pipe($.istanbul.hookRequire()) |
| 53 | +}) |
| 54 | + |
| 55 | +gulp.task('coverage', ['pre-coverage'], () => { |
| 56 | + return test(true) |
| 57 | + .pipe($.istanbul.writeReports()) |
| 58 | + .pipe($.istanbul.enforceThresholds({thresholds: {global: 85}})) |
| 59 | +}) |
| 60 | + |
| 61 | +gulp.task('coveralls', ['coverage'], () => { |
| 62 | + return gulp.src('coverage/lcov.info') |
| 63 | + .pipe($.coveralls()) |
| 64 | +}) |
| 65 | + |
| 66 | +gulp.task('test', test) |
| 67 | + |
| 68 | +gulp.task('write-exp', () => { |
| 69 | + const parse = require('./src/').default |
| 70 | + |
| 71 | + const fixturesRoot = path.resolve(__dirname, 'test/fixtures') |
| 72 | + const expectedRoot = path.resolve(__dirname, 'test/integration/expected') |
| 73 | + |
| 74 | + const promises = [] |
| 75 | + const errors = [] |
| 76 | + for (const relPath of globule.find({cwd: fixturesRoot, src: '**/*.xml'})) { |
| 77 | + const filePath = path.join(fixturesRoot, relPath) |
| 78 | + const expPath = path.join(expectedRoot, gutil.replaceExtension(relPath, '.json')) |
| 79 | + const working = fsp.readFile(filePath, 'utf8') |
| 80 | + .then((data) => parse(data)) |
| 81 | + .then((parsed) => marshal(parsed)) |
| 82 | + .then((marshaled) => stableStringify(marshaled, {space: 2})) |
| 83 | + .then((data) => fsp.outputFile(expPath, data, 'utf8')) |
| 84 | + .then(() => gutil.log('Created', gutil.colors.magenta(expPath))) |
| 85 | + .catch((err) => { |
| 86 | + gutil.log('Error creating', gutil.colors.magenta(expPath) + ':', err.stack) |
| 87 | + errors.push({path: expPath, error: err}) |
| 88 | + }) |
| 89 | + promises.push(working) |
| 90 | + } |
| 91 | + |
| 92 | + return Promise.all(promises) |
| 93 | + .then(() => { |
| 94 | + if (errors.length > 0) { |
| 95 | + throw new gutil.PluginError('write-exp', `Failed to create ${errors.length} file(s)`) |
| 96 | + } |
| 97 | + }) |
| 98 | +}) |
| 99 | + |
| 100 | +gulp.task('watch', () => gulp.watch('{src,test}/**/*', ['cleanbuild'])) |
| 101 | + |
| 102 | +gulp.task('default', ['cleanbuild'], () => gulp.start('watch')) |
0 commit comments