@enplug/scripts 1.11.4-dev3 → 1.11.4-dev30

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.
@@ -6,6 +6,7 @@ const inquirer = require('inquirer');
6
6
  const loadDevPrivateFile = require('../loadDevPrivateFile');
7
7
  const rootPath = __dirname.split('node_modules')[0];
8
8
  const getPackageJson = require('../getPackageJson');
9
+ const { Blob } = require('buffer');
9
10
 
10
11
  const { from, throwError } = require('rxjs');
11
12
  const { catchError, switchMap, tap, filter } = require('rxjs/operators');
@@ -109,26 +110,31 @@ function getCrowdinConfig() {
109
110
  }
110
111
 
111
112
  function generateFormData(crowdinPath, localPath) {
113
+ // const reader = new FileReader();
112
114
  const formData = new FormData();
113
-
115
+ console.log('localPath', localPath);
116
+ const file = fs.readFileSync('./'+localPath);
117
+ console.log('file', file);
118
+ let blob = new Blob([file]);
119
+ console.log('blob', blob);
114
120
  // 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');
121
+ // formData.append('json', '');
122
+ formData.append('file', blob, 'en.json');
123
+ // formData.append(`export_patterns[${crowdinPath}]`, '%locale%.json');
118
124
  return formData;
119
125
  }
120
126
 
