@enplug/scripts 1.11.4-dev37 → 1.11.4-dev39

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.
@@ -109,51 +109,19 @@ function getCrowdinConfig() {
109
109
  return pkg.config.crowdin;
110
110
  }
111
111
 
112
- function generateFormData(crowdinPath, localPath) {
113
-
114
- console.log('localPath', localPath);
115
-
116
- // let filePath = path.join(__dirname, localPath);
117
- // let fileData = fs.readFileSync(filePath);
118
- // console.log('fileData', fileData);
119
- // console.log(fileData.slice(0, 100));
120
- let formData = new FormData();
121
- let file = fs.readFileSync(localPath);
122
- console.log('file', file);
123
- console.log(file.slice(0, 100));
124
- // formData.append('file', file, 'en.json');
125
-
126
- return file;
127
- }
128
-
129
- function postFileToCrowdin(credentials, crowdinPath, localPath) {
130
- const AuthStr = 'Bearer '.concat(credentials.token);
131
- const formData = generateFormData(crowdinPath, localPath);
132
-
133
- const url = `${CROWDIN_STORAGE_URL}`;
134
- // console.log('formData', formData);
135
- return axios.post(url, formData, { headers: {
136
- "Authorization": AuthStr,
137
- "Content-Type": 'application/json',
138
- "Crowdin-API-FileName": 'en.json'
139
- } });
140
- }
141
-
142
- async function findCrowdinAppDirectoryId(credentials, crowdinPath) {
143
- const filepath = crowdinPath.startsWith('/') ? crowdinPath.substr(1) : crowdinPath;
144
- if(filepath.startsWith('apps')){
145
- const appName = filepath.split('/')[1];
112
+ async function findCrowdinAppDirectoryId(credentials, crowdinPath, appName) {
113
+ if(crowdinPath.startsWith('apps')){
146
114
  const url = `${CROWDIN_PROJECT_URL}/directories?filter=${appName}`;
147
115
  const AuthStr = 'Bearer '.concat(credentials.token);
148
116
  const resp = await axios.get(url, { headers: { Authorization: AuthStr } });
149
117
  const directory = resp.data.data.find(d => d.data.name === appName );
150
- return directory.data.id
151
- } else if(filepath.startsWith('dashboard')) {
118
+ return directory ? directory.data.id : undefined;
119
+ } else if(crowdinPath.startsWith('dashboard')) {
152
120
  const url = `${CROWDIN_PROJECT_URL}/directories?filter=dashboard`;
153
121
  const AuthStr = 'Bearer '.concat(credentials.token);
154
122
  const resp = axios.get(url, { headers: { Authorization: AuthStr } });
155
123
  const directory = resp.data.data.find(d => d.data.name === 'dashboard' && d.data.title === 'Dashboard' && d.data.path === '/dashboard');
156
- return directory.data.id;
124
+ return directory ? directory.data.id : undefined;
157
125
  }
158
126
  }
159
127
 
@@ -161,8 +129,8 @@ async function findCrowdinAppSubFolderId(credentials, directoryId, subfolderName
161
129
  const url = `${CROWDIN_PROJECT_URL}/directories?directoryId=${directoryId}`;
162
130
  const AuthStr = 'Bearer '.concat(credentials.token);
163
131
  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;
132
+ const subdirectory = resp.data.data.find(d => d.data.name === subfolderName);
133
+ return subdirectory ? subdirectory.data.id : undefined;
166
134
  }
167
135
 
168
136
  async function getFileIdIfExists(credentials, directoryId, fileName) {
@@ -170,7 +138,19 @@ async function getFileIdIfExists(credentials, directoryId, fileName) {
170
138
  const AuthStr = 'Bearer '.concat(credentials.token);
171
139
  const resp = await axios.get(url, { headers: { Authorization: AuthStr } });
172
140
  const file = resp.data.data.find(d => d.data.name === fileName);
173
- return file.data.id;
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
+ } }));
174
154
  }
175
155
 
