github.com/jfrog/frogbot@v1.1.1-0.20231221090046-821a26f50338/action/node_modules/@actions/io/lib/io-util.js (about)

     1  "use strict";
     2  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
     3      if (k2 === undefined) k2 = k;
     4      Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
     5  }) : (function(o, m, k, k2) {
     6      if (k2 === undefined) k2 = k;
     7      o[k2] = m[k];
     8  }));
     9  var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    10      Object.defineProperty(o, "default", { enumerable: true, value: v });
    11  }) : function(o, v) {
    12      o["default"] = v;
    13  });
    14  var __importStar = (this && this.__importStar) || function (mod) {
    15      if (mod && mod.__esModule) return mod;
    16      var result = {};
    17      if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    18      __setModuleDefault(result, mod);
    19      return result;
    20  };
    21  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    22      function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    23      return new (P || (P = Promise))(function (resolve, reject) {
    24          function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
    25          function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
    26          function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
    27          step((generator = generator.apply(thisArg, _arguments || [])).next());
    28      });
    29  };
    30  var _a;
    31  Object.defineProperty(exports, "__esModule", { value: true });
    32  exports.getCmdPath = exports.tryGetExecutablePath = exports.isRooted = exports.isDirectory = exports.exists = exports.IS_WINDOWS = exports.unlink = exports.symlink = exports.stat = exports.rmdir = exports.rename = exports.readlink = exports.readdir = exports.mkdir = exports.lstat = exports.copyFile = exports.chmod = void 0;
    33  const fs = __importStar(require("fs"));
    34  const path = __importStar(require("path"));
    35  _a = fs.promises, exports.chmod = _a.chmod, exports.copyFile = _a.copyFile, exports.lstat = _a.lstat, exports.mkdir = _a.mkdir, exports.readdir = _a.readdir, exports.readlink = _a.readlink, exports.rename = _a.rename, exports.rmdir = _a.rmdir, exports.stat = _a.stat, exports.symlink = _a.symlink, exports.unlink = _a.unlink;
    36  exports.IS_WINDOWS = process.platform === 'win32';
    37  function exists(fsPath) {
    38      return __awaiter(this, void 0, void 0, function* () {
    39          try {
    40              yield exports.stat(fsPath);
    41          }
    42          catch (err) {
    43              if (err.code === 'ENOENT') {
    44                  return false;
    45              }
    46              throw err;
    47          }
    48          return true;
    49      });
    50  }
    51  exports.exists = exists;
    52  function isDirectory(fsPath, useStat = false) {
    53      return __awaiter(this, void 0, void 0, function* () {
    54          const stats = useStat ? yield exports.stat(fsPath) : yield exports.lstat(fsPath);
    55          return stats.isDirectory();
    56      });
    57  }
    58  exports.isDirectory = isDirectory;
    59  /**
    60   * On OSX/Linux, true if path starts with '/'. On Windows, true for paths like:
    61   * \, \hello, \\hello\share, C:, and C:\hello (and corresponding alternate separator cases).
    62   */
    63  function isRooted(p) {
    64      p = normalizeSeparators(p);
    65      if (!p) {
    66          throw new Error('isRooted() parameter "p" cannot be empty');
    67      }
    68      if (exports.IS_WINDOWS) {
    69          return (p.startsWith('\\') || /^[A-Z]:/i.test(p) // e.g. \ or \hello or \\hello
    70          ); // e.g. C: or C:\hello
    71      }
    72      return p.startsWith('/');
    73  }
    74  exports.isRooted = isRooted;
    75  /**
    76   * Best effort attempt to determine whether a file exists and is executable.
    77   * @param filePath    file path to check
    78   * @param extensions  additional file extensions to try
    79   * @return if file exists and is executable, returns the file path. otherwise empty string.
    80   */
    81  function tryGetExecutablePath(filePath, extensions) {
    82      return __awaiter(this, void 0, void 0, function* () {
    83          let stats = undefined;
    84          try {
    85              // test file exists
    86              stats = yield exports.stat(filePath);
    87          }
    88          catch (err) {
    89              if (err.code !== 'ENOENT') {
    90                  // eslint-disable-next-line no-console
    91                  console.log(`Unexpected error attempting to determine if executable file exists '${filePath}': ${err}`);
    92              }
    93          }
    94          if (stats && stats.isFile()) {
    95              if (exports.IS_WINDOWS) {
    96                  // on Windows, test for valid extension
    97                  const upperExt = path.extname(filePath).toUpperCase();
    98                  if (extensions.some(validExt => validExt.toUpperCase() === upperExt)) {
    99                      return filePath;
   100                  }
   101              }
   102              else {
   103                  if (isUnixExecutable(stats)) {
   104                      return filePath;
   105                  }
   106              }
   107          }
   108          // try each extension
   109          const originalFilePath = filePath;
   110          for (const extension of extensions) {
   111              filePath = originalFilePath + extension;
   112              stats = undefined;
   113              try {
   114                  stats = yield exports.stat(filePath);
   115              }
   116              catch (err) {
   117                  if (err.code !== 'ENOENT') {
   118                      // eslint-disable-next-line no-console
   119                      console.log(`Unexpected error attempting to determine if executable file exists '${filePath}': ${err}`);
   120                  }
   121              }
   122              if (stats && stats.isFile()) {
   123                  if (exports.IS_WINDOWS) {
   124                      // preserve the case of the actual file (since an extension was appended)
   125                      try {
   126                          const directory = path.dirname(filePath);
   127                          const upperName = path.basename(filePath).toUpperCase();
   128                          for (const actualName of yield exports.readdir(directory)) {
   129                              if (upperName === actualName.toUpperCase()) {
   130                                  filePath = path.join(directory, actualName);
   131                                  break;
   132                              }
   133                          }
   134                      }
   135                      catch (err) {
   136                          // eslint-disable-next-line no-console
   137                          console.log(`Unexpected error attempting to determine the actual case of the file '${filePath}': ${err}`);
   138                      }
   139                      return filePath;
   140                  }
   141                  else {
   142                      if (isUnixExecutable(stats)) {
   143                          return filePath;
   144                      }
   145                  }
   146              }
   147          }
   148          return '';
   149      });
   150  }
   151  exports.tryGetExecutablePath = tryGetExecutablePath;
   152  function normalizeSeparators(p) {
   153      p = p || '';
   154      if (exports.IS_WINDOWS) {
   155          // convert slashes on Windows
   156          p = p.replace(/\//g, '\\');
   157          // remove redundant slashes
   158          return p.replace(/\\\\+/g, '\\');
   159      }
   160      // remove redundant slashes
   161      return p.replace(/\/\/+/g, '/');
   162  }
   163  // on Mac/Linux, test the execute bit
   164  //     R   W  X  R  W X R W X
   165  //   256 128 64 32 16 8 4 2 1
   166  function isUnixExecutable(stats) {
   167      return ((stats.mode & 1) > 0 ||
   168          ((stats.mode & 8) > 0 && stats.gid === process.getgid()) ||
   169          ((stats.mode & 64) > 0 && stats.uid === process.getuid()));
   170  }
   171  // Get the path of cmd.exe in windows
   172  function getCmdPath() {
   173      var _a;
   174      return (_a = process.env['COMSPEC']) !== null && _a !== void 0 ? _a : `cmd.exe`;
   175  }
   176  exports.getCmdPath = getCmdPath;
   177  //# sourceMappingURL=io-util.js.map