diff --git a/CHANGELOG.md b/CHANGELOG.md index 4307445..e06d836 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # @wakeadmin/bbt +## 0.2.1 + +### Patch Changes + +- feat: 现在会强制将`Number`类型转成`String` + + ## 0.2.0 ### Minor Changes diff --git a/package.json b/package.json index 2fa8520..ae33d84 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@wakeadmin/bbt", - "version": "0.2.0", + "version": "0.2.1", "description": "用来维护多语言信息的工具", "bin": { "bbt": "./bin/bbt" diff --git a/src/cli/collectAction.ts b/src/cli/collectAction.ts index d5bb1cf..c09009b 100644 --- a/src/cli/collectAction.ts +++ b/src/cli/collectAction.ts @@ -20,10 +20,11 @@ import { CommandLineFlagParameter } from '@rushstack/ts-command-line'; import { BBTPlugins } from '../plugins'; import { getExcelCtor, IBBTValue } from '../utils/treeExcel'; import { BaseAction } from './baseAction'; +import { toString } from 'lodash/fp'; function createNode(value: string | any[] | Record, parent: KeyTreeNode, key: string) { const nodeType = - typeof value === 'string' || typeof value === 'number' || Array.isArray(value) + typeof value === 'string' || Array.isArray(value) ? KeyTreeNodeType.Leaf : KeyTreeNodeType.Node; return parent.addChild(key, nodeType); @@ -38,13 +39,13 @@ function setNodeValue( value: Record | string | any[]; } ) { - const { key, path, lang, value } = obj; + const { key, path, lang, value: originValue } = obj; + const value = typeof originValue === 'number' ? toString(originValue) : originValue; + let node = parent.getChild(key) || createNode(value, parent, key); - // TODO - // 是否强制把 number 转成 string - if (typeof value === 'string' || typeof value === 'number' || Array.isArray(value)) { + if (typeof value === 'string' || Array.isArray(value)) { node.assign({ path, [lang]: value, diff --git a/src/cli/writeAction.ts b/src/cli/writeAction.ts index ac7e039..d77ec9c 100644 --- a/src/cli/writeAction.ts +++ b/src/cli/writeAction.ts @@ -4,6 +4,7 @@ import ora from 'ora'; import path from 'path'; import { strToArray, IBBTValue } from '../utils'; import { BaseAction } from './baseAction'; +import { toString } from 'lodash/fp'; /** * @@ -135,17 +136,20 @@ export class WriteAction extends BaseAction { .sort(compareFn) .reduce>((obj, key) => { const originalValue = record[key] ?? ''; - const value = - typeof originalValue === 'string' - ? strToArray(originalValue) - : Array.isArray(originalValue) - ? originalValue - : typeof originalValue === 'object' - ? this.deepSortRecord(originalValue) - : originalValue; - + const value = this.parseValue(originalValue); obj[key] = value; return obj; }, {}); } + + private parseValue(originalValue: any) { + switch (typeof originalValue) { + case 'string': + return strToArray(originalValue); + case 'object': + return this.deepSortRecord(originalValue); + default: + return toString(originalValue); + } + } } diff --git a/src/test/action/buildAction.test.ts b/src/test/action/buildAction.test.ts index 3018980..4bc49ba 100644 --- a/src/test/action/buildAction.test.ts +++ b/src/test/action/buildAction.test.ts @@ -3,9 +3,13 @@ import { setWith } from 'lodash'; import path from 'path'; import { BBTToolCommandLineParser } from '../../cli/commandLine'; import { BBTExcel, strToArray } from '../../utils'; +import { toString } from 'lodash/fp'; +import { sleep } from '../test-helper'; const parser: BBTToolCommandLineParser = new BBTToolCommandLineParser(); + + const data = [ ['sss', 'vvv', `丹唇外朗,皓齿内鲜`, ''], ['sss', 'aaaa', '髣髴兮若轻云之蔽月,飘飖兮若流风之回雪', ''], @@ -22,13 +26,15 @@ const zhData = data.reduce((obj, item) => { }, {} as Record); const enData = data.reduce((obj, item) => { - setWith(obj, item[1], item[3]); + setWith(obj, item[1], typeof item[3] === 'number' ? toString(item[3]) : item[3]); return obj; }, {} as Record); const langs = ['zh', 'en']; -const outBasePath = path.join(__dirname, './temp/tr'); +const dirPath = "./temp/write"; + +const outBasePath = path.join(__dirname, `${dirPath}/tr`); function createExcelFileIfNeed(file: string): Promise { if (!fse.existsSync(file)) { @@ -42,7 +48,7 @@ function createExcelFileIfNeed(file: string): Promise { async function createExcel(): Promise { const excel = new BBTExcel(); - const filePath = path.join(__dirname, './temp/excel/bbt.xlsx'); + const filePath = path.join(__dirname, `${dirPath}/excel/bbt.xlsx`); await createExcelFileIfNeed(filePath); await excel.readFile(filePath); data.forEach(row => { @@ -58,28 +64,22 @@ function getFileValue(path: string): Record { describe('build action', () => { beforeEach(() => { - fse.emptyDirSync(path.join(__dirname, './temp/excel')); + fse.emptyDirSync(path.join(__dirname, `${dirPath}/excel`)); fse.emptyDirSync(path.join(outBasePath, './sss')); }); afterEach(() => { - fse.emptyDirSync(path.join(__dirname, './temp/excel')); + fse.emptyDirSync(path.join(__dirname, `${dirPath}/excel`)); fse.emptyDirSync(path.join(outBasePath, './sss')); }); test('还原tr', async () => { await createExcel(); await sleep(200); - await parser.execute(['write', '-c', path.join(__dirname, './temp/config.js')]); + const a = path.join(__dirname, `${dirPath}/config.js`); + await parser.execute(['write', '-c', path.join(__dirname, `${dirPath}/config.js`)]); await sleep(200); expect(getFileValue(path.join(outBasePath, './sss/zh.tr'))).toEqual(zhData); expect(getFileValue(path.join(outBasePath, './sss/en.tr'))).toEqual(enData); }); }); -async function sleep(ms: number): Promise { - return new Promise(resolve => - setTimeout(() => { - resolve(); - }, ms) - ); -} diff --git a/src/test/action/collectionAction.test.ts b/src/test/action/collectionAction.test.ts index fbdc21a..83eb262 100644 --- a/src/test/action/collectionAction.test.ts +++ b/src/test/action/collectionAction.test.ts @@ -1,7 +1,14 @@ import { CollectAction } from '../../cli/collectAction'; -import { KeyTree, KeyTreeNodeType } from '../../utils'; +import { BBTToolCommandLineParser } from '../../cli/commandLine'; +import { KeyTree, KeyTreeNodeType, getExcel } from '../../utils'; import { DiffModeEnum, diffTree } from '../../utils/diffTree'; +import fse from 'fs-extra'; +import path from 'path'; +import { sleep } from '../test-helper'; + +const parser: BBTToolCommandLineParser = new BBTToolCommandLineParser(); + class TestAction extends CollectAction { config = { langs: ['zh', 'en'], @@ -10,7 +17,7 @@ class TestAction extends CollectAction { return super.createDiffer(mode); } } -const source = new KeyTree<{ key: string; path: string; zh: string; en: string } >(); +const source = new KeyTree<{ key: string; path: string; zh: string; en: string }>(); source.add('s', KeyTreeNodeType.Node); source.add('s.A', KeyTreeNodeType.Leaf).setValue({ key: 's.A', path: './temp', zh: '雨', en: 'rain' }); @@ -38,3 +45,61 @@ describe('CollectionAction Differ', () => { expect(result.get('s.A')!.getValue()).toEqual({ key: 's.A', path: './temp', zh: '雷', en: '' }); }); }); + +function createTrFile(path: string, value: Record) { + fse.ensureFileSync(path); + fse.writeFile(path, JSON.stringify(value), { encoding: 'utf8' }); +} + +function createConfig(path: string, value: string) { + fse.ensureFileSync(path); + fse.writeFile(path, value, { encoding: 'utf8' }); +} + +describe('CollectionAction', () => { + const dirPath = './temp/collect'; + const fullDirPath = path.join(__dirname, dirPath); + const excelPath = './src/test/action/temp/collect/bbt.csv'; + beforeEach(() => { + fse.emptyDirSync(fullDirPath); + }); + afterEach(() => { + fse.emptyDirSync(fullDirPath); + }); + + test('Relaxed', async () => { + createConfig( + path.join(fullDirPath, './config.js'), + `module.exports = { + langs: ['zh', 'en'], + src: './src/test/action/temp/collect', + test: '.*\\\\.tr', + exclude: ['node_modules'], + bbtExcelPath: '${excelPath}', + + }; + ` + ); + + createTrFile(path.join(fullDirPath, './zh.tr'), { + a: '5', + }); + + createTrFile(path.join(fullDirPath, './en.tr'), { + a: 5, + }); + + await sleep(200); + await parser.execute(['collect', '-c', path.join(fullDirPath, './config.js')]); + await sleep(200); + + const excel = await getExcel(excelPath); + + const tree = excel.toTree(); + + const value = tree.get('a')?.getValue() + + expect(value.zh).toEqual("5"); + expect(value.en).toEqual("5"); + }); +}); diff --git a/src/test/action/temp/config.js b/src/test/action/temp/config.js deleted file mode 100644 index 7b288be..0000000 --- a/src/test/action/temp/config.js +++ /dev/null @@ -1,10 +0,0 @@ -module.exports = { - langs: ['zh', 'en'], - resourcePath: './src/test/action/temp/tr', - test: '.*\\.tr', - src: './src/test/action/temp/tr', - exclude: [], - bbtExcelPath: './src/test/action/temp/excel/bbt.xlsx', - diffMode: 'relaxed', - outFileExtName: 'tr', -}; diff --git a/src/test/action/temp/write/config.js b/src/test/action/temp/write/config.js new file mode 100644 index 0000000..783f71a --- /dev/null +++ b/src/test/action/temp/write/config.js @@ -0,0 +1,10 @@ +module.exports = { + langs: ['zh', 'en'], + resourcePath: './src/test/action/temp/write/tr', + test: '.*\\.tr', + src: './src/test/action/temp/write/tr', + exclude: [], + bbtExcelPath: './src/test/action/temp/write/excel/bbt.xlsx', + diffMode: 'relaxed', + outFileExtName: 'tr', +}; diff --git a/src/test/test-helper/index.ts b/src/test/test-helper/index.ts new file mode 100644 index 0000000..8ebf6f8 --- /dev/null +++ b/src/test/test-helper/index.ts @@ -0,0 +1,8 @@ + +export async function sleep(ms: number): Promise { + return new Promise(resolve => + setTimeout(() => { + resolve(); + }, ms) + ); +} \ No newline at end of file diff --git a/src/utils/treeExcel.ts b/src/utils/treeExcel.ts index 87a56d1..daaefae 100644 --- a/src/utils/treeExcel.ts +++ b/src/utils/treeExcel.ts @@ -2,6 +2,7 @@ import { CellValue, Row, Workbook, Worksheet } from 'exceljs'; import fse from 'fs-extra'; import { dirname, extname } from 'path'; import { KeyTree, KeyTreeNodeType } from './keyTree'; +import { toString } from 'lodash/fp'; const SHEET_NAME = 'BBT'; @@ -163,7 +164,7 @@ export class BBTExcel { throw new Error(`value is Object -> rows: ${row.number}; key: ${key} `); } - return value as string; + return toString(value); } }