backend-manager 2.5.57 → 2.5.59

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "backend-manager",
3
- "version": "2.5.57",
3
+ "version": "2.5.59",
4
4
  "description": "Quick tools for developing Firebase functions",
5
5
  "main": "src/manager/index.js",
6
6
  "bin": {
@@ -47,6 +47,7 @@
47
47
  "lowdb": "^1.0.0",
48
48
  "mailchimp-api-v3": "^1.15.0",
49
49
  "moment": "^2.29.4",
50
+ "nanoid": "^3.3.4",
50
51
  "node-fetch": "^2.6.9",
51
52
  "node-powertools": "^0.0.23",
52
53
  "npm-api": "^1.0.1",
package/src/cli/cli.js CHANGED
@@ -227,6 +227,7 @@ Main.prototype.setup = async function () {
227
227
  self.runtimeConfigJSON = jetpack.read(`${self.firebaseProjectPath}/functions/.runtimeconfig.json`) || '{}';
228
228
  self.remoteconfigJSON = jetpack.read(`${self.firebaseProjectPath}/remoteconfig.template.json`) || '{}';
229
229
  self.projectPackage = jetpack.read(`${self.firebaseProjectPath}/package.json`) || '{}';
230
+ self.bemConfigJSON = jetpack.read(`${self.firebaseProjectPath}/functions/backend-manager-config.json`) || '{}';
230
231
 
231
232
  self.gitignore = jetpack.read(`${self.firebaseProjectPath}/functions/.gitignore`) || '';
232
233
  if (!self.package) {
@@ -261,19 +262,18 @@ Main.prototype.setup = async function () {
261
262
  self.bemApiURL = `https://us-central1-${_.get(self.firebaseRC, 'projects.default')}.cloudfunctions.net/bm_api?authenticationToken=${_.get(self.runtimeConfigJSON, 'backend_manager.key')}`;
262
263
  // const prepareStatsURL = `https://us-central1-${_.get(self.firebaseRC, 'projects.default')}.cloudfunctions.net/bm_api?authenticationToken=undefined`;
263
264
 
264
-
265
-
266
- log(`Id: `, chalk.bold(`${self.projectName}`));
267
- log(`Url:`, chalk.bold(`${self.projectUrl}`));
265
+ // Log
266
+ log(`ID: `, chalk.bold(`${self.projectName}`));
267
+ log(`URL:`, chalk.bold(`${self.projectUrl}`));
268
268
 
269
269
  if (!self.package || !self.package.engines || !self.package.engines.node) {
270
270
  throw new Error('Missing <engines.node> in package.json')
271
271
  }
272
272
 
273
-
274
273
  // Tests
275
274
  await self.test('is a firebase project', async function () {
276
275
  let exists = jetpack.exists(`${self.firebaseProjectPath}/firebase.json`);
276
+
277
277
  return exists;
278
278
  }, fix_isFirebase);
279
279
 
@@ -281,8 +281,8 @@ Main.prototype.setup = async function () {
281
281
  // return !!self.package.dependencies && !!self.package.devDependencies;
282
282
  // let gitignore = jetpack.read(path.resolve(`${__dirname}/../../templates/gitignore.md`));
283
283
  let nvmrc = jetpack.read(`${self.firebaseProjectPath}/functions/.nvmrc`) || '';
284
+
284
285
  return nvmrc === `v${CLI_CONFIG.node}/*`
285
-
286
286
  }, fix_nvmrc);
287
287
 
288
288
  await self.test(`using node ${CLI_CONFIG.node}`, function () {
@@ -390,22 +390,25 @@ Main.prototype.setup = async function () {
390
390
  let runtimeconfig = JSON.parse(jetpack.read(`${self.firebaseProjectPath}/functions/.runtimeconfig.json`) || '{}');
391
391
  let ogPaths = getObjectPaths(runtimeconfigTemplate).split('\n');
392
392
  let pass = true;
393
+
393
394
  for (var i = 0, l = ogPaths.length; i < l; i++) {
394
395
  let item = ogPaths[i];
395
396
  if (!item) {continue}
396
- pass = (_.get(runtimeconfig, item, undefined));
397
+ pass = _.get(runtimeconfig, item, undefined);
397
398
  if (typeof pass === 'undefined') {
398
399
  break;
399
400
  }
400
401
  }
401
- return !!pass;
402
402
 
403
+ return !!pass;
403
404
  }, fix_runtimeConfig);
404
405
 
405
406
  await self.test('using proper backend-manager-config.json', async function () {
406
- let bemConfig = JSON.parse(jetpack.read(`${self.firebaseProjectPath}/functions/backend-manager-config.json`) || '{}');
407
+ const bemConfig = JSON.parse(self.bemConfigJSON);
408
+
407
409
  let ogPaths = getObjectPaths(bemConfigTemplate).split('\n');
408
410
  let pass = true;
411
+
409
412
  for (var i = 0, l = ogPaths.length; i < l; i++) {
410
413
  let item = ogPaths[i];
411
414
  if (!item) {continue}
@@ -414,8 +417,13 @@ Main.prototype.setup = async function () {
414
417
  break;
415
418
  }
416
419
  }
417
- return !!pass;
418
420
 
421
+ if (self.projectName !== bemConfig.firebaseConfig.projectId) {
422
+ console.error(chalk.red('Mismatch between project name and firebaseConfig.projectId in backend-manager-config.json'));
423
+ return false;
424
+ }
425
+
426
+ return !!pass;
419
427
  }, fix_bemConfig);
420
428
 
421
429
  await self.test('has service-account.json', function () {
@@ -769,6 +777,12 @@ async function fix_isFirebase(self) {
769
777
  return;
770
778
  };
771
779
 
780
+ async function fix_isFirebase(self) {
781
+ log(chalk.red(`self is not a firebase project. Please use ${chalk.bold('firebase-init')} to set up.`));
782
+ throw '';
783
+ return;
784
+ };
785
+
772
786
  function fix_projpackage(self) {
773
787
  return new Promise(function(resolve, reject) {
774
788
  self.projectPackage = self.projectPackage || {};
@@ -1,5 +1,10 @@
1
+ let nanoId;
2
+
1
3
  function Utilities(Manager) {
2
4
  const self = this;
5
+
6
+ self.cache = null;
7
+ self._deleteThis = Math.random();
3
8
 
4
9
  self.Manager = Manager;
5
10
  }
@@ -134,4 +139,103 @@ Utilities.prototype.iterateUsers = function (callback, options) {
134
139
  });
135
140
  };
136
141
 
142
+ Utilities.prototype.randomId = function (options) {
143
+ const self = this;
144
+
145
+ options = options || {};
146
+ options.size = options.size || 14;
147
+
148
+ if (!nanoId) {
149
+ nanoId = require('nanoid');
150
+
151
+ nanoId = nanoId.customAlphabet(
152
+ nanoId.urlAlphabet.replace(/_|-/g, ''),
153
+ options.size,
154
+ )
155
+ }
156
+
157
+ return nanoId();
158
+ };
159
+
160
+ // Utilities.prototype.get = function (doc, options) {
161
+ // const self = this;
162
+
163
+ // return new Promise(function(resolve, reject) {
164
+
165
+ // options = options || {};
166
+ // options.maxAge = options.maxAge || (1000 * 60 * 5); // 5 minutes
167
+
168
+ // self.cache = self.cache || self.Manager.storage({name: 'cache', temporary: true, clear: false});
169
+
170
+ // const item = self.cache.get(doc).value();
171
+ // const age = item ? Date.now() - item.time : null;
172
+
173
+ // console.log('+++++ 111', doc, age, item);
174
+
175
+ // if (item && age < options.maxAge) {
176
+ // console.log('---GOT FROM CACHE---');
177
+
178
+ // return resolve(self.cache.map.get(doc).data);
179
+ // } else {
180
+ // console.log('---GOT FROM FIRESTORE---');
181
+
182
+ // self.Manager.libraries.admin.firestore().doc(doc)
183
+ // .get()
184
+ // .then(async (doc) => {
185
+ // const data = doc.data();
186
+
187
+ // if (!data) {
188
+ // throw new Error(`Document with ID ${doc} not found`);
189
+ // }
190
+
191
+ // self.cache.set(doc, {
192
+ // data: data,
193
+ // time: Date.now(),
194
+ // })
195
+ // .write();
196
+
197
+ // return resolve(data);
198
+ // })
199
+ // }
200
+ // });
201
+ // };
202
+
203
+
204
+ Utilities.prototype.get = function (docPath, options) {
205
+ const self = this;
206
+
207
+ return new Promise(function(resolve, reject) {
208
+
209
+ options = options || {};
210
+ options.maxAge = options.maxAge || (1000 * 60 * 5); // 5 minutes
211
+
212
+ self.cache = self.cache || self.Manager.storage({name: 'cache', temporary: true, clear: false});
213
+
214
+ const item = self.cache.get(docPath).value();
215
+ const age = item ? Date.now() - item.time : null;
216
+
217
+ if (item && age && age < options.maxAge) {
218
+ return resolve(item.doc);
219
+ } else {
220
+ self.Manager.libraries.admin.firestore().doc(docPath)
221
+ .get()
222
+ .then(async (doc) => {
223
+ const data = doc.data();
224
+
225
+ if (!data) {
226
+ throw new Error(`Document with ID ${doc} not found`);
227
+ }
228
+
229
+ self.cache.set(docPath, {
230
+ doc: doc,
231
+ time: Date.now(),
232
+ })
233
+ .write();
234
+
235
+ return resolve(doc);
236
+ })
237
+ }
238
+ });
239
+ };
240
+
137
241
  module.exports = Utilities;
@@ -3,6 +3,7 @@ const path = require('path');
3
3
  const { get, merge } = require('lodash');
4
4
  const jetpack = require('fs-jetpack');
5
5
  const JSON5 = require('json5');
6
+
6
7
  // const { debug, log, error, warn } = require('firebase-functions/lib/logger');
7
8
  // let User;
8
9
  // let Analytics;
@@ -570,12 +571,6 @@ Manager.prototype.ApiManager = function () {
570
571
  return new self.libraries.ApiManager(self, ...arguments);
571
572
  };
572
573
 
573
- Manager.prototype.Utilities = function () {
574
- const self = this;
575
- self.libraries.Utilities = self.libraries.Utilities || require('./helpers/utilities.js');
576
- return new self.libraries.Utilities(self, ...arguments);
577
- };
578
-
579
574
  Manager.prototype.Roles = function () {
580
575
  const self = this;
581
576
  self.libraries.Roles = self.libraries.Roles || require('./helpers/roles.js');
@@ -588,6 +583,23 @@ Manager.prototype.SubscriptionResolver = function () {
588
583
  return new self.libraries.SubscriptionResolver(...arguments);
589
584
  };
590
585
 
586
+ // Manager.prototype.Utilities = function () {
587
+ // const self = this;
588
+ // self.libraries.Utilities = self.libraries.Utilities || require('./helpers/utilities.js');
589
+ // return new self.libraries.Utilities(self, ...arguments);
590
+ // };
591
+
592
+ Manager.prototype.Utilities = function () {
593
+ const self = this;
594
+
595
+ if (!self._internal.utilities) {
596
+ self.libraries.Utilities = require('./helpers/utilities.js');
597
+ self._internal.utilities = new self.libraries.Utilities(self, ...arguments);
598
+ }
599
+
600
+ return self._internal.utilities;
601
+ };
602
+
591
603
  Manager.prototype.storage = function (options) {
592
604
  const self = this;
593
605
  options = options || {};
@@ -603,7 +615,7 @@ Manager.prototype.storage = function (options) {
603
615
  ? `${require('os').tmpdir()}/${options.name}.json`
604
616
  : `./.data/${options.name}.json`;
605
617
  const adapter = new FileSync(dbPath);
606
-
618
+
607
619
  if (
608
620
  options.temporary
609
621
  && self.assistant.meta.environment === 'development'
@@ -622,6 +634,8 @@ Manager.prototype.storage = function (options) {
622
634
  jetpack.write(dbPath, {});
623
635
  }
624
636
  self._internal.storage[options.name] = low(adapter);
637
+
638
+ self._internal.storage[options.name].set('_location', dbPath)
625
639
  }
626
640
 
627
641
  try {