@enplug/scripts 1.11.4-dev4 → 1.11.4-dev40

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');
@@ -108,62 +109,48 @@ function getCrowdinConfig() {
108
109
  return pkg.config.crowdin;
109
110
  }
110
111
 
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];
112
+ async function findCrowdinAppDirectoryId(credentials, crowdinPath, appName) {
113
+ if(crowdinPath.startsWith('apps')){
138
114
  const url = `${CROWDIN_PROJECT_URL}/directories?filter=${appName}`;
139
115
  const AuthStr = 'Bearer '.concat(credentials.token);
140
- const directories = await axios.get(url, { headers: { Authorization: AuthStr } });
141
- console.log('directories', directories.data.data)
142
- return directories.data.data.find(d => d.data.name === appName).id;
143
- } else if(path.startsWith('dashboard')) {
116
+ const resp = await axios.get(url, { headers: { Authorization: AuthStr } });
117
+ const directory = resp.data.data.find(d => d.data.name === appName );
118
+ return directory ? directory.data.id : undefined;
119
+ } else if(crowdinPath.startsWith('dashboard')) {
144
120
  const url = `${CROWDIN_PROJECT_URL}/directories?filter=dashboard`;
145
121
  const AuthStr = 'Bearer '.concat(credentials.token);
146
- const directories = axios.get(url, { headers: { Authorization: AuthStr } });
147
- console.log('directories', directories.data.data)
148
-
149
- return directories.data.data.find(d => d.data.name === 'dashboard' && d.data.title === 'Dashboard' && d.data.path === '/dashboard').id;
122
+ const resp = axios.get(url, { headers: { Authorization: AuthStr } });
123
+ const directory = resp.data.data.find(d => d.data.name === 'dashboard' && d.data.title === 'Dashboard' && d.data.path === '/dashboard');
124
+ return directory ? directory.data.id : undefined;
150
125
  }
151
126
  }
152
127
 
153
128
  async function findCrowdinAppSubFolderId(credentials, directoryId, subfolderName) {
154
129
  const url = `${CROWDIN_PROJECT_URL}/directories?directoryId=${directoryId}`;
155
130
  const AuthStr = 'Bearer '.concat(credentials.token);
156
- const directories = await axios.get(url, { headers: { Authorization: AuthStr } });
157
- console.log('subdirectories', directories.data.data)
158
-
159
- return directories.data.data.find(d => d.data.name === subfolderName).id;
131
+ const resp = await axios.get(url, { headers: { Authorization: AuthStr } });
132
+ const subdirectory = resp.data.data.find(d => d.data.name === subfolderName);
133
+ return subdirectory ? subdirectory.data.id : undefined;
160
134
  }
161
135
 
162
136
  async function getFileIdIfExists(credentials, directoryId, fileName) {
163
137
  const url = `${CROWDIN_PROJECT_URL}/files?directoryId=${directoryId}`;
164
138
  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;
139
+ const resp = await axios.get(url, { headers: { Authorization: AuthStr } });
140
+ const file = resp.data.data.find(d => d.data.name === fileName);
141
+ return file ? file.data.id : undefined;
142
+ }
143
+
144
+ function postFileToCrowdin(credentials, localPath) {
145
+ const AuthStr = 'Bearer '.concat(credentials.token);
146
+ const file = fs.readFileSync(localPath);
147
+ const url = `${CROWDIN_STORAGE_URL}`;
148
+
149
+ return from(axios.post(url, file, { headers: {
150
+ "Authorization": AuthStr,
151
+ "Content-Type": 'application/json',
152
+ "Crowdin-API-FileName": 'en.json'
153
+ } }));
167
154
  }
168
155
 
