Skip to content

Commit 88c4726

Browse files
committed
feat: add copyFile and fix copyFiles
1 parent 6a64b4b commit 88c4726

27 files changed

+780
-146
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ node_modules
22
.yalc
33
dist
44
src/fs/tmp.art
5+
/examples/copyFiles/target
6+
/examples/copyFiles/targetDirs

docs/guide/fs.md

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,15 @@ export async function copyDirs(
195195

196196
```ts
197197
export interface CopyFilesOptions {
198-
vars?: Record<string, any>; // 传递给模板的变量
199-
ignore?: string[]; // 忽略的文件或文件夹,支持通配符
200-
clean?:boolean // 是否清空目标文件夹
201-
cwd?:string; // pattern的cwd
202-
overwrite?: boolean | ((filename: string) => boolean | Promise<boolean>); // 是否覆盖已存在的文件,可以是boolean或返回boolean的同步/异步函数
203-
before?: (info:CopyFileInfo) => void | typeof ABORT; // 复制前的回调
204-
after?: (info:CopyFileInfo) => void | typeof ABORT; // 复制后的回调
205-
error?:(error:Error,{source,target}:{source: string, target: string})=>void | typeof ABORT // 复制出错的回调
198+
vars? : Record<string, any> | ((file: string) => Record<string, any> | Promise<Record<string, any>>); // 传递给模板的变量
199+
ignore? : string[]; // 忽略的文件或文件夹,支持通配符
200+
clean? : boolean; // 是否清空目标文件夹
201+
cwd? : string; // pattern的cwd
202+
overwrite? : boolean | ((filename: string) => boolean | Promise<boolean>); // 是否覆盖已存在的文件,可以是boolean或返回boolean的同步/异步函数
203+
before? : (info:CopyFileInfo) => void | typeof ABORT; // 复制前的回调
204+
after? : (info:CopyFileInfo) => void | typeof ABORT; // 复制后的回调
205+
error? : (error:Error,{source,target}:{source: string, target: string})=>void | typeof ABORT // 复制出错的回调
206+
templateOptions?: Record<string, any> | ((file: string) => Record<string, any> | Promise<Record<string, any>>);
206207
}
207208
export async function copyFiles(
208209
pattern: string,
@@ -211,12 +212,63 @@ export async function copyFiles(
211212
)
212213
```
213214

214-
- 支持与`copyDirs`相同的功能参数。
215215
- `pattern`参数支持通配符,内部使用`glob`
216216
- `targetDir`必须是一个文件夹,如果不存在则会自动创建。
217217
- 复制时会保持源文件夹的结构不变。
218+
- 如果源文件是模板文件``.art`,则会使用`art-template`进行渲染,并且会传递`vars`参数给模板引擎。
218219
219220
221+
- 使用`glob`来匹配文件,所以`pattern``ignore`支持通配符。
222+
- 复制文件时保持源文件夹的结构不变,所以`targetDir`必须是一个文件夹,如果不存在则会自动创建。
223+
- `vars`参数会传递给模板引擎,用于渲染模板文件,对所有`.art`模板文件生效。如果需要对某个文件传入单独的模板变量,可以在`before`回调中设置`vars`参数。
224+
225+
```typescript
226+
copyDirs("c://temp//myapp","d://temp/app",{
227+
vars:{ // 这个变量会传递给所有的模板文件
228+
name:"myapp",
229+
version:"1.0.0"
230+
},
231+
before(info){
232+
if(info.file === "package.json.art"){
233+
// 这里的变量会与全局变量合并后果传递给package.json.art模板文件
234+
info.vars = {
235+
name:"myapp",
236+
version:"1.0.0"
237+
}
238+
}
239+
}
240+
})
241+
```
242+
- `before`回调会在复制文件前调用,可以在这里修改`vars`参数,或者返回`ABORT`来阻止复制。
243+
244+
245+
246+
247+
```typescript
248+
copyFiles("c://temp//myapp/**/*","d://temp/app",{
249+
vars:{ // 这个变量会传递给所有的模板文件
250+
name:"myapp",
251+
version:"1.0.0"
252+
}
253+
})
254+
```
255+
256+
## copyFile
257+
258+
复制文件到指定的目标文件夹
259+
260+
```typescript
261+
copyFile( source: string,target: string, options?:CopyTemplateFileOptions)
262+
263+
type CopyTemplateFileOptions = {
264+
vars? : Record<string, any> | ((file: string) => Record<string, any> | Promise<Record<string, any>>);
265+
overwrite? : boolean | ((filename: string) => boolean | Promise<boolean>);
266+
templateOptions? : Record<string, any> | ((file: string) => Record<string, any> | Promise<Record<string, any>>);
267+
}
268+
```
269+
270+
- `copyFile`支持模板文件,如果源文件是`.art`文件,则会使用`art-template`进行渲染。
271+
220272
## getExistedDir
221273

222274
返回第一个存在的文件夹

examples/copyFiles/copyDirs.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { copyDirs } from "../../src/fs/copyDirs";
2+
import path from "node:path";
3+
4+
import { copyFiles } from "../../src/fs/copyFiles";
5+
6+
async function copydemo(){
7+
// await copyDirs("sourceDirs", "targetDirs",{
8+
// cwd: path.join(__dirname,"source"),
9+
// overwrite:true,
10+
// vars: {
11+
// scopeId: "app",
12+
// languages:[
13+
// { name:"zh-CN",title:"中文", nativeTitle:"中文" },
14+
// { name:"en-US",title:"English", nativeTitle:"English" },
15+
// { name:"ja-JP",title:"日本語", nativeTitle:"日本語" }
16+
// ],
17+
// library:false,
18+
// activeLanguage:"zh-CN",
19+
// defaultLanguage:"zh-CN"
20+
// }
21+
// })
22+
23+
await copyFiles("**/*", "targetDirs/x",{
24+
cwd: path.join(__dirname,"sourceDirs"),
25+
overwrite:true,
26+
vars: {
27+
scopeId: "app",
28+
languages:[
29+
{ name:"zh-CN",title:"中文", nativeTitle:"中文" },
30+
{ name:"en-US",title:"English", nativeTitle:"English" },
31+
{ name:"ja-JP",title:"日本語", nativeTitle:"日本語" }
32+
],
33+
library:false,
34+
activeLanguage:"zh-CN",
35+
defaultLanguage:"zh-CN"
36+
}
37+
})
38+
}
39+
40+
41+
copydemo().then(()=>{
42+
console.log("copyFiles done")
43+
})
44+

examples/copyFiles/copyFiles.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { copyFiles } from "../../src/fs/copyFiles";
2+
import path from "node:path";
3+
4+
5+
async function copydemo(){
6+
await copyFiles("*.*", "target",{
7+
cwd: path.join(__dirname,"source"),
8+
overwrite:true,
9+
vars: {
10+
scopeId: "app",
11+
languages:[
12+
{ name:"zh-CN",title:"中文", nativeTitle:"中文" },
13+
{ name:"en-US",title:"English", nativeTitle:"English" },
14+
{ name:"ja-JP",title:"日本語", nativeTitle:"日本語" }
15+
],
16+
library:false,
17+
activeLanguage:"zh-CN",
18+
defaultLanguage:"zh-CN"
19+
}
20+
})
21+
}
22+
23+
24+
copydemo().then(()=>{
25+
console.log("copyFiles done")
26+
})
27+

examples/copyFiles/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "copy",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.ts",
6+
"scripts": {
7+
"dev:files": "tsx copyFiles.ts",
8+
"dev:dirs": "tsx copyDirs.ts"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "ISC",
13+
"devDependencies": {
14+
"tsx": "^4.19.2"
15+
}
16+
}

0 commit comments

Comments
 (0)