176
156
  async function uploadFileToCrowdinStorage(credentials, crowdinPath, localPath) {
@@ -179,44 +159,38 @@ async function uploadFileToCrowdinStorage(credentials, crowdinPath, localPath) {
179
159
  throw new Error('Local file does not exist');
180
160
  }
181
161
 
182
- const response = await postFileToCrowdin(credentials, crowdinPath, localPath)
183
- return response
184
- // return postFileToCrowdin(credentials, crowdinPath, localPath).pipe(
185
- // catchError(error => {
186
- // // if (error.response.status === HTTP_NOT_FOUND_STATUS_CODE) {
187
- // // return promptAddFile(crowdinPath).pipe(
188
- // // filter(({addFileConfirm}) => addFileConfirm === true),
189
- // // switchMap(() => postFileToCrowdin(credentials, crowdinPath, localPath))
190
- // // );
191
- // // }
192
- // console('ERROR could not upload file to crowdin storage')
193
- // return throwError(error);
194
- // }),
195
- // tap({
196
- // next: response => {
197
- // if (response.data) {
198
- // console.log(`${chalk.green.bold('Translations uploaded to Crowdin')} ${chalk.yellow.bold(`[${crowdinPath}]`)}`);
199
- // } else {
200
- // console.error('Unexpected result');
201
- // console.log(response);
202
- // }
203
- // },
204
- // error: error => {
205
- // const crowdinError = error.response && error.response.data && error.response.data.error;
162
+ // const response = await postFileToCrowdin(credentials, crowdinPath, localPath)
163
+ // return response
164
+ return postFileToCrowdin(credentials, localPath).pipe(
165
+ catchError(error => {
166
+ console('ERROR could not upload file to crowdin storage')
167
+ return throwError(error);
168
+ }),
169
+ tap({
170
+ next: response => {
171
+ if (response.data) {
172
+ console.log(`${chalk.green.bold('Translations uploaded to Crowdin')} ${chalk.yellow.bold(`[${crowdinPath}]`)}`);
173
+ } else {
174
+ console.error('Unexpected result');
175
+ console.log(response);
176
+ }
177
+ },
178
+ error: error => {
179
+ const crowdinError = error.response && error.response.data && error.response.data.error;
206
180
 
207
- // if (crowdinError && crowdinError.code === CROWDIN_DIRECTORY_NOT_FOUND_ERROR_CODE) {
208
- // console.error(`\n${chalk.red.bold('Directory does not exist')} ${chalk.yellow.bold(`[${crowdinPath}]`)}`);
209
- // console.log('Create the directory in the Crowdin panel first.');
210
- // } else if (error.response.status === HTTP_UNAUTHORIZED_STATUS_CODE) {
211
- // console.error(`\n${chalk.red.bold('Provided Crowdin credentials are incorrect')}`);
212
- // console.log(`Check the ${chalk.default.yellow('dev.private.json')} file.`);
213
- // } else {
214
- // console.error('\nUnexpected error:');
215
- // console.error(error);
216
- // }
217
- // }
218
- // })
219
- // ).toPromise();
181
+ if (crowdinError && crowdinError.code === CROWDIN_DIRECTORY_NOT_FOUND_ERROR_CODE) {
182
+ console.error(`\n${chalk.red.bold('Directory does not exist')} ${chalk.yellow.bold(`[${crowdinPath}]`)}`);
183
+ console.log('Create the directory in the Crowdin panel first.');
184
+ } else if (error.response.status === HTTP_UNAUTHORIZED_STATUS_CODE) {
185
+ console.error(`\n${chalk.red.bold('Provided Crowdin credentials are incorrect')}`);
186
+ console.log(`Check the ${chalk.default.yellow('dev.private.json')} file.`);
187
+ } else {
188
+ console.error('\nUnexpected error:');
189
+ console.error(error);
190
+ }
191
+ }
192
+ })
193
+ ).toPromise();
220
194
  }
221
195
 
222
196
  function updateCrowdinFile(credentials, storageId, fileId) {
@@ -61,17 +61,34 @@ 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
- if(config.crowdinPath.includes('apps')) {
66
- const path = config.crowdinPath.startsWith('/') ? config.crowdinPath.substr(1) : config.crowdinPath;
67
- 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
68
77
  const folderId = await findCrowdinAppSubFolderId(credentials, appDirectoryId, subfolderName);
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);
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
82
+ if(fileId) {
83
+ const storageId = await uploadFileToCrowdinStorage(credentials, crowdiPath, config.localPath);
84
+ if(fileId && storageId) {
85
+ const response = await updateCrowdinFile(credentials, storageId.data.data.id, fileId);
86
+ console.log('response', response.data);
87
+ }
88
+ } else {
89
+ //TODO add file
74
90
  }
91
+
75
92
  } else {
76
93
  // TODO Update dashboard transtlations file
77
94
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@enplug/scripts",
3
- "version": "1.11.4-dev37",
3
+ "version": "1.11.4-dev39",
4
4
  "description": "Enplug scripts",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1",