update cli default command

This commit is contained in:
wxzhang 2023-01-29 20:28:46 +08:00
parent 5e4dbf21f0
commit bb9ae21c83
7 changed files with 189 additions and 2912 deletions

View File

@ -25,12 +25,19 @@
const glob = require("glob")
const createLogger = require("logsets")
const path = require("path")
const { findModuleType,getCurrentPackageJson, getInstalledPackageInfo, getPackageReleaseInfo} = require("@voerkai18n/utils")
const { t } = require("./i18nProxy")
const fs = require("fs-extra")
const logger = createLogger()
const artTemplate = require("art-template")
const semver = require("semver")
const {
findModuleType,
getCurrentPackageJson,
getInstalledPackageInfo,
getPackageReleaseInfo,
upgradePackage
} = require("@voerkai18n/utils")
function normalizeCompileOptions(opts={}) {
let options = Object.assign({
@ -68,14 +75,23 @@ function generateFormatterFile(langName,{isTypeScript,formattersFolder,templateC
* @voerkai18n/runtime更新到最新版本
*/
async function updateRuntime(){
const curVersion = getInstalledPackageInfo("@voerkai18n/runtime").version
const latestVersion = (await getPackageReleaseInfo("@voerkai18n/runtime")).lastVersion
if(semver.gt(latestVersion, curVersion)){
await upgradePackage("@voerkai18n/runtime")
}
const task = logger.task(t("更新@voerkai18n/runtime运行时"))
try{
const packageName = "@voerkai18n/runtime"
const curVersion = getInstalledPackageInfo(packageName).version
const latestVersion = (await getPackageReleaseInfo(packageName)).lastVersion
if(semver.gt(latestVersion, curVersion)){
await upgradePackage(packageName)
task.complete(t("Updated:{}",latestVersion))
return
}
task.complete(t("已经是最新的"))
}catch(e){
logger.log(t("更新@voerkai18n/runtime失败,请手动更新!"))
task.error(e.message)
}
}
module.exports =async function compile(langFolder,opts={}){
const options = normalizeCompileOptions(opts);
let { moduleType,isTypeScript } = options;

View File

@ -1,20 +1,20 @@
let i18nScope,t
let VoerkaI18nScope,t
try{
// @voerkai18n/cli工程本身使用了voerkai18n,即@voerkai18n/cli的extract和compile依赖于其自己生成的languages运行时
// 而@voerkai18n/cli又用来编译多语言包这样产生了鸡生蛋问题
// extract与compile调试阶段如果t函数无法使用(即编译的languages无法正常使用)则需要提供t函数
const language = require('./languages');
t = language.t
i18nScope = language.i18nScope
VoerkaI18nScope = language.VoerkaI18nScope
}catch{
t=v=>v
i18nScope={change:()=>{} }
VoerkaI18nScope={change:()=>{} }
}
module.exports = {
i18nScope,
VoerkaI18nScope,
t
}

View File

@ -5,7 +5,7 @@ const semver = require('semver')
const path = require("path")
const fs = require("fs-extra")
const logger = createLogger()
const { i18nScope ,t } = require("./i18nProxy")
const { VoerkaI18nScope ,t } = require("./i18nProxy")
const { getProjectSourceFolder,isTypeScriptProject, getPackageReleaseInfo,getInstalledPackageInfo } = require("@voerkai18n/utils");
@ -22,45 +22,10 @@ program
banner.add("VoerkaI18n command line interactive tools",{style:"darkGray"})
banner.add()
banner.add("installed: ",currentVersion," latest: ",newVersion,{style:["","yellow","","yellow"]})
banner.render()
const tasks = logger.tasklist("检测VoerkaI18n最新版本")
const packages = [
"@voerkai18n/cli",
"@voerkai18n/runtime",
"@voerkai18n/vue",
"@voerkai18n/react",
"@voerkai18n/vite",
"@voerkai18n/babel",
"voerkai18n-loader"
]
let needUpgrades = []
for(let package of packages){
try{
let info = getInstalledPackageInfo(package)
tasks.add(`${package}(${info ? info.version : logger.colors.red('未安装')})`)
let newInfo = await getPackageReleaseInfo(package)
if(info){
if(semver.gt(newInfo.latestVersion,info.version)){
needUpgrades.push(package)
tasks.fail(info.version)
}else if(newInfo.version == info.version){
tasks.complete("NEWEST")
}else{
tasks.skip("UNKNOWN")
}
}else{
tasks.fail(newInfo.version)
}
}catch(e) {
tasks.error(e.stack)
}
if(semver.gt(newVersion,currentVersion)){
banner.add("Run ","npm upgrade @voerkai18n/cli"," to upgrade!",{style:["","yellow",""]})
}
//
if(needUpgrades.length>0){
logger.log(logger.colors.red("\n请将{}升级到最新版本!",needUpgrades.join(",")))
}
banner.render()
})
program
.command('init')
@ -75,7 +40,7 @@ program
.option('-a, --activeLanguage <name>', t('激活语言'), 'zh')
.hook("preAction",async function(location){
const lang= process.env.LANGUAGE || "zh"
await i18nScope.change(lang)
await VoerkaI18nScope.change(lang)
})
.action((location,options) => {
options.isTypeScript = options.typescript==undefined ? isTypeScriptProject() : options.typescript
@ -104,7 +69,7 @@ program
.argument('[location]', t('工程项目所在目录'),"./")
.hook("preAction",async function(location){
const lang= process.env.LANGUAGE || "zh"
await i18nScope.change(lang)
await VoerkaI18nScope.change(lang)
})
.action(async (location,options) => {
location = getProjectSourceFolder(location)
@ -135,7 +100,7 @@ program
.argument('[location]', t('工程项目所在目录'),"./")
.hook("preAction",async function(location){
const lang= process.env.LANGUAGE || "zh"
await i18nScope.change(lang)
await VoerkaI18nScope.change(lang)
})
.action(async (location,options) => {
location = getProjectSourceFolder(location)
@ -162,7 +127,7 @@ program
.option('-q, --qps <value>', t('翻译速度限制,即每秒可调用的API次数'), 1)
.hook("preAction",async function(location){
const lang= process.env.LANGUAGE || "zh"
await i18nScope.change(lang)
await VoerkaI18nScope.change(lang)
})
.action((location,options) => {
location = getProjectSourceFolder(location)

View File

@ -3,12 +3,10 @@
*/
const messageIds = require("./idMap")
const { translate,i18nScope } = require("@voerkai18n/runtime")
const defaultFormatters = require("@voerkai18n/runtime/formatters/zh.js")
const activeFormatters = defaultFormatters
const { translate,VoerkaI18nScope } = require("@voerkai18n/runtime")
const defaultMessages = require("./zh.js") // 默认语言包
const activeMessages = defaultMessages
// 语言配置文件
const scopeSettings = {

File diff suppressed because it is too large Load Diff

View File

@ -1,36 +1,4 @@
{
"支持的语言\t: {}": {
"en": "Supported languages\t: {}",
"$file": [
"compile.command.js",
"extract.plugin.js"
],
"de": "支持的语言\t: {}"
},
"默认语言\t: {}": {
"en": "Default language\t: {}",
"$file": [
"compile.command.js",
"extract.plugin.js"
],
"de": "默认语言\t: {}"
},
"激活语言\t: {}": {
"en": "Active language\t\t: {}",
"$file": [
"compile.command.js",
"extract.plugin.js"
],
"de": "激活语言\t: {}"
},
"名称空间\t: {}": {
"en": "Namespaces\t\t: {}",
"$file": [
"compile.command.js",
"extract.plugin.js"
],
"de": "名称空间\t: {}"
},
" - 更新格式化器:{}": {
"en": " - Update formatters:{}",
"$file": [
@ -192,13 +160,6 @@
],
"de": "语言包文件夹<{}>不存在"
},
"语言配置文件{}文件已存在,跳过创建。\n使用{}可以重新覆盖创建": {
"en": "Language configuration {} file already exists, skipping creation\n use {} to overwrite the creation",
"$file": [
"init.command.js"
],
"de": "语言配置文件{}文件已存在,跳过创建。\n使用{}可以重新覆盖创建"
},
"生成语言配置文件:{}": {
"en": "Generate language configuration: {}",
"$file": [
@ -248,13 +209,6 @@
],
"de": "创建语言包文件夹: {}"
},
"模块类型\t: {}": {
"en": "Type of module\t\t: {}",
"$file": [
"compile.command.js"
],
"de": "模块类型\t: {}"
},
"编译结果输出至:{}": {
"en": "Compile to{}",
"$file": [
@ -297,42 +251,6 @@
],
"de": " - 共合成{}条文本"
},
" - 运行时: {}": {
"en": " - Runtime: {}",
"$file": [
"compile.command.js"
],
"de": " - 运行时: {}"
},
"自动安装默认语言": {
"en": "Auto install default language",
"$file": [
"index.js"
],
"de": "自动安装默认语言",
"zh": "自动安装默认语言"
},
"不嵌入运行时源码": {
"en": "Not inline runtime source",
"$file": [
"index.js"
],
"de": "不嵌入运行时源码"
},
" - 安装运行时: {}": {
"en": " - Install runtime: {}",
"$file": [
"compile.command.js"
],
"de": " - 安装运行时: {}"
},
" - 更新运行时:{}": {
"en": " - Update runtime{}",
"$file": [
"compile.command.js"
],
"de": " - 更新运行时:{}"
},
"调用在线翻译服务商的API翻译译指定项目的语言包,如使用百度云翻译服务": {
"en": "Call the API translation language package of the online translation service provider, eg:baidu translation service",
"$file": [
@ -395,5 +313,121 @@
"$file": [
"translate.command.js"
]
},
"更新@voerkai18n/runtime运行时": {
"en": "更新@voerkai18n/runtime运行时",
"de": "更新@voerkai18n/runtime运行时",
"$file": [
"compile.command.js"
]
},
"Updated:{}": {
"en": "Updated:{}",
"de": "Updated:{}",
"$file": [
"compile.command.js"
]
},
"已经是最新的": {
"en": "已经是最新的",
"de": "已经是最新的",
"$file": [
"compile.command.js"
]
},
"更新@voerkai18n/runtime失败,请手动更新!": {
"en": "更新@voerkai18n/runtime失败,请手动更新!",
"de": "更新@voerkai18n/runtime失败,请手动更新!",
"$file": [
"compile.command.js"
]
},
"支持的语言\\t: {}": {
"en": "支持的语言\\t: {}",
"de": "支持的语言\\t: {}",
"$file": [
"compile.command.js",
"extract.plugin.js"
]
},
"默认语言\\t: {}": {
"en": "默认语言\\t: {}",
"de": "默认语言\\t: {}",
"$file": [
"compile.command.js",
"extract.plugin.js"
]
},
"激活语言\\t: {}": {
"en": "激活语言\\t: {}",
"de": "激活语言\\t: {}",
"$file": [
"compile.command.js",
"extract.plugin.js"
]
},
"名称空间\\t: {}": {
"en": "名称空间\\t: {}",
"de": "名称空间\\t: {}",
"$file": [
"compile.command.js",
"extract.plugin.js"
]
},
"模块类型\\t: {}": {
"en": "模块类型\\t: {}",
"de": "模块类型\\t: {}",
"$file": [
"compile.command.js"
]
},
"TypeScript\\t: {}": {
"en": "TypeScript\\t: {}",
"de": "TypeScript\\t: {}",
"$file": [
"compile.command.js"
]
},
"输出typescript代码": {
"en": "输出typescript代码",
"de": "输出typescript代码",
"$file": [
"index.js"
]
},
"备份原始文件": {
"en": "备份原始文件",
"de": "备份原始文件",
"$file": [
"index.js"
]
},
"翻译模式取值auto=仅翻译未翻译的,full=全部翻译": {
"en": "翻译模式取值auto=仅翻译未翻译的,full=全部翻译",
"de": "翻译模式取值auto=仅翻译未翻译的,full=全部翻译",
"$file": [
"index.js"
]
},
"语言配置文件{}文件已存在,跳过创建。\\n使用{}可以重新覆盖创建": {
"en": "语言配置文件{}文件已存在,跳过创建。\\n使用{}可以重新覆盖创建",
"de": "语言配置文件{}文件已存在,跳过创建。\\n使用{}可以重新覆盖创建",
"$file": [
"init.command.js"
]
},
"安装运行时依赖@voerkai18n/runtime": {
"en": "安装运行时依赖@voerkai18n/runtime",
"de": "安装运行时依赖@voerkai18n/runtime",
"$file": [
"init.command.js"
]
},
"已安装运行时:{}": {
"en": "已安装运行时:{}",
"de": "已安装运行时:{}",
"$file": [
"init.command.js"
]
}
}

View File

@ -0,0 +1,36 @@
const tasks = logger.tasklist("检测VoerkaI18n最新版本")
const packages = [
"@voerkai18n/cli",
"@voerkai18n/runtime",
"@voerkai18n/vue",
"@voerkai18n/react",
"@voerkai18n/vite",
"@voerkai18n/babel",
"voerkai18n-loader"
]
let needUpgrades = []
for(let package of packages){
try{
let info = getInstalledPackageInfo(package)
tasks.add(`${package}(${info ? info.version : logger.colors.red('未安装')})`)
let newInfo = await getPackageReleaseInfo(package)
if(info){
if(semver.gt(newInfo.latestVersion,info.version)){
needUpgrades.push(package)
tasks.fail(info.version)
}else if(newInfo.version == info.version){
tasks.complete("NEWEST")
}else{
tasks.skip("UNKNOWN")
}
}else{
tasks.fail(newInfo.version)
}
}catch(e) {
tasks.error(e.stack)
}
}
//
if(needUpgrades.length>0){
logger.log(logger.colors.red("\n请将{}升级到最新版本!",needUpgrades.join(",")))
}