From fc9d97fd146c688fdccff897802207722ec51862 Mon Sep 17 00:00:00 2001 From: lowlighter <22963968+lowlighter@users.noreply.github.com> Date: Thu, 19 Aug 2021 22:39:50 +0200 Subject: [PATCH] Format .github code --- .github/index.mjs | 82 +++++++++++------------ .github/markdown_example.mjs | 32 ++++----- .github/preview.mjs | 126 +++++++++++++++++------------------ .github/quickstart/index.mjs | 56 ++++++++-------- .github/release.mjs | 78 +++++++++++----------- 5 files changed, 187 insertions(+), 187 deletions(-) diff --git a/.github/index.mjs b/.github/index.mjs index 1d23adfc..c2dd85fe 100644 --- a/.github/index.mjs +++ b/.github/index.mjs @@ -1,56 +1,56 @@ //Imports - import ejs from "ejs" - import fs from "fs/promises" - import paths from "path" - import url from "url" - import sgit from "simple-git" - import metadata from "../source/app/metrics/metadata.mjs" +import ejs from "ejs" +import fs from "fs/promises" +import paths from "path" +import url from "url" +import sgit from "simple-git" +import metadata from "../source/app/metrics/metadata.mjs" //Mode - const [mode = "dryrun"] = process.argv.slice(2) - console.log(`Mode: ${mode}`) +const [mode = "dryrun"] = process.argv.slice(2) +console.log(`Mode: ${mode}`) //Paths - const __metrics = paths.join(paths.dirname(url.fileURLToPath(import.meta.url)), "..") - const __action = paths.join(__metrics, "source/app/action") - const __web = paths.join(__metrics, "source/app/web") - const __readme = paths.join(__metrics, ".github/readme") +const __metrics = paths.join(paths.dirname(url.fileURLToPath(import.meta.url)), "..") +const __action = paths.join(__metrics, "source/app/action") +const __web = paths.join(__metrics, "source/app/web") +const __readme = paths.join(__metrics, ".github/readme") //Git setup - const git = sgit(__metrics) - const staged = new Set() +const git = sgit(__metrics) +const staged = new Set() //Load plugins metadata - const {plugins, templates, packaged, descriptor} = await metadata({log:false}) +const {plugins, templates, packaged, descriptor} = await metadata({log:false}) //Update generated files - async function update({source, output, options = {}}) { - //Regenerate file - console.log(`Generating ${output}`) - const content = await ejs.renderFile(source, {plugins, templates, packaged, descriptor}, {async:true, ...options}) - //Save result - const file = paths.join(__metrics, output) - await fs.writeFile(file, content) - //Add to git - staged.add(file) - } +async function update({source, output, options = {}}) { + //Regenerate file + console.log(`Generating ${output}`) + const content = await ejs.renderFile(source, {plugins, templates, packaged, descriptor}, {async:true, ...options}) + //Save result + const file = paths.join(__metrics, output) + await fs.writeFile(file, content) + //Add to git + staged.add(file) +} //Rendering - await update({source:paths.join(__readme, "README.md"), output:"README.md", options:{root:__readme}}) - await update({source:paths.join(__readme, "partials/documentation/plugins.md"), output:"source/plugins/README.md"}) - await update({source:paths.join(__readme, "partials/documentation/templates.md"), output:"source/templates/README.md"}) - await update({source:paths.join(__action, "action.yml"), output:"action.yml"}) - await update({source:paths.join(__web, "settings.example.json"), output:"settings.example.json"}) +await update({source:paths.join(__readme, "README.md"), output:"README.md", options:{root:__readme}}) +await update({source:paths.join(__readme, "partials/documentation/plugins.md"), output:"source/plugins/README.md"}) +await update({source:paths.join(__readme, "partials/documentation/templates.md"), output:"source/templates/README.md"}) +await update({source:paths.join(__action, "action.yml"), output:"action.yml"}) +await update({source:paths.join(__web, "settings.example.json"), output:"settings.example.json"}) //Commit and push - if (mode === "publish") { - console.log(`Pushing staged changes: \n${[...staged].map(file => ` - ${file}`).join("\n")}`) - const gitted = await git - .addConfig("user.name", "github-actions[bot]") - .addConfig("user.email", "41898282+github-actions[bot]@users.noreply.github.com") - .add([...staged]) - .commit("Auto-regenerate files") - .push("origin", "master") - console.log(gitted) - } - console.log("Success!") +if (mode === "publish") { + console.log(`Pushing staged changes: \n${[...staged].map(file => ` - ${file}`).join("\n")}`) + const gitted = await git + .addConfig("user.name", "github-actions[bot]") + .addConfig("user.email", "41898282+github-actions[bot]@users.noreply.github.com") + .add([...staged]) + .commit("Auto-regenerate files") + .push("origin", "master") + console.log(gitted) +} +console.log("Success!") diff --git a/.github/markdown_example.mjs b/.github/markdown_example.mjs index 2041d129..e70de343 100644 --- a/.github/markdown_example.mjs +++ b/.github/markdown_example.mjs @@ -1,21 +1,21 @@ //Imports - import puppeteer from "puppeteer" +import puppeteer from "puppeteer" //Setup browser - const browser = await puppeteer.launch({ - headless:true, - executablePath:process.env.PUPPETEER_BROWSER_PATH, - args:["--no-sandbox", "--disable-extensions", "--disable-setuid-sandbox", "--disable-dev-shm-usage"], - ignoreDefaultArgs:["--disable-extensions"], - }) - const page = await browser.newPage() +const browser = await puppeteer.launch({ + headless:true, + executablePath:process.env.PUPPETEER_BROWSER_PATH, + args:["--no-sandbox", "--disable-extensions", "--disable-setuid-sandbox", "--disable-dev-shm-usage"], + ignoreDefaultArgs:["--disable-extensions"], +}) +const page = await browser.newPage() //Select markdown example and take screenshoot - await page.setViewport({width:600, height:600}) - await page.goto("https://github.com/lowlighter/lowlighter/blob/master/metrics.markdown.md") - const clip = await page.evaluate(() => { - const {x, y, width, height} = document.querySelector("#readme").getBoundingClientRect() - return {x, y, width, height} - }) - await page.screenshot({type:"png", path:"metrics.markdown.png", clip, omitBackground:true}) - await browser.close() +await page.setViewport({width:600, height:600}) +await page.goto("https://github.com/lowlighter/lowlighter/blob/master/metrics.markdown.md") +const clip = await page.evaluate(() => { + const {x, y, width, height} = document.querySelector("#readme").getBoundingClientRect() + return {x, y, width, height} +}) +await page.screenshot({type:"png", path:"metrics.markdown.png", clip, omitBackground:true}) +await browser.close() diff --git a/.github/preview.mjs b/.github/preview.mjs index 392ccbcd..6bbc2c65 100644 --- a/.github/preview.mjs +++ b/.github/preview.mjs @@ -1,79 +1,79 @@ //Imports - import fs from "fs/promises" - import paths from "path" - import url from "url" - import setup from "../source/app/metrics/setup.mjs" +import fs from "fs/promises" +import paths from "path" +import url from "url" +import setup from "../source/app/metrics/setup.mjs" //Paths - const __metrics = paths.join(paths.dirname(url.fileURLToPath(import.meta.url)), "..") - const __templates = paths.join(paths.join(__metrics, "source/templates/")) - const __node_modules = paths.join(paths.join(__metrics, "node_modules")) - const __web = paths.join(paths.join(__metrics, "source/app/web/statics")) - const __web_about = paths.join(paths.join(__web, "about")) +const __metrics = paths.join(paths.dirname(url.fileURLToPath(import.meta.url)), "..") +const __templates = paths.join(paths.join(__metrics, "source/templates/")) +const __node_modules = paths.join(paths.join(__metrics, "node_modules")) +const __web = paths.join(paths.join(__metrics, "source/app/web/statics")) +const __web_about = paths.join(paths.join(__web, "about")) - const __preview = paths.join(paths.join(__web, "preview")) - const __preview_js = paths.join(__preview, ".js") - const __preview_css = paths.join(__preview, ".css") - const __preview_templates = paths.join(__preview, ".templates") - const __preview_templates_ = paths.join(__preview, ".templates_") - const __preview_about = paths.join(__preview, "about/.statics") +const __preview = paths.join(paths.join(__web, "preview")) +const __preview_js = paths.join(__preview, ".js") +const __preview_css = paths.join(__preview, ".css") +const __preview_templates = paths.join(__preview, ".templates") +const __preview_templates_ = paths.join(__preview, ".templates_") +const __preview_about = paths.join(__preview, "about/.statics") //Extract from web server - const {conf, Templates} = await setup({nosettings:true, log:false}) - const templates = Object.entries(Templates).map(([name]) => ({name, enabled:true})) - const metadata = Object.fromEntries(Object.entries(conf.metadata.plugins) - .map(([key, value]) => [key, Object.fromEntries(Object.entries(value).filter(([key]) => ["name", "icon", "category", "web", "supports"].includes(key)))]) - .map(([key, value]) => [key, key === "core" ? {...value, web:Object.fromEntries(Object.entries(value.web).filter(([key]) => /^config[.]/.test(key)).map(([key, value]) => [key.replace(/^config[.]/, ""), value]))} : value])) +const {conf, Templates} = await setup({nosettings:true, log:false}) +const templates = Object.entries(Templates).map(([name]) => ({name, enabled:true})) +const metadata = Object.fromEntries(Object.entries(conf.metadata.plugins) + .map(([key, value]) => [key, Object.fromEntries(Object.entries(value).filter(([key]) => ["name", "icon", "category", "web", "supports"].includes(key)))]) + .map(([key, value]) => [key, key === "core" ? {...value, web:Object.fromEntries(Object.entries(value.web).filter(([key]) => /^config[.]/.test(key)).map(([key, value]) => [key.replace(/^config[.]/, ""), value]))} : value])) //Directories - await fs.mkdir(__preview, {recursive:true}) - await fs.mkdir(__preview_js, {recursive:true}) - await fs.mkdir(__preview_css, {recursive:true}) - await fs.mkdir(__preview_templates, {recursive:true}) - await fs.mkdir(__preview_templates_, {recursive:true}) - await fs.mkdir(__preview_about, {recursive:true}) +await fs.mkdir(__preview, {recursive:true}) +await fs.mkdir(__preview_js, {recursive:true}) +await fs.mkdir(__preview_css, {recursive:true}) +await fs.mkdir(__preview_templates, {recursive:true}) +await fs.mkdir(__preview_templates_, {recursive:true}) +await fs.mkdir(__preview_about, {recursive:true}) //Web - fs.copyFile(paths.join(__web, "index.html"), paths.join(__preview, "index.html")) - fs.copyFile(paths.join(__web, "favicon.png"), paths.join(__preview, ".favicon.png")) - fs.copyFile(paths.join(__web, "opengraph.png"), paths.join(__preview, ".opengraph.png")) +fs.copyFile(paths.join(__web, "index.html"), paths.join(__preview, "index.html")) +fs.copyFile(paths.join(__web, "favicon.png"), paths.join(__preview, ".favicon.png")) +fs.copyFile(paths.join(__web, "opengraph.png"), paths.join(__preview, ".opengraph.png")) //Plugins and templates - fs.writeFile(paths.join(__preview, ".plugins"), JSON.stringify(Object.entries(metadata).filter(([_name, {category}]) => category !== "core").map(([name]) => ({name, enabled:false})))) - fs.writeFile(paths.join(__preview, ".plugins.base"), JSON.stringify(conf.settings.plugins.base.parts)) - fs.writeFile(paths.join(__preview, ".plugins.metadata"), JSON.stringify(metadata)) - fs.writeFile(paths.join(__preview, ".templates__"), JSON.stringify(templates)) - for (const template in conf.templates) { - fs.writeFile(paths.join(__preview_templates_, template), JSON.stringify(conf.templates[template])) - const __partials = paths.join(__templates, template, "partials") - const __preview_partials = paths.join(__preview_templates, template, "partials") - await fs.mkdir(__preview_partials, {recursive:true}) - for (const file of await fs.readdir(__partials)) - fs.copyFile(paths.join(__partials, file), paths.join(__preview_partials, file)) - } +fs.writeFile(paths.join(__preview, ".plugins"), JSON.stringify(Object.entries(metadata).filter(([_name, {category}]) => category !== "core").map(([name]) => ({name, enabled:false})))) +fs.writeFile(paths.join(__preview, ".plugins.base"), JSON.stringify(conf.settings.plugins.base.parts)) +fs.writeFile(paths.join(__preview, ".plugins.metadata"), JSON.stringify(metadata)) +fs.writeFile(paths.join(__preview, ".templates__"), JSON.stringify(templates)) +for (const template in conf.templates) { + fs.writeFile(paths.join(__preview_templates_, template), JSON.stringify(conf.templates[template])) + const __partials = paths.join(__templates, template, "partials") + const __preview_partials = paths.join(__preview_templates, template, "partials") + await fs.mkdir(__preview_partials, {recursive:true}) + for (const file of await fs.readdir(__partials)) + fs.copyFile(paths.join(__partials, file), paths.join(__preview_partials, file)) +} //Styles - fs.copyFile(paths.join(__web, "style.css"), paths.join(__preview_css, "style.css")) - fs.copyFile(paths.join(__web, "style.vars.css"), paths.join(__preview_css, "style.vars.css")) - fs.copyFile(paths.join(__node_modules, "prismjs/themes/prism-tomorrow.css"), paths.join(__preview_css, "style.prism.css")) +fs.copyFile(paths.join(__web, "style.css"), paths.join(__preview_css, "style.css")) +fs.copyFile(paths.join(__web, "style.vars.css"), paths.join(__preview_css, "style.vars.css")) +fs.copyFile(paths.join(__node_modules, "prismjs/themes/prism-tomorrow.css"), paths.join(__preview_css, "style.prism.css")) //Scripts - fs.writeFile(paths.join(__preview_js, "app.js"), `${await fs.readFile(paths.join(__web, "app.js"))}`) - fs.writeFile(paths.join(__preview_js, "app.placeholder.js"), `${await fs.readFile(paths.join(__web, "app.placeholder.js"))}`) - fs.copyFile(paths.join(__node_modules, "ejs/ejs.min.js"), paths.join(__preview_js, "ejs.min.js")) - fs.copyFile(paths.join(__node_modules, "faker/dist/faker.min.js"), paths.join(__preview_js, "faker.min.js")) - fs.copyFile(paths.join(__node_modules, "axios/dist/axios.min.js"), paths.join(__preview_js, "axios.min.js")) - fs.copyFile(paths.join(__node_modules, "axios/dist/axios.min.map"), paths.join(__preview_js, "axios.min.map")) - fs.copyFile(paths.join(__node_modules, "vue/dist/vue.min.js"), paths.join(__preview_js, "vue.min.js")) - fs.copyFile(paths.join(__node_modules, "vue-prism-component/dist/vue-prism-component.min.js"), paths.join(__preview_js, "vue.prism.min.js")) - fs.copyFile(paths.join(__node_modules, "vue-prism-component/dist/vue-prism-component.min.js.map"), paths.join(__preview_js, "vue-prism-component.min.js.map")) - fs.copyFile(paths.join(__node_modules, "prismjs/prism.js"), paths.join(__preview_js, "prism.min.js")) - fs.copyFile(paths.join(__node_modules, "prismjs/components/prism-yaml.min.js"), paths.join(__preview_js, "prism.yaml.min.js")) - fs.copyFile(paths.join(__node_modules, "prismjs/components/prism-markdown.min.js"), paths.join(__preview_js, "prism.markdown.min.js")) - fs.copyFile(paths.join(__node_modules, "clipboard/dist/clipboard.min.js"), paths.join(__preview_js, "clipboard.min.js")) +fs.writeFile(paths.join(__preview_js, "app.js"), `${await fs.readFile(paths.join(__web, "app.js"))}`) +fs.writeFile(paths.join(__preview_js, "app.placeholder.js"), `${await fs.readFile(paths.join(__web, "app.placeholder.js"))}`) +fs.copyFile(paths.join(__node_modules, "ejs/ejs.min.js"), paths.join(__preview_js, "ejs.min.js")) +fs.copyFile(paths.join(__node_modules, "faker/dist/faker.min.js"), paths.join(__preview_js, "faker.min.js")) +fs.copyFile(paths.join(__node_modules, "axios/dist/axios.min.js"), paths.join(__preview_js, "axios.min.js")) +fs.copyFile(paths.join(__node_modules, "axios/dist/axios.min.map"), paths.join(__preview_js, "axios.min.map")) +fs.copyFile(paths.join(__node_modules, "vue/dist/vue.min.js"), paths.join(__preview_js, "vue.min.js")) +fs.copyFile(paths.join(__node_modules, "vue-prism-component/dist/vue-prism-component.min.js"), paths.join(__preview_js, "vue.prism.min.js")) +fs.copyFile(paths.join(__node_modules, "vue-prism-component/dist/vue-prism-component.min.js.map"), paths.join(__preview_js, "vue-prism-component.min.js.map")) +fs.copyFile(paths.join(__node_modules, "prismjs/prism.js"), paths.join(__preview_js, "prism.min.js")) +fs.copyFile(paths.join(__node_modules, "prismjs/components/prism-yaml.min.js"), paths.join(__preview_js, "prism.yaml.min.js")) +fs.copyFile(paths.join(__node_modules, "prismjs/components/prism-markdown.min.js"), paths.join(__preview_js, "prism.markdown.min.js")) +fs.copyFile(paths.join(__node_modules, "clipboard/dist/clipboard.min.js"), paths.join(__preview_js, "clipboard.min.js")) //Meta - fs.writeFile(paths.join(__preview, ".version"), JSON.stringify(`${conf.package.version}-preview`)) - fs.writeFile(paths.join(__preview, ".hosted"), JSON.stringify({by:"metrics", link:"https://github.com/lowlighter/metrics"})) +fs.writeFile(paths.join(__preview, ".version"), JSON.stringify(`${conf.package.version}-preview`)) +fs.writeFile(paths.join(__preview, ".hosted"), JSON.stringify({by:"metrics", link:"https://github.com/lowlighter/metrics"})) //About - fs.copyFile(paths.join(__web, "about", "index.html"), paths.join(__preview, "about", "index.html")) - for (const file of await fs.readdir(__web_about)) - if (file !== ".statics") - fs.copyFile(paths.join(__web_about, file), paths.join(__preview_about, file)) +fs.copyFile(paths.join(__web, "about", "index.html"), paths.join(__preview, "about", "index.html")) +for (const file of await fs.readdir(__web_about)) + if (file !== ".statics") + fs.copyFile(paths.join(__web_about, file), paths.join(__preview_about, file)) diff --git a/.github/quickstart/index.mjs b/.github/quickstart/index.mjs index 6df2f5c8..c038e920 100644 --- a/.github/quickstart/index.mjs +++ b/.github/quickstart/index.mjs @@ -1,43 +1,43 @@ //Imports - import fs from "fs" - import paths from "path" - import url from "url" - import ejs from "ejs" +import fs from "fs" +import paths from "path" +import url from "url" +import ejs from "ejs" //Mode - const [mode, name] = process.argv.slice(2) +const [mode, name] = process.argv.slice(2) //Paths - const __metrics = paths.join(paths.dirname(url.fileURLToPath(import.meta.url)), "../..") - const __quickstart = paths.join(__metrics, ".github/quickstart") +const __metrics = paths.join(paths.dirname(url.fileURLToPath(import.meta.url)), "../..") +const __quickstart = paths.join(__metrics, ".github/quickstart") //Check arguments - if ((!mode)||(!name)) - throw new Error(`Usage is "npm run quickstart -- "`) - if (!["plugin", "template"].includes(mode)) - throw new Error(`Unsupported mode ${mode}`) +if ((!mode)||(!name)) + throw new Error(`Usage is "npm run quickstart -- "`) +if (!["plugin", "template"].includes(mode)) + throw new Error(`Unsupported mode ${mode}`) //Check if target directory already exists - const target = paths.join(__metrics, `source/${mode}s`, name) - if (fs.existsSync(target)) - throw new Error(`A ${mode} named ${name} already exists!`) +const target = paths.join(__metrics, `source/${mode}s`, name) +if (fs.existsSync(target)) + throw new Error(`A ${mode} named ${name} already exists!`) //Copy quickstart content - console.log(`quickstart for ${mode}`) - await fs.promises.mkdir(target) - await rcopy(paths.join(__quickstart, mode), target) +console.log(`quickstart for ${mode}`) +await fs.promises.mkdir(target) +await rcopy(paths.join(__quickstart, mode), target) //Recursive copy - async function rcopy(from, to) { - for (const file of await fs.promises.readdir(from)) { - const path = paths.join(from, file) - if ((await fs.promises.lstat(path)).isDirectory()) { - await fs.promises.mkdir(paths.join(to, file)) - await rcopy(path, paths.join(to, file)) - } - else { - console.log(`copying ${path} to ${paths.join(to, file)}`) - await fs.promises.writeFile(paths.join(to, file), await ejs.renderFile(path, {name}, {async:true})) - } +async function rcopy(from, to) { + for (const file of await fs.promises.readdir(from)) { + const path = paths.join(from, file) + if ((await fs.promises.lstat(path)).isDirectory()) { + await fs.promises.mkdir(paths.join(to, file)) + await rcopy(path, paths.join(to, file)) + } + else { + console.log(`copying ${path} to ${paths.join(to, file)}`) + await fs.promises.writeFile(paths.join(to, file), await ejs.renderFile(path, {name}, {async:true})) } } +} diff --git a/.github/release.mjs b/.github/release.mjs index d7a4b60d..55727a1e 100644 --- a/.github/release.mjs +++ b/.github/release.mjs @@ -1,56 +1,56 @@ //Imports - import github from "@actions/github" - import paths from "path" - import url from "url" - import sgit from "simple-git" +import github from "@actions/github" +import paths from "path" +import url from "url" +import sgit from "simple-git" //Git setup - const __metrics = paths.join(paths.dirname(url.fileURLToPath(import.meta.url)), "..") - const git = sgit(__metrics) +const __metrics = paths.join(paths.dirname(url.fileURLToPath(import.meta.url)), "..") +const git = sgit(__metrics) //Setup octokit - const token = process.env.GITHUB_TOKEN - const rest = github.getOctokit(token) +const token = process.env.GITHUB_TOKEN +const rest = github.getOctokit(token) //Environment - const maintainer = "lowlighter" - const repository = process.env.GITHUB_REPOSITORY.match(/^(?[\s\S]+)[/](?[\s\S]+)$/)?.groups ?? null - const version = process.env.GITHUB_COMMIT_MESSAGE.match(/(?v\d+[.]\d+)/)?.groups?.version ?? null +const maintainer = "lowlighter" +const repository = process.env.GITHUB_REPOSITORY.match(/^(?[\s\S]+)[/](?[\s\S]+)$/)?.groups ?? null +const version = process.env.GITHUB_COMMIT_MESSAGE.match(/(?v\d+[.]\d+)/)?.groups?.version ?? null //Check arguments - if ((!repository)||(!repository.name)||(!repository.owner)) - throw new Error(`Could not parse repository "${process.env.GITHUB_REPOSITORY}"`) - console.log(`Repository: ${repository.owner}/${repository.name}`) - if (!version) - throw new Error(`Could not parse version from "${process.env.GITHUB_COMMIT_MESSAGE}"`) - console.log(`Version: ${version}`) +if ((!repository)||(!repository.name)||(!repository.owner)) + throw new Error(`Could not parse repository "${process.env.GITHUB_REPOSITORY}"`) +console.log(`Repository: ${repository.owner}/${repository.name}`) +if (!version) + throw new Error(`Could not parse version from "${process.env.GITHUB_COMMIT_MESSAGE}"`) +console.log(`Version: ${version}`) //Load related pr - const {data:{items:prs}} = await rest.search.issuesAndPullRequests({ - q:`repo:${repository.owner}/${repository.name} is:pr is:merged author:${maintainer} assignee:${maintainer} Release ${version} in:title` - }) +const {data:{items:prs}} = await rest.search.issuesAndPullRequests({ + q:`repo:${repository.owner}/${repository.name} is:pr is:merged author:${maintainer} assignee:${maintainer} Release ${version} in:title` +}) //Ensure that there is exactly one pr matching - if (prs.length < 1) - throw new Error(`No matching prs found`) - if (prs.length > 1) - throw new Error(`Multiple prs found (${prs.length} matching)`) - const [patchnote] = prs - console.log(`Using pr#${patchnote.number}: ${patchnote.title}`) +if (prs.length < 1) + throw new Error(`No matching prs found`) +if (prs.length > 1) + throw new Error(`Multiple prs found (${prs.length} matching)`) +const [patchnote] = prs +console.log(`Using pr#${patchnote.number}: ${patchnote.title}`) //Check whether release already exists - try { - const {data:{id}} = await rest.repos.getReleaseByTag({owner:repository.owner, repo:repository.name, tag:version}) - console.log(`Release ${version} already exists (#${id}), will replace it`) - await rest.repos.deleteRelease({owner:repository.owner, repo:repository.name, release_id:id}) - console.log(`Deleting tag ${version}`) - await git.push(["--delete", "origin", version]) - await new Promise(solve => setTimeout(solve, 15*1000)) - } - catch { - console.log(`Release ${version} does not exists yet, will create it`) - } +try { + const {data:{id}} = await rest.repos.getReleaseByTag({owner:repository.owner, repo:repository.name, tag:version}) + console.log(`Release ${version} already exists (#${id}), will replace it`) + await rest.repos.deleteRelease({owner:repository.owner, repo:repository.name, release_id:id}) + console.log(`Deleting tag ${version}`) + await git.push(["--delete", "origin", version]) + await new Promise(solve => setTimeout(solve, 15*1000)) +} +catch { + console.log(`Release ${version} does not exists yet, will create it`) +} //Publish release - await rest.repos.createRelease({owner:repository.owner, repo:repository.name, tag_name:version, name:`Version ${version.replace(/^v/g, "")}`, body:patchnote.body}) - console.log(`Successfully published`) +await rest.repos.createRelease({owner:repository.owner, repo:repository.name, tag_name:version, name:`Version ${version.replace(/^v/g, "")}`, body:patchnote.body}) +console.log(`Successfully published`)