|
| 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 | +} |
0 commit comments