UNPKG

2.09 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3const
4 argv = require(`minimist`)(process.argv.slice(2)),
5 https = require(`https`),
6 shell = require(`shelljs`),
7
8 statusCodeAuthError = 401,
9 statusCodeGood = 200,
10 getGitUser = (args) => {
11 return args.u || shell.exec(`git config --get user.name`, {silent: true})
12 .output.replace(`\n`, ``);
13 },
14 getRepoName = (args) => {
15 return args.n || process.cwd().split(`/`).reverse()[0];
16 },
17 getData = (data, args) => {
18 const p = args.p || false;
19
20 if (args.g) {
21 data.name = getRepoName(args);
22 data.private = p;
23 } else {
24 data.scm = `git`;
25 data.is_private = p;
26 }
27 return data;
28 },
29 getOptions = (options, args) => {
30 const
31 repoName = getRepoName(args),
32 userName = getGitUser(args),
33 auth = `${userName}:${args.t || ``}`,
34 basic = new Buffer(auth, `ascii`).toString(`base64`);
35
36 if (args.g) {
37 options.hostname = `api.github.com`;
38 options.path = `/user/repos`;
39 } else {
40 options.hostname = `api.bitbucket.org`;
41 options.path = `/2.0/repositories/${userName}/${repoName}`;
42 }
43 options.method = `POST`;
44 options.headers = {
45 'Authorization': `BASIC ${basic}`,
46 'Content-Length': JSON.stringify(getData({}, argv)).length,
47 'Content-Type': `application/json`,
48 'User-Agent': `NodeJS HTTP Client`
49 };
50 return options;
51 },
52 req = https.request(getOptions({}, argv), (res) => {
53 if (res.statusCode === statusCodeGood) {
54 console.log(`grc: repo created`);
55 } else if (res.statusCode === statusCodeAuthError) {
56 console.log(`Authorization error, check provided credentials`);
57 } else {
58 console.log(res.statusCode);
59 res.on(`data`, (resData) => {
60 console.log(`grc: Seems to be a problem with the request:`);
61 console.log(`Response:\n-----------------------`);
62 console.log(JSON.stringify(resData), null, 2);
63 });
64 }
65 });
66
67req.on(`error`, (err) => {
68 console.log(`grc: Sorry, grc ran into an error:\n${err}`);
69});
70req.write(JSON.stringify(getData({}, argv)));
71req.end();