-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
151 lines (123 loc) · 4.92 KB
/
cli.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env node
let args = process.argv.slice(2)
let cmd = args.shift()
const fs = require('fs')
const path = require('path')
const prefix = process.cwd() //, '../../../')
switch (cmd.trim().toLowerCase()) {
case 'init':
// Setup dependencies
let currentPath = path.join(prefix, 'package.json')
let pkg
if (fs.existsSync(currentPath)) {
pkg = require(currentPath)
} else {
pkg = {
name: path.basename(prefix),
version: '1.0.0-alpha.1',
private: true,
description: "Cloud Functions for Firebase",
engines: {
node: '10'
}
}
}
pkg.scripts = pkg.scripts || {}
pkg.scripts = Object.assign(pkg.scripts, {
serve: 'firebase serve --only functions',
shell: 'firebase functions:shell',
start: 'firebase emulators:start --only functions',
logs: 'firebase functions:log',
deploy: 'fb deploy',
configure: 'fb configure',
'configure:debug': 'fb configure --debug',
setup: 'fb setup'
})
let modPkg = require(path.join(__dirname, 'package.json'))
pkg.devDependencies = pkg.devDependencies || {}
pkg.devDependencies[modPkg.name] = `^${modPkg.version}`
pkg.dependencies['@butlerlogic/firebase'] = `^${modPkg.dependencies['@butlerlogic/firebase']}`.replace(/\^+/gi, '^')
delete pkg.devDependencies['@butlerlogic/firebase']
delete pkg.devDependencies['@butlerlogic/firebase-api']
pkg.dependencies['localenvironment'] = `^${modPkg.dependencies.localenvironment}`.replace(/\^+/gi, '^')
pkg.dependencies['firebase-admin'] = `^${modPkg.dependencies['firebase-admin']}`.replace(/\^+/gi, '^')
pkg.dependencies['firebase-functions'] = `^${modPkg.dependencies['firebase-functions']}`.replace(/\^+/gi, '^')
fs.writeFileSync(currentPath, JSON.stringify(pkg, null, 2))
// Look for the service account file
let serviceKeyPath = null
fs.readdirSync(prefix).forEach(filepath => {
if (path.extname(filepath).toLowerCase() === '.json') {
let filename = path.basename(filepath, '.json')
if (['package', 'package-lock', 'firebase', '.runtimeconfig', 'env'].indexOf(filename) < 0 && filename.indexOf('fire') !== 0) {
serviceKeyPath = filepath
}
}
})
if (serviceKeyPath === null) {
let fp = path.join(prefix, '.firebase_credentials.json')
fs.writeFileSync(fp, JSON.stringify({
"type": "service_account",
"project_id": "fill_me_in",
"private_key_id": "fill_me_in",
"private_key": "fill_me_in",
"client_email": "fill_me_in",
"client_id": "fill_me_in",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "fill_me_in"
}, null, 2))
serviceKeyPath = fp
}
// Make sure the env.json file exists for local dev
currentPath = path.join(prefix, 'env.json')
let env = {}
if (fs.existsSync(currentPath)) {
env = require(currentPath)
}
env.GOOGLE_APPLICATION_CREDENTIALS = serviceKeyPath
fs.writeFileSync(currentPath, JSON.stringify(env, null, 2))
// Setup the base file
currentPath = path.join(prefix, 'index.js')
if (!fs.existsSync(currentPath)) {
let content = `const FirebaseAPI = require('@butlerlogic/firebase-api')
// Create the following global references:
// - functions: Reference to the firebase-functions module
// - admin: Reference to a preauthorized firebase admin SDK instance.
// Remember to examine the env.json file to assure the appropriate
// service key file is used.
// Initialize the API with the appropriate credentials.
FirebaseAPI.init()
// Assign the FirebaseAPI exports to the main exports
Object.assign(exports, FirebaseAPI.exports)
`.trim()
fs.writeFileSync(currentPath, content)
// Setup an example routing system
let apiPath = path.join(path.dirname(currentPath), 'api')
if (!fs.existsSync(apiPath)) {
fs.mkdirSync(apiPath)
}
if (!fs.existsSync(path.join(apiPath, 'routes.js'))) {
fs.writeFileSync(path.join(apiPath, 'routes.js'), `const express = require('express')
const API = require('@butlerlogic/common-api')
const app = express()
API.applySimpleCORS(app)
API.applyCommonConfiguration(app)
app.get('/hello', API.reply({
msg: 'hi!',
action: 'Check out the /info endpoint. It has useful details about this API.'
}))
// functions is made available globally in the index.js file (init method)
const api = functions.https.onRequest(app)
module.exports = { api }
`.trim() + '\n')
}
} else {
console.log(`Did not generate ${currentPath} because it already exists. Example routes were also not created for this reason.`)
}
return
default:
console.log('Usage: api <cmd>\n\n')
console.log('Commands:\n')
console.log('\tinit:\tInitialize an API shell.')
}