# io/index.js

    1// Copyright 2013 Selenium committers2// Copyright 2013 Software Freedom Conservancy3//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 at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing, software11// 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 and14// limitations under the License.1516var fs = require('fs'),17    path = require('path'),18    tmp = require('tmp');1920var promise = require('..').promise;212223var PATH_SEPARATOR = process.platform === 'win32' ? ';' : ':';242526// PUBLIC API27282930/**31 * Copies one file to another.32 * @param {string} src The source file.33 * @param {string} dst The destination file.34 * @return {!promise.Promise.<string>} A promise for the copied file's path.35 */36exports.copy = function(src, dst) {37  var copied = promise.defer();3839  var rs = fs.createReadStream(src);40  rs.on('error', copied.reject);41  rs.on('end', function() {42    copied.fulfill(dst);43  });4445  var ws = fs.createWriteStream(dst);46  ws.on('error', copied.reject);4748  rs.pipe(ws);4950  return copied.promise;51};525354/**55 * Recursively copies the contents of one directory to another.56 * @param {string} src The source directory to copy.57 * @param {string} dst The directory to copy into.58 * @param {(RegEx|function(string): boolean)=} opt_exclude An exclusion filter59 *     as either a regex or predicate function. All files matching this filter60 *     will not be copied.61 * @return {!promise.Promise.<string>} A promise for the destination62 *     directory's path once all files have been copied.63 */64exports.copyDir = function(src, dst, opt_exclude) {65  var predicate = opt_exclude;66  if (opt_exclude && typeof opt_exclude !== 'function') {67    predicate = function(p) {68      return !opt_exclude.test(p);69    };70  }7172  // TODO(jleyba): Make this function completely async.73  if (!fs.existsSync(dst)) {74    fs.mkdirSync(dst);75  }7677  var files = fs.readdirSync(src);78  files = files.map(function(file) {79    return path.join(src, file);80  });8182  if (predicate) {83    files = files.filter(predicate);84  }8586  var results = [];87  files.forEach(function(file) {88    var stats = fs.statSync(file);89    var target = path.join(dst, path.basename(file));9091    if (stats.isDirectory()) {92      if (!fs.existsSync(target)) {93        fs.mkdirSync(target, stats.mode);94      }95      results.push(exports.copyDir(file, target, predicate));96    } else {97      results.push(exports.copy(file, target));98    }99  });100101  return promise.all(results).then(function() {102    return dst;103  });104};105106107/**108 * Tests if a file path exists.109 * @param {string} path The path to test.110 * @return {!promise.Promise.<boolean>} A promise for whether the file exists.111 */112exports.exists = function(path) {113  var result = promise.defer();114  fs.exists(path, result.fulfill);115  return result.promise;116};117118119/**120 * @return {!promise.Promise.<string>} A promise for the path to a temporary121 *     directory.122 * @see https://www.npmjs.org/package/tmp123 */124exports.tmpDir = function() {125  return promise.checkedNodeCall(tmp.dir);126};127128129/**130 * @return {!promise.Promise.<string>} A promise for the path to a temporary131 *     file.132 * @see https://www.npmjs.org/package/tmp133 */134exports.tmpFile = function() {135  return promise.checkedNodeCall(tmp.file);136};137138139/**140 * Searches the {@code PATH} environment variable for the given file.141 * @param {string} file The file to locate on the PATH.142 * @param {boolean=} opt_checkCwd Whether to always start with the search with143 *     the current working directory, regardless of whether it is explicitly144 *     listed on the PATH.145 * @return {?string} Path to the located file, or {@code null} if it could146 *     not be found.147 */148exports.findInPath = function(file, opt_checkCwd) {149  if (opt_checkCwd) {150    var tmp = path.join(process.cwd(), file);151    if (fs.existsSync(tmp)) {152      return tmp;153    }154  }155156  var dirs = process.env['PATH'].split(PATH_SEPARATOR);157  var found = null;158  dirs.forEach(function(dir) {159    var tmp = path.join(dir, file);160    if (!found && fs.existsSync(tmp)) {161      found = tmp;162    }163  });164  return found;165};