@enplug/scripts 1.11.4-dev3 → 1.11.4-dev31

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,30 @@ 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
+
114
119
  // 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');
120
+ // formData.append('json', '');
121
+ formData.append('file', file, 'en.json');
122
+ // formData.append(`export_patterns[${crowdinPath}]`, '%locale%.json');
118
123
  return formData;
119
124
  }
120
125
 
121
- function postFileToCrowdin(operation, credentials, crowdinPath, localPath) {
126
+ function postFileToCrowdin(credentials, crowdinPath, localPath) {
122
127
  const AuthStr = 'Bearer '.concat(credentials.token);
123
128
  const formData = generateFormData(crowdinPath, localPath);
124
129
 
125
130
  const url = `${CROWDIN_STORAGE_URL}`;
126
-
127
- return from(axios.post(url, formData, { headers: {
131
+ console.log('formData', formData);
132
+ return axios.post(url, formData, { headers: {
128
133
  "Authorization": AuthStr,
129
134
  "Content-Type": "application/json",
130
135
  "Crowdin-API-FileName": "en.json"
131
- } }));
136
+ } });
132
137
  }
133
138
 
134
139
  async function findCrowdinAppDirectoryId(credentials, crowdinPath) {
@@ -137,33 +142,32 @@ async function findCrowdinAppDirectoryId(credentials, crowdinPath) {
137
142
  const appName = path.split('/')[1];
138
143
  const url = `${CROWDIN_PROJECT_URL}/directories?filter=${appName}`;
139
144
  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;
145
+ const resp = await axios.get(url, { headers: { Authorization: AuthStr } });
146
+ const directory = resp.data.data.find(d => d.data.name === appName );
147
+ return directory.data.id
143
148
  } else if(path.startsWith('dashboard')) {
144
149
  const url = `${CROWDIN_PROJECT_URL}/directories?filter=dashboard`;
145
150
  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;
151
+ const resp = axios.get(url, { headers: { Authorization: AuthStr } });
152
+ const directory = resp.data.data.find(d => d.data.name === 'dashboard' && d.data.title === 'Dashboard' && d.data.path === '/dashboard');
153
+ return directory.data.id;
150
154
  }
151
155
  }
152
156
 
153
157
  async function findCrowdinAppSubFolderId(credentials, directoryId, subfolderName) {
154
158
  const url = `${CROWDIN_PROJECT_URL}/directories?directoryId=${directoryId}`;
155
159
  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;
160
+ const resp = await axios.get(url, { headers: { Authorization: AuthStr } });
161
+ const subdirectory = resp.data.data.find(d => d.data.name === subfolderName)
162
+ return subdirectory.data.id;
160
163
  }
161
164
 
162
165
  async function getFileIdIfExists(credentials, directoryId, fileName) {
163
166
  const url = `${CROWDIN_PROJECT_URL}/files?directoryId=${directoryId}`;
164
167
  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;
168
+ const resp = await axios.get(url, { headers: { Authorization: AuthStr } });
169
+ const file = resp.data.data.find(d => d.data.name === fileName);
170
+ return file.data.id;
167
171
  }
168
172
 
169
173
  async function uploadFileToCrowdinStorage(credentials, crowdinPath, localPath) {
@@ -172,45 +176,49 @@ async function uploadFileToCrowdinStorage(credentials, crowdinPath, localPath) {
172
176
  throw new Error('Local file does not exist');
173
177
  }
174
178
 
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;
179
+ const response = await postFileToCrowdin(credentials, crowdinPath, localPath)
180
+ return response
181
+ // return postFileToCrowdin(credentials, crowdinPath, localPath).pipe(
182
+ // catchError(error => {
183
+ // // if (error.response.status === HTTP_NOT_FOUND_STATUS_CODE) {
184
+ // // return promptAddFile(crowdinPath).pipe(
185
+ // // filter(({addFileConfirm}) => addFileConfirm === true),
186
+ // // switchMap(() => postFileToCrowdin(credentials, crowdinPath, localPath))
187
+ // // );
188
+ // // }
189
+ // console('ERROR could not upload file to crowdin storage')
190
+ // return throwError(error);
191
+ // }),
192
+ // tap({
193
+ // next: response => {
194
+ // if (response.data) {
195
+ // console.log(`${chalk.green.bold('Translations uploaded to Crowdin')} ${chalk.yellow.bold(`[${crowdinPath}]`)}`);
196
+ // } else {
197
+ // console.error('Unexpected result');
198
+ // console.log(response);
199
+ // }
200
+ // },
201
+ // error: error => {
202
+ // const crowdinError = error.response && error.response.data && error.response.data.error;
197
203
 
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();
204
+ // if (crowdinError && crowdinError.code === CROWDIN_DIRECTORY_NOT_FOUND_ERROR_CODE) {
205
+ // console.error(`\n${chalk.red.bold('Directory does not exist')} ${chalk.yellow.bold(`[${crowdinPath}]`)}`);
206
+ // console.log('Create the directory in the Crowdin panel first.');
207
+ // } else if (error.response.status === HTTP_UNAUTHORIZED_STATUS_CODE) {
208
+ // console.error(`\n${chalk.red.bold('Provided Crowdin credentials are incorrect')}`);
209
+ // console.log(`Check the ${chalk.default.yellow('dev.private.json')} file.`);
210
+ // } else {
211
+ // console.error('\nUnexpected error:');
212
+ // console.error(error);
213
+ // }
214
+ // }
215
+ // })
216
+ // ).toPromise();
211
217
  }
212
218
 
213
219
  function updateCrowdinFile(credentials, storageId, fileId) {
220
+ console.log('storageId', storageId);
221
+ console.log('fileId', fileId);
214
222
  const url = `${CROWDIN_PROJECT_URL}/files/${fileId}`;
215
223
  const AuthStr = 'Bearer '.concat(credentials.token);
216
224
  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-dev31",
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",