2 * Command to extract source strings for translations
3 * @link {https://confluence.protontech.ch/display/CP/Localisation+on+Front-end+-+rules+and+best+practices}
5 * `node tasks/extract-translations.mjs` output -> `po/template.pot`
7 import path from "node:path";
8 import { constants as FS_CONSTANTS } from "node:fs";
9 import { mkdir, access, rm } from "node:fs/promises";
10 import { execaCommand } from "execa";
12 const DEFAULT_LANGUAGE = "en";
13 const PO_OUTPUT_FILE = "po/template.pot";
16 * Test if a path exists
17 * @param {string} itemPath path to a file/directory
18 * @return {Promise<bool>} True if it exists
20 export const pathExists = async (itemPath) => {
22 await access(itemPath, FS_CONSTANTS.R_OK);
29 async function main() {
30 const rootDir = path.dirname(PO_OUTPUT_FILE);
32 // always clean previous file -> if we test on our own pc
33 if (await pathExists(PO_OUTPUT_FILE)) {
34 await rm(PO_OUTPUT_FILE);
37 await mkdir(rootDir, { recursive: true });
39 await execaCommand(`npx ttag init ${DEFAULT_LANGUAGE} ${PO_OUTPUT_FILE}`);
40 await execaCommand(`npx ttag update ${PO_OUTPUT_FILE} src`);
41 console.log("source string extracted to", PO_OUTPUT_FILE);