|
Lines 47-55
Link Here
|
| 47 |
return null; // suppress rejection |
47 |
return null; // suppress rejection |
| 48 |
}); |
48 |
}); |
| 49 |
}); |
49 |
}); |
|
|
50 |
}, function reject(err) { |
| 51 |
return []; |
| 50 |
}) |
52 |
}) |
| 51 |
.then(function(results) { |
53 |
.then(function(results) { |
| 52 |
return results.filter(function(r) { return r; }); |
54 |
return results.filter(function(r) { return r; }); |
|
|
55 |
}, function reject(err) { |
| 56 |
return []; |
| 53 |
}); |
57 |
}); |
| 54 |
}; |
58 |
}; |
| 55 |
|
59 |
|
|
Lines 77-88
Link Here
|
| 77 |
return safePath(workspaceDir, path.join(workspaceDir, filepath)); |
81 |
return safePath(workspaceDir, path.join(workspaceDir, filepath)); |
| 78 |
}; |
82 |
}; |
| 79 |
|
83 |
|
| 80 |
var getParents = exports.getParents = function(fileRoot, relativePath) { |
84 |
var getParents = exports.getParents = function(fileRoot, relativePath, includeFolder) { |
| 81 |
var segs = relativePath.split('/'); |
85 |
var segs = relativePath.split('/'); |
| 82 |
if(segs && segs.length > 0 && segs[segs.length-1] === ""){// pop the last segment if it is empty. In this case wwwpath ends with "/". |
86 |
if(segs && segs.length > 0 && segs[segs.length-1] === ""){// pop the last segment if it is empty. In this case wwwpath ends with "/". |
| 83 |
segs.pop(); |
87 |
segs.pop(); |
| 84 |
} |
88 |
} |
| 85 |
segs.pop();//The last segment now is the directory itself. We do not need it in the parents array. |
89 |
if(!includeFolder) { |
|
|
90 |
segs.pop();//The last segment now is the directory itself. We do not need it in the parents array. |
| 91 |
} |
| 86 |
var loc = fileRoot; |
92 |
var loc = fileRoot; |
| 87 |
var parents = []; |
93 |
var parents = []; |
| 88 |
for (var i=0; i < segs.length; i++) { |
94 |
for (var i=0; i < segs.length; i++) { |
|
Lines 99-104
Link Here
|
| 99 |
}; |
105 |
}; |
| 100 |
|
106 |
|
| 101 |
/** |
107 |
/** |
|
|
108 |
* @description Tries to compute the project path for the given file path |
| 109 |
* @param {string} workspaceDir The root workspace directory |
| 110 |
* @param {string} fileRoot The root file path of the server |
| 111 |
* @param {string} relativePath The path we want the project for |
| 112 |
* @param {?} options The optional map of options to use while looking for a project |
| 113 |
* @returns {Promise} A promise to resolve the project |
| 114 |
* @since 14.0 |
| 115 |
*/ |
| 116 |
var getProject = exports.getProject = function getProject(workspaceDir, fileRoot, relativePath, options) { |
| 117 |
var parents = getParents(fileRoot, relativePath, true), |
| 118 |
names = options && typeof options.names === "object" ? options.names : {}; |
| 119 |
names['.git'] = {isDirectory: true}; |
| 120 |
names['project.json'] = Object.create(null); |
| 121 |
if(Array.isArray(parents) && parents.length > 0) { |
| 122 |
var promises = []; |
| 123 |
for(var i = 0, len = parents.length; i < len; i++) { |
| 124 |
var parentFile = parents[i], |
| 125 |
filepath = parentFile.Location.slice(fileRoot.length); |
| 126 |
if(filepath.indexOf(workspaceDir) !== 0) { |
| 127 |
break; |
| 128 |
} |
| 129 |
promises.push(findProject(fileRoot, workspaceDir, filepath, names)); |
| 130 |
} |
| 131 |
return Promise.any(promises); |
| 132 |
} |
| 133 |
}; |
| 134 |
/** |
| 135 |
* @description Finds the project from the given file context |
| 136 |
* @param {string} fileRoot The root file context |
| 137 |
* @param {string} workspaceDir The root workspace location |
| 138 |
* @param {string} filepath The absolute path to the file context we are starting from |
| 139 |
* @param {?} names The map of names to check against to determine project-ness |
| 140 |
* @since 14.0 |
| 141 |
* @returns {Promise} Returns a promise to try and resolve the project from the context |
| 142 |
*/ |
| 143 |
function findProject(fileRoot, workspaceDir, filepath, names) { |
| 144 |
return new Promise(function(resolve, reject) { |
| 145 |
getChildren(fileRoot, workspaceDir, filepath, 1).then(function(children) { |
| 146 |
if(Array.isArray(children) && children.length > 0) { |
| 147 |
for(var i = 0, len = children.length; i < len; i++) { |
| 148 |
var c = children[i], |
| 149 |
n = names[c.Name]; |
| 150 |
if(n && (c.Directory && n.isDirectory)) { |
| 151 |
var wwwpath = api.toURLPath(filepath.substring(workspaceDir.length + 1)); |
| 152 |
return resolve({ |
| 153 |
Name: path.basename(filepath), |
| 154 |
Location: getFileLocation(fileRoot, wwwpath, true), |
| 155 |
Directory: true, |
| 156 |
Parents: getParents(fileRoot, wwwpath) |
| 157 |
}); |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
return reject(new Error('not a project')); |
| 162 |
}); |
| 163 |
}); |
| 164 |
} |
| 165 |
/** |
| 102 |
* Performs the equivalent of rm -rf on a directory. |
166 |
* Performs the equivalent of rm -rf on a directory. |
| 103 |
* @param {Function} callback Invoked as callback(error) |
167 |
* @param {Function} callback Invoked as callback(error) |
| 104 |
*/ |
168 |
*/ |