-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
task.deploy.js
80 lines (68 loc) · 2 KB
/
task.deploy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const FtpDeploy = require('ftp-deploy')
const {execSync} = require('child_process')
const fetch = require('node-fetch')
const argv = require('yargs').argv
const fs = require('fs')
const FtpInfo = require('./ftp.info.js')
const siteURL = 'https://glisp.app'
const gitHash = execSync('git rev-parse HEAD').toString().trim().slice(0, 7)
async function upload() {
// Upload to the subdirectory with git hash
if (argv.commit) {
await deploy('commit')
}
if (argv.docs) {
await deploy('doc')
}
}
upload()
async function deploy(mode) {
const ftpDeploy = new FtpDeploy()
const urlSuffix = mode === 'commit' ? `commit:${gitHash}` : 'docs'
const remoteRoot = `${FtpInfo.remoteRoot}/${urlSuffix}`
const localRoot = `${__dirname}/${mode === 'commit' ? 'dist' : 'docs'}/`
const publishedURL = `${siteURL}/${urlSuffix}`
console.log(`Start Uploading: ${publishedURL}`)
const config = {
...FtpInfo,
localRoot,
remoteRoot,
include: ['*', '**/*', '.htaccess'],
exclude: ['**/*.map', 'node_modules/**', 'node_modules/**/.*', '.git/**'],
// delete ALL existing files at destination before uploading, if true
deleteRemote: true,
// Passive mode is forced (EPSV command is not sent)
forcePasv: true,
}
ftpDeploy.on('uploading', function (data) {
console.log(`[${urlSuffix}]`, 'Uploading...', data.filename)
})
// Upload
try {
await ftpDeploy.deploy(config)
console.log(`Uploaded: ${publishedURL}`)
} catch (err) {
console.error(err)
}
// Update Commits.json
if (mode === 'commit') {
const res = await fetch(`${siteURL}/commits.json`)
const commits = await res.json()
if (commits.length === 0 || commits[commits.length - 1][0] !== gitHash) {
commits.push([gitHash, Date.now()])
}
fs.writeFileSync(`${localRoot}/commits.json`, JSON.stringify(commits))
try {
await ftpDeploy.deploy({
...FtpInfo,
localRoot,
remoteRoot: FtpInfo.remoteRoot,
include: ['commits.json'],
forcePasv: true,
})
console.log('Updated commits.json')
} catch (err) {
console.error(err)
}
}
}