backend-manager 3.2.136 → 3.2.138

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": "3.2.136",
3
+ "version": "3.2.138",
4
4
  "description": "Quick tools for developing Firebase functions",
5
5
  "main": "src/manager/index.js",
6
6
  "bin": {
package/src/cli/cli.js CHANGED
@@ -1284,7 +1284,7 @@ async function cmd_configSet(self, newPath, newValue) {
1284
1284
  } catch (e) {
1285
1285
  }
1286
1286
 
1287
- const isObject = typeof object === 'object';
1287
+ const isObject = object && typeof object === 'object';
1288
1288
 
1289
1289
  // If it's a string, ensure some things
1290
1290
  if (!isObject) {
@@ -60,10 +60,16 @@ Module.prototype.main = function () {
60
60
 
61
61
  // Fix required values
62
62
  payload.data.payload.url = payload.data.payload.url
63
- // Replace /blog/ (slashes may or may not be present)
63
+ // Replace blog/
64
64
  .replace(/blog\//ig, '')
65
65
  // Remove leading and trailing slashes
66
- .replace(/^\/|\/$/g, '');
66
+ .replace(/^\/|\/$/g, '')
67
+ // Replace anything that's not a letter or number with a hyphen
68
+ .replace(/[^a-zA-Z0-9]/g, '-')
69
+ // Remove multiple hyphens
70
+ .replace(/-+/g, '-')
71
+ // Lowercase
72
+ .toLowerCase();
67
73
 
68
74
  // Fix body
69
75
  payload.data.payload.body = payload.data.payload.body
@@ -108,16 +108,29 @@ Module.prototype.harvest = function (settings) {
108
108
  });
109
109
 
110
110
  // Log
111
- assistant.log('Get final content', final);
111
+ assistant.log('harvest(): Get final content', final);
112
112
 
113
113
  // Request to Ghostii
114
- const result = await self.requestGhostii(final).catch((e) => e);
115
- if (result instanceof Error) {
116
- assistant.error(`harvest(): Error requesting Ghostii`, result);
114
+ const article = await self.requestGhostii(final).catch((e) => e);
115
+ if (article instanceof Error) {
116
+ assistant.error(`harvest(): Error requesting Ghostii`, article);
117
117
 
118
118
  break;
119
119
  }
120
120
 
121
+ // Log
122
+ assistant.log(`harvest(): Article`, article);
123
+
124
+ // Upload post to blog
125
+ const uploadedPost = await self.uploadPost(article).catch((e) => e);
126
+ if (uploadedPost instanceof Error) {
127
+ assistant.error(`harvest(): Error uploading post to blog`, uploadedPost);
128
+
129
+ break;
130
+ }
131
+
132
+ // Log
133
+ assistant.log(`harvest(): Uploaded post`, uploadedPost);
121
134
  }
122
135
 
123
136
  // Log
@@ -209,17 +222,71 @@ Module.prototype.requestGhostii = function (content) {
209
222
  // Fetch URL
210
223
  fetch('https://api.ghostii.ai/write/article', {
211
224
  method: 'post',
212
- timeout: 30000,
213
- tries: 3,
225
+ timeout: 90000,
226
+ tries: 1,
214
227
  response: 'json',
215
228
  body: {
229
+ backendManagerKey: Manager.config.backend_manager.key,
216
230
  keywords: [''],
217
231
  description: content,
232
+ insertLinks: true,
218
233
  },
219
234
  })
220
- .then((r) => {
221
- return resolve(r);
235
+ .then((r) => resolve(r))
236
+ .catch((e) => reject(e));
237
+ });
238
+ }
239
+
240
+ Module.prototype.uploadPost = function (article) {
241
+ const self = this;
242
+
243
+ // Shortcuts
244
+ const Manager = self.Manager;
245
+ const libraries = self.libraries;
246
+ const assistant = self.assistant;
247
+ const context = self.context;
248
+
249
+ return new Promise(async function(resolve, reject) {
250
+ // request.body.command = 'admin:create-post';
251
+ // request.body.payload = {
252
+ // title: data.payload.postTitle,
253
+ // url: data.formatted.path,
254
+ // excerpt: data.payload.postExcerpt,
255
+ // headerImageURL: data.payload.postHeaderImageUrl,
256
+ // body: data.payload.postBody,
257
+ // id: self.postId++,
258
+ // author: 'alex',
259
+ // // affiliate: '',
260
+ // // tags: 'tag, tag2, tag3',
261
+ // // categories: 'marketing, business',
262
+ // githubUser: app.github.user,
263
+ // githubRepo: app.github.repo,
264
+ // }
265
+
266
+
267
+ // Fetch URL
268
+ fetch(`${self.appObject.server}/bm_api`, {
269
+ method: 'post',
270
+ timeout: 90000,
271
+ tries: 1,
272
+ response: 'json',
273
+ body: {
274
+ backendManagerKey: Manager.config.backend_manager.key,
275
+ command: '',
276
+ payload: {
277
+ title: article.title,
278
+ url: article.title, // This is formatted on the bm_api endpoint
279
+ excerpt: article.description,
280
+ headerImageURL: article.headerImageUrl,
281
+ body: article.body,
282
+ id: self.postId++,
283
+ author: 'alex',
284
+ githubUser: self.appObject.github.user,
285
+ githubRepo: self.appObject.github.repo,
286
+ },
287
+ },
222
288
  })
289
+ .then((r) => resolve(r))
223
290
  .catch((e) => reject(e));
224
291
  });
225
292
  }
@@ -67,6 +67,7 @@ Manager.prototype.init = function (exporter, options) {
67
67
  options.uniqueAppName = options.uniqueAppName || undefined;
68
68
  options.assistant = options.assistant || {};
69
69
  options.cwd = typeof options.cwd === 'undefined' ? process.cwd() : options.cwd;
70
+ options.projectPackageDirectory = typeof options.projectPackageDirectory === 'undefined' ? undefined : options.projectPackageDirectory;
70
71
  // options.assistant.optionsLogString = options.assistant.optionsLogString || undefined;
71
72
 
72
73
  // Load libraries
@@ -98,7 +99,7 @@ Manager.prototype.init = function (exporter, options) {
98
99
  self.project.backendManagerConfigPath = path.resolve(self.cwd, options.backendManagerConfigPath);
99
100
 
100
101
  // Load package.json
101
- self.package = resolveProjectPackage(self.cwd);
102
+ self.package = resolveProjectPackage(options.projectPackageDirectory || self.cwd);
102
103
 
103
104
  // Load config
104
105
  self.config = merge(
@@ -205,7 +206,7 @@ Manager.prototype.init = function (exporter, options) {
205
206
  if (self.options.initialize) {
206
207
  // Initialize Firebase
207
208
  try {
208
- // console.log('---process.env.GOOGLE_APPLICATION_CREDENTIALS', process.env.GOOGLE_APPLICATION_CREDENTIALS);
209
+ // Initialize Firebase
209
210
  if (process.env.GOOGLE_APPLICATION_CREDENTIALS) {
210
211
  self.libraries.initializedAdmin = self.libraries.admin.initializeApp();
211
212
  } else {
@@ -253,9 +254,9 @@ Manager.prototype.init = function (exporter, options) {
253
254
 
254
255
  // Set dotenv
255
256
  try {
256
- require('dotenv').config();
257
+ const env = require('dotenv').config();
257
258
  } catch (e) {
258
- self.assistant.error(new Error('Failed to set up environment variables from .env file'));
259
+ self.assistant.error(new Error(`Failed to set up environment variables from .env file: ${e.message}`));
259
260
  }
260
261
 
261
262
  // Setup LocalDatabase
@@ -554,6 +555,19 @@ Manager.prototype.storage = function (options) {
554
555
  return self._internal.storage[options.name]
555
556
  };
556
557
 
558
+ Manager.prototype.getCustomServer = function () {
559
+ const self = this;
560
+
561
+ if (!self._internal.server || !self._internal.app) {
562
+ throw new Error('Server not set up');
563
+ }
564
+
565
+ return {
566
+ server: self._internal.server,
567
+ app: self._internal.app,
568
+ };
569
+ };
570
+
557
571
  Manager.prototype.install = function (controller, options) {
558
572
  const self = this;
559
573
 
@@ -856,7 +870,7 @@ Manager.prototype.setupCustomServer = function (_library, options) {
856
870
  // // querystringParser: str => querystring.parse(str.toLowerCase())
857
871
  // });
858
872
 
859
- //
873
+ // Setup express
860
874
  const app = require('express')({
861
875
  logger: true,
862
876
  // querystringParser: str => querystring.parse(str.toLowerCase())
@@ -979,6 +993,10 @@ Manager.prototype.setupCustomServer = function (_library, options) {
979
993
  self.assistant.log(`Server listening on ${address.address}:${address.port}`);
980
994
  }
981
995
 
996
+ // Set server and app to internal
997
+ self._internal.server = server;
998
+ self._internal.app = app;
999
+
982
1000
  // Emit event
983
1001
  self.emit('online', new Event('online'), server, app);
984
1002
  });