#!/bin/node import * as fs from 'fs'; let lines = ''; const langIDs = []; const langFiles = {}; fs.readdirSync('./channels/src/i18n').forEach(file => { if (file.endsWith('.json')) { const langID = file.substr(0, file.indexOf('.json')); // Skip default language if (langID === 'en') { return } langIDs.push(langID); lines += `import ${langID.replace('-', '')} from './${file}';\n`; langFiles[langID] = langID.replace('-', ''); } }); // To generate the file exports we need to do a bit more work to handle ids with dashes and also output a map of literals rather than strings. lines += ` // TypeScript thinks it's importing these language files' contents directly, but Webpack rewrites the above imports // to the URL the file for lazy loading. That's the reason for the ugly type assertions below. export const langFiles: { [langID: string]: string; } = { ` + Object.keys(langFiles).reduce((out, id, idx) => { out += ' '; if (id.includes('-')) { out += `'${id}': ${langFiles[id]}`; } else { out += `${id}: ${langFiles[id]}`; } out += ' as unknown as string'; if (idx !== (langIDs.length - 1)) { out += ','; } out += '\n'; return out; }, '') + '};'; const header = `// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information.\n\n// DO NOT EDIT - This file is generated by gen_lang_imports through the gen-lang-imports script\n\n/* eslint-disable */\n\n`; fs.writeFileSync('./channels/src/i18n/imports.ts', header+lines);