Skip to content

Instantly share code, notes, and snippets.

@holylander
Last active November 20, 2020 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save holylander/9d7fe178b2e50307bd0a89f180f92c47 to your computer and use it in GitHub Desktop.
Save holylander/9d7fe178b2e50307bd0a89f180f92c47 to your computer and use it in GitHub Desktop.
spfx localized strings Jest testing workaround
//just save a copy of this script under ./scripts/
const fs = require("fs");
/** returns an arrays with as many indexes as "target" appeards on input */
const findIndexes = (input, target) => {
const indexes = [];
var currentIndex = 0;
while (currentIndex < input.length) {
currentIndex = input.indexOf(target, currentIndex)
if (currentIndex >= 0) {
indexes.push(currentIndex)
currentIndex++
}
else {
currentIndex = input.length
}
}
return indexes
}
const config = JSON.parse(fs.readFileSync("./config/config.json").toString());
const targetLang = "en-us"
/** create a folder in node_modules, with an exportable string loc Js object created "gulp build"
* please ensure you define an existing language ( usually "en-us" is fine )
*/
Object.keys(config.localizedResources).forEach((stringModule, index) => {
const stringModuleFilePath = Object.values(config.localizedResources)[index].replace("{locale}", targetLang)
const stringModuleFolder = `node_modules/${stringModule}`
if (!fs.existsSync(stringModuleFolder)) {
fs.mkdirSync(stringModuleFolder);
}
const fileRawContent = fs.readFileSync(stringModuleFilePath, "utf8");
const secondOpenCurlyBracket = findIndexes(fileRawContent, "{")[1]
let fileUsefulContent = fileRawContent.slice(secondOpenCurlyBracket + 1)
fileUsefulContent = fileUsefulContent.slice(0, fileUsefulContent.indexOf("}"))
fs.writeFileSync(`node_modules/${stringModule}/index.js`, `module.exports = {${fileUsefulContent}}`);
fs.writeFileSync(`node_modules/${stringModule}/package.json`, `{"name":"${stringModule.toLowerCase()}","main":"index.js"}`);
});
// this is just some script you can add yo your package.json so you
// ensure you have an updated version of the JS object that contains the language files
{
"scripts": {
"build": "gulp bundle",
"clean": "gulp clean",
"update-loc":"gulp build && node ./scripts/allowStringsInJest.js",
"pretest": "npm run update-loc",
"test": "jest",
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment