Skip to content

Commit

Permalink
feat: add getDynamicValue
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangfisher committed Jan 18, 2025
1 parent 91f2eaf commit bdf7782
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 15 deletions.
12 changes: 12 additions & 0 deletions docs/guide/misc.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,15 @@ const switcher = {
- 也可以自定义类型匹配器,比如`User`类型,可以自定义类型匹配器`User: (value:any)=> value instanceof User`,这样就可以匹配`User`类型了。



## getDynamicValue

获取动态值。

```typescript
getDynamicValue(1) // 1
getDynamicValue(()=>1) // 1
getDynamicValue(async ()=>1) // 1
getDynamicValue(async (n)=>n+1,{args:[1]}) // 2
```

11 changes: 4 additions & 7 deletions src/fs/copyDirs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { ABORT } from "../consts";
import path from "node:path"
import { cleanDir } from "./cleanDir";
import type {CopyFileInfo} from "./copyFiles"
import { getDynamicValue } from "../misc/getDynamicValue";

export type {CopyFileInfo} from "./copyFiles"

Expand Down Expand Up @@ -91,14 +92,10 @@ export async function copyDirs(
continue;
}
const template = artTemplate(fileInfo.source);
const templateVars = typeof vars === 'function'
? await Promise.resolve(vars(file))
: vars || {};
const templateVars =await getDynamicValue.call(opts,opts.vars,[file]);
await writeFile(targetFile, template(templateVars), {encoding:"utf-8"});
}else{// 普通文件
const shouldOverwrite = typeof opts.overwrite === 'function'
? await Promise.resolve(opts.overwrite(fileInfo.target))
: opts.overwrite;
}else{// 普通文件
const shouldOverwrite = await getDynamicValue.call(opts,opts.overwrite,[file]) as boolean
if (shouldOverwrite === false && existsSync(fileInfo.target)) {
continue;
}
Expand Down
12 changes: 5 additions & 7 deletions src/fs/copyFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ import artTemplate from "art-template";
import { ABORT } from "../consts";
import path from "node:path"
import { cleanDir } from "./cleanDir";
import { getDynamicValue } from "../misc/getDynamicValue";

export interface CopyFileInfo{
file? : string // 相对于源文件夹的文件路径
source?: string // 源文件路径
target?: string // 目标文件路径
vars? : null | undefined | Record<string,any> // 模板变量
}

export interface CopyFilesOptions {
vars? : Record<string, any> | ((file: string) => Record<string, any> | Promise<Record<string, any>>); // 传递给模板的变量
ignore? : string[]; // 忽略的文件或文件夹,支持通配符
Expand All @@ -38,6 +39,7 @@ export interface CopyFilesOptions {
before? : (info:CopyFileInfo) => void | typeof ABORT; // 复制前的回调
after? : (info:CopyFileInfo) => void | typeof ABORT; // 复制后的回调
error? : (error:Error,{source,target}:{source: string, target: string})=>void | typeof ABORT // 复制出错的回调
template?: Record<string, any> | ((file: string) => Record<string, any> | Promise<Record<string, any>>);
}

export async function copyFiles(
Expand Down Expand Up @@ -103,14 +105,10 @@ export async function copyFiles(
continue;
}
const template = artTemplate(fileInfo.source);
const templateVars = typeof vars === 'function'
? await Promise.resolve(vars(file))
: vars;
const templateVars = await getDynamicValue.call(opts,opts.vars,[file]);
await writeFile(targetFile, template(templateVars), {encoding:"utf-8"});
}else{// 模板文件
const shouldOverwrite = typeof opts.overwrite === 'function'
? await Promise.resolve(opts.overwrite(fileInfo.target))
: opts.overwrite;
const shouldOverwrite = await getDynamicValue.call(opts,opts.overwrite,[file]) as boolean
if (shouldOverwrite === false && existsSync(fileInfo.target)) {
continue;
}
Expand Down
9 changes: 9 additions & 0 deletions src/misc/getDynamicValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

export async function getDynamicValue<T=any>(this:any,value:any,args?:any[]): Promise<T | undefined>{
if(value==null || value==undefined) return undefined
if(typeof value === "function"){
return ( await Promise.resolve(value.apply(this,args)) ) as T
}else{
return value as T
}
}
3 changes: 2 additions & 1 deletion src/misc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export * from "./parseTimeDuration"
export * from "./formatDateTime"
export * from "./relativeTime"
export * from "./execScript"
export * from "./switchValue"
export * from "./switchValue"
export * from "./getDynamicValue"

0 comments on commit bdf7782

Please sign in to comment.