UNPKG

i18n-pro

Version:

An out-of-the-box, lightweight JavaScript i18n auto-translation solution

171 lines (170 loc) 6.23 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.readConfig = exports.parseJsConfig = exports.initConfig = void 0; const path_1 = require("path"); const fs_1 = __importStar(require("fs")); const esbuild_1 = require("esbuild"); const url_1 = require("url"); const utils_1 = require("./utils"); const chalk_1 = __importDefault(require("./chalk")); const constants_1 = require("./constants"); const configPath = (0, path_1.join)(process.cwd(), constants_1.JS_CONFIG_NAME); const tsConfigPath = (0, path_1.join)(process.cwd(), constants_1.TS_CONFIG_NAME); const dynamicImport = new Function('path', 'return import(path)'); async function loadTsEsmConfig(tsPath) { const outputDir = (0, path_1.dirname)(tsPath); const outputPath = (0, path_1.join)(outputDir, `i18nrc.mjs`); const url = (0, url_1.pathToFileURL)((0, path_1.resolve)(outputPath)).href; const isTest = (0, utils_1.checkIsInTest)(); const isPkg = (0, utils_1.checkIsInPkg)(); if (isTest && isPkg) { return undefined; } if (isTest) { const res = await Promise.resolve().then(() => __importStar(require(tsPath))); if (typeof res?.default != 'undefined') { return res?.default; } return res; } await (0, esbuild_1.build)({ entryPoints: [tsPath], outfile: outputPath, bundle: true, format: 'esm', platform: 'node', logLevel: 'silent', }); let config = undefined; try { config = await dynamicImport(url); } catch (error) { (0, utils_1.logError)(error); } finally { (0, fs_1.unlinkSync)(outputPath); } return config.default || config; } function initConfig(type = 'ts', pathProp) { const configName = type === 'ts' ? constants_1.TS_CONFIG_NAME : constants_1.JS_CONFIG_NAME; const targetPath = (() => { return typeof pathProp === 'string' ? (0, path_1.join)(process.cwd(), pathProp, configName) : type === 'ts' ? tsConfigPath : configPath; })(); const sourcePath = (0, path_1.join)(__dirname, constants_1.RELATIVE_PATH + 'template/' + configName); try { let content = fs_1.default.readFileSync(sourcePath, { encoding: constants_1.FILE_ENCODING }); content = content.replace('../src/type', 'i18n-pro'); fs_1.default.writeFileSync(targetPath, content, { encoding: constants_1.FILE_ENCODING, }); console.log('\n'); (0, utils_1.logSuccess)(t(`初始化配置完成,已将配置文件写入到 {0} 中`, targetPath), '\n'); } catch (error) { (0, utils_1.logError)(error); } } exports.initConfig = initConfig; async function parseTsConfig(props) { const { path, isFile } = props || {}; const currentConfigPath = path ? isFile ? path : (0, path_1.join)(path, constants_1.TS_CONFIG_NAME) : tsConfigPath; const isTest = (0, utils_1.checkIsInTest)(); try { console.log(chalk_1.default.greenBright(t('读取配置文件')), currentConfigPath); const isExist = fs_1.default.existsSync(currentConfigPath); if (!isExist) return false; const res = await loadTsEsmConfig(currentConfigPath); if (typeof res !== 'object') { throw new Error(t('配置文件不是有效配置')); } else if (Object.keys(res).length === 0) { throw new Error(t('配置文件为空', JSON.stringify(res))); } return res; } catch (error) { (0, utils_1.logError)(error?.message || error); if (!isTest) { process.exit(0); } } } function parseJsConfig(props) { const { path, isFile = false } = props || {}; const currentConfigPath = path ? isFile ? path : (0, path_1.join)(path, constants_1.JS_CONFIG_NAME) : configPath; const isTest = (0, utils_1.checkIsInTest)(); try { console.log(chalk_1.default.greenBright(t('读取配置文件')), currentConfigPath); const res = require(currentConfigPath); if (typeof res !== 'object') { throw new Error(t('配置文件不是有效配置')); } else if (Object.keys(res).length === 0) { throw new Error(t('配置文件为空', JSON.stringify(res))); } return res; } catch (error) { (0, utils_1.logError)(error?.message || error); if (!isTest) { process.exit(0); } } } exports.parseJsConfig = parseJsConfig; async function readConfig(props) { const { path, isFile = false } = props || {}; if (isFile && path) { return path.endsWith('.ts') ? parseTsConfig(props) : parseJsConfig(props); } let config; config = await parseTsConfig(props); if (config) return config; config = parseJsConfig(props); return config; } exports.readConfig = readConfig;