1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | var fs = require('fs');
|
9 | var pathParser = require('path');
|
10 | var stringUtil = require('./stringUtil');
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | function isFile(path) {
|
16 | return fs.statSync(path).isFile();
|
17 | }
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | function isDir(path) {
|
23 | return fs.statSync(path).isDirectory();
|
24 | }
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | function writeFile(filepath, content, force) {
|
30 | var pathSections = filepath.split('/');
|
31 | var lastSectionIndex = pathSections.length - 1;
|
32 | var path = '';
|
33 | pathSections.forEach(function(section, index) {
|
34 | if (index < lastSectionIndex) {
|
35 | path += section + '/';
|
36 | if (!has(path)) {
|
37 | createDir(path);
|
38 | }
|
39 | } else {
|
40 | path += section;
|
41 | if (has(path) && !force) {
|
42 | console.warn('file: `' + path + '` has existed, has been ignored, has not been overridden, your could use `force` for force overridding!');
|
43 | return;
|
44 | }
|
45 | fs.writeFileSync(path, content);
|
46 | console.log('SUCCESS: write into file `' + pathParser.resolve(path) + '` !');
|
47 | }
|
48 | });
|
49 | }
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | function readFile(filepath) {
|
55 | return fs.readFileSync(filepath, 'utf8');
|
56 | }
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | function appendFile(filepath, content) {
|
62 | return fs.appendFileSync(filepath, content);
|
63 | }
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | function delFile(filepath) {
|
69 | return fs.unlinkSync(filepath);
|
70 | }
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | function delDir(dirpath) {
|
76 | var fileNames = getFileNames(dirpath);
|
77 | fileNames.forEach(function(fileName) {
|
78 | var path = dirpath + '/' + fileName;
|
79 | if (isDir(path)) {
|
80 | delDir(path);
|
81 | } else {
|
82 | delFile(path);
|
83 | }
|
84 | });
|
85 |
|
86 | return fs.rmdirSync(dirpath);
|
87 | }
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | function createDir(dirpath) {
|
93 | var pathSections = dirpath.split('/');
|
94 | var path = '';
|
95 | pathSections.forEach(function(section) {
|
96 | path += section + '/';
|
97 | if (!has(path)) {
|
98 | fs.mkdirSync(path);
|
99 | }
|
100 | });
|
101 | }
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | function getFileNames(dirpath) {
|
107 | return fs.readdirSync(dirpath);
|
108 | }
|
109 |
|
110 |
|
111 |
|
112 |
|
113 | function has(path) {
|
114 | return fs.existsSync(path);
|
115 | }
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | function copyFile(srcfilepath, destfilepath, data, prefix, force) {
|
121 | if (!has(srcfilepath)) {
|
122 | console.error('file: `' + srcfilepath + '` not exists!');
|
123 | return;
|
124 | }
|
125 |
|
126 | if (!isFile(srcfilepath)) {
|
127 | console.error('file: `' + srcfilepath + '` is not a file, maybe a dir!');
|
128 | return;
|
129 | }
|
130 |
|
131 | if (has(destfilepath) && !force) {
|
132 | console.warn('file: `' + destfilepath + '` has existed, has been ignored, has not been overridden, your could use `force` for force overridding!');
|
133 | return;
|
134 | }
|
135 |
|
136 | var pathSections = destfilepath.split('/');
|
137 |
|
138 | var fileName = pathSections.pop();
|
139 | if (prefix) {
|
140 | fileName = prefix + stringUtil.capitalize(fileName);
|
141 | }
|
142 |
|
143 | var destdirpath = pathSections.join('/');
|
144 | if (!destdirpath) {
|
145 | destfilepath = fileName;
|
146 | } else {
|
147 | destfilepath = destdirpath + '/' + fileName;
|
148 | }
|
149 |
|
150 | var content = readFile(srcfilepath);
|
151 | data = data || {};
|
152 | data.__filename__ = fileName;
|
153 | data.__filepath__ = destfilepath;
|
154 | content = stringUtil.format(content, data);
|
155 | writeFile(destfilepath, content, force);
|
156 | }
|
157 |
|
158 |
|
159 |
|
160 |
|
161 | function copyDir(srcdirpath, destdirpath, data, prefix, force) {
|
162 | if (!has(srcdirpath)) {
|
163 | console.error('dir: `' + srcdirpath + '` not exists!');
|
164 | return;
|
165 | }
|
166 |
|
167 | if (!isDir(srcdirpath)) {
|
168 | console.error('dir: `' + srcdirpath + '` is not a dir, maybe a file!');
|
169 | return;
|
170 | }
|
171 |
|
172 | var fileNames;
|
173 |
|
174 | if (has(destdirpath)) {
|
175 | fileNames = getFileNames(destdirpath);
|
176 | if (fileNames.length && force) {
|
177 | delDir(destdirpath);
|
178 | createDir(destdirpath);
|
179 | } else {
|
180 | console.error('dir: `' + destdirpath + '` has existed, has been ignored, has not been overridden, your could use `force` for force overridding!');
|
181 | return;
|
182 | }
|
183 | } else {
|
184 | createDir(destdirpath);
|
185 | }
|
186 |
|
187 |
|
188 |
|
189 | fileNames = getFileNames(srcdirpath);
|
190 | fileNames.forEach(function(fileName) {
|
191 | var srcpath = srcdirpath + '/' + fileName;
|
192 | var destpath = destdirpath ? destdirpath + '/' + fileName : fileName;
|
193 | if (isDir(srcpath)) {
|
194 | copyDir(srcpath, destpath, data, prefix, force);
|
195 | } else {
|
196 | copyFile(srcpath, destpath, data, prefix, force);
|
197 | }
|
198 | });
|
199 | }
|
200 |
|
201 | module.exports = {
|
202 | appendFile: appendFile,
|
203 | copyFile: copyFile,
|
204 | copyDir: copyDir,
|
205 | writeFile: writeFile,
|
206 | readFile: readFile,
|
207 | createDir: createDir,
|
208 | has: has
|
209 | }; |
\ | No newline at end of file |