1 | // Copyright 2013 Selenium committers |
2 | // Copyright 2013 Software Freedom Conservancy |
3 | // |
4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
5 | // you may not use this file except in compliance with the License. |
6 | // You may obtain a copy of the License at |
7 | // |
8 | // http://www.apache.org/licenses/LICENSE-2.0 |
9 | // |
10 | // Unless required by applicable law or agreed to in writing, software |
11 | // distributed under the License is distributed on an "AS IS" BASIS, |
12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | // See the License for the specific language governing permissions and |
14 | // limitations under the License. |
15 | |
16 | var fs = require('fs'), |
17 | path = require('path'); |
18 | |
19 | |
20 | var PATH_SEPARATOR = process.platform === 'win32' ? ';' : ':'; |
21 | |
22 | |
23 | // PUBLIC API |
24 | |
25 | |
26 | /** |
27 | * Searches the {@code PATH} environment variable for the given file. |
28 | * @param {string} file The file to locate on the PATH. |
29 | * @param {boolean=} opt_checkCwd Whether to always start with the search with |
30 | * the current working directory, regardless of whether it is explicitly |
31 | * listed on the PATH. |
32 | * @return {?string} Path to the located file, or {@code null} if it could |
33 | * not be found. |
34 | */ |
35 | exports.findInPath = function(file, opt_checkCwd) { |
36 | if (opt_checkCwd) { |
37 | var tmp = path.join(process.cwd(), file); |
38 | if (fs.existsSync(tmp)) { |
39 | return tmp; |
40 | } |
41 | } |
42 | |
43 | var dirs = process.env['PATH'].split(PATH_SEPARATOR); |
44 | var found = null; |
45 | dirs.forEach(function(dir) { |
46 | var tmp = path.join(dir, file); |
47 | if (!found && fs.existsSync(tmp)) { |
48 | found = tmp; |
49 | } |
50 | }); |
51 | return found; |
52 | }; |