f06859534cccb2091b8d9e7a1fbc41fcc576ab638c6c9eae3404c4a6c774f9bdeac5d7a4b20946fca52b4cdad60c5b5f8fe0b88c290f542978f120cb7dbec9 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. const { fileForEach } = require('@jiaminghi/fs')
  2. const Client = require('ftp')
  3. const print = require('./plugin/print')
  4. const { emptyDir, put } = require('./plugin/ftp')
  5. const getNodeParams = require('./plugin/nodeParams')
  6. let config = null
  7. try {
  8. config = require('./config')
  9. } catch (err) {
  10. void 0
  11. }
  12. const DIST_PATH = './dist/'
  13. const FTP_PATH = './bezierCurve/'
  14. const ftp = new Client()
  15. ftp.on('ready', async foo => {
  16. print.tip('FTP connected!')
  17. const isEmpty = await emptyDir(ftp, FTP_PATH)
  18. if (!isEmpty) {
  19. print.error('Exception in emptyDir!')
  20. return false
  21. }
  22. let status = true
  23. await fileForEach(DIST_PATH, async src => {
  24. const destPath = FTP_PATH + src.split('/').slice(-1)[0]
  25. print.tip('Upload: ' + destPath)
  26. if (!await put(ftp, src, destPath)) {
  27. status = false
  28. print.error('Exception in upload ' + destPath)
  29. }
  30. })
  31. if (status) {
  32. print.yellow('-------------------------------------')
  33. print.success(' Automatic Deployment Success! ')
  34. print.yellow('-------------------------------------')
  35. }
  36. ftp.destroy()
  37. })
  38. ftp.on('greeting', foo => {
  39. print.tip('FTP greeting')
  40. })
  41. ftp.on('close', foo => {
  42. print.tip('FTP close')
  43. })
  44. ftp.on('end', foo => {
  45. print.tip('FTP end')
  46. })
  47. ftp.on('error', foo => {
  48. print.tip('FTP error')
  49. })
  50. const { host, user, pass } = config || getNodeParams()
  51. if (!host || !user || !pass) {
  52. print.error('Upload Dist to FTP Missing Parameters!')
  53. return false
  54. }
  55. print.tip('Start Upload!')
  56. ftp.connect({
  57. host,
  58. user,
  59. password: pass
  60. })