|
| 1 | +const { Base64 } = require("js-base64"); |
| 2 | +const Conf = require("conf"); |
| 3 | +const fetch = require("node-fetch"); |
| 4 | +const { Octokit } = require("@octokit/core"); |
| 5 | +const prompts = require("prompts"); |
| 6 | + |
| 7 | +const account = new Conf(); |
| 8 | +const questions = require("../util/questions").register; |
| 9 | + |
| 10 | +function delay(time) { |
| 11 | + return new Promise((resolve) => setTimeout(resolve, time)); |
| 12 | +} |
| 13 | + |
| 14 | +module.exports = async function register() { |
| 15 | + if(!account.has("username")) { |
| 16 | + console.log("You are not logged in!"); |
| 17 | + console.log("To log in, run the command: `is-a-dev login`"); |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + console.log(`Username: ${account.get("username")}`); |
| 22 | + console.log(`Email: ${account.get("email")}\n`); |
| 23 | + |
| 24 | + const octokit = new Octokit({ auth: account.get("token") }); |
| 25 | + |
| 26 | + const response = await prompts(questions); |
| 27 | + |
| 28 | + let forkName; |
| 29 | + |
| 30 | + await octokit.request("POST /repos/{owner}/{repo}/forks", { |
| 31 | + owner: "is-a-dev", |
| 32 | + repo: "register", |
| 33 | + default_branch_only: true |
| 34 | + }).then(res => forkName = res.data.name) |
| 35 | + |
| 36 | + const username = account.get("username"); |
| 37 | + const email = account.get("email"); |
| 38 | + |
| 39 | + const subdomain = response.subdomain.toLowerCase(); |
| 40 | + const recordType = response.record; |
| 41 | + let recordValue = response.record_value.toLowerCase(); |
| 42 | + |
| 43 | + if(recordType === "A" || recordType === "AAAA" || recordType === "MX") { |
| 44 | + recordValue = JSON.stringify(recordValue.split(",").map((s) => s.trim())); |
| 45 | + } else { |
| 46 | + recordValue = `"${recordValue.trim()}"`; |
| 47 | + } |
| 48 | + |
| 49 | +let fullContent = `{ |
| 50 | + "owner": { |
| 51 | + "username": "${username}", |
| 52 | + "email": "${email}" |
| 53 | + }, |
| 54 | +
|
| 55 | + "records": { |
| 56 | + "${recordType}": ${recordValue} |
| 57 | + } |
| 58 | +} |
| 59 | +`; |
| 60 | + |
| 61 | + const contentEncoded = Base64.encode(fullContent); |
| 62 | + |
| 63 | + fetch( |
| 64 | + `https://api.github.com/repos/is-a-dev/register/contents/domains/${subdomain}.json`, |
| 65 | + { |
| 66 | + method: "GET", |
| 67 | + headers: { |
| 68 | + "User-Agent": username, |
| 69 | + }, |
| 70 | + } |
| 71 | + ).then(async (res) => { |
| 72 | + if(res.status && res.status == 404) { |
| 73 | + octokit |
| 74 | + .request("PUT /repos/{owner}/{repo}/contents/{path}", { |
| 75 | + owner: username, |
| 76 | + repo: forkName, |
| 77 | + path: "domains/" + subdomain + ".json", |
| 78 | + message: `feat(domain): add \`${subdomain}.is-a.dev\``, |
| 79 | + content: contentEncoded |
| 80 | + }) |
| 81 | + .catch((err) => { throw new Error(err); }); |
| 82 | + } else throw new Error("That subdomain is taken!"); |
| 83 | + }) |
| 84 | + |
| 85 | + await delay(2000); |
| 86 | + |
| 87 | + const pr = await octokit.request("POST /repos/{owner}/{repo}/pulls", { |
| 88 | + owner: "is-a-dev", |
| 89 | + repo: "register", |
| 90 | + title: `Register ${subdomain}.is-a.dev`, |
| 91 | + body: `Added \`${subdomain}.is-a.dev\` using the [CLI](https://www.npmjs.com/package/@is-a-dev/cli).`, |
| 92 | + head: username + ":main", |
| 93 | + base: "main" |
| 94 | + }) |
| 95 | + |
| 96 | + console.log(`\nYour pull request has been submitted.\nYou can check the status of your pull request here: ${pr.data.html_url}`); |
| 97 | +} |
0 commit comments