backend-manager
Advanced tools
Comparing version
{ | ||
"name": "backend-manager", | ||
"version": "2.0.28", | ||
"version": "2.0.29", | ||
"description": "Quick tools for developing Firebase functions", | ||
@@ -5,0 +5,0 @@ "main": "src/manager/index.js", |
@@ -60,24 +60,24 @@ // CLI GUIDE: | ||
Main.prototype.process = async function (args) { | ||
let self = this; | ||
this.options = {}; | ||
this.argv = argv; | ||
this.firebaseProjectPath = process.cwd(); | ||
this.firebaseProjectPath = this.firebaseProjectPath.match(/\/functions$/) ? this.firebaseProjectPath.replace(/\/functions$/, '') : this.firebaseProjectPath; | ||
this.testCount = 0; | ||
this.testTotal = 0; | ||
this.default = {}; | ||
this.packageJSON = require('../../package.json'); | ||
this.default.version = this.packageJSON.version; | ||
const self = this; | ||
self.options = {}; | ||
self.argv = argv; | ||
self.firebaseProjectPath = process.cwd(); | ||
self.firebaseProjectPath = self.firebaseProjectPath.match(/\/functions$/) ? self.firebaseProjectPath.replace(/\/functions$/, '') : self.firebaseProjectPath; | ||
self.testCount = 0; | ||
self.testTotal = 0; | ||
self.default = {}; | ||
self.packageJSON = require('../../package.json'); | ||
self.default.version = self.packageJSON.version; | ||
for (var i = 0; i < args.length; i++) { | ||
this.options[args[i]] = true; | ||
self.options[args[i]] = true; | ||
} | ||
// console.log(args); | ||
// console.log(options); | ||
if (this.options.v || this.options.version || this.options['-v'] || this.options['-version']) { | ||
console.log(`Backend manager is version: ${this.default.version}`); | ||
if (self.options.v || self.options.version || self.options['-v'] || self.options['-version']) { | ||
console.log(`Backend manager is version: ${self.default.version}`); | ||
} | ||
// https://gist.github.com/timneutkens/f2933558b8739bbf09104fb27c5c9664 | ||
if (this.options.clear) { | ||
if (self.options.clear) { | ||
process.stdout.write("\u001b[3J\u001b[2J\u001b[1J"); | ||
@@ -87,10 +87,10 @@ console.clear(); | ||
} | ||
if (this.options.cwd) { | ||
console.log('cwd: ', this.firebaseProjectPath); | ||
if (self.options.cwd) { | ||
console.log('cwd: ', self.firebaseProjectPath); | ||
} | ||
if (this.options.setup) { | ||
if (self.options.setup) { | ||
await cmd_configGet(self).catch(e => log(chalk.red(`Failed to run config:get`))); | ||
await self.setup(); | ||
} | ||
if ((this.options.i || this.options.install) && (this.options.local || this.options.dev || this.options.development)) { | ||
if ((self.options.i || self.options.install) && (self.options.local || self.options.dev || self.options.development)) { | ||
await uninstallPkg('backend-manager'); | ||
@@ -101,3 +101,3 @@ return await installPkg('file:../../../ITW-Creative-Works/backend-manager'); | ||
} | ||
if ((this.options.i || this.options.install) && (this.options.live || this.options.prod || this.options.production)) { | ||
if ((self.options.i || self.options.install) && (self.options.live || self.options.prod || self.options.production)) { | ||
await uninstallPkg('backend-manager'); | ||
@@ -107,4 +107,4 @@ return await installPkg('backend-manager'); | ||
} | ||
if (this.options.serve) { | ||
if (!this.options.quick && !this.options.q) { | ||
if (self.options.serve) { | ||
if (!self.options.quick && !self.options.q) { | ||
} | ||
@@ -114,3 +114,3 @@ await cmd_configGet(self); | ||
let port = this.argv.port || _.get(self.argv, '_', [])[1] || '5000'; | ||
let port = self.argv.port || _.get(self.argv, '_', [])[1] || '5000'; | ||
let ls = spawn(`firebase serve --port ${port}`, {shell: true}); | ||
@@ -127,11 +127,11 @@ | ||
if (this.options['firestore:indexes:get'] || this.options['firestore:indexes'] || this.options['indexes:get']) { | ||
return await cmd_indexesGet(self); | ||
if (self.options['firestore:indexes:get'] || self.options['firestore:indexes'] || self.options['indexes:get']) { | ||
return await cmd_indexesGet(self, undefined, true); | ||
} | ||
if (this.options['functions:config:get'] || this.options['config:get']) { | ||
if (self.options['functions:config:get'] || self.options['config:get']) { | ||
return await cmd_configGet(self); | ||
} | ||
if (this.options['functions:config:set'] || this.options['config:set']) { | ||
if (self.options['functions:config:set'] || self.options['config:set']) { | ||
await cmd_configSet(self); | ||
@@ -141,3 +141,3 @@ return await cmd_configGet(self); | ||
if (this.options['functions:config:unset'] || this.options['config:unset'] || this.options['config:delete'] || this.options['config:remove']) { | ||
if (self.options['functions:config:unset'] || self.options['config:unset'] || self.options['config:delete'] || self.options['config:remove']) { | ||
await cmd_configUnset(self); | ||
@@ -147,3 +147,3 @@ return await cmd_configGet(self); | ||
if (this.options['rules:default'] || this.options['rules:getdefault']) { | ||
if (self.options['rules:default'] || self.options['rules:getdefault']) { | ||
self.getRulesFile(); | ||
@@ -154,3 +154,3 @@ console.log(self.default.firestoreRulesWhole.match(bem_fsRulesDefaultRegex)[0].replace(' ///', '///')); | ||
if (this.options.deploy) { | ||
if (self.options.deploy) { | ||
await self.setup(); | ||
@@ -178,3 +178,3 @@ | ||
} | ||
if (this.options['test']) { | ||
if (self.options['test']) { | ||
await self.setup(); | ||
@@ -193,3 +193,3 @@ // firebase emulators:exec --only firestore 'npm test' | ||
if (this.options['clean:npm']) { | ||
if (self.options['clean:npm']) { | ||
// await self.setup(); | ||
@@ -206,6 +206,6 @@ // firebase emulators:exec --only firestore 'npm test' | ||
// if (this.options['url']) { | ||
// if (self.options['url']) { | ||
// // await self.setup(); | ||
// // firebase emulators:exec --only firestore 'npm test' | ||
// log(this.projectUrl) | ||
// log(self.projectUrl) | ||
// } | ||
@@ -219,5 +219,5 @@ | ||
Main.prototype.getRulesFile = function () { | ||
let self = this; | ||
this.default.firestoreRulesWhole = (jetpack.read(path.resolve(`${__dirname}/../../templates/firestore.rules`))).replace('=0.0.0-', `-${self.default.version}-`); | ||
this.default.firestoreRulesCore = this.default.firestoreRulesWhole.match(bem_fsRulesRegex)[0]; | ||
const self = this; | ||
self.default.firestoreRulesWhole = (jetpack.read(path.resolve(`${__dirname}/../../templates/firestore.rules`))).replace('=0.0.0-', `-${self.default.version}-`); | ||
self.default.firestoreRulesCore = self.default.firestoreRulesWhole.match(bem_fsRulesRegex)[0]; | ||
@@ -227,12 +227,14 @@ }; | ||
Main.prototype.setup = async function () { | ||
let self = this; | ||
const self = this; | ||
let cwd = jetpack.cwd(); | ||
log(chalk.green(`\n---- RUNNING v${this.default.version} SETUP ----`)); | ||
this.package = jetpack.read(`${this.firebaseProjectPath}/functions/package.json`) || '{}'; | ||
this.firebaseJSON = jetpack.read(`${this.firebaseProjectPath}/firebase.json`) || '{}'; | ||
this.firebaseRC = jetpack.read(`${this.firebaseProjectPath}/.firebaserc`) || '{}'; | ||
this.runtimeConfigJSON = jetpack.read(`${this.firebaseProjectPath}/functions/.runtimeconfig.json`) || '{}'; | ||
this.projectPackage = jetpack.read(`${this.firebaseProjectPath}/package.json`) || '{}'; | ||
this.gitignore = jetpack.read(`${this.firebaseProjectPath}/functions/.gitignore`) || ''; | ||
if (!this.package) { | ||
log(chalk.green(`\n---- RUNNING SETUP v${self.default.version} ----`)); | ||
self.package = jetpack.read(`${self.firebaseProjectPath}/functions/package.json`) || '{}'; | ||
self.firebaseJSON = jetpack.read(`${self.firebaseProjectPath}/firebase.json`) || '{}'; | ||
self.firebaseRC = jetpack.read(`${self.firebaseProjectPath}/.firebaserc`) || '{}'; | ||
self.runtimeConfigJSON = jetpack.read(`${self.firebaseProjectPath}/functions/.runtimeconfig.json`) || '{}'; | ||
self.remoteconfigJSON = jetpack.read(`${self.firebaseProjectPath}/remoteconfig.template.json`) || '{}'; | ||
self.projectPackage = jetpack.read(`${self.firebaseProjectPath}/package.json`) || '{}'; | ||
self.gitignore = jetpack.read(`${self.firebaseProjectPath}/functions/.gitignore`) || ''; | ||
if (!self.package) { | ||
log(chalk.red(`Missing functions/package.json :(`)); | ||
@@ -247,11 +249,14 @@ return; | ||
this.package = JSON.parse(this.package); | ||
this.firebaseJSON = JSON.parse(this.firebaseJSON); | ||
this.firebaseRC = JSON.parse(this.firebaseRC); | ||
this.runtimeConfigJSON = JSON.parse(this.runtimeConfigJSON); | ||
this.projectPackage = JSON.parse(this.projectPackage); | ||
self.package = JSON.parse(self.package); | ||
self.firebaseJSON = JSON.parse(self.firebaseJSON); | ||
self.firebaseRC = JSON.parse(self.firebaseRC); | ||
self.runtimeConfigJSON = JSON.parse(self.runtimeConfigJSON); | ||
self.remoteconfigJSON = JSON.parse(self.remoteconfigJSON); | ||
self.projectPackage = JSON.parse(self.projectPackage); | ||
self.remoteconfigJSONExists = Object.keys(self.remoteconfigJSON).length > 0; | ||
self.getRulesFile(); | ||
this.default.firestoreRulesVersionRegex = new RegExp(`///---version-${self.default.version}---///`) | ||
self.default.firestoreRulesVersionRegex = new RegExp(`///---version-${self.default.version}---///`) | ||
// bem_giRegex = new RegExp(jetpack.read(path.resolve(`${__dirname}/../../templates/gitignore.md`)).replace(/\./g, '\\.'), 'm' ) | ||
@@ -261,6 +266,6 @@ bem_giRegex = new RegExp(jetpack.read(path.resolve(`${__dirname}/../../templates/gitignore.md`)), 'm' ) | ||
// tests | ||
this.projectName = this.firebaseRC.projects.default; | ||
this.projectUrl = `https://console.firebase.google.com/project/${this.projectName}`; | ||
log(chalk.black(`Id: `, chalk.bold(`${this.projectName}`))); | ||
log(chalk.black(`Url:`, chalk.bold(`${this.projectUrl}`))); | ||
self.projectName = self.firebaseRC.projects.default; | ||
self.projectUrl = `https://console.firebase.google.com/project/${self.projectName}`; | ||
log(chalk.black(`Id: `, chalk.bold(`${self.projectName}`))); | ||
log(chalk.black(`Url:`, chalk.bold(`${self.projectUrl}`))); | ||
@@ -271,3 +276,3 @@ if (!self.package || !self.package.engines || !self.package.engines.node) { | ||
await this.test('is a firebase project', async function () { | ||
await self.test('is a firebase project', async function () { | ||
let exists = jetpack.exists(`${self.firebaseProjectPath}/firebase.json`); | ||
@@ -277,3 +282,3 @@ return exists; | ||
await this.test('.nvmrc file has proper version', async function () { | ||
await self.test('.nvmrc file has proper version', async function () { | ||
// return !!self.package.dependencies && !!self.package.devDependencies; | ||
@@ -286,3 +291,3 @@ // let gitignore = jetpack.read(path.resolve(`${__dirname}/../../templates/gitignore.md`)); | ||
await this.test(`using node ${CLI_CONFIG.node}`, function () { | ||
await self.test(`using node ${CLI_CONFIG.node}`, function () { | ||
let processMajor = parseInt(process.versions.node.split('.')[0]); | ||
@@ -296,15 +301,15 @@ let engineMajor = parseInt(self.package.engines.node.split('.')[0]); | ||
// await this.test('project level package.json exists', async function () { | ||
// await self.test('project level package.json exists', async function () { | ||
// return !!(self.projectPackage && self.projectPackage.version && self.projectPackage.name); | ||
// }, fix_projpackage); | ||
await this.test('functions level package.json exists', async function () { | ||
await self.test('functions level package.json exists', async function () { | ||
return !!self.package && !!self.package.dependencies && !!self.package.devDependencies && !!self.package.version; | ||
}, fix_functionspackage); | ||
// await this.test('functions level package.json has updated version', async function () { | ||
// await self.test('functions level package.json has updated version', async function () { | ||
// return self.package.version === self.projectPackage.version; | ||
// }, fix_packageversion); | ||
await this.test('using updated firebase-admin', async function () { | ||
await self.test('using updated firebase-admin', async function () { | ||
let pkg = 'firebase-admin'; | ||
@@ -325,3 +330,3 @@ // let latest = semver.clean(await getPkgVersion(pkg)); | ||
await this.test('using updated firebase-functions', async function () { | ||
await self.test('using updated firebase-functions', async function () { | ||
let pkg = 'firebase-functions'; | ||
@@ -342,3 +347,3 @@ // let latest = semver.clean(await getPkgVersion(pkg)); | ||
await this.test('using updated backend-manager', async function () { | ||
await self.test('using updated backend-manager', async function () { | ||
let pkg = 'backend-manager'; | ||
@@ -363,3 +368,3 @@ let latest = semver.clean(await getPkgVersion(pkg)); | ||
// await this.test('using updated backend-assistant', async function () { | ||
// await self.test('using updated backend-assistant', async function () { | ||
// let pkg = 'backend-assistant'; | ||
@@ -371,3 +376,3 @@ // let latest = semver.clean(await getPkgVersion(pkg)); | ||
// await this.test('using updated ultimate-jekyll-poster', async function () { | ||
// await self.test('using updated ultimate-jekyll-poster', async function () { | ||
// let pkg = 'ultimate-jekyll-poster'; | ||
@@ -379,3 +384,3 @@ // let latest = semver.clean(await getPkgVersion(pkg)); | ||
// await this.test('using updated @firebase/testing', async function () { | ||
// await self.test('using updated @firebase/testing', async function () { | ||
// let pkg = '@firebase/testing'; | ||
@@ -387,3 +392,3 @@ // let latest = semver.clean(await getPkgVersion(pkg)); | ||
// await this.test('using updated mocha', async function () { | ||
// await self.test('using updated mocha', async function () { | ||
// let pkg = 'mocha'; | ||
@@ -395,3 +400,3 @@ // let latest = semver.clean(await getPkgVersion(pkg)); | ||
await this.test('using proper .runtimeconfig', async function () { | ||
await self.test('using proper .runtimeconfig', async function () { | ||
let runtimeconfig = JSON.parse(jetpack.read(`${self.firebaseProjectPath}/functions/.runtimeconfig.json`) || '{}'); | ||
@@ -412,3 +417,3 @@ let ogPaths = getObjectPaths(runtimeconfigTemplate).split('\n'); | ||
await this.test('using proper backend-manager-config.json', async function () { | ||
await self.test('using proper backend-manager-config.json', async function () { | ||
let bemConfig = JSON.parse(jetpack.read(`${self.firebaseProjectPath}/functions/backend-manager-config.json`) || '{}'); | ||
@@ -429,3 +434,3 @@ let ogPaths = getObjectPaths(bemConfigTemplate).split('\n'); | ||
await this.test('has service-account.json', function () { | ||
await self.test('has service-account.json', function () { | ||
let exists = jetpack.exists(`${self.firebaseProjectPath}/functions/service-account.json`); | ||
@@ -435,3 +440,3 @@ return !!exists; | ||
await this.test('has correct .gitignore', function () { | ||
await self.test('has correct .gitignore', function () { | ||
let match = self.gitignore.match(bem_giRegexOuter); | ||
@@ -450,3 +455,3 @@ if (!match) { | ||
// Check firebase.json fields | ||
await this.test('firestore rules in JSON', function () { | ||
await self.test('firestore rules in JSON', function () { | ||
const firestore = _.get(self.firebaseJSON, 'firestore', {}); | ||
@@ -456,3 +461,3 @@ return (firestore.rules === 'firestore.rules') | ||
await this.test('firestore indexes in JSON', function () { | ||
await self.test('firestore indexes in JSON', function () { | ||
let firestore = _.get(self.firebaseJSON, 'firestore', {}); | ||
@@ -462,3 +467,3 @@ return (firestore.indexes === 'firestore.indexes.json') | ||
await this.test('realtime rules in JSON', function () { | ||
await self.test('realtime rules in JSON', function () { | ||
const database = _.get(self.firebaseJSON, 'database', {}); | ||
@@ -468,3 +473,3 @@ return (database.rules === 'database.rules.json') | ||
await this.test('storage rules in JSON', function () { | ||
await self.test('storage rules in JSON', function () { | ||
const storage = _.get(self.firebaseJSON, 'storage', {}); | ||
@@ -474,8 +479,13 @@ return (storage.rules === 'storage.rules') | ||
await this.test('remoteconfig template in JSON', function () { | ||
await self.test('remoteconfig template in JSON', function () { | ||
const remoteconfig = _.get(self.firebaseJSON, 'remoteconfig', {}); | ||
return (remoteconfig.template === 'remoteconfig.template.json') | ||
if (self.remoteconfigJSONExists) { | ||
return (remoteconfig.template === 'remoteconfig.template.json') | ||
} else { | ||
return (remoteconfig.template === '') | ||
} | ||
}, fix_remoteconfigTemplate); | ||
await this.test('firestore indexes synced', async function () { | ||
await self.test('firestore indexes synced', async function () { | ||
const tempPath = '_firestore.indexes.json' | ||
@@ -492,3 +502,6 @@ const liveIndexes = await cmd_indexesGet(self, tempPath, false); | ||
if (localIndexes_exists && !equal) { | ||
console.log(chalk.red(`Run ${chalk.bold('npx bm indexes:get')} to overwrite the local Firestore indexes with the live ones.`)); | ||
console.log(chalk.red(`To fix this...`)); | ||
console.log(chalk.red(` - ${chalk.bold('npx bm indexes:get')} to overwrite Firestore's local indexes with the live indexes`)); | ||
console.log(chalk.red(' OR')); | ||
console.log(chalk.red(` - ${chalk.bold('firebase deploy --only firestore:indexes')} to replace the live indexes.`)); | ||
} | ||
@@ -503,3 +516,3 @@ | ||
// Update actual files | ||
await this.test('update firestore rules file', function () { | ||
await self.test('update firestore rules file', function () { | ||
let exists = jetpack.exists(`${self.firebaseProjectPath}/firestore.rules`); | ||
@@ -513,3 +526,3 @@ let contents = jetpack.read(`${self.firebaseProjectPath}/firestore.rules`) || ''; | ||
await this.test('update firestore indexes file', function () { | ||
await self.test('update firestore indexes file', function () { | ||
let exists = jetpack.exists(`${self.firebaseProjectPath}/firestore.indexes.json`); | ||
@@ -519,3 +532,3 @@ return (!!exists); | ||
await this.test('update realtime rules file', function () { | ||
await self.test('update realtime rules file', function () { | ||
let exists = jetpack.exists(`${self.firebaseProjectPath}/database.rules.json`); | ||
@@ -525,3 +538,3 @@ return (!!exists); | ||
await this.test('update storage rules file', function () { | ||
await self.test('update storage rules file', function () { | ||
let exists = jetpack.exists(`${self.firebaseProjectPath}/storage.rules`); | ||
@@ -531,3 +544,3 @@ return (!!exists); | ||
await this.test('update remoteconfig template file', function () { | ||
await self.test('update remoteconfig template file', function () { | ||
let exists = jetpack.exists(`${self.firebaseProjectPath}/remoteconfig.template.json`); | ||
@@ -538,3 +551,3 @@ return (!!exists); | ||
// Hosting | ||
await this.test('hosting is set to dedicated folder in JSON', function () { | ||
await self.test('hosting is set to dedicated folder in JSON', function () { | ||
let hosting = _.get(self.firebaseJSON, 'hosting', {}); | ||
@@ -544,3 +557,3 @@ return (hosting.public && (hosting.public === 'public' || hosting.public !== '.')) | ||
await this.test('update backend-manager-tests.js', function () { | ||
await self.test('update backend-manager-tests.js', function () { | ||
jetpack.write(`${self.firebaseProjectPath}/test/backend-manager-tests.js`, | ||
@@ -552,3 +565,3 @@ (jetpack.read(path.resolve(`${__dirname}/../../templates/backend-manager-tests.js`))) | ||
// await this.test('has mocha package.json script', function () { | ||
// await self.test('has mocha package.json script', function () { | ||
// let script = _.get(self.package, 'scripts.test', '') | ||
@@ -558,3 +571,3 @@ // return script === MOCHA_PKG_SCRIPT; | ||
// await this.test('has clean:npm package.json script', function () { | ||
// await self.test('has clean:npm package.json script', function () { | ||
// let script = _.get(self.package, 'scripts.clean:npm', '') | ||
@@ -574,4 +587,4 @@ // return script === NPM_CLEAN_SCRIPT; | ||
const prepareStatsURL = `https://us-central1-${_.get(this.firebaseRC, 'projects.default')}.cloudfunctions.net/bm_api?authenticationToken=${_.get(this.runtimeConfigJSON, 'backend_manager.key')}`; | ||
// const prepareStatsURL = `https://us-central1-${_.get(this.firebaseRC, 'projects.default')}.cloudfunctions.net/bm_api?authenticationToken=undefined`; | ||
const prepareStatsURL = `https://us-central1-${_.get(self.firebaseRC, 'projects.default')}.cloudfunctions.net/bm_api?authenticationToken=${_.get(self.runtimeConfigJSON, 'backend_manager.key')}`; | ||
// const prepareStatsURL = `https://us-central1-${_.get(self.firebaseRC, 'projects.default')}.cloudfunctions.net/bm_api?authenticationToken=undefined`; | ||
const statsFetchResult = await fetch(prepareStatsURL, { | ||
@@ -646,3 +659,3 @@ method: 'post', | ||
Main.prototype.test = async function(name, fn, fix, args) { | ||
let self = this; | ||
const self = this; | ||
let status; | ||
@@ -822,3 +835,8 @@ let passed = await fn(); | ||
async function fix_bem(self) { | ||
return await installPkg('backend-manager') | ||
await installPkg('backend-manager'); | ||
console.log(chalk.green(`Process has exited since a new version of backend-manager was installed. Run ${chalk.bold('npx bm setup')} again.`)); | ||
process.exit(0); | ||
return; | ||
}; | ||
@@ -888,3 +906,3 @@ // async function fix_bea(self) { | ||
return new Promise(function(resolve, reject) { | ||
_.set(self.firebaseJSON, 'remoteconfig.template', 'remoteconfig.template.json') | ||
_.set(self.firebaseJSON, 'remoteconfig.template', self.remoteconfigJSONExists ? 'remoteconfig.template.json' : '') | ||
jetpack.write(`${self.firebaseProjectPath}/firebase.json`, JSON.stringify(self.firebaseJSON, null, 2)); | ||
@@ -1069,4 +1087,4 @@ resolve(); | ||
return new Promise(async function(resolve, reject) { | ||
// console.log(this.options); | ||
// console.log(this.argv); | ||
// console.log(self.options); | ||
// console.log(self.argv); | ||
newPath = newPath || await inquirer.prompt([ | ||
@@ -1147,4 +1165,4 @@ { | ||
return new Promise(async function(resolve, reject) { | ||
// console.log(this.options); | ||
// console.log(this.argv); | ||
// console.log(self.options); | ||
// console.log(self.argv); | ||
await inquirer | ||
@@ -1151,0 +1169,0 @@ .prompt([ |
@@ -14,3 +14,3 @@ function Module() { | ||
if (payload.user.authenticated || payload.user.roles.admin) { | ||
if (payload.user.roles.admin) { | ||
@@ -17,0 +17,0 @@ // console.log('---payload.data.payload', payload.data.payload); |
219729
0.35%5667
0.23%