Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = false
50 changes: 50 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"env": {
"commonjs": true,
"es6": true,
"node": true,
"jest": true
},
"extends": "prettier",
"parserOptions": {
"ecmaFeatures": {
"modules": true,
"experimentalObjectRestSpread": true
},
"sourceType": "module",
"ecmaVersion": 2017
},
"rules": {
"prettier/prettier": "error",
"indent": [
"error",
2,
{
"VariableDeclarator": {
"var": 2,
"let": 2,
"const": 3
}
}
],
"linebreak-style": ["error", "unix"],
"no-multiple-empty-lines": [
"error",
{
"max": 2,
"maxEOF": 1
}
],
"prefer-const": "warn",
"no-const-assign": "warn",
"no-this-before-super": "warn",
"no-undef": "warn",
"no-unreachable": "warn",
"no-unused-vars": "warn",
"constructor-super": "warn",
"valid-typeof": "warn",
"no-eval": "error",
"no-useless-return": "warn"
},
"plugins": ["prettier"]
}
127 changes: 127 additions & 0 deletions index-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
const Gists = require("gists");
const chalk = require("chalk");
const program = require("commander");
const prompt = require("prompt-sync")(); // Sync cli input
const CLI = require("clui");
const Spinner = CLI.Spinner;

const { utils } = require("./utils");

const logError = error => {
console.log(error);
console.error(chalk.red(error));
process.exit(1);
};

const checkAndPrintGists = gists => {
if (!gists || gists.length === 0) console.log(chalk.green("Nothing to show"));
else {
gists.forEach(utils.printGists);
console.log(chalk.green("Done"));
}
};

const initializeLoader = () => {
const countdown = new Spinner("Fetching gists... ", [
"⣾",
"⣽",
"⣻",
"⢿",
"⡿",
"⣟",
"⣯",
"⣷"
]);

console.log("Fetching... ");
countdown.start();

return countdown;
};

const getGists = async ({ credentials, username }) => {
const areCredentials = credentials && Object.keys(credentials).length > 0;

const gist = new Gists(areCredentials ? { ...credentials } : undefined);

const { body } = await gist
.list(username ? username : undefined)
.catch(logError);

return body;
};

program
.option(
"-t, --token <token>",
"Set token to log in into GitHub (use it when you have two-factor auth activated in your GitHub account)"
)
.option(
"--no-login",
"Do not log in into GitHub. If token provided, this will be ignored. This option only shows public repos"
)
.parse(process.argv);

const usernameGists = program.args[0];
const login = program.login;
const secret = program.token ? program.token.trim() : undefined;

if (!login) {
(async function() {
const username =
usernameGists || prompt(chalk.yellow("Username to get gists: "));

if (!username) logError("Username not provided");

const spinner = initializeLoader();
const gists = await getGists({ username }).catch(logError);

checkAndPrintGists(gists);
spinner.stop();

process.exit(0);
})();
} else if (secret) {
(async function() {
console.log(chalk.cyan("Getting Gists..."));

const spinner = initializeLoader();
const gists = await getGists({
credentials: { token: secret },
username: usernameGists ? usernameGists : undefined
}).catch(logError);

checkAndPrintGists(gists);
spinner.stop();
process.exit(0);
})();
} else {
const fromPropt = prompt(
chalk.yellow(
"Username to log in (push enter to use the username argument): "
)
);
const username = fromPropt ? fromPropt : usernameGists;
if (!username) {
console.error(chalk.red("Username not provided"));
process.exit(1);
}

const password = prompt.hide(chalk.yellow("Password: "));
if (!password) {
console.error(chalk.red("Password or Token not provided"));
process.exit(1);
}

(async function() {
const spinner = initializeLoader();
const gists = await getGists({
credentials: { username, password },
username: usernameGists ? usernameGists : username
}).catch(logError);

checkAndPrintGists(gists);
spinner.stop();
process.exit(0);
})();
}
175 changes: 175 additions & 0 deletions index-push.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
const Gists = require('gists');
const fs = require('fs');
//const ProgressBar = require('progress');
const chalk = require('chalk');
const request = require('superagent');
const co = require('co');
//const prompt = require('co-prompt');
const program = require('commander');
const terminalLink = require('terminal-link');
const exit = require('./success.js');
const prompt = require('prompt-sync')(); // Sync cli input
const CLI = require('clui'),
Spinner = CLI.Spinner;

