const { fileForEach, readFile, writeFile } = require('@jiaminghi/fs') const Client = require('ftp') const print = require('./plugin/print') const { emptyDir, put, mkDir } = require('./plugin/ftp') const getNodeParams = require('./plugin/nodeParams') let config = null try { config = require('./config') } catch (err) { void 0 } const DIST_PATH = './docs/.vuepress/dist' const ftp = new Client() ftp.on('ready', async foo => { print.tip('FTP connected!') const isEmpty = await emptyDir(ftp, '/', ['demo']) if (!isEmpty) { print.error('Exception in emptyDir!') return false } let status = true await fileForEach(DIST_PATH, async src => { const destPath = '/' + src.split('dist/')[1] const destDir = destPath.split('/').slice(0, -1).join('/') await mkDir(ftp, destDir, true) print.tip('Upload: ' + destPath) if (!await put(ftp, src, destPath)) { status = false print.error('Exception in upload ' + destPath) } }) if (status) { print.yellow('-------------------------------------') print.success(' Automatic Deployment Success! ') print.yellow('-------------------------------------') } ftp.destroy() }) ftp.on('greeting', foo => { print.tip('FTP greeting') }) ftp.on('close', foo => { print.tip('FTP close') }) ftp.on('end', foo => { print.tip('FTP end') }) ftp.on('error', foo => { print.tip('FTP error') }) const GrowingIO_SDK = ` ` async function addGrowingIOSDK () { const indexPagePath = DIST_PATH + '/index.html' let indexPage = await readFile(indexPagePath) if (!indexPage) return false if (indexPage.indexOf(GrowingIO_SDK) !== -1) return true const addedSDKHead = indexPage.match(/(\s|\S)*<\/head>/)[0] .replace('', `${GrowingIO_SDK}\n `) indexPage = indexPage.replace(/(\s|\S)*<\/head>/, addedSDKHead) return writeFile(indexPagePath, indexPage) } const { host, user, pass } = config || getNodeParams() if (!host || !user || !pass) { print.error('Upload Dist to FTP Missing Parameters!') return false } try { (async function () { print.tip('Start add Growing IO SDK!') const addGIOSDK = await addGrowingIOSDK() if (!addGIOSDK) return print.error('Add Growing IO SDK fail!') print.tip('Add Growing IO SDK success!') print.tip('Start Upload!') ftp.connect({ host, user, password: pass }) })() } catch { print.error('Deploy Fail!') }