-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathindex.js
More file actions
68 lines (62 loc) · 2.51 KB
/
Copy pathindex.js
File metadata and controls
68 lines (62 loc) · 2.51 KB
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
const baseURL = 'https://robotframework.org/live/Examples'
const getProjectsList = () => new Promise((resolve) => {
fetch(`${baseURL}/index.json`)
.then((res) => res.json())
.then((json) => resolve(json))
})
const getRobotFrameworkVersions = () => new Promise((resolve) => {
fetch('https://pypi.org/pypi/robotframework/json')
.then((res) => res.json())
.then((json) => resolve(Object.keys(json.releases).reverse()))
})
const getProjectFromGitHub = async(ghURL) => {
const [url, user, repo, branch, path] = ghURL.match(/^(?:https:\/\/github\.com\/(.+?)\/(.+?)(?:\/tree\/(.+?)(?:\/(.+?))?)?)?\/?$/)
console.log(`Fetching example from GitHub project: ${url}`)
const downloadURL = `https://raw.githubusercontent.com/${user}/${repo}/${branch || 'main'}${(path) ? '/' + path : ''}`
console.log(downloadURL)
const project = await getProject(downloadURL)
// project.description = `## ⚠️ Caution: User Created Content\n\nBe aware that this code is created by a user of that page and not by Robot Framework Foundation. Therefore we are not liable for the content. The code is loaded from the following GitHub Repo.\n<a href="${url}" target="_blank">${url}</a>\n\nIf you run this code it will be executed in your browser.\n\n---\n${project.description}`
return project
}
const getProjectFromLiveDir = async(projectDir) => {
const projectUrl = `${baseURL}/${projectDir}`
return await getProject(projectUrl)
}
const getProject = async(projectUrl) => {
console.log(`Loading data from ${projectUrl}`)
const configFile = await fetch(`${projectUrl}/config.json`)
.then(response => response.json())
const project = { name: configFile.name, files: [], description: '' }
if (configFile.robotVersion) {
project.robotVersion = configFile.robotVersion
}
if (configFile.robotArgs) {
project.robotArgs = configFile.robotArgs
}
if (configFile.requirements) {
project.requirements = configFile.requirements
}
const descriptionFileName = configFile.description
if (descriptionFileName) {
project.description = await fetch(projectUrl + '/' + descriptionFileName)
.then(response => response.text())
}
for (const file of configFile.files) {
const { fileName, hidden } = file
const content = await fetch(`${projectUrl}/${fileName}`)
.then(response => response.text())
project.files.push({
fileName,
hidden,
content
})
}
return project
}
export {
getProjectsList,
getProjectFromLiveDir,
getProjectFromGitHub,
getProject,
getRobotFrameworkVersions
}