Comparing version 0.0.2 to 1.0.0
40
main.js
#!/usr/bin/env node | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const path = require("path"); | ||
const fs = require("fs"); | ||
combineInputs = (dir) => { | ||
var combineInputs = (dir) => { | ||
// expected input: a directory containing subdirs, each of which contains displayMessages.json | ||
var ret = []; | ||
subDirs = fs.readdirSync(dir); | ||
var subDirs = fs.readdirSync(dir); | ||
subDirs.forEach((subdir) => { | ||
messageFile = path.join(dir, subdir, 'displayMessages.json'); | ||
messages = JSON.parse(fs.readFileSync(messageFile)); | ||
var messageFile = path.join(dir, subdir, "displayMessages.json"); | ||
var messages = JSON.parse(fs.readFileSync(messageFile)); | ||
messages.forEach((message) => {ret.push(message);}); | ||
}); | ||
return ret; | ||
} | ||
}; | ||
var inData = combineInputs(process.argv[2]); | ||
var outData = {} | ||
var outData = {}; | ||
inData.forEach((message) => { | ||
outData[message["id"]] = message["defaultMessage"]; | ||
}); | ||
fs.writeFileSync(process.argv[3], JSON.stringify(outData, null, 2)); | ||
if (process.argv[3] === "--comments") { | ||
process.stdout.write("generating bash scripts...\n"); | ||
var messageInfo = JSON.parse(fs.readFileSync(__dirname + "/bash_scripts/hashmap.json")); | ||
var scriptPath = __dirname + "/bash_scripts/put_comments.sh"; | ||
fs.writeFileSync(scriptPath, "#!/bin/bash\n"); | ||
fs.writeFileSync(scriptPath, "set -x\n"); | ||
inData.forEach((message) => { | ||
var info = messageInfo.find(mi => mi.key == message.id); | ||
if (info) { | ||
var header = " -H \"Content-Type: application/json\""; | ||
var escapedDescription = message.description.replace(/(["])/g, "\\\\\\$1"); | ||
var data = " --data \"{\\\"comment\\\": \\\"" + escapedDescription + "\\\"}\""; | ||
var url = " $1" + info.string_hash; | ||
fs.appendFileSync(scriptPath, "curl -L -w \"\\n\" --user $SECRET_USER:$SECRET_PWD -X PUT" + header + data + url + "\n"); | ||
} else { | ||
process.stdout.write("string " + message.id + " does not yet exist on transifex!\n"); | ||
} | ||
}); | ||
fs.writeFileSync(scriptPath, "set +x\n"); | ||
} else { | ||
fs.writeFileSync(process.argv[3], JSON.stringify(outData, null, 2)); | ||
} |
{ | ||
"name": "reactifex", | ||
"version": "0.0.2", | ||
"version": "1.0.0", | ||
"description": "A helper for moving react-intl messages to transifex and back", | ||
"bin": "./main.js", | ||
"scripts": { | ||
"lint": "eslint .", | ||
"test": "node main.js test_data/input/ test_data/test_output.json && diff test_data/test_output.json test_data/expected_output/messages.json" | ||
@@ -24,3 +25,6 @@ }, | ||
}, | ||
"homepage": "https://github.com/efischer19/reactifex#readme" | ||
"homepage": "https://github.com/efischer19/reactifex#readme", | ||
"devDependencies": { | ||
"eslint": "^4.18.2" | ||
} | ||
} |
# reactifex | ||
Helper utility designed to make it easy to upload react-intl extracted messages to transifex, with support for ICU plurals and translator comments. | ||
[![dependencies](https://img.shields.io/badge/dependencies-none-brightgreen.svg)](reactifex) | ||
@@ -8,4 +10,9 @@ [![license](https://img.shields.io/npm/l/reactifex.svg)](reactifex) | ||
Helper utility designed to make it easy to upload react-intl extracted messages to transifex, with support for ICU plurals and translator comments. | ||
There are two modes of usage - compilation and comment pushing. | ||
## Compilation mode | ||
In this mode, messages that have been extracted by `babel-plugin-react-intl` (into individual files) are combined. The resulting json is suitable to upload to Transifex and matches their specified `KEYVALUEJSON` format. | ||
usage: `$(npm bin)/reactifex <input_folder> <output_file>` | ||
@@ -15,1 +22,26 @@ - `input_folder` corresponds to the `messagesDir` option used by `babel-plugin-react-intl` | ||
- `output_file` will be suitable for upload to transifex | ||
## Comment pushing | ||
This mode is why I wrote this library in the first place - I wanted the ability to use comments as `PO` files do, but none of the tools I found to convert react-intl messages to `PO` files were able to properly handle ICU pluralization. By keeping everything in a js context with `KEYVALUEJSON`, plurals work correctly *and* we now have comment support for translators (by default, Transifex's `KEYVALUEJSON` file format does not allow for comments to be included with strings for translation). | ||
Note that tests for this mode aren't included, as it relies on Transifex's API being up and responsive. | ||
Usage is a little complicated, I'm sorry about that; you're going to be running this server-side as a series of bash commands. Do note that I assume `$SECRET_USER` and `$SECRET_PWD` env vars exist for basic auth purposes. See [Transifex's API Introduction](https://docs.transifex.com/api/introduction) for more details on authentication. Here's an example, written as it would be in the Makefile of a project that makes use of reactifex: | ||
``` | ||
tx_url1 = https://www.transifex.com/api/2/project/<project>/resource/<resource>/translation/<default_language_code>/strings/ | ||
tx_url2 = https://www.transifex.com/api/2/project/<project>/resource/<resource>/source/ | ||
push_translations: | ||
./node_modules/reactifex/bash_scripts/get_hashed_strings.sh $(tx_url1) | ||
$$(npm bin)/reactifex <input_folder> --comments | ||
./node_modules/reactifex/bash_scripts/put_comments.sh $(tx_url2) | ||
``` | ||
- [tx_url1](https://docs.transifex.com/api/translation-strings#identifying-strings-using-hashes) and [tx_url2](https://docs.transifex.com/api/resource-strings) are just variables as defined by the Transifex API documentation, extracted for readability. | ||
- First, `bash_scripts/get_hashed_strings.sh` is called with a url argument. This will populate `bash_scripts/hashmap.json` with data about the strings in your resource, including the all-important `string_hash`. | ||
- Next, the main reactifex script (node js) runs with an additional `--comments` flag, and no output file. This has the effect of gathering up all your `babel-plugin-react-intl` extracted messages *with* their comments attached. From there, it's simple enough to match up each message with its `string_hash`, and makes it possible to generate `bash_scripts/put_comments.sh` (a series of curl requests, one per message) | ||
- Finally, `bash_scripts/put_comments.sh` is run with the base PUT url as an argument (we generated the specific `string_hash` portion in the previous step), updating translator comments for each message on Transifex via their API. |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
8682
12
91
0
46
1