Skip to content

Commit

Permalink
chore: improved bin/extract-strings.mjs
Browse files Browse the repository at this point in the history
  • Loading branch information
Fredx87 committed Dec 20, 2024
1 parent cd349a7 commit d4f8614
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
56 changes: 38 additions & 18 deletions bin/extract-strings.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
* The script uses the i18next-parser library for extracting the strings and it adds a custom transformer for creating
* the file in the required YAML format.
*/
// Usage: node bin/extract-strings.mjs [PACKAGE_NAME] [SOURCE_FILES_GLOB] [OUTPUT_PATH]
// Example: node bin/extract-strings.mjs new-request-form 'src/modules/new-request-form/**/*.{ts,tsx}' src/modules/new-request-form/translations
import vfs from "vinyl-fs";
import Vinyl from "vinyl";
import { transform as I18NextTransform } from "i18next-parser";
Expand All @@ -32,9 +30,15 @@ import { existsSync, readFileSync } from "node:fs";
import { load, dump } from "js-yaml";
import { resolve } from "node:path";

const PACKAGE_NAME = process.argv[2];
const INPUT_GLOB = `${process.cwd()}/${process.argv[3]}`;
const OUTPUT_DIR = resolve(process.cwd(), process.argv[4]);
/**
* Maps each folder in the `src/modules` directory with its package name on the translation system
*/
const MODULES = {
"new-request-form": "new-request-form",
"service-catalog": "service-catalog",
"ticket-fields": "cph-theme-ticket-fields",
shared: "cph-theme-shared",
};

const OUTPUT_YML_FILE_NAME = "en-us.yml";

Expand Down Expand Up @@ -62,20 +66,26 @@ const config = {
};

class SourceYmlTransform extends Transform {
#defaultContent = {
title: "",
packages: [PACKAGE_NAME],
parts: [],
};
#defaultContent;
#outputDir;
#packageName;

#counters = {
added: 0,
obsolete: 0,
mismatch: 0,
};

constructor() {
constructor(packageName, outputDir) {
super({ objectMode: true });

this.#packageName = packageName;
this.#defaultContent = {
title: "",
packages: [packageName],
parts: [],
};
this.#outputDir = outputDir;
}

_transform(file, encoding, done) {
Expand Down Expand Up @@ -125,7 +135,7 @@ class SourceYmlTransform extends Transform {
});
this.push(virtualFile);

console.log(`String extraction completed!
console.log(`Module ${this.#packageName} - String extraction completed!
Added strings: ${this.#counters.added}
Removed strings (marked as obsolete): ${this.#counters.obsolete}
Strings with mismatched value: ${this.#counters.mismatch}`);
Expand All @@ -138,7 +148,7 @@ class SourceYmlTransform extends Transform {
}

#getSourceYmlContent() {
const outputPath = resolve(OUTPUT_DIR, OUTPUT_YML_FILE_NAME);
const outputPath = resolve(this.#outputDir, OUTPUT_YML_FILE_NAME);
if (existsSync(outputPath)) {
return load(readFileSync(outputPath, "utf-8"));
}
Expand All @@ -153,8 +163,18 @@ class SourceYmlTransform extends Transform {
}
}

vfs
.src([INPUT_GLOB])
.pipe(new I18NextTransform(config))
.pipe(new SourceYmlTransform())
.pipe(vfs.dest(OUTPUT_DIR));
// Example: node bin/extract-strings.mjs new-request-form 'src/modules/new-request-form/**/*.{ts,tsx}' src/modules/new-request-form/translations

for (const [moduleName, packageName] of Object.entries(MODULES)) {
const inputGlob = `${process.cwd()}/src/modules/${moduleName}/**/*.{ts,tsx}`;
const outputDir = resolve(
process.cwd(),
`src/modules/${moduleName}/translations`
);

vfs
.src([inputGlob])
.pipe(new I18NextTransform(config))
.pipe(new SourceYmlTransform(packageName, outputDir))
.pipe(vfs.dest(outputDir));
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"download-locales": "node ./bin/update-translations",
"test": "jest",
"test-a11y": "node bin/lighthouse/index.js",
"i18n:extract": "node bin/extract-strings.mjs new-request-form 'src/modules/new-request-form/**/*.{ts,tsx}' src/modules/new-request-form/translations",
"i18n:extract": "node bin/extract-strings.mjs",
"i18n:update-translations": "node bin/update-modules-translations.mjs",
"zcli": "zcli"
},
Expand Down

0 comments on commit d4f8614

Please sign in to comment.