Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
dansteren committed Jan 20, 2018
0 parents commit d22a609
Showing 5 changed files with 223 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Dan Steren

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Graphcool Account Manager

## Installation

```bash
npm install -g gam
```

## Usage

```
ls List available graphcool accounts
add <account-alias> Save the current graphcool account to gam
rm <account-alias> Remove account from gam
use <account-alias> Switch to a different graphcool account
help Show this message
-v, --version Print out the installed version of gam
```

## Examples

List available graphcool accounts:

```bash
gam ls
```

Save the current graphcool account to gam:

```bash
gam add account1
```

Switch to a different graphcool account:

```bash
gam use account1
```

## Uninstallation

Gam stores accounts in `~/.gam`. To uninstall gam just delete this directory and run `npm uninstall -g gam`.
138 changes: 138 additions & 0 deletions gam.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#! /usr/bin/env node
const fs = require('fs');
const path = require('path');

const homeDir = process.env['HOME'];
const gamDir = path.join(homeDir, '.gam');
const graphcoolRCFile = path.join(homeDir, '.graphcoolrc');

if (!fs.existsSync(gamDir)){
console.log('Initializing...');
fs.mkdirSync(gamDir);
}

switch (process.argv[2]) {
case "-v":
case "--version":
console.log('0.1.0');
break;
case "ls":
case "list":
listAccounts();
break;
case "add":
addAccount(process.argv[3]);
break;
case "rm":
case "remove":
removeAccount(process.argv[3]);
break;
case "use":
useAccount(process.argv[3]);
break;
default:
printUsage();
}

function listAccounts() {
const graphcoolFileExists = fs.existsSync(graphcoolRCFile);
const graphcoolFile = graphcoolFileExists ? fs.readFileSync(graphcoolRCFile) : undefined;
fs.readdir(gamDir, (error, items) => {
if(error) {
return printError('Unexpected I/O error, try again.');
}
if(items.length === 0) {
printError('No accounts found.','Try adding one with `$ gcam add <account-name>`');
}
items.forEach((item) => {
const accountFile = fs.readFileSync(path.join(gamDir, item));
if(graphcoolFileExists && accountFile.equals(graphcoolFile)) {
printGreen('-> ' + item);
} else {
console.log(' ' + item);
}
});
})
}

function addAccount(accountAlias) {
if (!fs.existsSync(graphcoolRCFile)){
return printError('Unable to detect a graphcool account.','Verify that one exists with `$ graphcool account`');
}
if (!accountAlias) {
return printError('No account name provided.', 'Usage: gam add <account-alias>')
}
if(fs.existsSync(path.join(gamDir, accountAlias))) {
return printError('Account already exists!');
}
var accounts = fs.readdirSync(gamDir);
const graphcoolFile = fs.readFileSync(graphcoolRCFile);
accounts.forEach((file => {
const accountFile = fs.readFileSync(path.join(gamDir, file));
if(graphcoolFile.equals(accountFile)) {
printError('Current account is already saved as "' + file + '".', 'Run `$ gam use '+ file + '`');
process.exit();
}
}))
fs.createReadStream(graphcoolRCFile).pipe(fs.createWriteStream(path.join(gamDir, accountAlias)));
console.log('Current graphcool account saved as ' + accountAlias + '.');
}

function removeAccount(accountAlias) {
if (!accountAlias) {
return printError('No account name provided.', 'Usage: gam rm <account-alias>')
}
const aliasFile = path.join(gamDir, accountAlias);
if (!fs.existsSync(aliasFile)){
return printError('Account "' + accountAlias + '" doesn\'t exist.', 'Use `$ gam ls` to list available accounts.');
}
fs.unlinkSync(aliasFile);
console.log('Account "' + accountAlias + '" removed.');
}

function useAccount(accountAlias) {
if (!accountAlias) {
return printError('No account name provided.', 'Usage: gam use <account-alias>')
}
const aliasFile = path.join(gamDir, accountAlias);
if (!fs.existsSync(aliasFile)){
return printError('Account "' + accountAlias + '" doesn\'t exist.', 'Use `$ gam ls` to list available accounts.');
}
fs.createReadStream(aliasFile).pipe(fs.createWriteStream(graphcoolRCFile));
console.log('Now using "' + accountAlias + '".');
}

function printUsage() {
console.log(`
Graphcool Account Manager
Usage: gam COMMAND
COMMANDS:
ls List available graphcool accounts
add <account-alias> Save the current graphcool account to gam
rm <account-alias> Remove account from gam
use <account-alias> Switch to a different graphcool account
help Show this message
-v, --version Print out the installed version of gam
Examples:
- List available graphcool accounts
$ gam ls
- Save the current graphcool account to gam
$ gam add account1
- Switch to a different graphcool account
$ gam use account1
`);
}

function printGreen(text){
console.log('\x1b[32m%s\x1b[0m', text);
}

function printError(error, suggestion){
console.log('\x1b[31m' + error + '\x1b[0m' + (suggestion ? '\n' + suggestion : '') +'\n');
}
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "gam",
"version": "0.1.0",
"description": "Graphcool Account Manager",
"bin": {
"gam": "./gam.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"graphcool"
],
"repository": "github:dansteren/gam",
"author": "Dan Steren <daniel.d.steren@gmail.com> (http://github.com/dansteren)",
"license": "MIT"
}

0 comments on commit d22a609

Please sign in to comment.