Skip to content

Commit

Permalink
feat: 现在会强制将Number类型转成String
Browse files Browse the repository at this point in the history
Change-Id: I70d3380686d83382dc530408ee543aba3a70cfe4
  • Loading branch information
njikm2010 committed Dec 14, 2023
1 parent 31bd9f9 commit 54002dd
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 41 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @wakeadmin/bbt

## 0.2.1

### Patch Changes

- feat: 现在会强制将`Number`类型转成`String`


## 0.2.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wakeadmin/bbt",
"version": "0.2.0",
"version": "0.2.1",
"description": "用来维护多语言信息的工具",
"bin": {
"bbt": "./bin/bbt"
Expand Down
11 changes: 6 additions & 5 deletions src/cli/collectAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>, parent: KeyTreeNode<IBBTValue>, 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);
Expand All @@ -38,13 +39,13 @@ function setNodeValue(
value: Record<string, any> | 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,
Expand Down
22 changes: 13 additions & 9 deletions src/cli/writeAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
*
Expand Down Expand Up @@ -135,17 +136,20 @@ export class WriteAction extends BaseAction {
.sort(compareFn)
.reduce<Record<string, any>>((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);
}
}
}
26 changes: 13 additions & 13 deletions src/test/action/buildAction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', '髣髴兮若轻云之蔽月,飘飖兮若流风之回雪', ''],
Expand All @@ -22,13 +26,15 @@ const zhData = data.reduce((obj, item) => {
}, {} as Record<string, string>);

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<string, string>);

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<void> {
if (!fse.existsSync(file)) {
Expand All @@ -42,7 +48,7 @@ function createExcelFileIfNeed(file: string): Promise<void> {

async function createExcel(): Promise<void> {
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 => {
Expand All @@ -58,28 +64,22 @@ function getFileValue(path: string): Record<string, string> {

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<void> {
return new Promise(resolve =>
setTimeout(() => {
resolve();
}, ms)
);
}
69 changes: 67 additions & 2 deletions src/test/action/collectionAction.test.ts
Original file line number Diff line number Diff line change
@@ -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'],
Expand All @@ -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' });
Expand Down Expand Up @@ -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<string, any>) {
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");
});
});
10 changes: 0 additions & 10 deletions src/test/action/temp/config.js

This file was deleted.

10 changes: 10 additions & 0 deletions src/test/action/temp/write/config.js
Original file line number Diff line number Diff line change
@@ -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',
};
8 changes: 8 additions & 0 deletions src/test/test-helper/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

export async function sleep(ms: number): Promise<void> {
return new Promise(resolve =>
setTimeout(() => {
resolve();
}, ms)
);
}
3 changes: 2 additions & 1 deletion src/utils/treeExcel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -163,7 +164,7 @@ export class BBTExcel<T extends IBBTValue = any> {
throw new Error(`value is Object -> rows: ${row.number}; key: ${key} `);
}

return value as string;
return toString(value);
}
}

Expand Down

0 comments on commit 54002dd

Please sign in to comment.