@ansstory/hias 1.0.7 → 1.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/core/translate.js
CHANGED
|
@@ -288,7 +288,8 @@ function resolveName(options, fallbackPath) {
|
|
|
288
288
|
* 生成 locale maps (textToKey + 各语言的 map)
|
|
289
289
|
*/
|
|
290
290
|
function generateLocaleMaps(name, uniqueTexts, translatedTexts, translationPerformed, targetLocale) {
|
|
291
|
-
const map = { [name]: {} }
|
|
291
|
+
const map = { [name]: {} } // target locale: key -> translated text
|
|
292
|
+
const sourceMap = { [name]: {} } // source locale: key -> original Chinese
|
|
292
293
|
const textToKey = new Map()
|
|
293
294
|
|
|
294
295
|
if (translationPerformed) {
|
|
@@ -297,6 +298,7 @@ function generateLocaleMaps(name, uniqueTexts, translatedTexts, translationPerfo
|
|
|
297
298
|
const subKey = subKeys[index]
|
|
298
299
|
if (!textToKey.has(text)) textToKey.set(text, `${name}.${subKey}`)
|
|
299
300
|
map[name][subKey] = translatedTexts[index]
|
|
301
|
+
sourceMap[name][subKey] = text
|
|
300
302
|
})
|
|
301
303
|
} else {
|
|
302
304
|
const keyCount = new Map()
|
|
@@ -311,10 +313,11 @@ function generateLocaleMaps(name, uniqueTexts, translatedTexts, translationPerfo
|
|
|
311
313
|
usedFallbackKeys.add(subKey)
|
|
312
314
|
if (!textToKey.has(text)) textToKey.set(text, `${name}.${subKey}`)
|
|
313
315
|
map[name][subKey] = text
|
|
316
|
+
sourceMap[name][subKey] = text
|
|
314
317
|
})
|
|
315
318
|
}
|
|
316
319
|
|
|
317
|
-
return { map, textToKey }
|
|
320
|
+
return { map, sourceMap, textToKey }
|
|
318
321
|
}
|
|
319
322
|
|
|
320
323
|
/**
|
|
@@ -491,7 +494,7 @@ async function handleTranslateFileAction(filePath, options) {
|
|
|
491
494
|
// source map 仅第一次写入
|
|
492
495
|
if (!localeMaps[fromLang]) localeMaps[fromLang] = {}
|
|
493
496
|
if (!localeMaps[fromLang][name]) localeMaps[fromLang][name] = {}
|
|
494
|
-
Object.assign(localeMaps[fromLang][name], result.
|
|
497
|
+
Object.assign(localeMaps[fromLang][name], result.sourceMap[name])
|
|
495
498
|
localeMaps[toLang] = result.map
|
|
496
499
|
}
|
|
497
500
|
|
|
@@ -528,7 +531,7 @@ async function handleTranslateFileAction(filePath, options) {
|
|
|
528
531
|
? applyJsonTranslation(content, extractions, localeMaps[primaryTarget] || localeMaps[fromLang], name)
|
|
529
532
|
: applyReplacements(content, extractions, name, fileType, settings.i18nCallTemplate)
|
|
530
533
|
|
|
531
|
-
await saveBackupSync(name, settings.replaceOriginalFile ? [fullPath] : [], cwd, settings.replaceOriginalFile)
|
|
534
|
+
await saveBackupSync(name, settings.replaceOriginalFile ? [fullPath] : [], cwd, settings.replaceOriginalFile, settings.outDir)
|
|
532
535
|
writeTranslatedFile(fullPath, translatedContent, outDir, settings)
|
|
533
536
|
|
|
534
537
|
if (settings.replaceOriginalFile) {
|
|
@@ -698,7 +701,7 @@ async function handleTranslateFolderAction(folderPath, options) {
|
|
|
698
701
|
if (!textToKey) textToKey = result.textToKey
|
|
699
702
|
if (!localeMaps[fromLang]) localeMaps[fromLang] = {}
|
|
700
703
|
if (!localeMaps[fromLang][name]) localeMaps[fromLang][name] = {}
|
|
701
|
-
Object.assign(localeMaps[fromLang][name], result.
|
|
704
|
+
Object.assign(localeMaps[fromLang][name], result.sourceMap[name])
|
|
702
705
|
localeMaps[toLang] = result.map
|
|
703
706
|
if (translationPerformed) anyTranslated = true
|
|
704
707
|
}
|
|
@@ -749,7 +752,7 @@ async function handleTranslateFolderAction(folderPath, options) {
|
|
|
749
752
|
|
|
750
753
|
// 备份所有原文件用于回滚
|
|
751
754
|
if (fileEntries.length > 0) {
|
|
752
|
-
await saveBackupSync(name, settings.replaceOriginalFile ? fileEntries.map((e) => e.file) : [], cwd, settings.replaceOriginalFile)
|
|
755
|
+
await saveBackupSync(name, settings.replaceOriginalFile ? fileEntries.map((e) => e.file) : [], cwd, settings.replaceOriginalFile, settings.outDir)
|
|
753
756
|
}
|
|
754
757
|
|
|
755
758
|
// 写入每个翻译后的文件
|
|
@@ -857,7 +860,7 @@ const BACKUP_ROOT = '.hias/.langbackup'
|
|
|
857
860
|
/**
|
|
858
861
|
* 备份原文件内容到 .hias/.langbackup/<timestamp>_<name>/manifest.json,保留所有历史
|
|
859
862
|
*/
|
|
860
|
-
async function saveBackupSync(name, filePaths, cwd, replaceOriginalFile) {
|
|
863
|
+
async function saveBackupSync(name, filePaths, cwd, replaceOriginalFile, outDir) {
|
|
861
864
|
const now = new Date()
|
|
862
865
|
const ts = `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')}_${String(now.getHours()).padStart(2, '0')}${String(now.getMinutes()).padStart(2, '0')}${String(now.getSeconds()).padStart(2, '0')}`
|
|
863
866
|
const backupDir = path.join(cwd, BACKUP_ROOT, `${ts}_${name}`)
|
|
@@ -876,7 +879,7 @@ async function saveBackupSync(name, filePaths, cwd, replaceOriginalFile) {
|
|
|
876
879
|
timestamp: now.toISOString(),
|
|
877
880
|
replaceOriginalFile,
|
|
878
881
|
files,
|
|
879
|
-
outputDir: path.join('.hias
|
|
882
|
+
outputDir: path.join(outDir || '.hias/lang', name).replace(/\\/g, '/'),
|
|
880
883
|
}
|
|
881
884
|
|
|
882
885
|
await fsp.writeFile(path.join(backupDir, 'manifest.json'), JSON.stringify(manifest, null, 2), 'utf-8')
|
package/package.json
CHANGED
|
@@ -3,6 +3,10 @@ const os = require('os')
|
|
|
3
3
|
const path = require('path')
|
|
4
4
|
const { spawnSync } = require('child_process')
|
|
5
5
|
|
|
6
|
+
function getNpmCommand(platform = process.platform) {
|
|
7
|
+
return platform === 'win32' ? 'npm.cmd' : 'npm'
|
|
8
|
+
}
|
|
9
|
+
|
|
6
10
|
function getConfigDirectory(homeDir = os.homedir()) {
|
|
7
11
|
return path.join(homeDir, '.hias-cli')
|
|
8
12
|
}
|
|
@@ -28,9 +32,16 @@ function clearConfigDirectory({ homeDir = os.homedir(), logger = console, dryRun
|
|
|
28
32
|
function uninstallPackage({ homeDir = os.homedir(), logger = console, runCommand = spawnSync } = {}) {
|
|
29
33
|
clearConfigDirectory({ homeDir, logger })
|
|
30
34
|
|
|
31
|
-
const
|
|
35
|
+
const command = getNpmCommand()
|
|
36
|
+
const args = ['uninstall', '@ansstory/hias', '-g']
|
|
37
|
+
|
|
38
|
+
if (logger) {
|
|
39
|
+
logger.log(`Running: npm ${args.join(' ')}`)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const result = runCommand(command, args, {
|
|
32
43
|
stdio: 'inherit',
|
|
33
|
-
shell:
|
|
44
|
+
shell: false,
|
|
34
45
|
})
|
|
35
46
|
|
|
36
47
|
if (typeof result.status === 'number' && result.status !== 0) {
|
|
@@ -64,6 +75,7 @@ if (require.main === module) {
|
|
|
64
75
|
|
|
65
76
|
module.exports = {
|
|
66
77
|
clearConfigDirectory,
|
|
78
|
+
getNpmCommand,
|
|
67
79
|
getConfigDirectory,
|
|
68
80
|
uninstallPackage,
|
|
69
81
|
printUsage,
|
|
@@ -4,7 +4,7 @@ const fs = require('fs')
|
|
|
4
4
|
const os = require('os')
|
|
5
5
|
const path = require('path')
|
|
6
6
|
|
|
7
|
-
const { clearConfigDirectory, getConfigDirectory, uninstallPackage } = require('../scripts/cleanup-config')
|
|
7
|
+
const { clearConfigDirectory, getConfigDirectory, getNpmCommand, uninstallPackage } = require('../scripts/cleanup-config')
|
|
8
8
|
|
|
9
9
|
test('clearConfigDirectory removes the hias cli config directory', () => {
|
|
10
10
|
const tempHome = fs.mkdtempSync(path.join(os.tmpdir(), 'hias-clear-'))
|
|
@@ -56,6 +56,7 @@ test('uninstallPackage clears config before uninstalling the global package', ()
|
|
|
56
56
|
homeDir: tempHome,
|
|
57
57
|
logger: null,
|
|
58
58
|
runCommand: (command, args, options) => {
|
|
59
|
+
assert.equal(fs.existsSync(configDir), false)
|
|
59
60
|
calls.push({ command, args, options })
|
|
60
61
|
return { status: 0 }
|
|
61
62
|
},
|
|
@@ -64,9 +65,9 @@ test('uninstallPackage clears config before uninstalling the global package', ()
|
|
|
64
65
|
assert.equal(fs.existsSync(configDir), false)
|
|
65
66
|
assert.deepEqual(calls, [
|
|
66
67
|
{
|
|
67
|
-
command:
|
|
68
|
+
command: getNpmCommand(),
|
|
68
69
|
args: ['uninstall', '@ansstory/hias', '-g'],
|
|
69
|
-
options: { stdio: 'inherit', shell:
|
|
70
|
+
options: { stdio: 'inherit', shell: false },
|
|
70
71
|
},
|
|
71
72
|
])
|
|
72
73
|
})
|
package/test/translate.test.js
CHANGED
|
@@ -507,7 +507,7 @@ test('saveBackupSync creates backup directory with manifest', async () => {
|
|
|
507
507
|
fs.writeFileSync(testFile, 'content', 'utf-8')
|
|
508
508
|
|
|
509
509
|
try {
|
|
510
|
-
await saveBackupSync('testns', [testFile], cwd, true)
|
|
510
|
+
await saveBackupSync('testns', [testFile], cwd, true, '.hias/lang')
|
|
511
511
|
|
|
512
512
|
const backupRoot = path.join(cwd, '.hias', '.langbackup')
|
|
513
513
|
assert.ok(fs.existsSync(backupRoot), `backup root should exist at ${backupRoot}`)
|