-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
94 lines (79 loc) · 3.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env node
const {ArgumentParser, Const, RawTextHelpFormatter} = require('argparse');
const crossEnv = require('cross-env');
const Configstore = require('configstore');
const packageJson = require('./package.json');
const store = new Configstore(packageJson.name, {projects: {}, version: 1});
const parser = new ArgumentParser({
version: packageJson.version,
addHelp: true,
prog: 'firebase-multi',
formatterClass: RawTextHelpFormatter,
description: 'firebase-multi supports running multiple projects from multiple accounts on one machine, leveraging `firebase login:ci`'
});
const subparsers = parser.addSubparsers({
title: 'Commands',
set: 'Add/Update a project token',
get: 'Print token for project',
use: 'Invoke a command with project token',
unset: 'Delete stored token',
dest: 'action',
description:
'set Add/Update project \n' +
'get Print token for project \n' +
'use Invoke a command with a project token \n' +
'unset Delete stored token',
})
const setParser = subparsers.addParser('set', {addHelp: true, description: 'Add or update a ci token to be used for a specific project'})
setParser.addArgument('project', {help: 'The firebase project'})
setParser.addArgument('token', {help: 'The ci token obtained from `firebase login:ci`'})
const getParser = subparsers.addParser('get', {addHelp: true, description: 'Print the stored ci token for the specified project'})
getParser.addArgument('project', {help: 'The firebase project'})
const unsetParser = subparsers.addParser('unset', {addHelp: true, description: 'Remove the stored ci token for the specified project'})
unsetParser.addArgument('project', {help: 'The firebase project'})
const useParser = subparsers.addParser('use', {addHelp: true, description: 'Set FIREBASE_TOKEN env param with the stored ci token, and run the specified command'})
useParser.addArgument('project', {help: 'The firebase project'})
useParser.addArgument('command', {nargs: Const.REMAINDER, help: 'Command to run after FIREBASE_TOKEN env param set'})
const {project, token, action} = parser.parseArgs();
switch (action) {
case 'get':
invokeGet();
break;
case 'set':
invokeSet();
break;
case 'unset':
invokeUnset();
break;
case 'use':
invokeUse();
break;
default:
console.error('Error: unknown command');
process.exit(1);
}
function invokeGet() {
const token = store.get(`projects.${project}`);
if (token) {
console.log(token);
} else {
console.error(`Error: No token for project ${project}`)
process.exit(1);
}
}
function invokeSet() {
store.set(`projects.${project}`, token)
}
function invokeUnset() {
store.delete(`projects.${project}`)
}
function invokeUse() {
const token = process.env.FIREBASE_TOKEN || store.get(`projects.${project}`);
if (!token) {
console.error(`Error: No token for project ${project}. Use command 'firebase-multi set <project> <token>'`)
process.exit(1);
}
const commandArray = process.argv.slice(4);
const crossCommand = [`FIREBASE_TOKEN=${token}`].concat(commandArray)
crossEnv(crossCommand)
}