-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
98 lines (75 loc) · 2.47 KB
/
index.js
File metadata and controls
98 lines (75 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
'use strict';
const PLUGIN_NAME = 'gulp-studio-push';
let StudioHelper = require('studio-helper'),
gutil = require('gulp-util'),
PluginError = gutil.PluginError,
fs = require('fs'),
path = require('path'),
through = require('through2'),
studio = null;
module.exports = function(settings) {
let pushStarted = false,
currentDir = process.cwd(),
folders = [],
filteredFolders = [];
if(!studio) {
if(!settings) {
throw new PluginError(PLUGIN_NAME, 'Settings (object) missing');
}
if(typeof settings !== 'object') {
throw new PluginError(PLUGIN_NAME, 'Settings: object expected');
}
if(!settings.studio) {
throw new PluginError(PLUGIN_NAME, "Settings: property 'studio' (string) missing");
}
if(!settings.folders) {
throw new PluginError(PLUGIN_NAME, "Settings: property 'folders' (Array<object>) missing");
}
if(!settings.concurrentUploads) {
settings.concurrentUploads = 5;
}
studio = new StudioHelper(settings);
}
// Deep copy folders
folders = JSON.parse(JSON.stringify(settings.folders));
return through.obj(function (file, encoding, callback) {
let relativePath;
if(file.isNull()) {
// Folder
relativePath = fs.realpathSync(file.path).replace(path.join(currentDir, '/'), '');
} else {
// File
relativePath = path.dirname(fs.realpathSync(file.path)).replace(path.join(currentDir, '/'), '');
}
// If root dir, include all folders
if(currentDir === fs.realpathSync(file.path)) {
filteredFolders = folders.slice(0, folders.length);
}
for(let i=folders.length-1; i>=0; i--) {
let folder = folders[i],
folderPath = path.normalize(folder.localFolder);
// If path is found in passed folders add it
if(folderPath.indexOf(relativePath) === 0) {
filteredFolders.push(folders.splice(i, 1)[0]);
}
}
callback(null, file);
}, function (callback) {
if(!filteredFolders.length) {
console.log('[' + PLUGIN_NAME + '] No folders found to upload');
callback();
return;
}
// And push filteredFolders to Studio
return studio.push({
folders: filteredFolders
}).then(function (results) {
if (!results.length) {
console.log('[Studio] No changes to upload');
} else {
console.log('[Studio] ' + results.length + ' ' + (results.length === 1 ? 'file' : 'files') + ' uploaded');
}
callback();
});
});
};