@enplug/scripts 1.11.4-dev2 → 1.11.4-dev21

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.
@@ -1,6 +1,7 @@
1
1
  const axios = require('axios');
2
2
  const chalk = require('chalk');
3
3
  const fs = require('fs');
4
+ const Blob = require('blob');
4
5
  const FormData = require('form-data');
5
6
  const inquirer = require('inquirer');
6
7
  const loadDevPrivateFile = require('../loadDevPrivateFile');
@@ -109,16 +110,23 @@ function getCrowdinConfig() {
109
110
  }
110
111
 
111
112
  function generateFormData(crowdinPath, localPath) {
113
+ // const reader = new FileReader();
114
+ const buffer = fs.readFileSync(localPath);
115
+ console.log('buffer',buffer);
116
+ const stream = fs.createReadStream(localPath)
117
+ console.log('stream',stream);
118
+ const blob = new Blob([buffer]);
119
+ console.log('blob', blob);
112
120
  const formData = new FormData();
113
121
 
114
122
  // formData.append('update_option', 'update_without_changes'); // Previous translations should remain valid
115
- formData.append('json', '');
116
- formData.append(`files[${crowdinPath}]`, fs.createReadStream(localPath));
117
- formData.append(`export_patterns[${crowdinPath}]`, '%locale%.json');
123
+ // formData.append('json', '');
124
+ formData.append('file', fs.createReadStream(localPath), 'en.json');
125
+ // formData.append(`export_patterns[${crowdinPath}]`, '%locale%.json');
118
126
  return formData;
119
127
  }
120
128
 
