#!/usr/bin/env node const fs = require("fs"); const path = require("path"); const argv = require("yargs").argv; export default () => { const i18nDocumentUpdate = (lng: string) => { /* next-i18next.config.js */ const workingDir = path.resolve(process.cwd()); const i18nPath = path.resolve(workingDir, "next-i18next.config.js"); if (!fs.existsSync(i18nPath)) { return; } const i18nData = fs.readFileSync(i18nPath, { encoding: "utf8", flag: "r", }); const regExpLiteral = "locales:(.+)+"; const result = i18nData.match(regExpLiteral); if (result[1].search(lng) < 0) { throw new Error(`${lng.toUpperCase()} not available`); } let updatedData = i18nData.replace( /defaultLocale: '.*'/, `defaultLocale: '${lng}'` ); updatedData = updatedData.replace( /fallbackLng: '.*'/, `fallbackLng: '${lng}'` ); fs.writeFileSync(i18nPath, updatedData); }; const init = () => { if (!argv.defaultLanguage) return; let lng = argv.defaultLanguage; try { i18nDocumentUpdate(lng); console.log( "\x1b[32m%s\x1b[0m", `\n✓ Set as the default language option ${lng.toUpperCase()}.\n` ); console.log("\x1b[33m%s\x1b[0m", "Project Zero - Akinon\n"); } catch (err) { const typedError = err as Error; console.log( "\n\x1b[31m%s\x1b[0m", `${ typedError.message ? typedError.message + "\n" : "Something went wrong.\n" }` ); } }; init(); };