Skip to content

Commit

Permalink
Set obsolete tag if strings doesn't exist in strings from code
Browse files Browse the repository at this point in the history
  • Loading branch information
Mavrin committed Nov 13, 2015
1 parent 00e0e9a commit fcb60d5
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 36 deletions.
20 changes: 3 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var Promise = require('bluebird');
var utils = require('./lib/utils');
var generateHash = utils.generateHash;
var mergeStrings = utils.mergeStrings;
var applyTagsToStrings = utils.applyTagsToStrings;
/**
*
* @param {{login:String, password:String, projectSlug: String, resourceSlug: String, skipTags: Array[String], obsoleteTag:String, requestConcurrency: Number, stringWillRemove:{tags:Array[String]}}} config
Expand Down Expand Up @@ -93,13 +94,7 @@ var transifex = function (config) {
};

var removeStringsWithCertainTags = function (strings, tags) {
var content = _.reduce(strings, function (content, item) {
if (_.contains.apply(_, [item.tags || []].concat(tags))) {
return content;
}
content[item.token] = item.token;
return content;
}, {});
var content = utils.removeStringsWithCertainTags(strings, tags);
var url = `project/${resourceFile}content/`;
return makeRequest(url, 'PUT', {content: JSON.stringify(content)})
};
Expand All @@ -115,16 +110,7 @@ var transifex = function (config) {
}).then(function (res) {
return Promise.all([getResourceStrings(res[1].updateStrings), res[1].obsoleteStrings]);
}).then(function (res) {
return _.map(_.compact(res[0]), function (string) {
var token = string.token;
_.each(dictionaries, function (dictionary, scope) {
if (dictionary[token]) {
var tags = _.chain((string.tags || []).concat(scope)).compact().uniq().value();
string.tags = _.difference(tags, config.skipTags);
}
});
return string;
})
return applyTagsToStrings(dictionaries,res[0], res[1], config)
}).then(function (strings) {
return putResourceStrings(strings);
}).then(function (strings) {
Expand Down
13 changes: 10 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = {
string.tags = _.difference(tags, config.skipTags);
} else {
if(_.contains(obsoleteStrings, token)) {
tags = _.chain((string.tags || []).concat(scope)).compact().uniq().value();
tags = _.chain((string.tags || [])).compact().uniq().value();
tags.push(obsoleteTag);
tags =_.uniq(tags);
string.tags = _.difference(tags, config.skipTags);
Expand All @@ -40,7 +40,14 @@ module.exports = {
});
return strings;
},
removeStringsWithCertainTags: function () {

removeStringsWithCertainTags: function (strings, tags) {
var content = _.reduce(strings, function (content, item) {
if (_.contains.apply(_, [item.tags || []].concat(tags))) {
return content;
}
content[item.token] = item.token;
return content;
}, {});
return content;
}
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tau-transifex",
"version": "0.8.0",
"version": "0.9.0",
"description": "Simple API for request to transifex REST API",
"main": "index.js",
"scripts": {
Expand Down
49 changes: 49 additions & 0 deletions test/fixtures/expectedTags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module.exports = [
{
"comment": "",
"character_limit": null,
"tags": [
"none"
],
"token": "deep nested message"
},
{
"comment": "",
"character_limit": null,
"tags": [
"none"
],
"token": "test1"
},
{
"comment": "",
"character_limit": null,
"tags": [
"obsolete"
],
"token": "test"
},
{
"comment": "",
"character_limit": null,
"tags": [
"test",
"obsolete"
],
"token": "test2"
},
{
"comment": "",
"character_limit": null,
"tags": [
"custom_js_scope"
],
"token": "custom js scope"
},
{
"comment": "",
"character_limit": null,
"tags": [],
"token": "remove"
}
];
File renamed without changes.
File renamed without changes.
File renamed without changes.
35 changes: 20 additions & 15 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var utils = require('../lib/utils');
var test = require('tape');
var newStings = require('./fextures/newStrings');
var transifexStrings = require('./fextures/transifexStrings');
var newStings = require('./fixtures/newStrings');
var transifexStrings = require('./fixtures/transifexStrings');
var stringsWithTags =
[
{
Expand All @@ -26,6 +26,12 @@ var stringsWithTags =
"tags": null,
"token": "test"
},
{
"comment": "",
"character_limit": null,
"tags": ['test'],
"token": "test2"
},
{
"comment": "",
"character_limit": null,
Expand Down Expand Up @@ -75,21 +81,20 @@ test('merge strings', function (assert) {
});

test('apply tags', function (assert) {
assert.deepEqual(
utils.applyTagsToStrings(newStings, stringsWithTags, ['test'], {
assert.deepEquals(
utils.applyTagsToStrings(newStings, stringsWithTags, ['test', 'test2'], {
obsoleteTag: 'obsolete',
skipTags: ['remove']
}),
{
obsoleteStrings: ['test'],
updateStrings: {
'custom js scope': 'custom js scope',
'deep nested message': 'deep nested message',
remove: 'remove',
test: 'test',
test1: 'test1'
}
}
require('./fixtures/expectedTags')
);
assert.end();
});
});

test('remove strings with tags', function (assert) {
assert.deepEquals(
utils.removeStringsWithCertainTags(stringsWithTags, ['none']),
{'custom js scope': 'custom js scope', remove: 'remove', test: 'test', test2: 'test2'}
);
assert.end();
});

0 comments on commit fcb60d5

Please sign in to comment.