forked from pytorch-ignite/code-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
67 lines (62 loc) · 1.83 KB
/
Copy pathutils.js
File metadata and controls
67 lines (62 loc) · 1.83 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
// Below imports are defined in
// `external_node_modules` of [functions] in netlify.toml
// They are required for this function to run
import { Octokit } from '@octokit/core'
import JSZip from 'jszip'
import { v5 as uuidv5 } from 'uuid'
const repoOwner = process.env.VUE_APP_GH_USER
const repo = process.env.VUE_APP_GH_REPO
/**
* Create a file on GitHub with Octokit.
* @param {string} content
* @param {string} filename
* @param {string} nbUid
* @param {string} repoOwner
* @param {string} repo
* @returns download_url
*/
export async function pushToGitHub(content, filename, nbUid) {
const octokit = new Octokit({
auth: process.env.VUE_APP_GH_TOKEN
})
try {
const res = await octokit.request(
'PUT /repos/{owner}/{repo}/contents/{path}',
{
owner: repoOwner,
repo: repo,
path: `nbs/${nbUid}/${filename}`,
message: `nb: add ${nbUid}`,
content: content
}
)
return res.data.content.download_url
} catch (e) {
console.error(e)
}
}
// This function is the one Netlify function runs on
// https://docs.netlify.com/functions/build-with-javascript/#synchronous-function-format
export async function getZip_Uid(data) {
const zip = new JSZip()
const code = data.code
let hash = ''
const template = `ignite-${data.template}`
// As usual from Download component,
// we will zip the files and
// generate a base64 format for pushing to GitHub
// with Octokit.
// we generate a hash for unique code identification and
// zip generation
for (const filename in code) {
hash += code[filename]
zip.file(filename, code[filename])
}
const nbUid = uuidv5(hash, uuidv5.URL)
const content = await zip.generateAsync({ type: 'base64' })
const zipRes = await pushToGitHub(content, `${template}.zip`, nbUid)
return {
zipRes: zipRes,
nbUid: nbUid
}
}