forked from Bhacaz/checkout-files
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (36 loc) · 1.11 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
const core = require('@actions/core');
const github = require('@actions/github');
const fs = require('fs');
const token = core.getInput('token');
const octokit = github.getOctokit(token)
const files = core.getInput('files').split(' ');
const repository = process.env['GITHUB_REPOSITORY']
const owner = repository.split('/')[0]
const repo = repository.split('/')[1]
const ref = core.getInput('branch');
function getContent(path) {
octokit.repos.getContent({
owner,
repo,
path,
ref,
}).then(data => {
if (Array.isArray(data.data)) {
data.data.forEach(fileData => getContent(fileData.path))
} else {
saveContent(data.data)
}
})
}
function saveContent(data) {
const fileContent = Buffer.from(data.content, 'base64').toString('utf-8');
if (data.path.includes('/')) {
let foldersPath = data.path.split('/')
foldersPath.pop()
fs.mkdirSync(foldersPath.join('/'), { recursive: true });
}
fs.writeFile(data.path, fileContent, err => { if (err) throw err });
}
files.forEach(file =>{
getContent(file)
})