Skip to content

Instantly share code, notes, and snippets.

@ezzabuzaid
Created January 26, 2020 09:20
Show Gist options
  • Save ezzabuzaid/b9558567b1979a700d8d6bad9838a707 to your computer and use it in GitHub Desktop.
Save ezzabuzaid/b9558567b1979a700d8d6bad9838a707 to your computer and use it in GitHub Desktop.
Upload your static site over FTP protocol using on command `node ftp-upload.js --from="localFolder" --to="serverFolder"`
const fs = require('fs');
const path = require('path');
const Client = require('ftp');
const program = require('commander');
const client = new Client();
client.connect({
user: "",
password: "",
host: '',
});
function upload(files, from, to) {
files.forEach((file, i) => {
if (file instanceof FolderParser) {
const splittedName = file.path.split('/');
splittedName.splice(0, 1);
const name = splittedName.join('/')
client.mkdir(path.join(serverDist, name), true, () => { });
upload(file.files, file.path, name)
} else {
const fromPath = path.join(from, file);
const toPath = path.join(serverDist, to, file);
client.append(fromPath, toPath, function (error) {
console.log(`Upload FROM:: ${fromPath},`, `\n Upload TO:: ${toPath}`);
if (error) {
console.warn(error);
throw error;
}
});
}
});
}
class FileSystemUtils {
static getDirectories(srcPath) {
return fs.readdirSync(srcPath)
.map(file => {
const folderPath = path.join(srcPath, file);
if (fs.statSync(folderPath).isDirectory()) {
return new FolderParser(folderPath);
}
return file;
});
}
}
class FolderParser {
constructor(path) {
const names = path.split('/');
this.name = names[names.length - 1];
this.files = FileSystemUtils.getDirectories(path);
this.path = path;
}
}
var serverDist;
// node ftp --from="localFolder" --to="serverFolder"
program
.option('-f, --from <required>', 'The name of folder in local point that contain { this (ftp file) }')
.option('-t, --to <required>', 'The name of folder in server')
.action(({ from, to }) => {
client.on('ready', function () {
const folder = new FolderParser(from);
serverDist = `public_html/${to}`;
client.mkdir(serverDist, true, () => { });
upload(folder.files, folder.path, '');
client.end();
});
})
.parse(process.argv);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment