@enplug/scripts 1.11.4-dev5 → 1.11.4-dev51

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 path = require('path');
4
5
  const FormData = require('form-data');
5
6
  const inquirer = require('inquirer');
6
7
  const loadDevPrivateFile = require('../loadDevPrivateFile');
@@ -13,9 +14,7 @@ const { catchError, switchMap, tap, filter } = require('rxjs/operators');
13
14
  const HTTP_UNAUTHORIZED_STATUS_CODE = 401;
14
15
  const HTTP_NOT_FOUND_STATUS_CODE = 404;
15
16
 
16
- // const CROWDIN_PROJECT_ID = 'enplug';
17
17
  const CROWDIN_PROJECT_ID = '401630';
18
- // const CROWDIN_PROJECT_URL = `https://api.crowdin.com/api/project/${CROWDIN_PROJECT_ID}`;
19
18
  const CROWDIN_PROJECT_URL = `https://api.crowdin.com/api/v2/projects/${CROWDIN_PROJECT_ID}`;
20
19
  const CROWDIN_STORAGE_URL = `https://api.crowdin.com/api/v2/storages`;
21
20
  const CROWDIN_DIRECTORY_NOT_FOUND_ERROR_CODE = 17;
@@ -108,110 +107,72 @@ function getCrowdinConfig() {
108
107
  return pkg.config.crowdin;
109
108
  }
110
109
 
