feat: manually mirror opencoze's code from bytedance

Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
fanlv
2025-07-20 17:36:12 +08:00
commit 890153324f
14811 changed files with 1923430 additions and 0 deletions

View File

@@ -0,0 +1,67 @@
# @coze-arch/rush-logger
rush logger
## Overview
This package is part of the Coze Studio monorepo and provides utilities functionality. It includes logger.
## Getting Started
### Installation
Add this package to your `package.json`:
```json
{
"dependencies": {
"@coze-arch/rush-logger": "workspace:*"
}
}
```
Then run:
```bash
rush update
```
### Usage
```typescript
import { /* exported functions/components */ } from '@coze-arch/rush-logger';
// Example usage
// TODO: Add specific usage examples
```
## Features
- Logger
## API Reference
### Exports
- `logger ;`
- `default logger;`
For detailed API documentation, please refer to the TypeScript definitions.
## Development
This package is built with:
- TypeScript
- Modern JavaScript
- Vitest for testing
- ESLint for code quality
## Contributing
This package is part of the Coze Studio monorepo. Please follow the monorepo contribution guidelines.
## License
Apache-2.0

View File

@@ -0,0 +1,95 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { vi } from 'vitest';
const mockWriteLine = vi.fn();
vi.mock('@rushstack/node-core-library', () => ({
Terminal: vi.fn().mockImplementation(() => ({ writeLine: mockWriteLine })),
ConsoleTerminalProvider: vi.fn(),
Colors: new Proxy(
{},
{
get(_, color: string) {
return () => color;
},
},
),
}));
describe('test log', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('info log', async () => {
const { logger } = await vi.importActual('../src');
const info = vi.fn(logger.info.bind(logger));
info('hello');
expect(info).toHaveBeenCalled();
expect(info).toHaveBeenCalledWith('hello');
const success = vi.fn(logger.success.bind(logger));
success('hello');
expect(success).toHaveBeenCalled();
expect(success).toHaveBeenCalledWith('hello');
logger.default('test');
expect(mockWriteLine).toBeCalledWith('test');
});
it('debug log', async () => {
const { logger } = await vi.importActual('../src');
const debug = vi.fn(logger.debug.bind(logger));
debug('hello');
expect(debug).toHaveBeenCalled();
expect(debug).toHaveBeenCalledWith('hello');
});
it('warning log', async () => {
const { logger } = await vi.importActual('../src');
const warning = vi.fn(logger.warning.bind(logger));
warning('hello');
expect(warning).toHaveBeenCalled();
expect(warning).toHaveBeenCalledWith('hello');
});
it('error log', async () => {
const { logger } = await vi.importActual('../src');
const error = vi.fn(logger.error.bind(logger));
error('hello');
expect(error).toHaveBeenCalled();
expect(error).toHaveBeenCalledWith('hello');
});
it('no prefix log', async () => {
const { logger } = await vi.importActual('../src');
const info = vi.fn(logger.info.bind(logger));
info('no prefix info', false);
expect(info).toHaveBeenCalled();
expect(info).toHaveBeenCalledWith('no prefix info', false);
});
it('turn on/off', async () => {
const { logger } = await vi.importActual('../src');
const info = vi.fn(logger.info.bind(logger));
logger.turnOff();
info('no prefix info', false);
expect(mockWriteLine).not.toHaveBeenCalled();
logger.turnOn();
info('test', false);
expect(mockWriteLine).toHaveBeenCalled();
});
});

View File

@@ -0,0 +1,12 @@
{
"operationSettings": [
{
"operationName": "test:cov",
"outputFolderNames": ["coverage"]
},
{
"operationName": "ts-check",
"outputFolderNames": ["./dist"]
}
]
}

View File

@@ -0,0 +1,6 @@
const { defineConfig } = require('@coze-arch/eslint-config');
module.exports = defineConfig({
preset: 'node',
packageRoot: __dirname,
});

View File

@@ -0,0 +1,30 @@
{
"name": "@coze-arch/rush-logger",
"version": "0.0.2-beta.6",
"description": "rush logger ",
"license": "Apache-2.0",
"author": "fanwenjie.fe@bytedance.com",
"main": "src/index.ts",
"types": "src/index.ts",
"files": [
"dist",
"CHANGELOG.md"
],
"scripts": {
"build": "exit 0",
"lint": "eslint ./src ./__tests__ --cache",
"test": "vitest --run",
"test:cov": "vitest run --coverage"
},
"dependencies": {
"@rushstack/node-core-library": "3.55.2"
},
"devDependencies": {
"@coze-arch/eslint-config": "workspace:*",
"@coze-arch/ts-config": "workspace:*",
"@coze-arch/vitest-config": "workspace:*",
"@vitest/coverage-v8": "~3.0.5",
"vitest": "~3.0.5"
}
}

View File

@@ -0,0 +1,84 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
Colors,
ConsoleTerminalProvider,
Terminal,
} from '@rushstack/node-core-library';
class Logger {
private terminal: Terminal;
private $silent = false;
constructor() {
this.terminal = new Terminal(new ConsoleTerminalProvider());
}
warning(content: string, prefix?: boolean) {
this.$writeLine(content, Colors.yellow, prefix, '[WARNING]');
}
debug(content: string, prefix?: boolean) {
this.$writeLine(content, Colors.bold, prefix, '[DEBUG]');
}
success(content: string, prefix?: boolean) {
this.$writeLine(content, Colors.green, prefix, '[SUCCESS]');
}
error(content: string, prefix?: boolean) {
this.$writeLine(content, Colors.red, prefix, '[ERROR]');
}
info(content: string, prefix?: boolean) {
this.$writeLine(content, Colors.blue, prefix, '[INFO]');
}
default(content: string) {
this.terminal.writeLine(content);
}
turnOff() {
this.$silent = true;
}
turnOn() {
this.$silent = false;
}
// eslint-disable-next-line max-params
private $writeLine(
content: string,
colorFn: typeof Colors.bold,
prefix?: boolean,
prefixText?: string,
) {
prefix = prefix ?? true;
const formattedContent = prefix ? `${prefixText} ${content}` : content;
if (this.$silent === true && prefixText !== '[ERROR]') {
// do nothings
return;
}
return this.terminal.writeLine(colorFn(`${formattedContent}`));
}
}
const logger = new Logger();
export { logger };
/** @deprecated 该使用方式已废弃,请使用`import { logger } from '@coze-arch/rush-logger' */
export default logger;

View File

@@ -0,0 +1,22 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "@coze-arch/ts-config/tsconfig.node.json",
"compilerOptions": {
"types": ["vitest/globals"],
"rootDir": "./src",
"outDir": "./dist",
"tsBuildInfoFile": "dist/tsconfig.build.tsbuildinfo"
},
"include": ["src"],
"references": [
{
"path": "../../../config/eslint-config/tsconfig.build.json"
},
{
"path": "../../../config/ts-config/tsconfig.build.json"
},
{
"path": "../../../config/vitest-config/tsconfig.build.json"
}
]
}

View File

@@ -0,0 +1,15 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"composite": true
},
"references": [
{
"path": "./tsconfig.build.json"
},
{
"path": "./tsconfig.misc.json"
}
],
"exclude": ["**/*"]
}

View File

@@ -0,0 +1,16 @@
{
"extends": "@coze-arch/ts-config/tsconfig.node.json",
"$schema": "https://json.schemastore.org/tsconfig",
"include": ["./__tests__", "vitest.config.ts"],
"exclude": ["./dist"],
"references": [
{
"path": "./tsconfig.build.json"
}
],
"compilerOptions": {
"rootDir": "./",
"types": ["vitest/globals"],
"outDir": "./dist"
}
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { defineConfig } from '@coze-arch/vitest-config';
export default defineConfig({
dirname: __dirname,
preset: 'node',
test: {
coverage: {
all: true,
},
},
});