Skip to content

Instantly share code, notes, and snippets.

@rafaelrinaldi
Created June 14, 2016 19:51
Show Gist options
  • Select an option

  • Save rafaelrinaldi/cc5bf69068783fcf793c817c7a58435b to your computer and use it in GitHub Desktop.

Select an option

Save rafaelrinaldi/cc5bf69068783fcf793c817c7a58435b to your computer and use it in GitHub Desktop.
Deploy stuff through FTP via npm scripts/CLI
'use strict';
/**
* Simple deployment script.
*
* Run it through:
* $ npm run deploy
*
* Note that the script will add all files to remote server without doing diffs
* or anything. It behaves pretty much like rsync.
*
* It accepts file ignoring through wildcards, which might come in handy:
* $ npm run deploy -- -i '*.jpg' # Do not deploy .jpg files
**/
const argv = require('yargs').alias('i', 'ignore').argv;
const figures = require('figures');
const chalk = require('chalk');
const logUpdate = require('log-update');
const FTPDeploy = require('ftp-deploy');
const deployer = new FTPDeploy();
const options = require('../options.json');
const ignore = argv.ignore ? argv.ignore.split(',') : [];
let settings = {
username: options.credentials.username,
password: options.credentials.password,
host: options.host,
port: 21,
localRoot: options.localFolder,
remoteRoot: options.remoteFolder,
continueOnError: true,
exclude: ['.git', '.keep', '.DS_Store'].concat(ignore)
};
let log = {};
let interval = 0;
let hasStarted = false;
// Start deployment
const start = () => {
hasStarted = true;
prompt('Deploy started');
// Freezes `stdout` and update log with deploy information
interval = setInterval(() => {
logUpdate(`${chalk.green(figures.pointer)} Uploaded ${chalk.bold(log.transferredFileCount)} of ${chalk.bold(log.totalFileCount)} files (${log.percentComplete}% complete)
${chalk.underline(log.filename)}`);
}, 250);
};
// Helper to prompt a fancy message to `stdout`
const prompt = message => {
console.log(`${chalk.green(figures.pointer)} ${message}`);
};
prompt('Connecting to server...');
deployer.deploy(settings, error => {
// Failure
if(error) throw error;
// Success
clearInterval(interval);
prompt(`Successfully deployed ${figures.tick}`);
});
deployer.on('uploading', data => {
if(!hasStarted) start();
// Save data to a local variable so we can easily use it w/ logUpdate
log = data;
});
// Notifies bad upload
deployer.on('upload-error', data => {
console.error(`${chalk.red(figures.warning)} Problem uploading file "${data.filename}"`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment