更新init和extract脚本

This commit is contained in:
wxzhang 2022-03-11 17:56:08 +08:00
parent f4cf83cfea
commit 54546276ab
6 changed files with 103 additions and 40 deletions

View File

@ -1,15 +1,3 @@
{
"name": "app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@voerkai18n/tools": "workspace:^1.0.0"
}
}
"type": "module"
}

View File

@ -0,0 +1,36 @@
const { findModuleType } = require("./utils")
const path = require("path")
const fs = require("fs")
const gulp = require("gulp")
const createLogger = require("logsets")
const logger = createLogger()
module.exports = function(targetPath,options={}){
let { filetypes,exclude} = options
if(!filetypes) filetypes = ["*.js","*.json","*.jsx","*.ts","*.tsx","*.vue","*.html"]
if(!Array.isArray(filetypes)) filetypes = filetypes.split(",")
const folders = filetypes.map(ftype=>{
if(ftype.startsWith(".")) ftype = "*"+ftype
if(!ftype.startsWith("*.")) ftype = "*."+ftype
return path.join(targetPath,ftype)
})
folders.push(`!${path.join(targetPath,"languages")}`)
folders.push(`!${path.join(targetPath,"node_modules")}`)
folders.push(`!${path.join(targetPath,"**","node_modules","**")}`)
// 排除文件夹
console.log("exclude",exclude)
if(!Array.isArray(exclude) && exclude){
exclude = exclude.split(",")
}
if(exclude){
exclude.forEach(folder=>{
folders.push(`!${path.join(targetPath,folder)}`)
})
}
console.log(folders)
//gulp.src(path.join(targetPath,"**/*.json"))
}

View File

