UNPKG

4.71 kBJavaScriptView Raw
1/**
2 * @file : config.js
3 * @author: liwei
4 * @email : iamweilee@gmail.com
5 * @date : 2014/11/15
6 */
7
8var fs = require('fs');
9var pathParser = require('path');
10var stringUtil = require('./stringUtil');
11
12/**
13 * 判断是否为文件
14 */
15function isFile(path) {
16 return fs.statSync(path).isFile();
17}
18
19/**
20 * 判断是否为目录
21 */
22function isDir(path) {
23 return fs.statSync(path).isDirectory();
24}
25
26/**
27 * 写文件
28 */
29function 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 */
54function readFile(filepath) {
55 return fs.readFileSync(filepath, 'utf8');
56}
57
58/**
59 * 在文件末尾添加数据
60 */
61function appendFile(filepath, content) {
62 return fs.appendFileSync(filepath, content);
63}
64
65/**
66 * 删除文件
67 */
68function delFile(filepath) {
69 return fs.unlinkSync(filepath);
70}
71
72/**
73 * 删除目录及各级子目录和它们的文件
74 */
75function 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 */
92function 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 */
106function getFileNames(dirpath) {
107 return fs.readdirSync(dirpath);
108}
109
110/**
111 * 检测文件或目录是否存在
112 */
113function has(path) {
114 return fs.existsSync(path);
115}
116
117/**
118 * 复制文件,替换源文件中的占位符
119 */
120function 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 */
161function 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
201module.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