UNPKG

996 BJavaScriptView Raw
1/**
2 * Allow remote paths in vinyl
3 * Should be removed if https://github.com/gulpjs/vinyl/issues/127
4 * is accepted and released.
5 */
6
7'use strict';
8
9const url = require('url');
10const File = require('vinyl');
11
12Object.defineProperty(File.prototype, 'remotePath', {
13 get() {
14 return this.history[this.history.length - 1];
15 },
16 set(remotePath) {
17 if (typeof remotePath !== 'string') {
18 throw new TypeError('path should be a string.');
19 }
20
21 // Check url
22 if (!/(^\/\/)|(:\/\/)/.test(remotePath)) {
23 this.path = remotePath;
24 return;
25 }
26
27 // eslint-disable-next-line node/no-deprecated-api
28 const urlObj = url.parse(remotePath);
29 remotePath = `${urlObj.protocol}//${urlObj.host}${urlObj.pathname}`;
30
31 // Record history only when path changed
32 if (remotePath && remotePath !== this.remotePath) {
33 this.history.push(remotePath);
34 }
35 }
36});
37
38module.exports = File;