121
- function postFileToCrowdin(operation, credentials, crowdinPath, localPath) {
127
+ function postFileToCrowdin(credentials, crowdinPath, localPath) {
122
128
  const AuthStr = 'Bearer '.concat(credentials.token);
123
129
  const formData = generateFormData(crowdinPath, localPath);
124
130
 
125
131
  const url = `${CROWDIN_STORAGE_URL}`;
126
-
127
- return from(axios.post(url, formData, { headers: {
132
+ console.log('formData', formData);
133
+ return axios.post(url, formData, { headers: {
128
134
  "Authorization": AuthStr,
129
135
  "Content-Type": "application/json",
130
136
  "Crowdin-API-FileName": "en.json"
131
- } }));
137
+ } });
132
138
  }
133
139
 
134
140
  async function findCrowdinAppDirectoryId(credentials, crowdinPath) {
@@ -137,33 +143,32 @@ async function findCrowdinAppDirectoryId(credentials, crowdinPath) {
137
143
  const appName = path.split('/')[1];
138
144
  const url = `${CROWDIN_PROJECT_URL}/directories?filter=${appName}`;
139
145
  const AuthStr = 'Bearer '.concat(credentials.token);
140
- const directories = await axios.get(url, { headers: { Authorization: AuthStr } });
141
- console.log('directories', directories)
142
- return directories.data.find(d => d.data.name === appName).id;
146
+ const resp = await axios.get(url, { headers: { Authorization: AuthStr } });
147
+ const directory = resp.data.data.find(d => d.data.name === appName );
148
+ return directory.data.id
143
149
  } else if(path.startsWith('dashboard')) {
144
150
  const url = `${CROWDIN_PROJECT_URL}/directories?filter=dashboard`;
145
151
  const AuthStr = 'Bearer '.concat(credentials.token);
146
- const directories = axios.get(url, { headers: { Authorization: AuthStr } });
147
- console.log('directories', directories)
148
-
149
- return directories.data.find(d => d.data.name === 'dashboard' && d.data.title === 'Dashboard' && d.data.path === '/dashboard').id;
152
+ const resp = axios.get(url, { headers: { Authorization: AuthStr } });
153
+ const directory = resp.data.data.find(d => d.data.name === 'dashboard' && d.data.title === 'Dashboard' && d.data.path === '/dashboard');
154
+ return directory.data.id;
150
155
  }
151
156
  }
152
157
 
153
158
  async function findCrowdinAppSubFolderId(credentials, directoryId, subfolderName) {
154
159
  const url = `${CROWDIN_PROJECT_URL}/directories?directoryId=${directoryId}`;
155
160
  const AuthStr = 'Bearer '.concat(credentials.token);
156
- const directories = await axios.get(url, { headers: { Authorization: AuthStr } });
157
- console.log('subdirectories', directories)
158
-
159
- return directories.data.find(d => d.data.name === subfolderName).id;
161
+ const resp = await axios.get(url, { headers: { Authorization: AuthStr } });
162
+ const subdirectory = resp.data.data.find(d => d.data.name === subfolderName)
163
+ return subdirectory.data.id;
160
164
  }
161
165
 
162
166
  async function getFileIdIfExists(credentials, directoryId, fileName) {
163
167
  const url = `${CROWDIN_PROJECT_URL}/files?directoryId=${directoryId}`;
164
168
  const AuthStr = 'Bearer '.concat(credentials.token);
165
- const files = await axios.get(url, { headers: { Authorization: AuthStr } });
166
- return files.data.find(d => d.data.name === fileName).id;
169
+ const resp = await axios.get(url, { headers: { Authorization: AuthStr } });
170
+ const file = resp.data.data.find(d => d.data.name === fileName);
171
+ return file.data.id;
167
172
  }
168
173
 
169
174
  async function uploadFileToCrowdinStorage(credentials, crowdinPath, localPath) {
@@ -172,45 +177,49 @@ async function uploadFileToCrowdinStorage(credentials, crowdinPath, localPath) {
172
177
  throw new Error('Local file does not exist');
173
178
  }
174
179
 
175
- return postFileToCrowdin('update', credentials, crowdinPath, localPath).pipe(
176
- catchError(error => {
177
- if (error.response.status === HTTP_NOT_FOUND_STATUS_CODE) {
178
- return promptAddFile(crowdinPath).pipe(
179
- filter(({addFileConfirm}) => addFileConfirm === true),
180
- switchMap(() => postFileToCrowdin('add', credentials, crowdinPath, localPath))
181
- );
182
- }
183
-
184
- return throwError(error);
185
- }),
186
- tap({
187
- next: response => {
188
- if (response.data) {
189
- console.log(`${chalk.green.bold('Translations uploaded to Crowdin')} ${chalk.yellow.bold(`[${crowdinPath}]`)}`);
190
- } else {
191
- console.error('Unexpected result');
192
- console.log(response);
193
- }
194
- },
195
- error: error => {
196
- const crowdinError = error.response && error.response.data && error.response.data.error;
180
+ const response = await postFileToCrowdin(credentials, crowdinPath, localPath)
181
+ return response
182
+ // return postFileToCrowdin(credentials, crowdinPath, localPath).pipe(
183
+ // catchError(error => {
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')
191
+ // return throwError(error);
192
+ // }),
193
+ // tap({
194
+ // next: response => {
195
+ // if (response.data) {
196
+ // console.log(`${chalk.green.bold('Translations uploaded to Crowdin')} ${chalk.yellow.bold(`[${crowdinPath}]`)}`);
197
+ // } else {
198
+ // console.error('Unexpected result');
199
+ // console.log(response);
200
+ // }
201
+ // },
202
+ // error: error => {
203
+ // const crowdinError = error.response && error.response.data && error.response.data.error;
197
204
 
198
- if (crowdinError && crowdinError.code === CROWDIN_DIRECTORY_NOT_FOUND_ERROR_CODE) {
199
- console.error(`\n${chalk.red.bold('Directory does not exist')} ${chalk.yellow.bold(`[${crowdinPath}]`)}`);
200
- console.log('Create the directory in the Crowdin panel first.');
201
- } else if (error.response.status === HTTP_UNAUTHORIZED_STATUS_CODE) {
202
- console.error(`\n${chalk.red.bold('Provided Crowdin credentials are incorrect')}`);
203
- console.log(`Check the ${chalk.default.yellow('dev.private.json')} file.`);
204
- } else {
205
- console.error('\nUnexpected error:');
206
- console.error(error);
207
- }
208
- }
209
- })
210
- ).toPromise();
205
+ // if (crowdinError && crowdinError.code === CROWDIN_DIRECTORY_NOT_FOUND_ERROR_CODE) {
206
+ // console.error(`\n${chalk.red.bold('Directory does not exist')} ${chalk.yellow.bold(`[${crowdinPath}]`)}`);
207
+ // console.log('Create the directory in the Crowdin panel first.');
208
+ // } else if (error.response.status === HTTP_UNAUTHORIZED_STATUS_CODE) {
209
+ // console.error(`\n${chalk.red.bold('Provided Crowdin credentials are incorrect')}`);
210
+ // console.log(`Check the ${chalk.default.yellow('dev.private.json')} file.`);
211
+ // } else {
212
+ // console.error('\nUnexpected error:');
213
+ // console.error(error);
214
+ // }
215
+ // }
216
+ // })
217
+ // ).toPromise();
211
218
  }
212
219
 
213
220
  function updateCrowdinFile(credentials, storageId, fileId) {
221
+ console.log('storageId', storageId);
222
+ console.log('fileId', fileId);
214
223
  const url = `${CROWDIN_PROJECT_URL}/files/${fileId}`;
215
224
  const AuthStr = 'Bearer '.concat(credentials.token);
216
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-dev3",
3
+ "version": "1.11.4-dev30",
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
+ "buffer": "^6.0.3",
32
33
  "chalk": "2.4.1",
33
34
  "command-line-args": "5.0.2",
34
35
  "fs": "0.0.1-security",