UNPKG

3.51 kBJavaScriptView Raw
1var assert = require("assert");
2var file = require('../file-system');
3var fs = require('fs');
4var path = require('path');
5
6function getPath(filepath) {
7 return path.join(__dirname, filepath);
8}
9
10describe('copy', function() {
11 var allFiles = [
12 [
13 getPath('var/copy/simple/1/demo.html'),
14 getPath('var/copy/simple/1/demo.css'),
15 getPath('var/copy/simple/1/demo.js'),
16 getPath('var/copy/simple/1/2/demo.css'),
17 getPath('var/copy/simple/1/2/demo.html'),
18 getPath('var/copy/simple/file.js/demo.css'),
19 getPath('var/copy/simple/demo.js'),
20 getPath('var/copy/simple/demo.css')
21 ]
22 ];
23
24 before(function() {
25 allFiles.forEach(function(files) {
26 files.forEach(function(item) {
27 file.writeFileSync(item, 'a');
28 });
29 });
30 });
31
32 // it('copySync files with filter', function() {
33 // var dirpath = getPath('var/copy/simple');
34 // var destpath = getPath('var/copy/simpledest');
35
36 // file.copySync(dirpath, destpath, {
37 // filter: [
38 // '**/*.js',
39 // '1/**/*.css',
40 // '1/demo.html'
41 // ]
42 // });
43
44 // var dirDest = [
45 // getPath('var/copy/simpledest/1/demo.html'),
46 // getPath('var/copy/simpledest/1/demo.css'),
47 // getPath('var/copy/simpledest/1/2/demo.css'),
48 // getPath('var/copy/simpledest/1/demo.js'),
49 // getPath('var/copy/simpledest/demo.js')
50 // ];
51 // var result = [];
52
53 // file.recurseSync(destpath, function(filepath, filename) {
54 // if (!filename) return;
55
56 // result.push(filepath);
57 // });
58
59 // assert.equal(result.length, dirDest.length);
60 // });
61
62 it('copySync replace filepath', function() {
63 var dirpath = getPath('var/copy/simple');
64 var destpath = getPath('var/copy/simple-replace');
65
66 file.copySync(dirpath, destpath, {
67 process: function(contents, filepath) {
68 var basename = path.basename(filepath);
69 var relative = path.relative(dirpath, filepath);
70 var newpath = path.join(destpath, relative);
71
72 // Replace html to txt
73 newpath = newpath.replace(
74 /\.html$/,
75 '.txt'
76 );
77
78 // Move all css to rootpath of destpath
79 if (/\.css$/.test(basename)) {
80 var prefix = path.basename(path.dirname(newpath));
81 newpath = path.join(destpath, prefix + '-' + basename);
82 }
83
84 return {
85 contents: contents,
86 filepath: newpath
87 };
88 }
89 });
90
91 assert.equal(true, file.existsSync(
92 path.join(destpath, '1/demo.txt')
93 ));
94 });
95
96 it('copySync with noProcess', function() {
97 var dirpath = getPath('var/copy/simple');
98 var destpath = getPath('var/copy/simple-noprocess');
99
100 file.copySync(dirpath, destpath, {
101 filter: [
102 '**/*demo.css',
103 '!**/1/demo.css'
104 ],
105 noProcess: 'demo.css',
106 process: function(contents, filepath) {
107 return 'b';
108 }
109 });
110
111 assert.equal(true, file.existsSync(
112 path.join(destpath, 'demo.css')
113 ));
114
115 assert.equal(false, file.existsSync(
116 path.join(destpath, '1/demo.css')
117 ));
118
119 assert.equal(true, file.existsSync(
120 path.join(destpath, '1/2/demo.css')
121 ));
122
123 assert.equal(true, file.existsSync(
124 path.join(destpath, 'file.js/demo.css')
125 ));
126
127 var content = file.readFileSync(
128 path.join(destpath, 'demo.css'),
129 { encoding: 'utf8' }
130 );
131
132 assert.equal('a', content);
133 });
134
135
136 after(function() {
137 file.rmdirSync(getPath('var/copy'));
138 });
139});
\No newline at end of file