This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
111 lines (89 loc) · 2.57 KB
/
index.js
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const { Octokit } = require("@octokit/core");
const fetch = require("node-fetch");
const inquirer = require("inquirer");
require("dotenv").config();
const octokit = new Octokit();
/**
* @typedef {Object} GistFile
* @param {string} filename // EG 'evilAnswer.js'
* @param {string} type // EG: 'application/javascript'
* @param {string} language // EG: 'JavaScript'
* @param {string} raw_url
* @param {number} size
* @param {boolean} truncated
* @param {string} content
*/
/**
* @returns {GistFile}
*/
async function getGist() {
const gistRegex = /https:\/\/gist.github.com\/.*\/([0-9a-f]{32}$)/;
const gistQ = await inquirer.prompt([
{
type: "input",
name: "gist_url",
message: "Enter the URL or ID for the Gist you want to import",
validate: function (value) {
var pass = value.match(gistRegex);
if (pass) return true;
const validId = value.match(/^[0-9a-f]{32}$/);
if (validId) return true;
return "Please enter a valid Gist URL or ID";
},
},
]);
const gistURL = gistQ["gist_url"];
let [_, gistId] = gistRegex.exec(gistURL) || [];
if (!gistId) gistId = gistURL;
const { data } = await octokit.request("GET /gists/{gist_id}", {
gist_id: gistId,
});
const { files } = data;
/**
* @type {GistFile | null}
*/
let selectedFile = null;
const fileEntries = Object.entries(files);
if (fileEntries.length === 1) {
// The "value" of the first file
selectedFile = fileEntries[0][1];
} else if (fileEntries.length > 1) {
const qAnswer = await inquirer.prompt([
{
type: "rawlist",
name: "promptFile",
message: "What file do you want to import?",
choices: Object.keys(files),
},
]);
selectedFile = files[qAnswer["promptFile"]];
} else {
throw "There don't seem to be any files in this Gist to import";
}
return selectedFile;
}
const CODERPAD_KEY = process.env.CODERPAD_API;
/**
* @param {GistFile} selectedFile
*/
async function getCoderPad(selectedFile) {
const url = "https://app.coderpad.io/api/pads";
const res = await fetch(url, {
method: "post",
body: JSON.stringify({
// title: '',
language: selectedFile["language"].toLowerCase(),
contents: selectedFile["content"],
}),
headers: {
"Content-Type": "application/json",
Authorization: `Token token="${CODERPAD_KEY}"`,
},
}).then((res) => res.json());
console.log(res);
}
async function main() {
const selectedFile = await getGist();
await getCoderPad(selectedFile);
}
main().catch(console.error);