Example #1
0
async function _rotateHHVMLogs(path: string): Promise<void> {
  let fileStat;
  try {
    fileStat = await fsPromise.stat(path);
  } catch (_) {
    return;
  }

  // Cap the size of the log file so it can't grow forever.
  const MAX_LOG_FILE_SIZE_BYTES = 512 * 1024; // 0.5 MB
  const MAX_LOGS_TO_KEEP = 5;
  if (fileStat.size >= MAX_LOG_FILE_SIZE_BYTES) {
    // Rotate the logs.
    for (let i = MAX_LOGS_TO_KEEP - 1; i >= 0; i--) {
      const fromFile = i > 0 ? path + i : path;
      const toFile = path + (i + 1);

      // eslint-disable-next-line no-await-in-loop
      const exists = await fsPromise.exists(toFile);
      if (exists) {
        try {
          // eslint-disable-next-line no-await-in-loop
          await fsPromise.unlink(toFile).catch(() => {});
        } catch (_) {}
      }

      try {
        // eslint-disable-next-line no-await-in-loop
        await fsPromise.mv(fromFile, toFile).catch(() => {});
      } catch (_) {}
    }
  }
}
Example #2
0
async function getIsDirectory(filePath: NuclideUri): Promise<boolean> {
  try {
    if (nuclideUri.isRemote(filePath)) {
      return false;
    } else {
      const stats = await fsPromise.stat(filePath);
      return stats.isDirectory();
    }
  } catch (e) {
    return false;
  }
}
Example #3
0
async function getRealPath(
  entityPath: string,
  isFile: boolean,
): Promise<string> {
  // NOTE: this will throw when trying to watch non-existent entities.
  const stat = await fsPromise.stat(entityPath);
  if (stat.isFile() !== isFile) {
    getLogger('nuclide-filewatcher-rpc').warn(
      `FileWatcherService: expected ${entityPath} to be a ${
        isFile ? 'file' : 'directory'
      }`,
    );
  }
  return fsPromise.realpath(entityPath);
}
Example #4
0
async function getDefaultShellCommand(): Promise<?Command> {
  if (process.platform === 'win32') {
    return {
      file: 'cmd.exe',
      args: [],
    };
  }

  const userInfo = os.userInfo();
  const username = userInfo.username;
  let defaultShell = null;
  if (process.platform === 'darwin') {
    const homedir = userInfo.homedir;
    const output = await runCommand('dscl', [
      '.',
      '-read',
      homedir,
      'UserShell',
    ]).toPromise();
    // Expected output looks like:
    //   UserShell: /bin/bash
    const prefix = 'UserShell: ';
    if (output != null && output.startsWith(prefix)) {
      defaultShell = output.substring(prefix.length).trim();
    }
  } else if (process.platform === 'linux') {
    const output = await runCommand('getent', ['passwd', username]).toPromise();
    // Expected output looks like:
    //   userid:*:1000:1000:Full Name:/home/userid:/bin/bash
    defaultShell = output.substring(output.lastIndexOf(':') + 1).trim();
  }
  if (defaultShell == null || defaultShell === '') {
    return null;
  }

  // Sanity check that the file exists and is executable
  const stat = await fsPromise.stat(defaultShell);
  // eslint-disable-next-line no-bitwise
  if ((stat.mode & fs.constants.S_IXOTH) === 0) {
    return null;
  }

  return {
    file: defaultShell,
    args: ['-l'],
  };
}
async function newFileSearch(
  directory: string,
  ignoredNames: Array<string>,
): Promise<FileSearchProcess> {
  const exists = await fsPromise.exists(directory);
  if (!exists) {
    throw new Error('Could not find directory to search : ' + directory);
  }

  const stat = await fsPromise.stat(directory);
  if (!stat.isDirectory()) {
    throw new Error('Provided path is not a directory : ' + directory);
  }

  const task = new Task();
  await task.invokeRemoteMethod({
    file: require.resolve('./process/FileSearch'),
    method: 'initFileSearchForDirectory',
    args: [directory, ignoredNames],
  });
  return new FileSearchProcess(task, directory, ignoredNames);
}