-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.js
84 lines (75 loc) · 2.62 KB
/
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
81
82
83
84
const fs = require('fs-extra')
const shell = require('shelljs')
const path = require('path')
const os = require('os')
const DEPLOY_ENV = process.env.DEPLOY_ENV || 'staging'
const PROJECT_NAME = 'lisa-core'
const DEPLOYMENT_BRANCH = `docs-${DEPLOY_ENV}`
const REMOTE_BRANCH = 'git@github.com:LISTENAI/lisa-core.git'
function shellExecLog(cmd) {
try {
const result = shell.exec(cmd)
console.log(
`'CMD: ${cmd} (code: ${result.code})`,
)
return result
} catch (e) {
console.log(
`'CMD: ${cmd} (code: ${result.code})`,
)
throw e
}
}
(async () => {
const currentCommit = shellExecLog('git rev-parse HEAD').stdout.trim();
const fromPath = path.join(__dirname, 'docs')
const toPath = await fs.mkdtemp(
path.join(os.tmpdir(), `${PROJECT_NAME}-${DEPLOYMENT_BRANCH}`),
);
if (shellExecLog(`git clone ${REMOTE_BRANCH} ${toPath}`).code !== 0) {
throw new Error(`Running "git clone" command in "${toPath}" failed.`);
}
shell.cd(toPath);
const defaultBranch = shell
.exec('git rev-parse --abbrev-ref HEAD')
.stdout.trim();
if (defaultBranch !== DEPLOYMENT_BRANCH) {
if (shellExecLog(`git checkout origin/${DEPLOYMENT_BRANCH}`).code !== 0) {
if (
shellExecLog(`git checkout --orphan ${DEPLOYMENT_BRANCH}`).code !== 0
) {
throw new Error(
`Running "git checkout ${DEPLOYMENT_BRANCH}" command failed.`,
);
}
} else if (
shellExecLog(`git checkout -b ${DEPLOYMENT_BRANCH}`).code +
shellExecLog(
`git branch --set-upstream-to=origin/${DEPLOYMENT_BRANCH}`,
).code !== 0
) {
throw new Error(
`Running "git checkout ${DEPLOYMENT_BRANCH}" command failed.`,
);
}
}
shellExecLog('git rm -rf .');
try {
await fs.copy(fromPath, toPath);
} catch (error) {
throw new Error(
`Copying build assets from "${fromPath}" to "${toPath}" failed with error "${error}".`,
);
}
shell.cd(toPath);
shellExecLog('git add --all');
const commitResults = shellExecLog(`git commit -m "Deploy docs - based on ${currentCommit}"`);
if (
shellExecLog(`git push --force origin ${DEPLOYMENT_BRANCH}`).code !== 0
) {
throw new Error('Running "git push" command failed.');
} else if (commitResults.code === 0) {
shell.echo(`Docs 已deploy完成,文档文件在本仓库的 ${DEPLOYMENT_BRANCH} 分支`);
shell.exit(0);
}
})()