Last active
February 16, 2021 18:36
-
-
Save bhubr/ded755d9cadc66f6a271b26673ff6bed to your computer and use it in GitHub Desktop.
Create a Gist with Node.js (no dependencies)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const https = require('https') | |
const { resolve } = require('path') | |
const { readFileSync } = require('fs') | |
// In this example, we create a Gist from this very file! | |
const filename = resolve(__dirname, 'index.js') | |
// Put your auth string in the `auth` file | |
// with this format (you need a GitHub personal access token): | |
// token YourGitHubPersonalAccessToken | |
// Don't forget to either delete the token or the file afterwards! | |
const auth = readFileSync('auth', 'utf8').trim() | |
const content = readFileSync(filename, 'utf8') | |
const data = JSON.stringify({ | |
public: false, | |
description: 'Create a Gist with Node.js (no dependencies)', | |
files: { | |
'index.js': { | |
content, | |
}, | |
}, | |
}) | |
const options = { | |
hostname: 'api.github.com', | |
port: 443, | |
path: '/gists', | |
method: 'POST', | |
headers: { | |
Accept: 'application/vnd.github.v3+json', | |
'Content-Type': 'application/json', | |
'Content-Length': data.length, | |
Authorization: auth, | |
'User-Agent': 'nodejs-gist-creator', | |
}, | |
} | |
const req = https.request(options, (res) => { | |
console.log(`statusCode: ${res.statusCode}`) | |
res.on('data', (d) => { | |
process.stdout.write(d) | |
}) | |
}) | |
req.on('error', (error) => { | |
console.error(error) | |
}) | |
req.write(data) | |
req.end() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment