This commit is contained in:
wxzhang 2023-01-29 17:18:02 +08:00
parent 76f2edc408
commit 9b8f652a95
7 changed files with 163 additions and 19 deletions

View File

@ -24,11 +24,7 @@ pnpm add -g @voerkai18n/cli
npm install --save @voerkai18n/runtime
yarn add @voerkai18n/runtime
pnpm add @voerkai18n/runtime
```
## **@voerkai18/formatters**
**可选的**,一些额外的格式化器,可以按需进行安装到`dependencies`中,用来扩展翻译时对插值变量的额外处理。
```
## **@voerkai18/babel**
@ -45,3 +41,7 @@ pnpm add @voerkai18n/runtime
## **@voerkai18/vite**
可选的`vite`插件,用来为`vite`应用提供自动导入翻译函数和翻译文本映射自动替换。
## **@voerkai18/webpack**
可选的`voerkai18n-loader for webpack`,用来实现自动导入翻译函数和翻译文本映射自动替换,当使用`webpack`作为打包构建工具时使用,比如`react-native`应用。

View File

@ -1,12 +1,12 @@
#!/usr/bin/env node
const { Command } = require('commander');
const createLogger = require("logsets")
const semver = require('semver')
const path = require("path")
const fs = require("fs-extra")
const logger = createLogger()
const { i18nScope ,t } = require("./i18nProxy")
const { getProjectSourceFolder,isTypeScriptProject } = require("@voerkai18n/utils");
const { getProjectSourceFolder,isTypeScriptProject, getPackageReleaseInfo,getInstalledPackageInfo } = require("@voerkai18n/utils");
const program = new Command();
@ -14,13 +14,53 @@ program
.name("voerkai18n")
.option("-v, --version", "当前版本号")
.helpOption('-h, --help', '显示帮助')
.action((options) => {
.action(async (options) => {
const currentVersion = require("./package.json").version
const newVersion = (await getPackageReleaseInfo("@voerkai18n/cli")).latestVersion
const banner = logger.banner()
banner.add("VoerkaI18n CLI")
banner.add("VoerkaI18n")
banner.add("VoerkaI18n command line interactive tools",{style:"darkGray"})
banner.add()
banner.add("版本号:",`${require("./package.json").version}`,{style:["","yellow"]})
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(needUpgrades.length>0){
logger.log(logger.colors.red("\n请将{}升级到最新版本!",needUpgrades.join(",")))
}
})
program
.command('init')
@ -129,9 +169,6 @@ program
const translate = require("./translate.command")
translate(location,options)
});
program.parseAsync(process.argv);

View File

@ -43,6 +43,7 @@
"logsets": "^1.0.21",
"md5": "^2.3.0",
"qs": "^6.10.3",
"semver": "^7.3.7",
"shelljs": "^0.8.5",
"through2": "^4.0.2",
"vinyl": "^2.2.1"

View File

@ -3,6 +3,7 @@
"version": "1.0.27",
"description": "格式化器,提供对要翻译文本的转换功能",
"main": "index.js",
"private":true,
"homepage": "https://gitee.com/zhangfisher/voerka-i18n",
"repository": {
"type": "git",

View File

@ -1,6 +1,7 @@
const path = require("path")
const shelljs = require("shelljs")
const fs = require("fs-extra")
const semver = require('semver')
/**
*
@ -418,6 +419,66 @@ function getPackageTool(){
return 'npm'
}
}
/**
* 异步执行脚本并返回输出结果
* @param {*} script
* @param {*} options
* @returns
*/
async function asyncExecShellScript(script,options={}){
const { silent=true} = options
return new Promise((resolve,reject)=>{
shelljs.exec(script,{silent,...options,async:true},(code,stdout)=>{
if(code>0){
reject(new Error(`执行<${script}>失败: ${stdout.trim()}`))
}else{
resolve(stdout.trim())
}
})
})
}
/**
* 从NPM获取包最近发布的版本信息
* {
tags: { latest: '1.1.30' },
license: 'MIT',
author: 'wxzhang',
version: '1.1.30-latest',
latestVersion: '1.1.30',
firstCreated: '2022-03-24T09:32:51.748Z',
lastPublish: '2023-01-28T08:49:33.139Z',
size: 888125
}
* @param {*} packageName
*/
async function getPackageReleaseInfo(packageName) {
try{
let results = await asyncExecShellScript.call(this,`npm info ${packageName} --json`,{silent:true})
const info = JSON.parse(results)
const distTags = info["dist-tags"]
// 取得最新版本的版本号不是latest
let lastVersion = Object.entries(distTags).reduce((result,[tag,value])=>{
if(semver.gt(value, result.value)){
result = {tag,value}
}
return result
},{tag:'latest',value:info["version"]})
return {
tags : distTags,
license : info["license"],
author : info["author"],
version : `${lastVersion.value}-${lastVersion.tag}`,
latestVersion: info["version"],
firstCreated : info.time["created"],
lastPublish : info.time["modified"],
size : info.dist["unpackedSize"]
}
}catch(e){
console.error(`ERROR: 执行npm info ${packageName}出错: ${e.stack}`)
return null;
}
}
/**
@ -592,6 +653,42 @@ function importTranslateFunction(code,sourceFile,langPath){
return code
}
/**
* 检测当前环境是否已经安装了指定的包
* 如果已安装则返回
* {
* version:"<版本号>",
* path:"<安装路径>"
* }
* 如果未安装则返回null
* @param {*} packageName
*/
function getInstalledPackageInfo(packageName,fields=[]){
try{
const packagePath = path.dirname(require.resolve(packageName))
const pkgInfo = fs.readJSONSync(path.join(packagePath,"package.json"))
let results = {
version: pkgInfo.version,
path: packagePath,
}
for(let field in fields){
if(field in pkgInfo){
results[field] = pkgInfo[field]
}else{
results[field] = null
}
}
return results
}catch(e){
return null
// if(e instanceof Error && e.code=="MODULE_NOT_FOUND"){
// return null;
// }else{
// }
}
}
module.exports = {
fileMatcher, // 文件名称匹配器
@ -614,7 +711,10 @@ module.exports = {
getPackageTool, // 获取当前工程使用的包工具如pnpm,yarn,npm
installPackage, // 安装指定的包
readIdMapFile, // 读取当前工程下的idMap文件
replaceTranslateText,
hasImportTranslateFunction,
importTranslateFunction // 在代码中导入t函数
replaceTranslateText, //
hasImportTranslateFunction, // 检测代码中是否具有import { t } from "xxxx"
importTranslateFunction, // 在代码中导入t函数
asyncExecShellScript, // 异步执行一段脚本并返回结果
getPackageReleaseInfo, // 从npm上读取指定包的信息
getInstalledPackageInfo // 返回当前工程已安装的包信息,主要是版本号
}

View File

@ -11,6 +11,7 @@
"license": "ISC",
"dependencies": {
"fs-extra": "^10.0.1",
"semver": "^7.3.7",
"shelljs": "^0.8.5"
},
"lastPublish": "2023-01-28T16:49:11+08:00"

6
pnpm-lock.yaml generated
View File

@ -222,6 +222,7 @@ importers:
logsets: ^1.0.21
md5: ^2.3.0
qs: ^6.10.3
semver: ^7.3.7
shelljs: ^0.8.5
through2: ^4.0.2
vinyl: ^2.2.1
@ -241,6 +242,7 @@ importers:
logsets: 1.0.21
md5: 2.3.0
qs: 6.11.0
semver: 7.3.8
shelljs: 0.8.5
through2: 4.0.2
vinyl: 2.2.1
@ -295,9 +297,11 @@ importers:
packages/utils:
specifiers:
fs-extra: ^10.0.1
semver: ^7.3.7
shelljs: ^0.8.5
dependencies:
fs-extra: 10.1.0
semver: 7.3.8
shelljs: 0.8.5
packages/vite:
@ -4667,7 +4671,7 @@ packages:
fast-glob: 3.2.11
fs-extra: 10.1.0
logsets: 1.0.21
semver: 7.3.7
semver: 7.3.8
shelljs: 0.8.5
dev: true