121
- function postFileToCrowdin(operation, credentials, crowdinPath, localPath) {
129
+ function postFileToCrowdin(credentials, crowdinPath, localPath) {
122
130
  const AuthStr = 'Bearer '.concat(credentials.token);
123
131
  const formData = generateFormData(crowdinPath, localPath);
124
132
 
@@ -137,28 +145,32 @@ async function findCrowdinAppDirectoryId(credentials, crowdinPath) {
137
145
  const appName = path.split('/')[1];
138
146
  const url = `${CROWDIN_PROJECT_URL}/directories?filter=${appName}`;
139
147
  const AuthStr = 'Bearer '.concat(credentials.token);
140
- const directories = await axios.get(url, { headers: { Authorization: AuthStr } });
141
- return directories.data.find(d => d.data.name === appName).id;
148
+ const resp = await axios.get(url, { headers: { Authorization: AuthStr } });
149
+ const directory = resp.data.data.find(d => d.data.name === appName );
150
+ return directory.data.id
142
151
  } else if(path.startsWith('dashboard')) {
143
152
  const url = `${CROWDIN_PROJECT_URL}/directories?filter=dashboard`;
144
153
  const AuthStr = 'Bearer '.concat(credentials.token);
145
- const directories = axios.get(url, { headers: { Authorization: AuthStr } });
146
- return directories.data.find(d => d.data.name === 'dashboard' && d.data.title === 'Dashboard' && d.data.path === '/dashboard').id;
154
+ const resp = axios.get(url, { headers: { Authorization: AuthStr } });
155
+ const directory = resp.data.data.find(d => d.data.name === 'dashboard' && d.data.title === 'Dashboard' && d.data.path === '/dashboard');
156
+ return directory.data.id;
147
157
  }
148
158
  }
149
159
 
150
160
  async function findCrowdinAppSubFolderId(credentials, directoryId, subfolderName) {
151
161
  const url = `${CROWDIN_PROJECT_URL}/directories?directoryId=${directoryId}`;
152
162
  const AuthStr = 'Bearer '.concat(credentials.token);
153
- const directories = await axios.get(url, { headers: { Authorization: AuthStr } });
154
- return directories.data.find(d => d.data.name === subfolderName).id;
163
+ const resp = await axios.get(url, { headers: { Authorization: AuthStr } });
164
+ const subdirectory = resp.data.data.find(d => d.data.name === subfolderName)
165
+ return subdirectory.data.id;
155
166
  }
156
167
 
157
168
  async function getFileIdIfExists(credentials, directoryId, fileName) {
158
169
  const url = `${CROWDIN_PROJECT_URL}/files?directoryId=${directoryId}`;
159
170
  const AuthStr = 'Bearer '.concat(credentials.token);
160
- const files = await axios.get(url, { headers: { Authorization: AuthStr } });
161
- return files.data.find(d => d.data.name === fileName).id;
171
+ const resp = await axios.get(url, { headers: { Authorization: AuthStr } });
172
+ const file = resp.data.data.find(d => d.data.name === fileName);
173
+ return file.data.id;
162
174
  }
163
175
 
164
176
  async function uploadFileToCrowdinStorage(credentials, crowdinPath, localPath) {
@@ -167,15 +179,15 @@ async function uploadFileToCrowdinStorage(credentials, crowdinPath, localPath) {
167
179
  throw new Error('Local file does not exist');
168
180
  }
169
181
 
170
- return postFileToCrowdin('update', credentials, crowdinPath, localPath).pipe(
182
+ return postFileToCrowdin(credentials, crowdinPath, localPath).pipe(
171
183
  catchError(error => {
172
- if (error.response.status === HTTP_NOT_FOUND_STATUS_CODE) {
173
- return promptAddFile(crowdinPath).pipe(
174
- filter(({addFileConfirm}) => addFileConfirm === true),
175
- switchMap(() => postFileToCrowdin('add', credentials, crowdinPath, localPath))
176
- );
177
- }
178
-
184
+ // if (error.response.status === HTTP_NOT_FOUND_STATUS_CODE) {
185
+ // return promptAddFile(crowdinPath).pipe(
186
+ // filter(({addFileConfirm}) => addFileConfirm === true),
187
+ // switchMap(() => postFileToCrowdin(credentials, crowdinPath, localPath))
188
+ // );
189
+ // }
190
+ console('ERROR could not upload file to crowdin storage')
179
191
  return throwError(error);
180
192
  }),
181
193
  tap({
@@ -206,6 +218,8 @@ async function uploadFileToCrowdinStorage(credentials, crowdinPath, localPath) {
206
218
  }
207
219
 
208
220
  function updateCrowdinFile(credentials, storageId, fileId) {
221
+ console.log('storageId', storageId);
222
+ console.log('fileId', fileId);
209
223
  const url = `${CROWDIN_PROJECT_URL}/files/${fileId}`;
210
224
  const AuthStr = 'Bearer '.concat(credentials.token);
211
225
  const body = {
@@ -62,17 +62,15 @@ async function syncTranslations(s3Client, bucket) {
62
62
 
63
63
  if (isProduction) {
64
64
  const appDirectoryId = await findCrowdinAppDirectoryId(credentials, config.crowdinPath);
65
- console.log('found directoryId', appDirectoryId);
66
65
  if(config.crowdinPath.includes('apps')) {
67
- const path = crowdinPath.startsWith('/') ? crowdinPath.substr(1) : crowdinPath;
66
+ const path = config.crowdinPath.startsWith('/') ? config.crowdinPath.substr(1) : config.crowdinPath;
68
67
  const subfolderName = path.split('/')[2];
69
68
  const folderId = await findCrowdinAppSubFolderId(credentials, appDirectoryId, subfolderName);
70
- console.log('found folderId', folderId);
71
- const fileId = await getFileIdIfExists(credentials, appDirectoryId, path.split('/')[3]);
72
- console.log('found fileId', fileId);
73
- const storageId = await uploadFileToCrowdinStorage(credentials, config.crowdinPath, config.localPath)
74
- if(fileId) {
75
- await updateCrowdinFile(credentials, storageId, fileId);
69
+ const fileId = await getFileIdIfExists(credentials, folderId, path.split('/')[3]);
70
+ const storageId = await uploadFileToCrowdinStorage(credentials, config.crowdinPath, config.localPath);
71
+ if(fileId && storageId) {
72
+ const response = await updateCrowdinFile(credentials, storageId.data.data.id, fileId);
73
+ console.log('response', response.data);
76
74
  }
77
75
  } else {
78
76
  // TODO Update dashboard transtlations file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@enplug/scripts",
3
- "version": "1.11.4-dev2",
3
+ "version": "1.11.4-dev21",
4
4
  "description": "Enplug scripts",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1",
@@ -29,6 +29,7 @@
29
29
  "dependencies": {
30
30
  "aws-sdk": "^2.474.0",
31
31
  "axios": "^0.19.2",
32
+ "blob": "^0.1.0",
32
33
  "chalk": "2.4.1",
33
34
  "command-line-args": "5.0.2",
34
35
  "fs": "0.0.1-security",