Skip to content

Commit 249c123

Browse files
committed
递归创建文件夹兼容node.8.x
1 parent 40598a2 commit 249c123

File tree

5 files changed

+49
-2
lines changed

5 files changed

+49
-2
lines changed

src/piper/clean.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fs from 'fs';
22
const Clean = (targets: Array<string>, callback: Function) => {
33
targets.forEach(deletePath => {
4+
if(!fs.existsSync(deletePath)) return;
45
const stat = fs.statSync(deletePath);
56
if(stat.isFile()) {
67
return fs.unlinkSync(deletePath);

src/piper/copy.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import fs from 'fs';
22
import path from 'path';
3+
import { MakeDirs } from './utils';
34
const Copy = (source: string, dest: string, callback?: Function) => {
45
if(!fs.existsSync(path.dirname(dest))) {
5-
fs.mkdirSync(path.dirname(dest), { recursive: true });
6+
MakeDirs(path.dirname(dest))
67
}
78
const rs = fs.createReadStream(source);
89
const ws = fs.createWriteStream(dest);

src/piper/parseRender.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import ejs from 'ejs';
22
import fs from 'fs';
33
import path from 'path';
44
import Copy from './copy';
5+
import { MakeDirs } from './utils';
56
const ParseRender = (
67
sourcePath: string,
78
targetPath: string,
@@ -18,7 +19,7 @@ const ParseRender = (
1819
return;
1920
}
2021
if(!fs.existsSync(path.dirname(targetPath))) {
21-
fs.mkdirSync(path.dirname(targetPath), { recursive: true });
22+
MakeDirs(path.dirname(targetPath))
2223
}
2324
fs.writeFileSync(targetPath, data);
2425
callback && callback();

src/piper/utils.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
4+
export const MakeDirs = (dir: string): void => {
5+
const mkdirs = (dir: string, callback?: () => void) => {
6+
if(fs.existsSync(dir)) {
7+
callback && callback();
8+
return;
9+
}
10+
11+
mkdirs(path.dirname(dir), () => {
12+
fs.mkdirSync(dir);
13+
callback && callback();
14+
});
15+
}
16+
17+
if(fs.existsSync(dir)) return;
18+
mkdirs(dir)
19+
}

test/piper.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from 'fs';
22
import path from 'path';
33
import {expect} from 'chai';
44
import {FileCleanner, FilePiper} from '../src/piper';
5+
import {MakeDirs} from '../src/piper/utils';
56
import Analyse from '../src/piper/analyse';
67

78
describe('piper文件中转功能单元测试', () => {
@@ -159,6 +160,7 @@ describe('piper文件中转功能单元测试', () => {
159160
/tmp3.json$/
160161
]
161162
});
163+
162164
expect(fs.readFileSync(tmp1.replace(tmpDir, targetDir)).toString() === '测试文本000');
163165
expect(fs.readFileSync(tmp2.replace(tmpDir, targetDir)).toString() === 'const ddd = "000"');
164166
expect(fs.readFileSync(tmp3.replace(tmpDir, targetDir)).toString() === '{"name":"<%= author%>"}');
@@ -301,4 +303,27 @@ describe('piper文件中转功能单元测试', () => {
301303
expect(analyseResult).to.deep.equal({});
302304
});
303305
})
306+
307+
describe('Utils 方法测试', () => {
308+
const tmp = path.resolve(__dirname, './a')
309+
const dir = path.resolve(tmp, './b/c');
310+
311+
before(async () => {
312+
if(fs.existsSync(tmp)) {
313+
await FileCleanner(path.resolve(__dirname, './a'))
314+
}
315+
})
316+
317+
after(async () => {
318+
if(fs.existsSync(tmp)) {
319+
await FileCleanner(path.resolve(__dirname, './a'))
320+
}
321+
})
322+
323+
it('MakeDirs() 可以创建多级目录', () => {
324+
MakeDirs(dir);
325+
expect(fs.existsSync(dir)).to.be.ok;
326+
expect(fs.statSync(dir).isDirectory()).to.be.ok;
327+
})
328+
})
304329
})

0 commit comments

Comments
 (0)