refactor🎨: (阅读代码):utils.ts增加注释

master
yetao 2 weeks ago
parent 86aec22b0e
commit 2c6e892b66

@ -108,8 +108,17 @@ export async function catchingPromise<T>(executor: (resolve: (value?: T | Promis
});
}
// 定义了一个类型,用于表示异步操作的回调函数。
export type toPromiseCallback<T> = (err?: Error | null | void, res?: T) => void;
/** Wrapper around async callback-based functions */
/**
* Promise
* @param func -
* @returns Promise
* @description
* Promise
* Promise
*/
export async function toPromise<T>(func: (cb: toPromiseCallback<T>) => void): Promise<T> {
return catchingPromise((resolve, reject) => {
func((err, res) => err ? reject(err) : resolve(res!));
@ -117,43 +126,112 @@ export async function toPromise<T>(func: (cb: toPromiseCallback<T>) => void): Pr
}
/** Converts the given number/string to a port number. Throws an error for invalid strings or ports outside the 1-65565 range */
/**
*
* @param port -
* @returns
* @throws {Error}
* @throws {Error} 1-65565
*/
export function validatePort(port: string | number): number {
// 将端口号转换为数字
const p = Number(port);
// 如果转换后的结果不是整数,抛出错误
if (!Number.isInteger(p)) throw new Error(`Wanting to use non-int '${port}' as port`);
// 如果端口号小于 0 或大于 65565抛出错误
if (p < 0 || p > 65565) throw new Error(`Wanting to use port ${p} outside the 1-65565 range`);
// 返回有效的端口号
return p;
}
/**
*
* Bash
* 线
*/
const CLEAN_BASH_VALUE_REGEX = /^[\w-/\\]+$/;
/** Based on way 1 in https://stackoverflow.com/a/20053121 */
/**
* Bash
* @param value -
* @returns CLEAN_BASH_VALUE_REGEX
* @throws {Error} CLEAN_BASH_VALUE_REGEX
*/
export function escapeBashValue(value: string) {
// 如果值通过了 CLEAN_BASH_VALUE_REGEX 测试,则返回原始值
if (CLEAN_BASH_VALUE_REGEX.test(value)) return value;
// 否则,将值用单引号包围,并将所有单引号替换为 '\''
return `'${value.replace(/'/g, `'\\''`)}'`;
}
/** Convert an {@link EnvironmentVariable} array to a `export var1=val; export var2='escaped$val'` etc */
/**
*
* @param env -
* @param createSetEnv -
* @returns
* @description
* Bash shell
* `createSetEnv`
*/
export function environmentToExportString(env: EnvironmentVariable[], createSetEnv: (key: string, value: string) => string): string {
// 使用 map 方法遍历环境变量数组,为每个环境变量创建一个设置命令
return env.map(({ key, value }) => createSetEnv(escapeBashValue(key), escapeBashValue(value))).join('; ');
}
/** Returns a new {@link EnvironmentVariable} array with all the given environments merged into it, overwriting same-key variables */
/**
*
* @param environments -
* @returns
* @description
* `EnvironmentVariable` `Map`
* `Map` `Map`
* `Map`
*/
export function mergeEnvironment(...environments: (EnvironmentVariable[] | Record<string, string> | undefined)[]): EnvironmentVariable[] {
// 创建一个 Map 用于存储合并后的环境变量
const result = new Map<string, EnvironmentVariable>();
// 遍历所有传入的环境变量数组或对象
for (let other of environments) {
// 如果当前项为 undefined则跳过
if (!other) continue;
// 如果当前项是一个数组
if (Array.isArray(other)) {
for (const variable of other) result.set(variable.key, variable);
} else {
// 遍历数组中的每个环境变量
for (const variable of other) {
// 将环境变量的键和值添加到结果 Map 中
result.set(variable.key, variable);
}
}
// 如果当前项是一个对象
else {
// 遍历对象的所有键值对
for (const [key, value] of Object.entries(other)) {
// 将键值对转换为 EnvironmentVariable 对象并添加到结果 Map 中
result.set(key, { key, value });
}
}
}
// 将结果 Map 转换为数组并返回
return [...result.values()];
}
/** Joins the commands together using the given separator. Automatically ignores `undefined` and empty strings */
/**
* 使
* @param commands -
* @param separator -
* @returns commands undefined undefined
* @description
* 使
* commands
* commands undefined 使
* commands undefined undefined
*/
export function joinCommands(commands: string | string[] | undefined, separator: string): string | undefined {
// 如果 commands 是一个字符串,直接返回它
if (typeof commands === 'string') return commands;
// 如果 commands 是一个数组,使用分隔符连接其中的非空字符串
return commands?.filter(c => c?.trim()).join(separator);
}

Loading…
Cancel
Save