在init时自动安装runtime

This commit is contained in:
wxzhang 2023-01-12 10:00:12 +08:00
parent fb38603772
commit fe8afcaff6
4 changed files with 79 additions and 37 deletions

View File

@ -138,16 +138,7 @@ module.exports =async function compile(langFolder,opts={}){
fs.writeFileSync(idMapFile,`module.exports = ${JSON.stringify(messageIds,null,4)}`)
}
logger.log(t(" - idMap文件: {}"),path.basename(idMapFile))
// 嵌入运行时源码
if(!isInstallDependent("@voerkai18n/runtime")){
installVoerkai18nRuntime(langFolder)
logger.log(t(" - 安装运行时: {}"),"@voerkai18n/runtime")
}else{
updateVoerkai18nRuntime(langFolder)
logger.log(t(" - 更新运行时:{}"),"@voerkai18n/runtime")
}
const templateContext = {
scopeId:projectPackageJson.name,
languages,

View File

@ -33,7 +33,6 @@ program
.option('-r, --reset', t('重新生成当前项目的语言配置'))
.option('-lngs, --languages <languages...>', t('支持的语言列表'), ['zh','en'])
.option('-d, --defaultLanguage <name>', t('默认语言'), 'zh')
// .option('-i, --installRuntime', t('自动安装默认语言'),true)
.option('-a, --activeLanguage <name>', t('激活语言'), 'zh')
.hook("preAction",async function(location){
const lang= process.env.LANGUAGE || "zh"

View File

@ -6,12 +6,10 @@
const path = require("path")
const fs = require("fs")
const shelljs = require("shelljs")
const { t } = require("./i18nProxy")
const { findModuleType } = require("@voerkai18n/utils")
const createLogger = require("logsets")
const logger = createLogger()
const logger = createLogger()
const { installPackage } = require("@voerkai18n/utils")
function getLanguageList(langs,defaultLanguage){
try{
@ -41,34 +39,57 @@ function getLanguageList(langs,defaultLanguage){
module.exports = function(srcPath,{debug = true,languages=["zh","en"],defaultLanguage="zh",activeLanguage="zh",reset=false,installRuntime=true}={}){
let tasks = logger.tasklist("初始化VoerkaI18n工程")
// 语言文件夹名称
const langPath = "languages"
// 查找当前项目的语言包类型路径
const lngPath = path.join(srcPath,langPath)
if(!fs.existsSync(lngPath)){
fs.mkdirSync(lngPath)
if(debug) logger.log(t("创建语言包文件夹: {}"),lngPath)
try{
tasks.add("创建语言包文件夹")
const langPath = "languages"
// 查找当前项目的语言包类型路径
const lngPath = path.join(srcPath,langPath)
if(!fs.existsSync(lngPath)){
fs.mkdirSync(lngPath)
if(debug) logger.log(t("创建语言包文件夹: {}"),lngPath)
}
tasks.complete()
}catch(e){
tasks.error(e.message)
}
// 创建settings.json文件
const settingsFile = path.join(lngPath,"settings.json")
if(fs.existsSync(settingsFile) && !reset){
if(debug) logger.log(t("语言配置文件{}文件已存在,跳过创建。\n使用{}可以重新覆盖创建"),settingsFile,"-r")
return
}
const settings = {
languages:getLanguageList(languages,defaultLanguage),
defaultLanguage,
activeLanguage,
namespaces:{}
}
// 写入配置文件
fs.writeFileSync(settingsFile,JSON.stringify(settings,null,4))
try{
tasks.add("生成语言配置文件settings.json")
const settingsFile = path.join(lngPath,"settings.json")
if(fs.existsSync(settingsFile) && !reset){
if(debug) logger.log(t("语言配置文件{}文件已存在,跳过创建。\n使用{}可以重新覆盖创建"),settingsFile,"-r")
return
}
const settings = {
languages:getLanguageList(languages,defaultLanguage),
defaultLanguage,
activeLanguage,
namespaces:{}
}
// 写入配置文件
fs.writeFileSync(settingsFile,JSON.stringify(settings,null,4))
tasks.complete()
}catch(e){
tasks.error(e.message)
}
try{
tasks.add(t("安装运行时依赖@voerkai18n/runtime"))
installPackage('@voerkai18n/runtime')
tasks.complete()
}catch(e){
tasks.error(e.message)
}
if(debug) {
logger.log(t("生成语言配置文件:{}"),"./languages/settings.json")
logger.log(t("拟支持的语言:{}"),settings.languages.map(l=>l.name).join(","))
logger.log(t("已安装运行时:{}"),'@voerkai18n/runtime')
logger.log(t("初始化成功,下一步:"))
logger.log(t(" - 编辑{}确定拟支持的语言种类等参数"),"languages/settings.json")
logger.log(t(" - 运行<{}>扫描提取要翻译的文本"),"voerkai18n extract")

View File

@ -407,6 +407,19 @@ function deepMerge(toObj,formObj,options={}){
shelljs.exec("npm install @voerkai18n/runtime")
}
}
function getPackageTool(){
const projectFolder = getProjectRootFolder(srcPath || process.cwd())
if(fs.existsSync(path.join(projectFolder,"pnpm-lock.yaml"))){
return 'pnpm'
}else if(fs.existsSync(path.join(projectFolder,"yarn.lock"))){
return 'yarn'
}else{
return 'npm'
}
}
/**
* 在当前工程升级@voerkai18n/runtime
* @param {*} srcPath
@ -447,6 +460,22 @@ function createPackageJsonFile(targetPath,moduleType="auto"){
}
function installPackage(packageName){
const packageTool = getPackageTool()
try{
if(packageTool=='pnpm'){
shelljs.exec(`pnpm add ${packageName}`)
}else if(packageTool=='yarn'){
shelljs.exec(`yarn add ${packageName}`)
}else{
shelljs.exec(`npm install ${packageName}`)
}
}catch{
shelljs.exec(`npm install ${packageName}`)
}
}
module.exports = {
fileMatcher, // 文件名称匹配器
getProjectRootFolder, // 查找获取项目根目录
@ -464,5 +493,7 @@ module.exports = {
getDataTypeName, // 获取指定变量类型名称
isGitRepo, // 判断当前工程是否是git工程
fileIsExists,
isTypeScriptProject
isTypeScriptProject,
getPackageTool,
installPackage
}