-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
94 lines (86 loc) · 2.76 KB
/
index.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
85
86
87
88
89
90
91
92
93
94
const fs = require('fs')
const path = require('path')
const upload = require('./upload.js')
const core = require('@actions/core')
const checkBranch = require('./checkBranch.js')
const inputPath = core.getInput('file-path')
const inputRemoteDir = core.getInput('remote-dir')
const inputOwner = core.getInput('owner')
const inputRepo = core.getInput('repo')
const commitMessage = core.getInput('commit-message')
const branchName = core.getInput('branch-name')
const token = core.getInput('access-token')
core.debug('Input path: ' + inputPath)
core.debug('Input remoteDir: ' + inputRemoteDir)
core.debug('Input owner: ' + inputOwner)
core.debug('Input repo: ' + inputRepo)
core.debug('Input commitMessage: ' + commitMessage)
core.debug('Input branchName: ' + branchName)
if (!fs.existsSync(inputPath)) {
core.setFailed(`filePath doesn't exist: ${inputPath}`)
return
}
if (inputRepo.indexOf('/') !== -1) {
core.setFailed(
'inputRepo cannot contain any slashes use the owner parameter to indicate the owner'
)
return
}
const isInputPathDir = fs.lstatSync(inputPath).isDirectory()
const localDir = isInputPathDir ? inputPath : ''
function getAllFilePaths(curDir) {
function search(curPath, paths = []) {
const dir = fs.readdirSync(curPath)
dir.forEach(item => {
const itemPath = path.join(curPath, item)
const stat = fs.lstatSync(itemPath)
if (stat.isDirectory()) {
search(itemPath, paths)
} else {
paths.push(itemPath)
}
})
return paths
}
return search(curDir)
}
const filePaths = isInputPathDir ? getAllFilePaths(inputPath) : [inputPath]
core.debug(`filePaths: ${filePaths}`)
async function uploadAll() {
try {
await checkBranch({
token,
owner: inputOwner,
repo: inputRepo,
branchName,
})
} catch (error) {
core.setFailed(`checkBranch failed: ${error}`)
return
}
for (let index = 0; index < filePaths.length; index++) {
const curPath = filePaths[index]
const remotePath = path.join(
// `remotePath` can not start with `/`
inputRemoteDir.replace(/^\//, ''),
path.relative(localDir, curPath)
)
core.debug(
`Upload ${curPath} to ${remotePath} on branch ${branchName} for repository ${inputRepo}`
)
try {
await upload(curPath, {
token,
owner: inputOwner,
repo: inputRepo,
commitMessage,
remotePath,
branchName,
})
} catch (error) {
core.setFailed(`Upload ${curPath} failed: ${error}`)
return
}
}
}
uploadAll()