Lanti
Lanti

Reputation: 2329

Using gulp with request

I have the following Gulpfile.js:

'use strict';

const gulp    = require('gulp'),
      request = require('request');

const paths = {
  vendor: [
    'https://raw.githubusercontent.com/jquery/jquery-dist/master/dist/jquery.min.js',
    'https://raw.githubusercontent.com/kenwheeler/slick/master/slick/slick.js'
  ]
};

gulp.task('vendor', (res) => {
  const url = request.get(paths.vendor).pipe(res);
  return gulp.src(url)
    .pipe(gulp.dest('public/vendor'));
});

gulp.task('default', gulp.parallel('vendor'));

I'm getting the following error:

Error: options.uri is a required argument

With this method I trying to dicthing client-side package managers, like Bower. Is there a way to use request with gulp and looping through a list of object?

EDIT:

I placed this code for testing, only returning the first line from the loop:

gulp.task('vendor', () => {
  for (let i=0; i<paths.vendor.length; i++) {
    return console.log(paths.vendor[i]);
  };
});

Just like:

gulp.task('vendor', (res) => {
  const url = request.get(paths.vendor[index++]).pipe(res);
  return gulp.src(url)
    .pipe(gulp.dest('public/vendor'));
});

Upvotes: 2

Views: 4094

Answers (2)

Sven Schoenung
Sven Schoenung

Reputation: 30564

You cannot pass a URL to gulp.src(). The gulp instance inherits src() and dest() from vinyl-fs meaning you can only use it to read from and write to the local file system.

Try gulp-download instead, which wraps request into a vinyl stream:

var download = require('gulp-download');

gulp.task('vendor', () => {
  return download(paths.vendor)
    .pipe(gulp.dest('public/vendor'));
});

Upvotes: 5

Rob M.
Rob M.

Reputation: 36511

request.get only works on one URI at a time and you are passing an array, also AFAIK parallel expects a list of tasks, not one task that processes many items. Maybe this would work for you:

'use strict';

const gulp    = require('gulp'),
      request = require('request');

const paths = {
  vendor: [
    'https://raw.githubusercontent.com/jquery/jquery-dist/master/dist/jquery.min.js',
    'https://raw.githubusercontent.com/kenwheeler/slick/master/slick/slick.js'
  ]
};
let index = 0;

gulp.task('vendor', (res) => {
  const url = request.get(paths.vendor[index++]).pipe(res);
  return gulp.src(url)
    .pipe(gulp.dest('public/vendor'));
});

let parallelTasks = (new Array(paths.vendor.length)).fill('vendor');

gulp.task('default', gulp.parallel(...parallelTasks));

Upvotes: 1

Related Questions