diff --git a/.gitignore b/.gitignore index 70a131b..d0a6da7 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ daily .env uploaded-jsons rename.json +delete.json diff --git a/README.md b/README.md index 81f6e46..35963e8 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,12 @@ Output example: * if you want to upload into different groups then the uploader will do it in parallel requests. __This MAY KILL the cms api__. +### delete + +`npm run delete` - use the _./delete.json_ file. + +* read label keys from delete.json and try to delete all labels + ### get-everything `npm run get-everything` - download all defined groups. diff --git a/delete.json.exmple b/delete.json.exmple new file mode 100644 index 0000000..515dfa0 --- /dev/null +++ b/delete.json.exmple @@ -0,0 +1,6 @@ +[ + { + "ClassId": "WizzBookingFlowResources", + "Keys": ["demo-pass-enter-email", "demo-pass-buy-promo-line-1", "demo-pass-buy-promo-line-2"] + } +] diff --git a/package.json b/package.json index cef298f..beab7c9 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "get": "node src/get.js", "post": "node src/post.js", "rename": "cross-env OPERATION=rename node src/post.js", + "delete": "cross-env OPERATION=delete node src/post.js", "get-everything": "shx rm -rf output && shx mkdir output && node src/get-everything", "daily-backup": "node src/get-everything daily all-langs", "postinstall": "shx mkdir -p daily", diff --git a/src/post.js b/src/post.js index 6e992e0..93e0886 100644 --- a/src/post.js +++ b/src/post.js @@ -8,13 +8,21 @@ const config = require('./config'); const uris = config.api.split(',').map(s => s + '/labels'); const rename = process.env.OPERATION === 'rename'; +const isDeleteKeys = process.env.OPERATION === 'delete'; const backupDir = 'uploaded-jsons'; -const fileName = process.env.UPLOAD_FILE_NAME || (rename ? 'rename.json' : 'upload.json'); -const now = (new Date()).toISOString().substring(0, 19).replace('T', '_').replace(/:/g, '-'); -const method = rename ? 'PUT' : 'POST'; +const fileName = + process.env.UPLOAD_FILE_NAME || + (isDeleteKeys ? 'delete.json' : rename ? 'rename.json' : 'upload.json'); +const now = new Date() + .toISOString() + .substring(0, 19) + .replace('T', '_') + .replace(/:/g, '-'); +const method = isDeleteKeys ? 'DELETE' : rename ? 'PUT' : 'POST'; let input = cat(fileName); const inputSize = input.length; + try { input = JSON.parse(input); } catch (err) { @@ -35,10 +43,21 @@ function upload (uri, count) { delete body.TaskId; // not yet implemented in the api const environment = uri.split('.')[1]; console.log(`Uploading to ${environment}.... please wait!`.grey); - return request({method, uri, body, json: true, rejectUnauthorized: false, timeout: config.timeout}) - .then((result) => { - console.log(`Uploaded to ${environment}: ${i + 1}. (out of ${l}) - Result: ${JSON.stringify(result)}`.green.bold); + return request({ + method, + uri, + body, + json: true, + rejectUnauthorized: false, + timeout: config.timeout + }) + .then(result => { + console.log( + `Uploaded to ${environment}: ${i + + 1}. (out of ${l}) - Result: ${JSON.stringify(result)}`.green.bold + ); shelljs.config.silent = true; + if (isDeleteKeys) return; mkdir(backupDir); let target = `${backupDir}/${now}_#${tfsId}_${fileName}`; if (count === 0) { @@ -47,7 +66,11 @@ function upload (uri, count) { } }) .catch(err => { - console.log(`Upload failed to ${environment}: ${i + 1}. [${id}] - Err: ${err.response.statusCode}`.bold.red); + console.log( + `Upload failed to ${environment}: ${i + 1}. [${id}] - Err: ${ + err.response.statusCode + }`.bold.red + ); }); } }