require('dotenv').config()
const path      = require('path')
const FtpDeploy = require('ftp-deploy')
const ftpDeploy = new FtpDeploy()

const config = {
	project : process.env.WP_THEME,
	host    : process.env.DEPLOY_HOST,
	username: process.env.DEPLOY_USER,
	password: process.env.DEPLOY_PASSWORD,
	port    : 21
}

if (!config.host || !config.username || !config.password) {
	console.log('Error: Missing deploy info in .env!')
	return
}

console.log('Deploying...')

ftpDeploy.deploy({
	host      : `ftp.${config.host}`,
	username  : config.username,
	password  : config.password,
	port      : config.port,
	localRoot : path.join(__dirname, '../../../..'),
	remoteRoot: `/public_html/${config.project}`,
	include   : [
		'.env',
		'composer.json',
		'package.json',
		'mix-manifest.json',
		'public/app/mu-plugins/autoloader.php'
	],
	exclude   : [
		'.*',
		'*.json',
		'*.md',
		'.git',
		'webpack.mix.js',
		'wp-cli.yml',
		'ruleset.xml',
		'yarn.lock',
		'composer.lock',
		'**/.gitkeep',
		'**/.gitignore',
		'vendor',
		'node_modules',
		'public/wp',
		'public/app/mu-plugins/**',
		'public/app/plugins/**',
		'public/app/uploads/.cache',
		'public/app/uploads/gravity_forms',
		'public/app/uploads/wp-sync-db',
		`public/app/themes/${config.project}/resources/assets`
	]
}, error => {
	if (error) {
		console.error(error)
	} else {
		console.log('Deploy done!')
	}
})
