require('dotenv').config()
const path      = require('path')
const FtpDeploy = require('ftp-deploy')
const project = 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,
	nas     : process.env.NAS_UPLOAD,
	nasIP   : process.env.NAS_IP,
}

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

console.log('Syncing...')

project.deploy({
	host      : `ftp.${config.host}`,
	username  : config.username,
	password  : config.password,
	port      : config.port,
	localRoot : path.join(__dirname, '../../../..'),
	remoteRoot: `/public_html/${config.project}`,
	include   : [
		'composer.json',
		'package.json',
		'mix-manifest.json'
	],
	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 {
		if (config.nas == 'true') {
			const nas = new FtpDeploy()

			nas.deploy({
				host      : `ftp.${config.host}`,
				username  : config.username,
				password  : config.password,
				port      : config.port,
				localRoot : `\\\\${config.nasIP}/web/uploads/${config.project}`,
				remoteRoot: `/public_html/${config.project}/public/app/uploads`,
				exclude   : [
					'.cache'
				]
			}, error => {
				if (error) console.error(error)
			})
		}

		console.log('Syncing done!')
	}
})
