Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var gulp = require("gulp");
- var FtpClient = require("ftp");
- var minimist = require("minimist");
- var readdirp = require("readdirp");
- var paths = require('../paths');
- var _maxConcurrentActions = 10;
- var _knownOptions = {
- string: [
- "source",
- "url",
- "username",
- "password"
- ],
- default: {
- source: "./"
- }
- };
- var _options = minimist(process.argv.slice(2), _knownOptions);
- var _client = null;
- var _createdPaths = [];
- gulp.task("clean-ftp", function (done) {
- var actions = [];
- openClient(function () {
- _client.list(".", false, function (err, list) {
- if (err) {
- console.log("Error reading root dir from server: " + err);
- closeClient(done);
- }
- else {
- for (var item of list) {
- if (item.type === "d") {
- actions.push(deleteDirAction(item.name));
- }
- else if (item.type === "-") {
- actions.push(deleteFileAction(item.name));
- }
- }
- executeActions(actions, function () {
- closeClient(done);
- });
- }
- });
- });
- });
- gulp.task("deploy-ftp", ["clean-ftp"], function (done) {
- var actions = [];
- openClient(function () {
- readdirp({
- root: _options.source
- }, function (entryInfo) {
- actions.push(uploadFileAction(entryInfo));
- }, function (err, res) {
- if (err) {
- console.log("Error reading local directory: " + err);
- closeClient(done);
- }
- else {
- executeActions(actions, function () {
- closeClient(done);
- });
- }
- });
- });
- });
- gulp.task("deploy", ["deploy-ftp"]);
- function openClient(callback) {
- console.log("Opening FTP connection to '" + _options.url + "'...");
- _client = new FtpClient();
- _client.on("ready", callback);
- _client.on("error", console.log);
- _client.connect({
- host: _options.url,
- user: _options.username,
- password: _options.password
- });
- }
- function closeClient(callback) {
- if (_client) {
- _client.end();
- }
- callback();
- }
- function deleteDirAction(path) {
- return function (callback) {
- console.log("Deleting dir '" + path + "' from server...");
- _client.rmdir(path, true, callback);
- };
- }
- function deleteFileAction(path) {
- return function (callback) {
- console.log("Deleting file '" + path + "' from server...");
- _client.delete(path, callback);
- };
- }
- function createDirAction(path) {
- return function (callback) {
- console.log("Creating dir '" + path + "'...");
- _client.mkdir(path, false, function (err) {
- if (err) {
- console.log("Error creating dir '" + path + "': " + err);
- }
- callback();
- });
- };
- }
- function uploadFileAction(entryInfo) {
- return function (callback) {
- var actions = [];
- if (entryInfo.parentDir != "") {
- var dirs = entryInfo.parentDir.split("\\");
- var fullDir = "";
- dirs.forEach(function (dir) {
- if (fullDir.length > 0) {
- fullDir += "\\";
- }
- fullDir += dir;
- if (_createdPaths.indexOf(fullDir) < 0) {
- actions.push(createDirAction(fullDir));
- _createdPaths.push(fullDir);
- }
- });
- }
- executeActions(actions, function () {
- console.log("Uploading '" + entryInfo.path + "'...");
- _client.put(entryInfo.fullPath, entryInfo.path, function (err) {
- if (err) {
- console.log("Error uploading '" + entryInfo.path + "': " + err);
- }
- callback();
- });
- });
- };
- }
- function executeActions(actions, callback) {
- executeActionsRecursive(actions, 0, callback);
- }
- function executeActionsRecursive(actions, startIndex, callback) {
- var endIndex = (startIndex + _maxConcurrentActions);
- var actionsToExecute = actions.slice(startIndex, endIndex);
- var promises = [];
- if (actionsToExecute.length > 0) {
- actionsToExecute.forEach(function (action) {
- promises.push(new Promise(function (resolve) {
- action(resolve);
- }));
- });
- Promise
- .all(promises)
- .then(function () {
- executeActionsRecursive(actions, endIndex, callback);
- });
- }
- else {
- callback();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement