UNPKG

4.54 kBJavaScriptView Raw
1var logger = require('./logger');
2var maven = require('./maven');
3var path = require('path');
4var fs = require('fs');
5var cwd = process.cwd();
6var _string = require('underscore.string');
7
8if (maven.isMavenProject) {
9 exports.srcPaths = ['src/main/js','src/main/less'];
10 exports.testSrcPath = 'src/test/js';
11} else {
12 exports.srcPaths = ['./js', './less'];
13 exports.testSrcPath = './spec';
14}
15
16/**
17 * Get the parent directory of the supplied file/directory.
18 * @param file The file/directory.
19 * @returns The parent directory.
20 */
21exports.parentDir = function(file) {
22 return path.dirname(file);
23};
24
25/**
26 * Get the absolute path to the javascript src root directory.
27 */
28exports.getAbsoluteJSRoot = function() {
29 // The assumption is that the first srcPaths is the one
30 // we're interested in :)
31 return path.resolve(cwd, exports.srcPaths[0]);
32};
33
34/**
35 * Get the absolute path to a resource within the javascript root directory.
36 * @param jsPath The path to the resource, relative to the javascript src
37 * root directory.
38 */
39exports.toAbsoluteJSPath = function(jsPath) {
40 return path.resolve(exports.getAbsoluteJSRoot(), jsPath);
41};
42
43/**
44 * Create the directory path, including missing parent dirs.
45 * <p>
46 * Equivalent to <code>mkdir -p</code>.
47 *
48 * @param thePath The dir path to create.
49 */
50exports.mkdirp = function(thePath) {
51 if (!path.isAbsolute(thePath)) {
52 thePath = path.resolve(cwd, thePath);
53 }
54 if (!fs.existsSync(thePath)) {
55 var parentDir = path.dirname(thePath);
56 if (!fs.existsSync(parentDir)) {
57 exports.mkdirp(parentDir);
58 }
59 fs.mkdirSync(thePath);
60 }
61};
62
63/**
64 * Do files with the specified extension exist in any of the src directories.
65 */
66exports.hasSourceFiles = function(ext) {
67 var glob = require('glob');
68 var hasFiles = false;
69 var options = {
70 nodir: true
71 };
72
73 function _hasFiles(path) {
74 if (!_string.endsWith(path, '/')) {
75 path += '/';
76 }
77
78 var files = glob.sync(path + "**/*." + ext, options);
79 hasFiles = (files && files.length > 0);
80 }
81
82 for (var i = 0; hasFiles === false && i < exports.srcPaths.length; i++) {
83 _hasFiles(exports.srcPaths[i]);
84 }
85 if (hasFiles === false) {
86 _hasFiles(exports.testSrcPath);
87 }
88
89 return hasFiles;
90};
91
92/**
93 * Search back through the directory hierarchy looking for the named file,
94 * starting at the {@code startDir}, or the current working directory if
95 * not {@code startDir} is not specific.
96 * @param fileName The file name to look for.
97 * @param startDir The directory to start looking from.
98 * @returns The closest directory containing the file, or {@code undefined} if no
99 * such file was found in the hierarchy.
100 */
101exports.findClosest = function(fileName, startDir) {
102 var lookInDir = (startDir || cwd);
103 var existsCheckPath = path.resolve(lookInDir, fileName);
104
105 if (fs.existsSync(existsCheckPath)) {
106 return existsCheckPath;
107 }
108
109 var parentDir = exports.parentDir(lookInDir);
110 if (parentDir && parentDir !== lookInDir) {
111 return exports.findClosest(fileName, parentDir);
112 }
113
114 return undefined;
115};
116
117/**
118 * Recursively walk a directory tree.
119 * @param startDir The directory on which to start.
120 * @param callback The callback to call for each directory n the tree.
121 * @param stopAtDepth The depth at which recursion should stop. {@code undefined}
122 * or {@code -1} for infinite recursion.
123 */
124exports.walkDirs = function(startDir, callback, stopAtDepth) {
125 stopAtDepth = (stopAtDepth || -1); // -1 means no stop depth i.e. infinite
126 walkDirs(startDir, callback, stopAtDepth);
127};
128
129function walkDirs(startDir, callback, stopAtDepth) {
130 if (!fs.existsSync(startDir)) {
131 return;
132 }
133 var stats = fs.statSync(startDir);
134 if (!stats.isDirectory()) {
135 return;
136 }
137
138 if (callback(startDir) !== false) { // Stop recursion if the callback returns false.
139 if (stopAtDepth !== 0) { // Stop recursion when stopAtDepth hits zero. stopAtDepth can be < 0, which means infinite recursion.
140 var files = fs.readdirSync(startDir);
141 if (files) {
142 for (var i = 0; i < files.length; i++) {
143 // Recursively call walkDirs for each.
144 // It will ignore non-directory files.
145 walkDirs(path.resolve(startDir, files[i]), callback, (stopAtDepth - 1));
146 }
147 }
148 }
149 }
150}
\No newline at end of file