const path = require('path');
const gulp = require('gulp');
const babel = require('gulp-babel');
const changed = require('gulp-changed');
const fs = require('fs');
const plumber = require('gulp-plumber');
const rename = require('gulp-rename');
const request = require('request');
const sourcemaps = require('gulp-sourcemaps');
const rimraf = require('rimraf');

const paths = {
  source: {
    js: 'src/**/*.js',
  },
  sourceRoot: path.join(__dirname, 'src'),
  build: 'build',
  caches: 'caches',
};

const tasks = {
  babel() {
    return gulp
      .src(paths.source.js)
      .pipe(changed(paths.build))
      .pipe(plumber())
      .pipe(
        sourcemaps.init({
          identityMap: true,
        })
      )
      .pipe(babel())
      .pipe(sourcemaps.write('__sourcemaps__', { sourceRoot: '/xdl/src' }))
      .pipe(gulp.dest(paths.build));
  },

  flow() {
    return gulp
      .src(paths.source.js)
      .pipe(rename({ extname: '.js.flow' }))
      .pipe(gulp.dest(paths.build));
  },

  watchBabel(done) {
    gulp.watch(paths.source.js, gulp.parallel(tasks.flow, tasks.babel));
    done();
  },

  clean(done) {
    rimraf(paths.build, done);
  },

  caches(done) {
    request('https://exp.host/--/versions', (error, result, body) => {
      if (error) {
        throw error;
      }

      // we don't need to do anything here, just let it throw if invalid json
      const response = JSON.parse(body);

      fs.writeFileSync(path.join(paths.caches, 'versions.json'), body, {
        encoding: 'utf8',
      });

      for (let version of Object.keys(response.sdkVersions)) {
        request(`https://exp.host/--/xdl-schema/${version}`, (error, result, body) => {
          if (error) {
            throw error;
          }

          JSON.parse(body);
          fs.writeFileSync(path.join(paths.caches, `schema-${version}.json`), body, {
            encoding: 'utf8',
          });
        });
      }

      done();
    });
  },
};

module.exports = tasks;

@@ -355,16 +355,61 @@
 raf');%0A%0A
+const package = require('../package.json');%0A%0A
 const pa
@@ -460,51 +460,8 @@
  %7D,%0A
-  sourceRoot: path.join(__dirname, 'src'),%0A
   bu
@@ -747,16 +747,25 @@
   .pipe(
+%0A        
 sourcema
@@ -792,16 +792,26 @@
 ps__', %7B
+%0A         
  sourceR
@@ -819,21 +819,68 @@
 ot: 
-'/xdl/src' %7D)
+%60/$%7Bpackage.name%7D@$%7Bpackage.version%7D/src%60,%0A        %7D)%0A      
 )%0A  

Set sourceRoot in source maps