repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | import { |
| #2 | Dirent, |
| #3 | existsSync, |
| #4 | mkdirSync, |
| #5 | readFileSync, |
| #6 | readdirSync, |
| #7 | statSync, |
| #8 | unlinkSync, |
| #9 | writeFileSync, |
| #10 | } from 'node:fs'; |
| #11 | import { extname, join, resolve } from 'node:path'; |
| #12 | |
| #13 | import { config } from '../core/constants'; |
| #14 | import { Logger } from './logger'; |
| #15 | |
| #16 | export { readJSONSync } from 'fs-extra'; |
| #17 | |
| #18 | /** |
| #19 | * 规范化语言代码 |
| #20 | * @param locale 语言代码 |
| #21 | * @returns 标准化后的语言代码 |
| #22 | */ |
| #23 | export const normalizeLocale = (locale?: string): string => { |
| #24 | if (!locale) return 'en-US'; |
| #25 | |
| #26 | if (locale.startsWith('ar')) return 'ar'; |
| #27 | if (locale.startsWith('cn')) return 'zh-CN'; |
| #28 | |
| #29 | for (const l of config.outputLocales) { |
| #30 | if (l.startsWith(locale)) { |
| #31 | return l; |
| #32 | } |
| #33 | } |
| #34 | |
| #35 | return 'en-US'; |
| #36 | }; |
| #37 | |
| #38 | /** |
| #39 | * 写入 JSON 文件 |
| #40 | * @param filePath 文件路径 |
| #41 | * @param data 数据对象 |
| #42 | * @param format 是否格式化 |
| #43 | */ |
| #44 | export const writeJSON = (filePath: string, data: any, format = true) => { |
| #45 | const jsonStr = format ? JSON.stringify(data, null, 2) : JSON.stringify(data); |
| #46 | writeFileSync(filePath, jsonStr, 'utf8'); |
| #47 | }; |
| #48 | |
| #49 | /** |
| #50 | * 检查并创建目录 |
| #51 | * @param dirpath 目录路径 |
| #52 | */ |
| #53 | export const checkDir = (dirpath: string) => { |
| #54 | if (!existsSync(dirpath)) mkdirSync(dirpath, { recursive: true }); |
| #55 | }; |
| #56 | |
| #57 | /** |
| #58 | * 检查是否为 JSON 文件 |
| #59 | * @param file 文件信息 |
| #60 | * @returns 是否为 JSON 文件 |
| #61 | */ |
| #62 | export const checkJSON = (file: Dirent): boolean => { |
| #63 | return file.isFile() && file.name?.endsWith('.json'); |
| #64 | }; |
| #65 | |
| #66 | /** |
| #67 | * 删除空的 JSON 文件 |
| #68 | * @param dir 目录路径 |
| #69 | */ |
| #70 | export const deleteEmptyJsonFiles = (dir: string) => { |
| #71 | const files = readdirSync(dir); |
| #72 | files.forEach((file) => { |
| #73 | const filePath = resolve(dir, file); |
| #74 | const stats = statSync(filePath); |
| #75 | if (stats.isDirectory()) { |
| #76 | deleteEmptyJsonFiles(filePath); // 递归处理子目录 |
| #77 | } else if (stats.isFile() && extname(file) === '.json') { |
| #78 | const data = readFileSync(filePath, 'utf8').trim(); |
| #79 | if (!data || data === '' || data === '{}' || data === '[]') { |
| #80 | unlinkSync(filePath); |
| #81 | Logger.file('delete', filePath); |
| #82 | } |
| #83 | } |
| #84 | }); |
| #85 | }; |
| #86 | |
| #87 | /** |
| #88 | * 获取本地化 Agent 文件名 |
| #89 | * @param id Agent ID |
| #90 | * @param locale 语言代码 |
| #91 | * @returns 文件名 |
| #92 | */ |
| #93 | export const getLocaleAgentFileName = (id: string, locale?: string): string => { |
| #94 | const formatedLocale = normalizeLocale(locale); |
| #95 | const localeSuffix = formatedLocale === config.entryLocale ? '' : `.${formatedLocale}`; |
| #96 | return join(id, 'index' + localeSuffix + '.json'); |
| #97 | }; |
| #98 | |
| #99 | /** |
| #100 | * 获取构建时的本地化 Agent 文件名 |
| #101 | * @param id Agent ID |
| #102 | * @param locale 语言代码 |
| #103 | * @returns 文件名 |
| #104 | */ |
| #105 | export const getBuildLocaleAgentFileName = (id: string, locale?: string): string => { |
| #106 | const formatedLocale = normalizeLocale(locale); |
| #107 | const localeSuffix = formatedLocale === config.entryLocale ? '' : `.${formatedLocale}`; |
| #108 | return id + localeSuffix + '.json'; |
| #109 | }; |
| #110 | |
| #111 | |
| #112 |