@ -1,6 +1,9 @@
const { Command } = require('commander');
const createLogger = require("logsets")
const path = require("path")
const fs = require("fs");
const { importModule } = require('./utils');
const deepmerge = require('deepmerge');
const logger = createLogger()
const program = new Command();
@ -13,12 +16,16 @@ program
.argument('[location]', '工程项目所在目录')
.description('初始化项目国际化配置')
.option('-r, --reset', '重新生成当前项目的语言配置')
.option('-p, --langPath [name]', '语言包保存路径,默认<location>/langauges',"languages")
.option('-m, --moduleType [type]', '生成的js模块类型,默认esm',"esm")
.option('-lngs, --languages <languages...>', '支持的语言列表', ['cn','en'])
.action((location,options) => {
if(!location) location = process.cwd()
const initializer = require("./initializer")
if(!location) {
location = process.cwd()
}else{
location = path.join(process.cwd(),location)
}
logger.log("工程目录:{}",location)
const initializer = require("./init.command")
options.debug=true
initializer(location,options)
});
@ -27,16 +34,38 @@ program
program
.command('extract')
.description('扫描并提取所有待翻译的字符串到<languages/translates>文件夹中')
.argument('[location]', 'js项目所在目录')
.option('-d, --debug', '输出调试信息')
.option('-ls, --languages', '支持的语言', 'cn,en,de,fr,es,it,jp')
.option('-d, --default', '默认语言', 'cn')
.option('-a, --active', '激活语言', 'cn')
.option('-o, --output', '输出目录', './languages')
.argument('<location...>', '工程所在目录')
.action((location) => {
if(!location) location = process.cwd()
console.log('location=',location);
.option('-lngs, --languages', '支持的语言', 'cn,en,de,fr,es,it,jp')
.option('-d, --defaultLanguage', '默认语言', 'cn')
.option('-a, --activeLanguage', '激活语言', 'cn')
.option('-ns, --namespaces', '翻译名称空间')
.option('-e, --exclude <folders>', '排除要扫描的文件夹,多个用逗号分隔')
.option('-u, --updateMode', '本次提取内容与已存在内容的数据合并策略,默认取值sync=同步,overwrite=覆盖,merge=合并', 'sync')
.option('-f, --filetypes', '要扫描的文件类型', 'js,vue,html,jsx,ts')
.argument('[location]', '工程所在目录',"./")
.action(async (location,options) => {
if(!location) {
location = process.cwd()
}else{
location = path.join(process.cwd(),location)
}
if(options.languages){
options.languages = options.languages.split(",").map(l=>({name:l,title:l}))
}
//options = Object.assign({},options)
logger.log("工程目录:{}",location)
const langSettingsFile = path.join(location,"languages","settings.js")
if(fs.existsSync(langSettingsFile)){
logger.log("语言配置文件<{}>已存在.将优先使用此配置文件中参数来提取文本",langSettingsFile)
let lngOptions = (await importModule("file:///"+langSettingsFile)).default
options.languages = lngOptions.languages
options.defaultLanguage = lngOptions.defaultLanguage
options.activeLanguage = lngOptions.activeLanguage
options.namespaces = lngOptions.namespaces
}
const extractor = require('./extract.command');
extractor(location,options)
});

View File

@ -10,8 +10,11 @@ const fs = require("fs")
const createLogger = require("logsets")
const logger = createLogger()
module.exports = function(targetPath,{debug = true, langPath = "languages",languages=["cn","en"],defaultLanguage="cn",activeLanguage="cn",moduleType = "auto",reset=false}={}){
module.exports = function(targetPath,{debug = true,languages=["cn","en"],defaultLanguage="cn",activeLanguage="cn",moduleType = "auto",reset=false}={}){
// 语言文件夹名称
const langPath = "languages"
// 查找当前项目的语言包类型路径
if(moduleType==="auto"){
moduleType = findModuleType(targetPath)
}
@ -41,11 +44,21 @@ module.exports = function(targetPath,{debug = true, langPath = "languages",langu
activeLanguage,
namespaces:{}
}
const packageJsonFile = path.join(targetPath,"languages","package.json")
if(["esm","es"].includes(moduleType)){
fs.writeFileSync(settingsFile,`export default ${JSON.stringify(settings,null,4)}`)
fs.writeFileSync(packageJsonFile,JSON.stringify({type:"module"},null,4))
}else{
fs.writeFileSync(settingsFile,`module.exports = ${JSON.stringify(settings,null,4)}`)
fs.writeFileSync(packageJsonFile,JSON.stringify({},null,4))
}
if(debug) logger.log("创建语言配置文件<{}>成功",settingsFile)
if(debug) {
logger.log("生成语言配置文件:{}","./languages/settings.js")
logger.log("拟支持的语言:{}",settings.languages.map(l=>l.name).join(","))
logger.log("下一步:")
logger.log(" - 编辑{}确定拟支持的语言种类等参数","languages/settings.js")
logger.log(" - 运行<{}>扫描提取要翻译的文本","voerkai18n extract")
logger.log(" - 运行<{}>编译语言包","voerkai18n compile")
}
}

View File

@ -8,7 +8,7 @@
},
"author": "",
"bin": {
"voerkai18n": "./index.js"
"voerkai18n": "./index.js"
},
"license": "ISC",
"dependencies": {
@ -17,15 +17,12 @@
"@voerkai18n/runtime": "workspace:^1.0.0",
"art-template": "^4.13.2",
"commander": "^9.0.0",
"deepmerge": "^4.2.2",
"glob": "^7.2.0",
"logsets": "^1.0.6",
"gulp": "^4.0.2",
"logsets": "^1.0.7",
"readjson": "^2.2.2",
"through2": "^4.0.2",
"deepmerge": "^4.2.2",
"gulp": "^4.0.2",
"vinyl": "^2.2.1"
},
"devDependencies": {
}
}

8
pnpm-lock.yaml generated
View File

@ -94,7 +94,7 @@ importers:
deepmerge: ^4.2.2
glob: ^7.2.0
gulp: ^4.0.2
logsets: ^1.0.6
logsets: ^1.0.7
readjson: ^2.2.2
through2: ^4.0.2
vinyl: ^2.2.1
@ -107,7 +107,7 @@ importers:
deepmerge: 4.2.2
glob: 7.2.0
gulp: 4.0.2
logsets: 1.0.6
logsets: 1.0.7
readjson: 2.2.2
through2: 4.0.2
vinyl: 2.2.1
@ -4166,8 +4166,8 @@ packages:
resolution: {integrity: sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==}
dev: true
/logsets/1.0.6:
resolution: {integrity: sha512-tTtHpJ2pEVC3goIfqswDIaY5wwImPpfkfdKAcuQ6F6KdaDW3HvftL3X5WyFYAn02Ig/MPnLzgd/6DPm+ewFr3g==}
/logsets/1.0.7:
resolution: {integrity: sha512-O9VFAcMEuUY1tyq81hdZada+77fLl2Ff602AhlKpEpVr3gMyPFM67tILlBZZgnCfcmoRUo6nwhl26xXURHPH0A==}
dependencies:
'@babel/runtime-corejs3': registry.npmmirror.com/@babel/runtime-corejs3/7.17.2
ansicolor: registry.npmmirror.com/ansicolor/1.1.100