var username = '', password = '', details = [], privacy = [],
upload_complete = false;

/*
// Using promise (async)
function input_data(fileList) {
return new Promise(function(resolve, reject) {
// Do async job
co(function *() {
username = yield prompt(chalk.yellow('username: '));
password = yield prompt.password(chalk.yellow('password: '));

for(file of fileList)
{
console.log('for file: ' + chalk.cyan(file));
var detail_text = yield prompt(chalk.yellow('description: '));
var privacy_value = yield prompt(chalk.yellow('access [Public(t)/Private(f)]: '));
details.push(detail_text);
privacy.push(privacy_value);
}
});
resolve(true);
})
}
*/

// Using sync command line input
function input_data(fileList) {
username = prompt(chalk.yellow('username: '));
password = prompt.hide(chalk.yellow('password: '));

for(file of fileList)
{
console.log('for file: ' + chalk.cyan(file));
var detail_text = prompt(chalk.yellow('description: '));
var privacy_value = prompt(chalk.yellow('access [Public(t)/Private(f)]: '));
details.push(detail_text);
privacy.push(privacy_value);
}

return true;
}

function loading() {
var countdown = new Spinner('Uploading... ', ['⣾','⣽','⣻','⢿','⡿','⣟','⣯','⣷']);

console.log('Uploading... ');
countdown.start()

// var number = 10;
setInterval(function () {
// number--;
// countdown.message('Uploading... ');

if (upload_complete) {
countdown.stop();
process.stdout.write('\n');
process.exit(0);
}
}, 1000);
}

function main() {
program
.arguments('<file1> [file2]')
.option('-u, --username <username>', 'The user to authenticate as')
.option('-p, --password <password>', 'The user\'s password')
.option('-d, --description <description>', 'Description of the file')
.option('-a, --access <access>', 'Public(t)/Private(f) access for the file. Default is true.')
//.option('-c, --collect [value]', 'A repeatable value', collect, [])
//.option('-f, --multipleFiles <multipleFiles>', 't: multiple files | default: single file')

.action(function() {
//console.log(' collect: %j', program.collect);

var count_upload = 0;

co(function *() {
//console.log(program.args);
var fileList = [], links = [];
fileList = program.args[2]['rawArgs'].filter( function(val, index) {
if(index > 1)
return true;
});

// Taking inputs
var success = false;
success = input_data(fileList);

// After sync input is complete
if(success == true)
{
loading(); // Loading animation

var fileNumber = 0;
for(var file of fileList)
{
var description = details[fileNumber];
var access = privacy[fileNumber];

if(access == 't' || access == 'true')
access = true;
else
if(access == 'f' || access == 'false')
access = false;
else
access = true;

const gists = new Gists({
username: username,
password: password
});

var fileData = fs.readFileSync(file, 'utf8');

++fileNumber;

gists.create(
{
"description": description,
"public": access,
"files": {
[file]: {
"content": fileData
}
}
}
)
.then(function(res, err) {
if(res)
{
const url = terminalLink(res.body.html_url, res.body.html_url);
links.push(url);
++count_upload;

// Checking end of all file uploads
if(count_upload == fileList.length)
{
upload_complete = true;
exit(true, links, fileList);
}
}
})
.catch(function(err) {
console.log(chalk.red(err));
process.exit(1);
});

} // end for
} // end if


});
})
.parse(process.argv);

}


main();
module.exports = main;
Loading