111
- function generateFormData(crowdinPath, localPath) {
112
- const formData = new FormData();
113
-
114
- // 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');
118
- return formData;
119
- }
120
-
121
- function postFileToCrowdin(operation, credentials, crowdinPath, localPath) {
122
- const AuthStr = 'Bearer '.concat(credentials.token);
123
- const formData = generateFormData(crowdinPath, localPath);
124
-
125
- const url = `${CROWDIN_STORAGE_URL}`;
126
-
127
- return from(axios.post(url, formData, { headers: {
128
- "Authorization": AuthStr,
129
- "Content-Type": "application/json",
130
- "Crowdin-API-FileName": "en.json"
131
- } }));
132
- }
133
-
134
- async function findCrowdinAppDirectoryId(credentials, crowdinPath) {
135
- path = crowdinPath.startsWith('/') ? crowdinPath.substr(1) : crowdinPath;
136
- if(path.startsWith('apps')){
137
- const appName = path.split('/')[1];
138
- const url = `${CROWDIN_PROJECT_URL}/directories?filter=${appName}`;
139
- const AuthStr = 'Bearer '.concat(credentials.token);
140
- const directories = await axios.get(url, { headers: { Authorization: AuthStr } });
141
- console.log('directories', directories.data.data);
142
- console.log('appName', appName);
143
- return directories.data.data.find(d => d.data.name === appName).id;
144
- } else if(path.startsWith('dashboard')) {
145
- const url = `${CROWDIN_PROJECT_URL}/directories?filter=dashboard`;
146
- const AuthStr = 'Bearer '.concat(credentials.token);
147
- const directories = axios.get(url, { headers: { Authorization: AuthStr } });
148
- console.log('directories', directories.data.data)
149
-
150
- return directories.data.data.find(d => d.data.name === 'dashboard' && d.data.title === 'Dashboard' && d.data.path === '/dashboard').id;
110
+ async function findCrowdinAppDirectoryId(credentials, crowdinPath, appName) {
111
+ try {
112
+ if(crowdinPath.startsWith('apps')){
113
+ const url = `${CROWDIN_PROJECT_URL}/directories?filter=${appName}`;
114
+ const AuthStr = 'Bearer '.concat(credentials.token);
115
+ const resp = await axios.get(url, { headers: { Authorization: AuthStr } });
116
+ const directory = resp.data.data.find(d => d.data.name === appName );
117
+ return directory ? directory.data.id : undefined;
118
+ } else if(crowdinPath.startsWith('dashboard')) {
119
+ const url = `${CROWDIN_PROJECT_URL}/directories?filter=dashboard`;
120
+ const AuthStr = 'Bearer '.concat(credentials.token);
121
+ const resp = await axios.get(url, { headers: { Authorization: AuthStr } });
122
+ const directory = resp.data.data.find(d => d.data.name === 'dashboard' && d.data.title === 'Dashboard' && d.data.path === '/dashboard');
123
+ return directory ? directory.data.id : undefined;
124
+ }
125
+ } catch (err) {
126
+ if (err.response.status === HTTP_UNAUTHORIZED_STATUS_CODE) {
127
+ console.error(`\n${chalk.red.bold('Provided Crowdin token is invalid')}`);
128
+ console.log(`Check the ${chalk.default.yellow('dev.private.json')} file.`);
129
+ return throwError(err);
130
+ }
151
131
  }
152
132
  }
153
133
 
154
134
  async function findCrowdinAppSubFolderId(credentials, directoryId, subfolderName) {
155
135
  const url = `${CROWDIN_PROJECT_URL}/directories?directoryId=${directoryId}`;
156
136
  const AuthStr = 'Bearer '.concat(credentials.token);
157
- const directories = await axios.get(url, { headers: { Authorization: AuthStr } });
158
- console.log('subdirectories', directories.data.data)
159
-
160
- return directories.data.data.find(d => d.data.name === subfolderName).id;
137
+ const resp = await axios.get(url, { headers: { Authorization: AuthStr } });
138
+ const subdirectory = resp.data.data.find(d => d.data.name === subfolderName);
139
+ return subdirectory ? subdirectory.data.id : undefined;
161
140
  }
162
141
 
163
142
  async function getFileIdIfExists(credentials, directoryId, fileName) {
164
143
  const url = `${CROWDIN_PROJECT_URL}/files?directoryId=${directoryId}`;
165
144
  const AuthStr = 'Bearer '.concat(credentials.token);
166
- const files = await axios.get(url, { headers: { Authorization: AuthStr } });
167
- return files.data.find(d => d.data.name === fileName).id;
145
+ const resp = await axios.get(url, { headers: { Authorization: AuthStr } });
146
+ const file = resp.data.data.find(d => d.data.name === fileName);
147
+ return file ? file.data.id : undefined;
148
+ }
149
+
150
+ function postFileToCrowdin(credentials, localPath) {
151
+ const AuthStr = 'Bearer '.concat(credentials.token);
152
+ const file = fs.readFileSync(localPath);
153
+ const url = `${CROWDIN_STORAGE_URL}`;
154
+
155
+ return from(axios.post(url, file, { headers: {
156
+ "Authorization": AuthStr,
157
+ "Content-Type": 'application/json',
158
+ "Crowdin-API-FileName": 'en.json'
159
+ } }));
168
160
  }
169
161
 
170
- async function uploadFileToCrowdinStorage(credentials, crowdinPath, localPath) {
162
+ async function uploadFileToCrowdinStorage(credentials, localPath) {
171
163
  if (!fs.existsSync(localPath)) {
172
164
  console.error(`${chalk.red.bold('Local file does not exists')} ${chalk.yellow.bold(`[${localPath}]`)}`);
173
165
  throw new Error('Local file does not exist');
174
166
  }
175
-
176
- return postFileToCrowdin('update', credentials, crowdinPath, localPath).pipe(
167
+ return postFileToCrowdin(credentials, localPath).pipe(
177
168
  catchError(error => {
178
- if (error.response.status === HTTP_NOT_FOUND_STATUS_CODE) {
179
- return promptAddFile(crowdinPath).pipe(
180
- filter(({addFileConfirm}) => addFileConfirm === true),
181
- switchMap(() => postFileToCrowdin('add', credentials, crowdinPath, localPath))
182
- );
183
- }
184
-
169
+ console('ERROR could not upload file to crowdin storage')
185
170
  return throwError(error);
186
171
  }),
187
- tap({
188
- next: response => {
189
- if (response.data) {
190
- console.log(`${chalk.green.bold('Translations uploaded to Crowdin')} ${chalk.yellow.bold(`[${crowdinPath}]`)}`);
191
- } else {
192
- console.error('Unexpected result');
193
- console.log(response);
194
- }
195
- },
196
- error: error => {
197
- const crowdinError = error.response && error.response.data && error.response.data.error;
198
-
199
- if (crowdinError && crowdinError.code === CROWDIN_DIRECTORY_NOT_FOUND_ERROR_CODE) {
200
- console.error(`\n${chalk.red.bold('Directory does not exist')} ${chalk.yellow.bold(`[${crowdinPath}]`)}`);
201
- console.log('Create the directory in the Crowdin panel first.');
202
- } else if (error.response.status === HTTP_UNAUTHORIZED_STATUS_CODE) {
203
- console.error(`\n${chalk.red.bold('Provided Crowdin credentials are incorrect')}`);
204
- console.log(`Check the ${chalk.default.yellow('dev.private.json')} file.`);
205
- } else {
206
- console.error('\nUnexpected error:');
207
- console.error(error);
208
- }
209
- }
210
- })
211
172
  ).toPromise();
212
173
  }
213
174
 
214
- function updateCrowdinFile(credentials, storageId, fileId) {
175
+ async function updateCrowdinFile(credentials, crowdinPath, storageId, fileId) {
215
176
  const url = `${CROWDIN_PROJECT_URL}/files/${fileId}`;
216
177
  const AuthStr = 'Bearer '.concat(credentials.token);
217
178
  const body = {
@@ -226,7 +187,21 @@ function updateCrowdinFile(credentials, storageId, fileId) {
226
187
  },
227
188
  "replaceModifiedContext": false
228
189
  }
229
- return axios.put(url, body, { headers: { Authorization: AuthStr } });
190
+ return from(axios.put(url, body, { headers: { Authorization: AuthStr } })).pipe(tap({
191
+ next: response => {
192
+ if (response.data) {
193
+ console.log(`${chalk.green.bold('Translations uploaded to Crowdin')} ${chalk.yellow.bold(`[${crowdinPath}]`)}`);
194
+ } else {
195
+ console.error('Unexpected result');
196
+ console.log(response);
197
+ }
198
+ },
199
+ error: error => {
200
+ const crowdinError = error.response && error.response.data && error.response.data.error;
201
+ console.error('\nUnexpected error:');
202
+ console.error(error);
203
+ }
204
+ })).toPromise();
230
205
  }
231
206
 
232
207
  function fetchFileFromCrowdin(credentials, crowdinPath, language) {
@@ -61,21 +61,45 @@ async function syncTranslations(s3Client, bucket) {
61
61
  await uploadTranslationToS3(s3Client, bucket, s3EnPath, enTranslation);
62
62
 
63
63
  if (isProduction) {
64
- const appDirectoryId = await findCrowdinAppDirectoryId(credentials, config.crowdinPath);
65
- console.log('found directoryId', appDirectoryId);
66
- if(config.crowdinPath.includes('apps')) {
67
- const path = crowdinPath.startsWith('/') ? crowdinPath.substr(1) : crowdinPath;
68
- const subfolderName = path.split('/')[2];
64
+ const crowdinPath = config.crowdinPath.startsWith('/') ? config.crowdinPath.substr(1) : config.crowdinPath;
65
+ const crowdinPathSections = crowdinPath.split('/');
66
+ const appName = crowdinPathSections[1];
67
+ const appDirectoryId = await findCrowdinAppDirectoryId(credentials, crowdinPath, appName);
68
+ if(appDirectoryId === undefined) {
69
+ console.error(`\n${chalk.red.bold('Directory does not exist')} ${chalk.yellow.bold(`[${appName}]`)}`);
70
+ console.log('Create the directory in the Crowdin panel first.');
71
+ return;
72
+ }
73
+ if(crowdinPathSections[0] === 'apps' && appDirectoryId) {
74
+ if(crowdinPathSections.length !== 4 && crowdinPathSections[3] !== 'en.json') {
75
+ console.error(`\n${chalk.red.bold('Crowdin path provided does not match defined format, make sure the path is as follows /apps/{app-id}/dashboard/en.json or /apps/{app-id}/app/en.json')}`);
76
+ }
77
+ const subfolderName = crowdinPathSections[2]; //dashboard or app folder
69
78
  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)
79
+ if(folderId === undefined) {
80
+ console.error(`Could not find ${chalk.yellow.bold(`[${subfolderName}]`)} folder`);
81
+ }
82
+ const fileId = await getFileIdIfExists(credentials, folderId, crowdinPathSections[3]); // en.json file id to update
74
83
  if(fileId) {
75
- await updateCrowdinFile(credentials, storageId, fileId);
84
+ const storageId = await uploadFileToCrowdinStorage(credentials, config.localPath);
85
+ if(fileId && storageId) {
86
+ const response = await updateCrowdinFile(credentials, crowdinPath, storageId.data.data.id, fileId);
87
+ }
88
+ } else {
89
+ //TODO add file
76
90
  }
77
- } else {
78
- // TODO Update dashboard transtlations file
91
+ } else if(crowdinPathSections[0] === 'dashboard' && appDirectoryId) {
92
+ if(crowdinPathSections[1] === 'en.json') {
93
+ const fileId = await getFileIdIfExists(credentials, appDirectoryId, crowdinPathSections[1]); // en.json file id to update
94
+ if(fileId) {
95
+ const storageId = await uploadFileToCrowdinStorage(credentials, config.localPath);
96
+ if(fileId && storageId) {
97
+ const response = await updateCrowdinFile(credentials, crowdinPath, storageId.data.data.id, fileId);
98
+ }
99
+ } else {
100
+ console.error(`Could not upload ${chalk.yellow.bold(`[${rowdinPathSections[1]}]`)}. Only en.json supported.`);
101
+ }
102
+ } // TODO else for data-connector, regions-map and uploader
79
103
  }
80
104
  // await uploadFileToCrowdin(credentials, config.crowdinPath, config.localPath);
81
105
  // const { data: fakeTranslation } = await fetchFileFromCrowdin(credentials, config.crowdinPath, FAKE_IN_CONTEXT_LANGUAGE);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@enplug/scripts",
3
- "version": "1.11.4-dev5",
3
+ "version": "1.11.4-dev51",
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",