backend-manager 3.2.63 → 3.2.65

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.63",
3
+ "version": "3.2.65",
4
4
  "description": "Quick tools for developing Firebase functions",
5
5
  "main": "src/manager/index.js",
6
6
  "bin": {
@@ -21,19 +21,32 @@ Module.prototype.main = function () {
21
21
  return reject(assistant.errorify(`Admin required.`, {code: 401}));
22
22
  }
23
23
 
24
+ // Get repo info
24
25
  const repoInfo = assistant.parseRepo(get(self.Manager.config, 'github.repo_website'));
25
-
26
26
  const poster = new Poster();
27
27
 
28
+ assistant.log(`main(): Creating post...`, repoInfo);
29
+
30
+ // Set defaults
31
+ const githubUser = get(self.Manager.config, 'github.user');
32
+ const githubKey = get(self.Manager.config, 'github.key');
33
+ const filePath = poster.removeDirDot(finalPost.path);
34
+ const fileContent = finalPost.content;
35
+
28
36
  // Save to disk OR commit
29
37
  poster.onDownload = function (meta) {
30
38
  return new Promise(async function(resolve, reject) {
31
39
  const tempPath = (meta.tempPath);
32
40
  const finalPath = poster.removeDirDot(meta.finalPath);
33
41
 
42
+ // Log
43
+ assistant.log(`onDownload(): tempPath`, tempPath);
44
+ assistant.log(`onDownload(): finalPath`, finalPath);
45
+
46
+ // Save to disk
34
47
  poster.readImage(tempPath)
35
48
  .then(image => {
36
- createFile(get(self.Manager.config, 'github.user'), repoInfo.user, repoInfo.name, get(self.Manager.config, 'github.key'), finalPath, image)
49
+ self.createFile(githubUser, repoInfo.user, repoInfo.name, githubKey, finalPath, image)
37
50
  .then(() => {resolve()})
38
51
  .catch((e) => {reject(e)})
39
52
  })
@@ -42,8 +55,8 @@ Module.prototype.main = function () {
42
55
  });
43
56
  }
44
57
 
58
+ // Create post
45
59
  const finalPost = await poster.create(payload.data).catch(e => e);
46
-
47
60
  if (finalPost instanceof Error) {
48
61
  return reject(assistant.errorify(`Failed to post: ${finalPost}`, {code: 500}));
49
62
  }
@@ -64,7 +77,7 @@ Module.prototype.main = function () {
64
77
  // }
65
78
 
66
79
  // Save post OR commit
67
- await createFile(get(self.Manager.config, 'github.user'), repoInfo.user, repoInfo.name, get(self.Manager.config, 'github.key'), poster.removeDirDot(finalPost.path), finalPost.content)
80
+ await self.createFile(githubUser, repoInfo.user, repoInfo.name, githubKey, filePath, fileContent)
68
81
  .then(() => {
69
82
  return resolve({data: finalPost});
70
83
  })
@@ -77,19 +90,30 @@ Module.prototype.main = function () {
77
90
  };
78
91
 
79
92
  // HELPERS //
80
- async function createFile(user, repoUser, repoName, key, path, contents) {
81
- let fileParsed = pathApi.parse(path);
93
+ Module.prototype.createFile = function (user, repoUser, repoName, key, path, contents) {
94
+ const self = this;
95
+ const Manager = self.Manager;
96
+ const Api = self.Api;
97
+ const assistant = self.assistant;
98
+ const payload = self.payload;
82
99
 
83
- let base64Data = Buffer.from(contents).toString('base64');
84
- // base64Data = contents;
85
- // console.log('--------base64Data', base64Data);
86
100
  return new Promise(async (resolve, reject) => {
101
+ let fileParsed = pathApi.parse(path);
102
+ let base64Data = Buffer.from(contents).toString('base64');
87
103
  let sha;
88
- try {
89
104
 
90
- let branch = (repoName === 'ultimate-jekyll') ? 'template' : 'master';
105
+ // Log
106
+ assistant.log(`createFile(): Writing file to ${repoUser}/${repoName}/${path}`);
91
107
 
108
+ // Try to get sha
109
+ try {
110
+ let branch = repoName === 'ultimate-jekyll' ? 'template' : 'master';
92
111
  let pathGet = `https://api.github.com/repos/${repoUser}/${repoName}/git/trees/${branch}:${encodeURIComponent(pathApi.dirname(path))}`;
112
+
113
+ // Log
114
+ assistant.log(`createFile(): pathGet`, pathGet);
115
+
116
+ // Make request
93
117
  await makeRequest({
94
118
  method: 'GET',
95
119
  url: pathGet,
@@ -116,8 +140,7 @@ async function createFile(user, repoUser, repoName, key, path, contents) {
116
140
  }
117
141
 
118
142
  let pathPut = `https://api.github.com/repos/${repoUser}/${repoName}/contents/${path}`;
119
- let writeRequest =
120
- {
143
+ let writeRequest = {
121
144
  // url: `https://api.github.com/repos/:owner/:repo/contents/:path`,
122
145
  method: 'PUT',
123
146
  url: pathPut,
@@ -133,10 +156,16 @@ async function createFile(user, repoUser, repoName, key, path, contents) {
133
156
  'Authorization': `Basic ${Buffer.from(user + ':' + key).toString('base64')}`,
134
157
  }
135
158
  }
159
+
160
+ // Log
161
+ assistant.log(`createFile(): pathPut`, pathPut);
162
+
163
+ // Add sha if it exists
136
164
  if (sha) {
137
165
  writeRequest.body.sha = sha;
138
166
  }
139
- // console.log('--------PUT', pathPut);
167
+
168
+ // Make request
140
169
  await makeRequest(writeRequest)
141
170
  .then((json) => {
142
171
  if (!json || (json.message && (json.message === 'Not Found' || json.message.includes('Invalid request'))) ) {