1 | var path = require('path');
|
2 | var fs = require('fs');
|
3 | var fileset = require('fileset');
|
4 |
|
5 | var READ_FILE_CHUNK_SIZE = 64 * 1024;
|
6 | var RELATIVE_PATH_SIGN = "../";
|
7 | var seq = 0;
|
8 | function filesFor(options, callback) {
|
9 | if (!callback && typeof options === 'function') {
|
10 | callback = options;
|
11 | options = null;
|
12 | }
|
13 | options = options || {};
|
14 |
|
15 | var root = options.root,
|
16 | includes = options.includes,
|
17 | excludes = options.excludes,
|
18 | realpath = options.realpath,
|
19 | relative = options.relative,
|
20 | opts;
|
21 |
|
22 | root = root || process.cwd();
|
23 | includes = includes && Array.isArray(includes) ? includes : ['**/*.js'];
|
24 | excludes = excludes && Array.isArray(excludes) ? excludes : ['**/node_modules/**'];
|
25 |
|
26 | opts = { cwd: root, nodir: true };
|
27 | seq += 1;
|
28 | opts['x' + seq + new Date().getTime()] = true;
|
29 | fileset(includes.join(' '), excludes.join(' '), opts, function (err, files) {
|
30 | if (err) { return callback(err); }
|
31 | if (relative) { return callback(err, files); }
|
32 |
|
33 | if (!realpath) {
|
34 | files = files.map(function (file) { return path.resolve(root, file); });
|
35 | return callback(err, files);
|
36 | }
|
37 |
|
38 | var realPathCache = module.constructor._realpathCache || {};
|
39 |
|
40 | async.map(files, function (file, done) {
|
41 | fs.realpath(path.resolve(root, file), realPathCache, done);
|
42 | }, callback);
|
43 | });
|
44 | }
|
45 |
|
46 |
|
47 | module.exports={
|
48 |
|
49 | findFileRecursively: function (filename, workspacePath, lookInParentFolders, logger) {
|
50 | var searchedPaths = {};
|
51 | do {
|
52 | var filePath = path.resolve(workspacePath, filename);
|
53 |
|
54 | if (searchedPaths[filePath])
|
55 | return null;
|
56 | logger.debug("Looking for '%s' at '%s'", filename , path.normalize(workspacePath));
|
57 |
|
58 |
|
59 | searchedPaths[filePath] = true;
|
60 | try {
|
61 |
|
62 | var f = fs.statSync(filePath);
|
63 | if (f.isFile()) {
|
64 | logger.debug("found file: '%s' in: '%s'", filename , path.normalize(workspacePath));
|
65 | return filePath;
|
66 | }else{
|
67 | workspacePath = levelUp(workspacePath, filename, logger);
|
68 | }
|
69 | } catch (e) {
|
70 | workspacePath = levelUp(workspacePath, filename, logger);
|
71 | }
|
72 | if (!lookInParentFolders){
|
73 | logger.debug("should not search in parent directories, breaking out.")
|
74 | break;
|
75 | }
|
76 | } while (workspacePath.length > 1);
|
77 | logger.info(filename + ' not found');
|
78 | return null;
|
79 | },
|
80 |
|
81 | copyFileAsIs : function (inputFile, outputFile, name, logger, callback) {
|
82 | logger.info("Copying file '" + name + "' as is.");
|
83 |
|
84 | var readStream = fs.createReadStream(inputFile, {
|
85 | 'bufferSize': READ_FILE_CHUNK_SIZE
|
86 | });
|
87 | var writeStream = fs.createWriteStream(outputFile);
|
88 |
|
89 | readStream.on('error', callback);
|
90 | writeStream.on('error', callback);
|
91 |
|
92 | readStream.pipe(writeStream);
|
93 |
|
94 | var _this = this;
|
95 | readStream.on('end', function () {
|
96 | logger.debug("Finished copying '" + name + "'.");
|
97 | callback(null, name);
|
98 | });
|
99 | },
|
100 | copyDir: function (from, to, ignoredRelFiles, callback, onFileCopied, onFileError) {
|
101 | var _this = this;
|
102 | var processedFilesLookup = {};
|
103 | ignoredRelFiles.forEach(function (f) {
|
104 | processedFilesLookup[f] = true;
|
105 | });
|
106 |
|
107 | var existingDirsLookup = {};
|
108 |
|
109 | filesFor({
|
110 | root: from,
|
111 | includes: ['**/*'],
|
112 | excludes: [],
|
113 | relative: true
|
114 | }, function (err, files) {
|
115 | if (err) {
|
116 | return callback(err);
|
117 | }
|
118 | var copyErrors = [];
|
119 | files.forEach(function (relFile) {
|
120 | if (processedFilesLookup[relFile]) return;
|
121 | try {
|
122 | inputFile = path.resolve(from, relFile);
|
123 | outputFile = path.resolve(to, relFile);
|
124 | var dir = path.dirname(outputFile);
|
125 | if (!existingDirsLookup[dir]) {
|
126 | mkdirp.sync(dir);
|
127 | existingDirsLookup[dir] = true;
|
128 | }
|
129 | var stat = fs.statSync(inputFile);
|
130 | if (stat.isDirectory()) {
|
131 | mkdirp.sync(outputFile);
|
132 | existingDirsLookup[outputFile] = true;
|
133 | } else if (stat.isFile()) {
|
134 | fs.writeFileSync(outputFile, fs.readFileSync(inputFile));
|
135 | if (onFileCopied != null)
|
136 | {
|
137 | onFileCopied(inputFile);
|
138 | }
|
139 | }
|
140 | } catch (e) {
|
141 | var msg = "Error copying file " + inputFile + " to " + outputFile + ": " + e.toString();
|
142 | if (onFileError != null)
|
143 | {
|
144 | onFileError(inputFile, msg);
|
145 | }
|
146 | }
|
147 | });
|
148 |
|
149 | if (copyErrors.length)
|
150 | err = new Error("Some files could not be copied:\n" + copyErrors.join('\n'));
|
151 | return callback(err);
|
152 | });
|
153 | },
|
154 | scanDirRecursively: function (directoryName, logger) {
|
155 | var fileNames = [];
|
156 | try {
|
157 | walk(directoryName, fileNames, "");
|
158 | return fileNames;
|
159 | }catch(err){
|
160 | logger.error("Error while trying to get all files under '" + directoryName + "'. ", err);
|
161 | return [];
|
162 | }
|
163 | },
|
164 |
|
165 | removeRelativeSigns: function (filePath) {
|
166 | if (filePath.indexOf(RELATIVE_PATH_SIGN) == -1) {
|
167 | return filePath;
|
168 | }
|
169 | var trimmed = filePath.substring(filePath.lastIndexOf(RELATIVE_PATH_SIGN) + RELATIVE_PATH_SIGN.length);
|
170 | return trimmed;
|
171 | }
|
172 | };
|
173 |
|
174 | function levelUp(workspacePath, filename, logger) {
|
175 | logger.debug("file: '" + filename + "' not found in: '" + path.normalize(workspacePath) + "'. searching in parent directory");
|
176 | workspacePath = workspacePath + '/..';
|
177 | return workspacePath;
|
178 | }
|
179 | function walk(directoryName, fileNames, baseFolder){
|
180 | var files = fs.readdirSync(directoryName);
|
181 | files.forEach(function (file) {
|
182 | var fullPath = path.join(directoryName, file);
|
183 | var relativePath = path.join(baseFolder, file);
|
184 | var stat = fs.statSync(fullPath);
|
185 | if (stat.isDirectory()) {
|
186 | walk(fullPath, fileNames, relativePath);
|
187 | } else {
|
188 | fileNames.push(relativePath.replace(/\\/g,'/'));
|
189 | }
|
190 | });
|
191 | } |
\ | No newline at end of file |