linkmore-intl-generator
Advanced tools
Comparing version 0.0.15 to 0.0.16
@@ -20,2 +20,3 @@ "use strict"; | ||
const node_crypto_1 = require("node:crypto"); | ||
const node_readline_1 = require("node:readline"); | ||
function makeDeferred() { | ||
@@ -56,2 +57,24 @@ const deferred = {}; | ||
} | ||
readFileByLine(filepath, onLine) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
yield fs_1.default.promises.access(filepath); | ||
const rl = (0, node_readline_1.createInterface)(fs_1.default.createReadStream(filepath)); | ||
const deferred = makeDeferred(); | ||
const lines = []; | ||
rl.on('line', (input) => { | ||
lines.push(input); | ||
onLine === null || onLine === void 0 ? void 0 : onLine(input); | ||
}); | ||
rl.on('close', () => { | ||
deferred.resolve(lines); | ||
}); | ||
return deferred.promise; | ||
} | ||
catch (error) { | ||
console.error('warning warning!!!!', error); | ||
return []; | ||
} | ||
}); | ||
} | ||
// 获取config文件信息 | ||
@@ -63,6 +86,15 @@ getExistInfo(filepath) { | ||
yield fs_1.default.promises.access(filepath); | ||
const file = yield fs_1.default.promises.readFile(filepath, 'utf-8'); | ||
const start = file.indexOf('defineMessages('); | ||
const end = file.indexOf('as const'); | ||
const content = file.slice(start + 'defineMessages('.length, end); | ||
let filteredTypeContent = ''; | ||
yield this.readFileByLine(filepath, (line) => { | ||
if (line.includes('type')) { | ||
return; | ||
} | ||
filteredTypeContent += line; | ||
}); | ||
const start = filteredTypeContent.indexOf('defineMessages('); | ||
const end = filteredTypeContent.indexOf('as const'); | ||
const content = filteredTypeContent.slice(start + 'defineMessages('.length, end); | ||
if (!content) { | ||
return {}; | ||
} | ||
const formatS = `const data=${content}; module.exports = data`; | ||
@@ -77,2 +109,3 @@ const Module = module.constructor; | ||
catch (error) { | ||
console.error('warning warning!!!!', error); | ||
return {}; | ||
@@ -82,4 +115,4 @@ } | ||
} | ||
// 生成config文件 | ||
genGrabFile(chineseList, englishList, configFilepath) { | ||
// 合并新老数据 | ||
genMergeDefineData(chineseList, englishList, configFilepath) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
@@ -130,13 +163,25 @@ const { existData = {}, existChineseList = [] } = yield this.getExistInfo(configFilepath); | ||
}); | ||
const template = `/* eslint-disable max-lines */ | ||
import { defineMessages } from 'linkmore-intl'; | ||
const configs = defineMessages(${JSON.stringify(newResult, null, 2)} as const); | ||
export default configs; | ||
`; | ||
yield fs_1.default.promises.writeFile(configFilepath, template); | ||
return newResult; | ||
}); | ||
} | ||
genGrabFile(chineseList, englishList, configFilepath) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const newResult = yield this.genMergeDefineData(chineseList, englishList, configFilepath); | ||
let prefixTemplate = `/* eslint-disable max-lines */ | ||
import { defineMessages } from 'linkmore-intl';`; | ||
let suffixTemplate = `export default configs;`; | ||
const lines = yield this.readFileByLine(configFilepath); | ||
if (lines.length) { | ||
const start = lines.findIndex((line) => line.includes('defineMessages(')); | ||
const end = lines.findIndex((line) => line.includes('as const')); | ||
prefixTemplate = lines.slice(0, start).join('\n'); | ||
suffixTemplate = lines.slice(end + 1).join('\n'); | ||
} | ||
const template = `${prefixTemplate} | ||
const configs = defineMessages(${JSON.stringify(newResult, null, 2)} as const); | ||
${suffixTemplate} | ||
`; | ||
yield fs_1.default.promises.writeFile(configFilepath, template); | ||
}); | ||
} | ||
getChineseList(sourceFile) { | ||
@@ -335,2 +380,3 @@ const project = new ts_morph_1.Project({}); | ||
const texts = this.getChineseList(this.options.filepath); | ||
console.log('all chinese list', texts); | ||
const result = yield this.translationLanguage('zh', 'en', texts); | ||
@@ -342,2 +388,3 @@ yield this.genGrabFile(texts, result || [], this.options.intlConfigPath); | ||
}); | ||
console.log('all file done'); | ||
}); | ||
@@ -348,3 +395,3 @@ } | ||
// const filepath = path.resolve(process.cwd(), './Table/**/*.ts(x)?'); | ||
// const filepath = path.resolve(process.cwd(), './2.ts'); | ||
// const filepath = path.resolve(process.cwd(), './1.tsx'); | ||
// const configPath = path.resolve(process.cwd(), './common.ts'); | ||
@@ -351,0 +398,0 @@ // const util = new GenerateIntl({ |
{ | ||
"name": "linkmore-intl-generator", | ||
"version": "0.0.15", | ||
"version": "0.0.16", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "keywords": [], |
@@ -11,2 +11,3 @@ import fs from 'fs'; | ||
import { createHash } from 'node:crypto' | ||
import { createInterface } from 'node:readline'; | ||
@@ -82,2 +83,23 @@ interface IDeferred<T> { | ||
async readFileByLine(filepath: string, onLine?: (line: string) => void): Promise<string[]> { | ||
try { | ||
await fs.promises.access(filepath); | ||
const rl = createInterface(fs.createReadStream(filepath)) | ||
const deferred = makeDeferred<string[]>() | ||
const lines: string[] = [] | ||
rl.on('line', (input) => { | ||
lines.push(input) | ||
onLine?.(input) | ||
}) | ||
rl.on('close', () => { | ||
deferred.resolve(lines) | ||
}) | ||
return deferred.promise | ||
} catch (error) { | ||
console.error('warning warning!!!!', error); | ||
return []; | ||
} | ||
} | ||
// 获取config文件信息 | ||
@@ -87,6 +109,15 @@ async getExistInfo(filepath: string) { | ||
await fs.promises.access(filepath); | ||
const file = await fs.promises.readFile(filepath, 'utf-8'); | ||
const start = file.indexOf('defineMessages('); | ||
const end = file.indexOf('as const'); | ||
const content = file.slice(start + 'defineMessages('.length, end); | ||
let filteredTypeContent: string = '' | ||
await this.readFileByLine(filepath, (line) => { | ||
if (line.includes('type')) { | ||
return | ||
} | ||
filteredTypeContent += line | ||
}) | ||
const start = filteredTypeContent.indexOf('defineMessages('); | ||
const end = filteredTypeContent.indexOf('as const'); | ||
const content = filteredTypeContent.slice(start + 'defineMessages('.length, end); | ||
if (!content) { | ||
return {} | ||
} | ||
const formatS = `const data=${content}; module.exports = data`; | ||
@@ -102,2 +133,3 @@ const Module: any = module.constructor; | ||
} catch (error) { | ||
console.error('warning warning!!!!', error); | ||
return {}; | ||
@@ -107,4 +139,4 @@ } | ||
// 生成config文件 | ||
async genGrabFile( | ||
// 合并新老数据 | ||
async genMergeDefineData( | ||
chineseList: string[], | ||
@@ -162,17 +194,33 @@ englishList: string[], | ||
}); | ||
return newResult; | ||
} | ||
const template = `/* eslint-disable max-lines */ | ||
import { defineMessages } from 'linkmore-intl'; | ||
async genGrabFile( | ||
chineseList: string[], | ||
englishList: string[], | ||
configFilepath: string | ||
){ | ||
const newResult = await this.genMergeDefineData(chineseList, englishList, configFilepath) | ||
let prefixTemplate = `/* eslint-disable max-lines */ | ||
import { defineMessages } from 'linkmore-intl';` | ||
let suffixTemplate = `export default configs;` | ||
const configs = defineMessages(${JSON.stringify( | ||
newResult, | ||
null, | ||
2 | ||
)} as const); | ||
export default configs; | ||
`; | ||
const lines = await this.readFileByLine(configFilepath) | ||
if (lines.length) { | ||
const start = lines.findIndex((line) => line.includes('defineMessages(')) | ||
const end = lines.findIndex((line) => line.includes('as const')) | ||
prefixTemplate = lines.slice(0, start).join('\n') | ||
suffixTemplate = lines.slice(end + 1).join('\n') | ||
} | ||
const template = `${prefixTemplate} | ||
const configs = defineMessages(${JSON.stringify( | ||
newResult, | ||
null, | ||
2 | ||
)} as const); | ||
${suffixTemplate} | ||
` | ||
await fs.promises.writeFile(configFilepath, template); | ||
return newResult; | ||
} | ||
@@ -184,5 +232,7 @@ | ||
const result: string[] = []; | ||
sources.forEach((file) => { | ||
result.push(...this.getFileChineseList(file)); | ||
}); | ||
return result; | ||
@@ -393,2 +443,4 @@ } | ||
const texts = this.getChineseList(this.options.filepath); | ||
console.log('all chinese list', texts); | ||
const result = await this.translationLanguage('zh', 'en', texts); | ||
@@ -400,2 +452,3 @@ await this.genGrabFile(texts, result || [], this.options.intlConfigPath); | ||
}); | ||
console.log('all file done') | ||
} | ||
@@ -407,3 +460,3 @@ } | ||
// const filepath = path.resolve(process.cwd(), './Table/**/*.ts(x)?'); | ||
// const filepath = path.resolve(process.cwd(), './2.ts'); | ||
// const filepath = path.resolve(process.cwd(), './1.tsx'); | ||
@@ -410,0 +463,0 @@ // const configPath = path.resolve(process.cwd(), './common.ts'); |
62169
1386