@edx/reactifex
Advanced tools
Comparing version
88
main.js
#!/usr/bin/env node | ||
const fs = require("fs"); | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
const glob = require("glob"); | ||
const path = require("path"); | ||
const fs = require("fs"); | ||
var combineInputs = (dir) => { | ||
// expected input: a directory containing subdirs, each of which contains displayMessages.json | ||
var ret = []; | ||
var subDirs = fs.readdirSync(dir); | ||
subDirs.forEach((subdir) => { | ||
var messageFile = path.join(dir, subdir, "displayMessages.json"); | ||
var messages = JSON.parse(fs.readFileSync(messageFile)); | ||
messages.forEach((message) => {ret.push(message);}); | ||
/* | ||
* See the Makefile for how the required hash file is downloaded from Transifex. | ||
*/ | ||
/* | ||
* Expected input: a directory, possibly containing subdirectories, with .json files. Each .json | ||
* file is an array of translation triplets (id, description, defaultMessage). | ||
* | ||
* | ||
*/ | ||
function gatherJson(dir) { | ||
const ret = []; | ||
const files = glob.sync(`${dir}/**/*.json`); | ||
files.forEach((filename) => { | ||
const messages = JSON.parse(fs.readFileSync(filename)); | ||
ret.push(...messages); | ||
}); | ||
return ret; | ||
}; | ||
var inData = combineInputs(process.argv[2]); | ||
} | ||
var outData = {}; | ||
inData.forEach((message) => { | ||
outData[message["id"]] = message["defaultMessage"]; | ||
}); | ||
// the hash file returns ids whose periods are "escaped" (sort of), like this: | ||
// "key": "profile\\.sociallinks\\.social\\.links" | ||
// so our regular messageIds won't match them out of the box | ||
function escapeDots(messageId) { | ||
return messageId.replace(/\./g, "\\."); | ||
} | ||
if (process.argv[3] === "--comments") { | ||
process.stdout.write("generating bash scripts...\n"); | ||
var messageInfo = JSON.parse(fs.readFileSync(__dirname + "/bash_scripts/hashmap.json")); | ||
var dataPath = __dirname + "/bash_scripts/hashed_data.txt"; | ||
fs.writeFileSync(dataPath, ""); | ||
inData.forEach((message) => { | ||
var info = messageInfo.find(mi => mi.key == message.id); | ||
const jsonDir = process.argv[2]; | ||
const messageObjects = gatherJson(jsonDir); | ||
if (messageObjects.length === 0) { | ||
process.exitCode = 1; | ||
throw new Error("Found no messages"); | ||
} | ||
if (process.argv[3] === "--comments") { // prepare to handle the translator notes | ||
const loggingPrefix = path.basename(`${__filename}`); // the name of this JS file | ||
const bashScriptsPath = ( | ||
process.argv[4] && process.argv[4] === "--v3-scripts-path" | ||
? "./node_modules/@edx/reactifex/bash_scripts" | ||
: "./node_modules/reactifex/bash_scripts"); | ||
const hashFile = `${bashScriptsPath}/hashmap.json`; | ||
process.stdout.write(`${loggingPrefix}: reading hash file ${hashFile}\n`); | ||
const messageInfo = JSON.parse(fs.readFileSync(hashFile)); | ||
const outputFile = `${bashScriptsPath}/hashed_data.txt`; | ||
process.stdout.write(`${loggingPrefix}: writing to output file ${outputFile}\n`); | ||
fs.writeFileSync(outputFile, ""); | ||
messageObjects.forEach((message) => { | ||
const transifexFormatId = escapeDots(message.id); | ||
const info = messageInfo.find((mi) => mi.key === transifexFormatId); | ||
if (info) { | ||
fs.appendFileSync(dataPath, info.string_hash + "|" + message.description + "\n"); | ||
fs.appendFileSync(outputFile, `${info.string_hash}|${message.description}\n`); | ||
} else { | ||
process.stdout.write("string " + message.id + " does not yet exist on transifex!\n"); | ||
process.stdout.write(`${loggingPrefix}: string ${message.id} does not yet exist on transifex!\n`); | ||
} | ||
}); | ||
} else { | ||
fs.writeFileSync(process.argv[3], JSON.stringify(outData, null, 2)); | ||
const output = {}; | ||
messageObjects.forEach((message) => { | ||
output[message.id] = message.defaultMessage; | ||
}); | ||
fs.writeFileSync(process.argv[3], JSON.stringify(output, null, 2)); | ||
} |
{ | ||
"name": "@edx/reactifex", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "A helper for moving react-intl messages to transifex and back", | ||
"bin": "./main.js", | ||
"bin": { | ||
"edx_reactifex": "./main.js" | ||
}, | ||
"scripts": { | ||
@@ -7,0 +9,0 @@ "lint": "eslint .", |
@@ -8,5 +8,4 @@ # reactifex | ||
[](reactifex) | ||
[](https://travis-ci.org/efischer19/reactifex) | ||
[](https://github.com/edx/reactifex/actions?query=workflow%3A%22Node+CI%22) | ||
There are two modes of usage - compilation and comment pushing. | ||
@@ -13,0 +12,0 @@ |
18613
6.9%291
10.65%43
-2.27%