diff --git a/docs/guide/misc.md b/docs/guide/misc.md index c7fbd7f..c11266a 100644 --- a/docs/guide/misc.md +++ b/docs/guide/misc.md @@ -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 +``` + diff --git a/src/fs/copyDirs.ts b/src/fs/copyDirs.ts index 08eeabc..0842cd8 100644 --- a/src/fs/copyDirs.ts +++ b/src/fs/copyDirs.ts @@ -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" @@ -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; } diff --git a/src/fs/copyFiles.ts b/src/fs/copyFiles.ts index 4fbb175..b971ae3 100644 --- a/src/fs/copyFiles.ts +++ b/src/fs/copyFiles.ts @@ -21,6 +21,7 @@ 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 // 相对于源文件夹的文件路径 @@ -28,7 +29,7 @@ export interface CopyFileInfo{ target?: string // 目标文件路径 vars? : null | undefined | Record // 模板变量 } - + export interface CopyFilesOptions { vars? : Record | ((file: string) => Record | Promise>); // 传递给模板的变量 ignore? : string[]; // 忽略的文件或文件夹,支持通配符 @@ -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 | ((file: string) => Record | Promise>); } export async function copyFiles( @@ -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; } diff --git a/src/misc/getDynamicValue.ts b/src/misc/getDynamicValue.ts new file mode 100644 index 0000000..ec5736c --- /dev/null +++ b/src/misc/getDynamicValue.ts @@ -0,0 +1,9 @@ + +export async function getDynamicValue(this:any,value:any,args?:any[]): Promise{ + 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 + } +} diff --git a/src/misc/index.ts b/src/misc/index.ts index 5c9dbfe..c24b3cf 100644 --- a/src/misc/index.ts +++ b/src/misc/index.ts @@ -4,4 +4,5 @@ export * from "./parseTimeDuration" export * from "./formatDateTime" export * from "./relativeTime" export * from "./execScript" -export * from "./switchValue" \ No newline at end of file +export * from "./switchValue" +export * from "./getDynamicValue" \ No newline at end of file