diff --git a/index.js b/index.js index e8459e0..56127e0 100644 --- a/index.js +++ b/index.js @@ -1,117 +1 @@ -import fs from "fs"; -import fetch from "node-fetch"; -import FormData from "form-data"; -import path from "path"; - -let apiLink; -let token; -let repo; -let branch; -let tag; -let uploadDir; -let message; -try { - apiLink = process.argv[2]; - if (typeof apiLink != "string" || apiLink.length < 5) throw new Error("Invalid apiLink!"); - token = process.argv[3]; - if (typeof token != "string" || token.length < 5) throw new Error("Invalid Token!"); - repo = process.argv[4]; - if (typeof repo != "string" || repo.length < 3) throw new Error("Invalid repo!"); - branch = process.argv[5]; - if (typeof branch != "string" || branch.length < 1) throw new Error("Invalid branch!"); - tag = process.argv[6]; - if (typeof tag != "string") throw new Error("Invalid tag!"); - uploadDir = process.argv[7]; - if (typeof tag != "string") throw new Error("Invalid uplaodDir!"); - message = process.argv[8] || ""; -} catch (error) { - console.error("Invalid args", error); - console.log("call with this Arguments: [apiLink] [token] [repo] [branch] [tag] [uploadDir] [message]"); - process.exit(1); -} -if (tag.length < 1) { - console.log("No publish"); - process.exit(0); -} - -uploadDir = jpath(process.cwd(), uploadDir); - -let uploadContent = fs.readdirSync(uploadDir, { withFileTypes: true }); - -let releaseID; -try { - releaseID = await createRelease(apiLink, repo, token, message, tag, branch); -} catch (error) { - console.error("Error while creating release: ", error); - process.exit(1); -} -try { - for (let i in uploadContent) { - let cont = uploadContent[i]; - if (!cont.isFile()) continue; - console.log("try uploading: ",cont.name); - await uploadFile(apiLink, repo, token, releaseID, jpath(uploadDir, cont.name), cont.name); - console.log("done"); - } -} catch (error) { - console.error("Error while uploading Files:", error); - process.exit(1); -} - -function createRelease(apiLink, repo, token, message, tag, branch) { - return new Promise((res, rej) => { - fetch(apiLink + "repos/" + repo + "/releases?access_token=" + token, { - method: "post", - headers: { - accept: "application/json", - "Content-Type": "application/json" - }, - body: JSON.stringify({ - body: message, - draft: false, - name: tag, - tag_name: tag, - prerelease: true, - target_commitish: branch - }) - }) - .then(d => d.json()) - .then(json => { - let releaseID = json.id; - if (isNaN(releaseID)) { - return void rej("Invalid Id!"); - } - res(releaseID); - }) - .catch((e) => { - rej(e); - }) - }); -} - -function uploadFile(apiLink, repo, token, releaseID, path, name) { - return new Promise((res, rej) => { - const file = fs.createReadStream(path); - let data = new FormData(); - data.append("attachment", file); - fetch(apiLink + "repos/" + repo + "/releases/" + releaseID + "/assets?name=" + name + "&access_token=" + token, { - method: "post", - headers: Object.assign({ accept: "application/json" }, data.getHeaders()), - body: data - }) - .then(d => d.text()) - .then(json => { - res(); - }) - .catch((e) => { - rej(e); - }); - }); -} - -function jpath(a, b) { - if (b.startsWith("/")) { - return b; - } - return path.join(a, b); -} +//nothing important here \ No newline at end of file diff --git a/package.json b/package.json index ea2cc78..affa79d 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "url": "git@jusax.de:jusax23/gitea-release.git" }, "files": [ - "index.js" + "upload.js" ], "author": "jusax23", "license": "ISC", diff --git a/upload.js b/upload.js new file mode 100644 index 0000000..e8459e0 --- /dev/null +++ b/upload.js @@ -0,0 +1,117 @@ +import fs from "fs"; +import fetch from "node-fetch"; +import FormData from "form-data"; +import path from "path"; + +let apiLink; +let token; +let repo; +let branch; +let tag; +let uploadDir; +let message; +try { + apiLink = process.argv[2]; + if (typeof apiLink != "string" || apiLink.length < 5) throw new Error("Invalid apiLink!"); + token = process.argv[3]; + if (typeof token != "string" || token.length < 5) throw new Error("Invalid Token!"); + repo = process.argv[4]; + if (typeof repo != "string" || repo.length < 3) throw new Error("Invalid repo!"); + branch = process.argv[5]; + if (typeof branch != "string" || branch.length < 1) throw new Error("Invalid branch!"); + tag = process.argv[6]; + if (typeof tag != "string") throw new Error("Invalid tag!"); + uploadDir = process.argv[7]; + if (typeof tag != "string") throw new Error("Invalid uplaodDir!"); + message = process.argv[8] || ""; +} catch (error) { + console.error("Invalid args", error); + console.log("call with this Arguments: [apiLink] [token] [repo] [branch] [tag] [uploadDir] [message]"); + process.exit(1); +} +if (tag.length < 1) { + console.log("No publish"); + process.exit(0); +} + +uploadDir = jpath(process.cwd(), uploadDir); + +let uploadContent = fs.readdirSync(uploadDir, { withFileTypes: true }); + +let releaseID; +try { + releaseID = await createRelease(apiLink, repo, token, message, tag, branch); +} catch (error) { + console.error("Error while creating release: ", error); + process.exit(1); +} +try { + for (let i in uploadContent) { + let cont = uploadContent[i]; + if (!cont.isFile()) continue; + console.log("try uploading: ",cont.name); + await uploadFile(apiLink, repo, token, releaseID, jpath(uploadDir, cont.name), cont.name); + console.log("done"); + } +} catch (error) { + console.error("Error while uploading Files:", error); + process.exit(1); +} + +function createRelease(apiLink, repo, token, message, tag, branch) { + return new Promise((res, rej) => { + fetch(apiLink + "repos/" + repo + "/releases?access_token=" + token, { + method: "post", + headers: { + accept: "application/json", + "Content-Type": "application/json" + }, + body: JSON.stringify({ + body: message, + draft: false, + name: tag, + tag_name: tag, + prerelease: true, + target_commitish: branch + }) + }) + .then(d => d.json()) + .then(json => { + let releaseID = json.id; + if (isNaN(releaseID)) { + return void rej("Invalid Id!"); + } + res(releaseID); + }) + .catch((e) => { + rej(e); + }) + }); +} + +function uploadFile(apiLink, repo, token, releaseID, path, name) { + return new Promise((res, rej) => { + const file = fs.createReadStream(path); + let data = new FormData(); + data.append("attachment", file); + fetch(apiLink + "repos/" + repo + "/releases/" + releaseID + "/assets?name=" + name + "&access_token=" + token, { + method: "post", + headers: Object.assign({ accept: "application/json" }, data.getHeaders()), + body: data + }) + .then(d => d.text()) + .then(json => { + res(); + }) + .catch((e) => { + rej(e); + }); + }); +} + +function jpath(a, b) { + if (b.startsWith("/")) { + return b; + } + return path.join(a, b); +}