169
156
  async function uploadFileToCrowdinStorage(credentials, crowdinPath, localPath) {
@@ -171,18 +158,31 @@ async function uploadFileToCrowdinStorage(credentials, crowdinPath, localPath) {
171
158
  console.error(`${chalk.red.bold('Local file does not exists')} ${chalk.yellow.bold(`[${localPath}]`)}`);
172
159
  throw new Error('Local file does not exist');
173
160
  }
174
-
175
- return postFileToCrowdin('update', credentials, crowdinPath, localPath).pipe(
161
+ return postFileToCrowdin(credentials, localPath).pipe(
176
162
  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
-
163
+ console('ERROR could not upload file to crowdin storage')
184
164
  return throwError(error);
185
- }),
165
+ })
166
+ ).toPromise();
167
+ }
168
+
169
+ function updateCrowdinFile(credentials, storageId, fileId) {
170
+ console.log('fileId', fileId);
171
+ const url = `${CROWDIN_PROJECT_URL}/files/${fileId}`;
172
+ const AuthStr = 'Bearer '.concat(credentials.token);
173
+ const body = {
174
+ "storageId": storageId,
175
+ "updateOption": "keep_translations_and_approvals",
176
+ "importOptions": {
177
+ "contentSegmentation": false,
178
+ "customSegmentation": false
179
+ },
180
+ "exportOptions": {
181
+ "exportPattern": "%locale%.json"
182
+ },
183
+ "replaceModifiedContext": false
184
+ }
185
+ return axios.put(url, body, { headers: { Authorization: AuthStr } }).pipe(
186
186
  tap({
187
187
  next: response => {
188
188
  if (response.data) {
@@ -207,25 +207,7 @@ async function uploadFileToCrowdinStorage(credentials, crowdinPath, localPath) {
207
207
  }
208
208
  }
209
209
  })
210
- ).toPromise();
211
- }
212
-
213
- function updateCrowdinFile(credentials, storageId, fileId) {
214
- const url = `${CROWDIN_PROJECT_URL}/files/${fileId}`;
215
- const AuthStr = 'Bearer '.concat(credentials.token);
216
- const body = {
217
- "storageId": storageId,
218
- "updateOption": "keep_translations_and_approvals",
219
- "importOptions": {
220
- "contentSegmentation": false,
221
- "customSegmentation": false
222
- },
223
- "exportOptions": {
224
- "exportPattern": "%locale%.json"
225
- },
226
- "replaceModifiedContext": false
227
- }
228
- return axios.put(url, body, { headers: { Authorization: AuthStr } });
210
+ );
229
211
  }
230
212
 
231
213
  function fetchFileFromCrowdin(credentials, crowdinPath, language) {
@@ -61,19 +61,33 @@ 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 crowdiPath = config.crowdinPath.startsWith('/') ? config.crowdinPath.substr(1) : config.crowdinPath;
65
+ const crowdinPathSections = crowdiPath.split('/');
66
+ const appName = crowdinPathSections[1];
67
+ const appDirectoryId = await findCrowdinAppDirectoryId(credentials, crowdiPath, appName);
68
+ if(appDirectoryId === undefined) {
69
+ console(`ERROR could not find app ${chalk.yellow.bold(`[${appName}]`)} directory`)
70
+ return throwError(error);
71
+ }
72
+ if(crowdinPathSections[0] === 'apps' && appDirectoryId) {
73
+ if(crowdinPathSections.length !== 4 && crowdinPathSections[3] !== 'en.json') {
74
+ 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')}`);
75
+ }
76
+ const subfolderName = crowdinPathSections[2]; //dashboard or app folder
69
77
  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)
78
+ if(folderId === undefined) {
79
+ console.error(`Could not find ${chalk.yellow.bold(`[${subfolderName}]`)} folder`);
80
+ }
81
+ const fileId = await getFileIdIfExists(credentials, folderId, crowdinPathSections[3]); // en.json file id to update
74
82
  if(fileId) {
75
- await updateCrowdinFile(credentials, storageId, fileId);
83
+ const storageId = await uploadFileToCrowdinStorage(credentials, crowdiPath, config.localPath);
84
+ if(fileId && storageId) {
85
+ await updateCrowdinFile(credentials, storageId.data.data.id, fileId);
86
+ }
87
+ } else {
88
+ //TODO add file
76
89
  }
90
+
77
91
  } else {
78
92
  // TODO Update dashboard transtlations file
79
93
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@enplug/scripts",
3
- "version": "1.11.4-dev4",
3
+ "version": "1.11.4-dev40",
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",