Advertisement
Guest User

Untitled

a guest
Aug 10th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. var gulp = require("gulp");
  2. var FtpClient = require("ftp");
  3. var minimist = require("minimist");
  4. var readdirp = require("readdirp");
  5. var paths = require('../paths');
  6.  
  7. var _maxConcurrentActions = 10;
  8. var _knownOptions = {
  9. string: [
  10. "source",
  11. "url",
  12. "username",
  13. "password"
  14. ],
  15. default: {
  16. source: "./"
  17. }
  18. };
  19. var _options = minimist(process.argv.slice(2), _knownOptions);
  20. var _client = null;
  21. var _createdPaths = [];
  22.  
  23. gulp.task("clean-ftp", function (done) {
  24. var actions = [];
  25.  
  26. openClient(function () {
  27. _client.list(".", false, function (err, list) {
  28. if (err) {
  29. console.log("Error reading root dir from server: " + err);
  30. closeClient(done);
  31. }
  32. else {
  33. for (var item of list) {
  34. if (item.type === "d") {
  35. actions.push(deleteDirAction(item.name));
  36. }
  37. else if (item.type === "-") {
  38. actions.push(deleteFileAction(item.name));
  39. }
  40. }
  41.  
  42. executeActions(actions, function () {
  43. closeClient(done);
  44. });
  45. }
  46. });
  47. });
  48. });
  49.  
  50. gulp.task("deploy-ftp", ["clean-ftp"], function (done) {
  51. var actions = [];
  52.  
  53. openClient(function () {
  54. readdirp({
  55. root: _options.source
  56. }, function (entryInfo) {
  57. actions.push(uploadFileAction(entryInfo));
  58. }, function (err, res) {
  59. if (err) {
  60. console.log("Error reading local directory: " + err);
  61. closeClient(done);
  62. }
  63. else {
  64. executeActions(actions, function () {
  65. closeClient(done);
  66. });
  67. }
  68. });
  69. });
  70. });
  71.  
  72. gulp.task("deploy", ["deploy-ftp"]);
  73.  
  74. function openClient(callback) {
  75. console.log("Opening FTP connection to '" + _options.url + "'...");
  76.  
  77. _client = new FtpClient();
  78. _client.on("ready", callback);
  79. _client.on("error", console.log);
  80. _client.connect({
  81. host: _options.url,
  82. user: _options.username,
  83. password: _options.password
  84. });
  85. }
  86.  
  87. function closeClient(callback) {
  88. if (_client) {
  89. _client.end();
  90. }
  91.  
  92. callback();
  93. }
  94.  
  95. function deleteDirAction(path) {
  96. return function (callback) {
  97. console.log("Deleting dir '" + path + "' from server...");
  98. _client.rmdir(path, true, callback);
  99. };
  100. }
  101.  
  102. function deleteFileAction(path) {
  103. return function (callback) {
  104. console.log("Deleting file '" + path + "' from server...");
  105. _client.delete(path, callback);
  106. };
  107. }
  108.  
  109. function createDirAction(path) {
  110. return function (callback) {
  111. console.log("Creating dir '" + path + "'...");
  112. _client.mkdir(path, false, function (err) {
  113. if (err) {
  114. console.log("Error creating dir '" + path + "': " + err);
  115. }
  116.  
  117. callback();
  118. });
  119. };
  120. }
  121.  
  122. function uploadFileAction(entryInfo) {
  123. return function (callback) {
  124. var actions = [];
  125.  
  126. if (entryInfo.parentDir != "") {
  127. var dirs = entryInfo.parentDir.split("\\");
  128. var fullDir = "";
  129.  
  130. dirs.forEach(function (dir) {
  131. if (fullDir.length > 0) {
  132. fullDir += "\\";
  133. }
  134.  
  135. fullDir += dir;
  136.  
  137. if (_createdPaths.indexOf(fullDir) < 0) {
  138. actions.push(createDirAction(fullDir));
  139. _createdPaths.push(fullDir);
  140. }
  141. });
  142. }
  143.  
  144. executeActions(actions, function () {
  145. console.log("Uploading '" + entryInfo.path + "'...");
  146. _client.put(entryInfo.fullPath, entryInfo.path, function (err) {
  147. if (err) {
  148. console.log("Error uploading '" + entryInfo.path + "': " + err);
  149. }
  150.  
  151. callback();
  152. });
  153. });
  154. };
  155. }
  156.  
  157. function executeActions(actions, callback) {
  158. executeActionsRecursive(actions, 0, callback);
  159. }
  160.  
  161. function executeActionsRecursive(actions, startIndex, callback) {
  162. var endIndex = (startIndex + _maxConcurrentActions);
  163. var actionsToExecute = actions.slice(startIndex, endIndex);
  164. var promises = [];
  165.  
  166. if (actionsToExecute.length > 0) {
  167. actionsToExecute.forEach(function (action) {
  168. promises.push(new Promise(function (resolve) {
  169. action(resolve);
  170. }));
  171. });
  172.  
  173. Promise
  174. .all(promises)
  175. .then(function () {
  176. executeActionsRecursive(actions, endIndex, callback);
  177. });
  178. }
  179. else {
  180. callback();
  181. }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement