{"size": 2558, "ext": "js", "max_stars_repo_path": "adp-frontend-web-v4/gulp/tasks/load-external.js", "max_stars_repo_name": "FDA/Healthy-Citizen-Code", "max_stars_repo_stars_event_min_datetime": "2018-08-22T21:08:52.000Z", "max_stars_repo_stars_event_max_datetime": "2020-10-22T03:01:23.000Z", "max_issues_repo_path": "adp-frontend-web-v4/gulp/tasks/load-external.js", "max_issues_repo_name": "FDA/Healthy-Citizen-Code", "max_issues_repo_issues_event_min_datetime": "2018-11-26T18:17:10.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-03T15:43:41.000Z", "max_forks_repo_path": "adp-frontend-web-v4/gulp/tasks/load-external.js", "max_forks_repo_name": "FDA/Healthy-Citizen-Code", "max_forks_repo_forks_event_min_datetime": "2020-04-10T18:58:06.000Z", "max_forks_repo_forks_event_max_datetime": "2021-08-03T19:12:55.000Z"}
{"max_stars_count": 5, "max_issues_count": 57, "max_forks_count": 2, "avg_line_length": 25.58, "max_line_length": 122, "alphanum_fraction": 0.6571540266}
const gulp = require('gulp');
const conf = require('../config');
const APP_CONFIG = conf.APP_CONFIG();
const bufferToVinyl = require('buffer-to-vinyl');
const gulpNgConfig = require('gulp-ng-config');
const requestPromise = require('../utils/request-promise');

gulp.task('create:config', () => {
  const configJson = JSON.stringify({'APP_CONFIG': APP_CONFIG});
  const configBuffer = new Buffer(configJson);

  return bufferToVinyl.stream(configBuffer, './api_config.json')
    .pipe(gulpNgConfig('APP_MODEL_CONFIG', { pretty: 2 }))
    .pipe(gulp.dest(conf.paths.tmp));
});

gulp.task('load:script', () => {
  var endpoint = `${APP_CONFIG.apiUrl}/${conf.endpoints.scripts}`;

  var options = {
    url: endpoint,
    method: 'GET',
    json: true
  };

  return requestPromise(options)
    .then(createScripts.bind(this, conf.paths.serverScripts));
});

gulp.task('load:modules', () => uploadFile(conf.endpoints.serverModules, conf.paths.serverModules));
gulp.task('load:defaultModules', () => uploadFile(conf.endpoints.defaultServerModules,  conf.paths.defaultServerModules));
gulp.task('load:css', () => uploadFile(conf.endpoints.serverCss,  conf.paths.serverCss));

function uploadFile(serverPath, distPath) {
  if (!APP_CONFIG) throw new Error('Application config not found.');

  var endpoint = `${APP_CONFIG.apiUrl}/${serverPath}`;

  var options = {
    url: endpoint,
    method: 'GET',
    json: true
  };

  return requestPromise(options)
    .then(createScripts.bind(this, distPath));
}

function createScripts(fileName, body) {
  if (body.code === 'ResourceNotFound') {
    var file = '';
  } else {
    file = body;
  }
  
  return new Promise(function (resolve, reject) {
    bufferToVinyl.stream(new Buffer(file), fileName)
      .pipe(gulp.dest(conf.paths.tmp))
      .on('end', resolve)
      .on('error', reject);
  })
}

gulp.task('load:manifest', () => {
  if (!APP_CONFIG) throw new Error('Application config not found.');

  var endpoint = `${APP_CONFIG.apiUrl}/public/manifest.json`;

  var options = {
    url: endpoint,
    method: 'GET'
  };

  return requestPromise(options)
    .then(writeManifest)
    .catch(e => {
      console.log(`Error while loading ${endpoint}:`, e);
    });
});

function writeManifest(body) {
  if (body.code === 'ResourceNotFound') {
    var file = '';
  } else {
    file = body;
  }

  return new Promise(function (resolve, reject) {
    bufferToVinyl.stream(new Buffer(file), 'manifest.json')
      .pipe(gulp.dest(conf.paths.tmp))
      .on('end', resolve)
      .on('error', reject);
  })
}





