1 | var assert = require("assert");
|
2 | var file = require('../file-system');
|
3 | var fs = require('fs');
|
4 | var path = require('path');
|
5 |
|
6 | function getPath(filepath) {
|
7 | return path.join(__dirname, filepath);
|
8 | }
|
9 |
|
10 | describe('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 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
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 |
|
73 | newpath = newpath.replace(
|
74 | /\.html$/,
|
75 | '.txt'
|
76 | );
|
77 |
|
78 |
|
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 |