-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
42 lines (40 loc) · 1.33 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 fetch = require('node-fetch');
const fs = require('fs');
const path = require('path')
const makeSureFoldersAreCreated = filename => {
const folders = filename.split(path.sep).slice(0, -1)
if (folders.length) {
folders.reduce((last, folder) => {
const folderPath = last ? last + path.sep + folder : folder
if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath)
console.log(`created folder: ${folderPath}`);
}
return folderPath
},'');
}
}
try{
const url = core.getInput('url');
const file = core.getInput('file');
console.log(`Fetch data started with`, `url: ${url}`, `file: ${file}`);
if(!url){
core.setFailed('url required');
}
fetch(url)
.then(response => {
if(response.ok){
return response.json();
}
core.setFailed(`fetch to ${url} failed with status: ${response.status}`);
})
.then(data => {
makeSureFoldersAreCreated(file);
fs.writeFileSync(file, JSON.stringify(data, null, 2));
console.log(`successfully saved data from ${url} to ${file}`);
})
.catch(error => core.setFailed(error.message));
}catch(error){
core.setFailed(error.message);
}