Skip to content

Commit d22a609

Browse files
committed
Initial commit
0 parents  commit d22a609

File tree

5 files changed

+223
-0
lines changed

5 files changed

+223
-0
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Dan Steren
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Graphcool Account Manager
2+
3+
## Installation
4+
5+
```bash
6+
npm install -g gam
7+
```
8+
9+
## Usage
10+
11+
```
12+
ls List available graphcool accounts
13+
add <account-alias> Save the current graphcool account to gam
14+
rm <account-alias> Remove account from gam
15+
use <account-alias> Switch to a different graphcool account
16+
help Show this message
17+
-v, --version Print out the installed version of gam
18+
```
19+
20+
## Examples
21+
22+
List available graphcool accounts:
23+
24+
```bash
25+
gam ls
26+
```
27+
28+
Save the current graphcool account to gam:
29+
30+
```bash
31+
gam add account1
32+
```
33+
34+
Switch to a different graphcool account:
35+
36+
```bash
37+
gam use account1
38+
```
39+
40+
## Uninstallation
41+
42+
Gam stores accounts in `~/.gam`. To uninstall gam just delete this directory and run `npm uninstall -g gam`.

gam.js

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#! /usr/bin/env node
2+
const fs = require('fs');
3+
const path = require('path');
4+
5+
const homeDir = process.env['HOME'];
6+
const gamDir = path.join(homeDir, '.gam');
7+
const graphcoolRCFile = path.join(homeDir, '.graphcoolrc');
8+
9+
if (!fs.existsSync(gamDir)){
10+
console.log('Initializing...');
11+
fs.mkdirSync(gamDir);
12+
}
13+
14+
switch (process.argv[2]) {
15+
case "-v":
16+
case "--version":
17+
console.log('0.1.0');
18+
break;
19+
case "ls":
20+
case "list":
21+
listAccounts();
22+
break;
23+
case "add":
24+
addAccount(process.argv[3]);
25+
break;
26+
case "rm":
27+
case "remove":
28+
removeAccount(process.argv[3]);
29+
break;
30+
case "use":
31+
useAccount(process.argv[3]);
32+
break;
33+
default:
34+
printUsage();
35+
}
36+
37+
function listAccounts() {
38+
const graphcoolFileExists = fs.existsSync(graphcoolRCFile);
39+
const graphcoolFile = graphcoolFileExists ? fs.readFileSync(graphcoolRCFile) : undefined;
40+
fs.readdir(gamDir, (error, items) => {
41+
if(error) {
42+
return printError('Unexpected I/O error, try again.');
43+
}
44+
if(items.length === 0) {
45+
printError('No accounts found.','Try adding one with `$ gcam add <account-name>`');
46+
}
47+
items.forEach((item) => {
48+
const accountFile = fs.readFileSync(path.join(gamDir, item));
49+
if(graphcoolFileExists && accountFile.equals(graphcoolFile)) {
50+
printGreen('-> ' + item);
51+
} else {
52+
console.log(' ' + item);
53+
}
54+
});
55+
})
56+
}
57+
58+
function addAccount(accountAlias) {
59+
if (!fs.existsSync(graphcoolRCFile)){
60+
return printError('Unable to detect a graphcool account.','Verify that one exists with `$ graphcool account`');
61+
}
62+
if (!accountAlias) {
63+
return printError('No account name provided.', 'Usage: gam add <account-alias>')
64+
}
65+
if(fs.existsSync(path.join(gamDir, accountAlias))) {
66+
return printError('Account already exists!');
67+
}
68+
var accounts = fs.readdirSync(gamDir);
69+
const graphcoolFile = fs.readFileSync(graphcoolRCFile);
70+
accounts.forEach((file => {
71+
const accountFile = fs.readFileSync(path.join(gamDir, file));
72+
if(graphcoolFile.equals(accountFile)) {
73+
printError('Current account is already saved as "' + file + '".', 'Run `$ gam use '+ file + '`');
74+
process.exit();
75+
}
76+
}))
77+
fs.createReadStream(graphcoolRCFile).pipe(fs.createWriteStream(path.join(gamDir, accountAlias)));
78+
console.log('Current graphcool account saved as ' + accountAlias + '.');
79+
}
80+
81+
function removeAccount(accountAlias) {
82+
if (!accountAlias) {
83+
return printError('No account name provided.', 'Usage: gam rm <account-alias>')
84+
}
85+
const aliasFile = path.join(gamDir, accountAlias);
86+
if (!fs.existsSync(aliasFile)){
87+
return printError('Account "' + accountAlias + '" doesn\'t exist.', 'Use `$ gam ls` to list available accounts.');
88+
}
89+
fs.unlinkSync(aliasFile);
90+
console.log('Account "' + accountAlias + '" removed.');
91+
}
92+
93+
function useAccount(accountAlias) {
94+
if (!accountAlias) {
95+
return printError('No account name provided.', 'Usage: gam use <account-alias>')
96+
}
97+
const aliasFile = path.join(gamDir, accountAlias);
98+
if (!fs.existsSync(aliasFile)){
99+
return printError('Account "' + accountAlias + '" doesn\'t exist.', 'Use `$ gam ls` to list available accounts.');
100+
}
101+
fs.createReadStream(aliasFile).pipe(fs.createWriteStream(graphcoolRCFile));
102+
console.log('Now using "' + accountAlias + '".');
103+
}
104+
105+
function printUsage() {
106+
console.log(`
107+
Graphcool Account Manager
108+
109+
Usage: gam COMMAND
110+
111+
COMMANDS:
112+
ls List available graphcool accounts
113+
add <account-alias> Save the current graphcool account to gam
114+
rm <account-alias> Remove account from gam
115+
use <account-alias> Switch to a different graphcool account
116+
help Show this message
117+
-v, --version Print out the installed version of gam
118+
119+
Examples:
120+
121+
- List available graphcool accounts
122+
$ gam ls
123+
124+
- Save the current graphcool account to gam
125+
$ gam add account1
126+
127+
- Switch to a different graphcool account
128+
$ gam use account1
129+
`);
130+
}
131+
132+
function printGreen(text){
133+
console.log('\x1b[32m%s\x1b[0m', text);
134+
}
135+
136+
function printError(error, suggestion){
137+
console.log('\x1b[31m' + error + '\x1b[0m' + (suggestion ? '\n' + suggestion : '') +'\n');
138+
}

package-lock.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "gam",
3+
"version": "0.1.0",
4+
"description": "Graphcool Account Manager",
5+
"bin": {
6+
"gam": "./gam.js"
7+
},
8+
"scripts": {
9+
"test": "echo \"Error: no test specified\" && exit 1"
10+
},
11+
"keywords": [
12+
"graphcool"
13+
],
14+
"repository": "github:dansteren/gam",
15+
"author": "Dan Steren <[email protected]> (https://github.com/dansteren)",
16+
"license": "MIT"
17+
}

0 commit comments

Comments
 (0)