{"size": 1681, "ext": "js", "max_stars_repo_path": "scripts/deploy.js", "max_stars_repo_name": "stphnsn/mfc-gatsby", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "scripts/deploy.js", "max_issues_repo_name": "stphnsn/mfc-gatsby", "max_issues_repo_issues_event_min_datetime": "2022-02-27T22:27:59.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-27T22:27:59.000Z", "max_forks_repo_path": "scripts/deploy.js", "max_forks_repo_name": "stphnsn/mfc-gatsby", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 24.7205882353, "max_line_length": 58, "alphanum_fraction": 0.6745984533}
const fs = require('fs')
const ftp = require('ftp')
const glob = require('glob')

const basePath = '../public'
const destinationPath = '/manorfarm-cottages.co.uk/html'
const config = {
  // We store the credentials for
  // our FTP server as environemnt
  // variables for security reasons.
  host: process.env.FTP_HOST,
  user: process.env.FTP_USER,
  password: process.env.FTP_PASSWORD,
}

const ftpClient = new ftp()

function createDirectory(destination) {
  return ftpClient.mkdir(destination, true, error => {
    if (error) {
      console.log('createDirectory')
      throw error
    }

    ftpClient.end()
  })
}

function uploadFile(file, destination) {
  ftpClient.put(file, destination, error => {
    if (error) {
      console.log('uploadFile', file)
      console.log('destination', destination)
      throw error
    }

    console.log(`${file} => ${destination}`)
    ftpClient.end()
  })
}

// Check if the path is a directory and
// either create the directory on the server
// if it is a directory, or upload the file
// if it is a file.
function handlePath(path) {
  const relativeFile = path.replace(`${basePath}/`, '')
  const destination = `${destinationPath}/${relativeFile}`

  if (fs.lstatSync(path).isDirectory()) {
    return createDirectory(destination)
  }

  return uploadFile(path, destination)
}

ftpClient.on('ready', () => {
  console.log('FTP: Ready')
  // Get an array of all files and directories
  // in the given base path and send them to the
  // `handlePath()` function to decide if a
  // directory is created on the server or the
  // file is uploaded.
  glob.sync(`${basePath}/**/*`).forEach(handlePath)
})

ftpClient.connect(config)
