Skip to content

Edit Command and Linters #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
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"]
}
167 changes: 167 additions & 0 deletions gistpush-edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
// FIXME: we can't edit using a token
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 { fs, utils } = require("./utils");

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

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

return countdown;
};

const logError = err => {
console.error(
chalk.red(err.message === "Not Found" ? "Gist Not Found" : err)
);
process.exit(1);
};

const getCredentials = () => {
const username = prompt(chalk.yellow("Username: "));
const password = prompt.hide(chalk.yellow("Password: "));

return { username, password };
};

const createFile = async (filePath, deleteFile) => {
const fileName = filePath.replace(/(\.\/)?(.+\.[a-zA-Z]+)/, "$2");

if (deleteFile) {
return {
name: fileName,
deleteFile
};
}

const content = await fs.readFile(filePath).catch(logError);

return {
name: fileName,
content
};
};

const formatFile = (prev, current) => {
const { name, content, deleteFile } = current;
return { ...prev, [name]: deleteFile ? null : { content } };
};

const printSuccess = response => {
console.clear();
console.log(chalk.green("Gist updated succesfully!"));
utils.printGists(response);
process.exit(0);
};

const editGist = async (
gistId,
{ credentials, files = [], description = "", deleteFiles = false }
) => {
const areCredentials = Object.keys(credentials).length > 1;

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

console.log(chalk.cyan("\nReading files..."));
console.log(program.args);

const loader = initializeLoader();

const solved = await Promise.all(
files.map(file => createFile(file, deleteFiles))
).catch(logError);

const formattedFiles = solved.reduce(formatFile, {});

loader.stop();
console.log(chalk.green("\nDone..."));
console.clear();
console.log(chalk.cyan("\nSending files...\n"));
loader.start();

const { body } = await gist
.edit(gistId, {
description,
files: formattedFiles
})
.catch(logError);

loader.stop();
console.log(chalk.green("\nDone...\n"));
return body;
};

program
.description(
"update one or more files in a gist related to the account logged in"
)
.arguments("<filespath...>")
.option("-g, --gist-id <gist_id>", "identifier of Gist to edit")
.option(
"-t, --token <token>",
"Set token to log in into GitHub (recommended if you have two-factor auth activated in your GitHub account)"
)
.option(
"-d, --set-description <description>",
"new description to be saved in the gist"
)
.option(
"-D, --delete-files",
"Use the given paths in the arguments to eliminate files in the Gist"
)
.parse(process.argv);

const files = program.args;
const gistID = program.gistId;
const description = program.setDescription;
const deleteFiles = program.deleteFiles;
const token = program.token;

if (!gistID) {
logError(new Error("Gist ID not provided"));
}

if (!files || files.length === 0) {
console.log(chalk.orange("The Gist will be update without modfied files"));
}

(async function() {
if (token) {
const credentials = { token };

const gist = await editGist(gistID, {
credentials,
files,
description,
deleteFiles
});

printSuccess(gist);
} else {
const credentials = getCredentials();

const gist = await editGist(gistID, {
credentials,
files,
description,
deleteFiles
});

printSuccess(gist);
}
})();
16 changes: 11 additions & 5 deletions index-list.js → gistpush-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ const Gists = require("gists");
const chalk = require("chalk");
const program = require("commander");
const prompt = require("prompt-sync")(); // Sync cli input
const CLI = require("clui"),
Spinner = CLI.Spinner;
const CLI = require("clui");
const Spinner = CLI.Spinner;

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

Expand Down Expand Up @@ -62,7 +62,7 @@ program
)
.parse(process.argv);

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

Expand All @@ -78,6 +78,7 @@ if (!login) {

checkAndPrintGists(gists);
spinner.stop();
console.log("\n");

process.exit(0);
})();
Expand All @@ -93,17 +94,22 @@ if (!login) {

checkAndPrintGists(gists);
spinner.stop();
console.log("\n");
process.exit(0);
})();
} else {
const fromPropt = prompt(chalk.yellow("Username to log in (push enter to use the username argument): "));
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);
}

let password = prompt.hide(chalk.yellow("Password: "));
const password = prompt.hide(chalk.yellow("Password: "));
if (!password) {
console.error(chalk.red("Password or Token not provided"));
process.exit(1);
Expand Down
File renamed without changes.
26 changes: 26 additions & 0 deletions gistpush.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env node

(async function() {
const program = require("commander");

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

const version = await getVersion().catch(console.error);

function main() {
program
.version(version)
.command(
"push <file1> [file2]",
"push one or more files to github as a gist",
{ isDefault: true }
)
.command("list [username]", "list all gists for the given user")
.command("delete <gist_id...>", "delete one or more gists")
.command("edit <filespath...>", "update one or more files in a gist")
.parse(process.argv);
}

main();
module.exports = main;
})();
24 changes: 0 additions & 24 deletions index.js

This file was deleted.

Loading