-
Notifications
You must be signed in to change notification settings - Fork 0
/
gistbuilder.js
50 lines (39 loc) · 1.35 KB
/
gistbuilder.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
#!/usr/bin/env node
import { program } from 'commander';
import { readFileSync } from 'fs';
import { unified } from "unified";
import remarkParse from "remark-parse";
import { Octokit } from "@octokit/rest";
const processFile = async (file, options) =>
{
const { token, gist_id, description } = options;
const fileContent = readFileSync(file);
const { children } = unified()
.use(remarkParse)
.parse(fileContent);
const codeBlocks = children.filter(el => el.type == "code");
const octokit = new Octokit({
auth: token,
userAgent: "gistbuilder"
});
// Turn the array of code blocks into a dictionary
const files = Object.assign({}, ...codeBlocks.map(el => ({[el.lang] : { content: el.value }})));
// Create gist!
const params = {
gist_id,
description,
public: "false",
files
}
const { data: gist } = await octokit.gists.create(params);
// Print the embeddable urls
for (const file in gist.files) {
console.log(gist.html_url + '?file=' + file);
}
}
program.arguments('<file>')
.requiredOption('-t, --token <token>', `Your GitHub access token`)
.option('-d, --description <description>', 'Id of an existing gist to update')
.option('-g, --gist <gist_id>', 'Id of an existing gist to update')
.action(processFile)
.parse(process.argv);