035f25469894281aa86854151d22a619127f4560.svn-base
8.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
* grunt
* http://gruntjs.com/
*
* Copyright (c) 2012 "Cowboy" Ben Alman
* Licensed under the MIT license.
* https://github.com/gruntjs/grunt/blob/master/LICENSE-MIT
*/
var grunt = require('../grunt');
// Nodejs libs.
var fs = require('fs');
var path = require('path');
// In Nodejs 0.8.0, existsSync moved from path -> fs.
var existsSync = fs.existsSync || path.existsSync;
// The module to be exported.
var file = module.exports = {};
// External libs.
file.glob = require('glob-whatev');
file.findup = require('../util/findup');
// Change the current base path (ie, CWD) to the specified path.
file.setBase = function() {
var dirpath = path.join.apply(path, arguments);
process.chdir(dirpath);
};
// Match a filepath against one or more wildcard patterns. Returns true if
// any of the patterns match.
file.isMatch = function(patterns, filepath) {
patterns = Array.isArray(patterns) ? patterns : [patterns];
return patterns.some(function(pattern) {
return file.glob.minimatch(filepath, pattern, {matchBase: true});
});
};
// Return an array of all file paths that match the given wildcard patterns.
file.expand = function() {
var args = grunt.utils.toArray(arguments);
// If the first argument is an options object, save those options to pass
// into the file.glob.glob method for minimatch to use.
var options = grunt.utils.kindOf(args[0]) === 'object' ? args.shift() : {};
// Use the first argument if it's an Array, otherwise convert the arguments
// object to an array and use that.
var patterns = Array.isArray(args[0]) ? args[0] : args;
// Generate a should-be-unique number.
var uid = +new Date();
// Return a flattened, uniqued array of matching file paths.
return grunt.utils._(patterns).chain().flatten().map(function(pattern) {
// If pattern is a template, process it accordingly.
pattern = grunt.template.process(pattern);
// Just return the pattern if it's an internal directive.
if (grunt.task.getDirectiveParts(pattern)) { return pattern; }
// Otherwise, expand paths.
return file.glob.glob(pattern, options);
}).flatten().uniq(false, function(filepath) {
// Only unique file paths, but don't unique <something> directives, in case
// they are repeated intentionally.
return grunt.task.getDirectiveParts(filepath) ? ++uid : filepath;
}).value();
};
// Further filter file.expand.
function expandByType(type) {
var args = grunt.utils.toArray(arguments).slice(1);
return file.expand.apply(file, args).filter(function(filepath) {
// Just return the filepath if it's an internal directive.
if (grunt.task.getDirectiveParts(filepath)) { return filepath; }
try {
return fs.statSync(filepath)[type]();
} catch(e) {
throw grunt.task.taskError(e.message, e);
}
});
}
// A few type-specific file expansion methods.
file.expandDirs = expandByType.bind(file, 'isDirectory');
file.expandFiles = expandByType.bind(file, 'isFile');
// Return an array of all file paths that match the given wildcard patterns,
// plus any URLs that were passed at the end.
file.expandFileURLs = function() {
// Use the first argument if it's an Array, otherwise convert the arguments
// object to an array and use that.
var patterns = Array.isArray(arguments[0]) ? arguments[0] : grunt.utils.toArray(arguments);
var urls = [];
// Filter all URLs out of patterns list and store them in a separate array.
patterns = patterns.filter(function(pattern) {
if (/^(?:file|https?):\/\//i.test(pattern)) {
// Push onto urls array.
urls.push(pattern);
// Remove from patterns array.
return false;
}
// Otherwise, keep pattern.
return true;
});
// Return expanded filepaths with urls at end.
return file.expandFiles(patterns).map(function(filepath) {
var abspath = path.resolve(filepath);
// Convert C:\foo\bar style paths to /C:/foo/bar.
if (abspath.indexOf('/') !== 0) {
abspath = ('/' + abspath).replace(/\\/g, '/');
}
return 'file://' + abspath;
}).concat(urls);
};
// Like mkdir -p. Create a directory and any intermediary directories.
file.mkdir = function(dirpath) {
if (grunt.option('no-write')) { return; }
dirpath.split(/[\/\\]/).reduce(function(parts, part) {
parts += part + '/';
var subpath = path.resolve(parts);
if (!existsSync(subpath)) {
try {
fs.mkdirSync(subpath, '0755');
} catch(e) {
throw grunt.task.taskError('Unable to create directory "' + subpath + '" (Error code: ' + e.code + ').', e);
}
}
return parts;
}, '');
};
// Recurse into a directory, executing callback for each file.
file.recurse = function recurse(rootdir, callback, subdir) {
var abspath = subdir ? path.join(rootdir, subdir) : rootdir;
fs.readdirSync(abspath).forEach(function(filename) {
var filepath = path.join(abspath, filename);
if (fs.statSync(filepath).isDirectory()) {
recurse(rootdir, callback, path.join(subdir, filename));
} else {
callback(path.join(rootdir, subdir, filename), rootdir, subdir, filename);
}
});
};
// Is a given file path absolute?
file.isPathAbsolute = function() {
var filepath = path.join.apply(path, arguments);
return path.resolve(filepath) === filepath;
};
// Write a file.
file.write = function(filepath, contents) {
var nowrite = grunt.option('no-write');
grunt.verbose.write((nowrite ? 'Not actually writing ' : 'Writing ') + filepath + '...');
// Create path, if necessary.
file.mkdir(path.dirname(filepath));
try {
if (!nowrite) {
// Actually write file.
fs.writeFileSync(filepath, contents);
}
grunt.verbose.ok();
return true;
} catch(e) {
grunt.verbose.error();
throw grunt.task.taskError('Unable to write "' + filepath + '" file (Error code: ' + e.code + ').', e);
}
};
// Read a file, return its contents.
file.read = function(filepath, encoding) {
var src;
grunt.verbose.write('Reading ' + filepath + '...');
try {
src = fs.readFileSync(String(filepath), encoding ? null : 'utf8');
grunt.verbose.ok();
return src;
} catch(e) {
grunt.verbose.error();
throw grunt.task.taskError('Unable to read "' + filepath + '" file (Error code: ' + e.code + ').', e);
}
};
// Read a file, optionally processing its content, then write the output.
file.copy = function(srcpath, destpath, options) {
if (!options) { options = {}; }
var src = file.read(srcpath, true);
if (options.process && options.noProcess !== true &&
!(options.noProcess && file.isMatch(options.noProcess, srcpath))) {
grunt.verbose.write('Processing source...');
try {
src = options.process(src.toString('utf8'));
grunt.verbose.ok();
} catch(e) {
grunt.verbose.error();
throw grunt.task.taskError('Error while processing "' + srcpath + '" file.', e);
}
}
// Abort copy if the process function returns false.
if (src === false) {
grunt.verbose.writeln('Write aborted.');
} else {
file.write(destpath, src);
}
};
// Read a file, parse its contents, return an object.
file.readJSON = function(filepath) {
var src = this.read(String(filepath));
var result;
grunt.verbose.write('Parsing ' + filepath + '...');
try {
result = JSON.parse(src);
grunt.verbose.ok();
return result;
} catch(e) {
grunt.verbose.error();
throw grunt.task.taskError('Unable to parse "' + filepath + '" file (' + e.message + ').', e);
}
};
// Clear the require cache for all passed filepaths.
file.clearRequireCache = function() {
// If a non-string argument is passed, it's an array of filepaths, otherwise
// each filepath is passed individually.
var filepaths = typeof arguments[0] !== 'string' ? arguments[0] : grunt.utils.toArray(arguments);
// For each filepath, clear the require cache, if necessary.
filepaths.forEach(function(filepath) {
var abspath = path.resolve(filepath);
if (require.cache[abspath]) {
grunt.verbose.write('Clearing require cache for "' + filepath + '" file...').ok();
delete require.cache[abspath];
}
});
};
// Access files in the user's ".grunt" folder.
file.userDir = function() {
var dirpath = path.join.apply(path, arguments);
var win32 = process.platform === 'win32';
var homepath = process.env[win32 ? 'USERPROFILE' : 'HOME'];
dirpath = path.resolve(homepath, '.grunt', dirpath);
return existsSync(dirpath) ? dirpath : null;
};