fs#accessSync TypeScript Examples

The following examples show how to use fs#accessSync. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: init.ts    From OpenVolunteerPlatform with MIT License 6 votes vote down vote up
/**
 * Check if directory exists
 * @param path path of the directory
 * @param name name of the project folder
 */
function checkDirectory(path: string, name: string): void {
  try {
    accessSync(path)
    logError(`A folder with name ${name} exists. Remove it or try another name.`)
    process.exit(0)
  } catch (error) {
    return
  }
}
Example #2
Source File: DBHelper.ts    From insmemo with GNU General Public License v3.0 6 votes vote down vote up
function getDbInstance() {
  let temp: sqlite3.Database;

  try {
    accessSync(userDbFile);
    temp = new sqlite3.Database(userDbFile, (err) => {
      if (err) {
        console.error(err.message);
      } else {
        console.log("Connected to the user database.");
      }
    });
  } catch (error) {
    console.log("/data/memo.db file not exist");
    throw "/data/memo.db file not exist";
  }

  return temp;
}
Example #3
Source File: index.ts    From solita with Apache License 2.0 6 votes vote down vote up
/**
 * Ensures that a file or directory is accessible to the current user.
 * @private
 */
export function canAccessSync(p: PathLike, mode: number = R_OK | W_OK) {
  try {
    accessSync(p, mode)
    return true
  } catch (_) {
    return false
  }
}
Example #4
Source File: lsp.ts    From coffeesense with MIT License 5 votes vote down vote up
private warnProjectIfNeed(projectConfig: ProjectConfig) {
    if (projectConfig.lspFullConfig.coffeesense.ignoreProjectWarning) {
      return;
    }

    const isFileCanAccess = (fsPath: string) => {
      try {
        accessSync(fsPath, constants.R_OK);
        return true;
      } catch {
        return false;
      }
    };
    const showErrorIfCantAccess = (name: string, fsPath: string) => {
      this.lspConnection.window.showErrorMessage(`CoffeeSense can't access ${fsPath} for ${name}.`);
    };

    const showWarningAndLearnMore = (message: string, url: string) => {
      this.lspConnection.window.showWarningMessage(message, { title: 'Learn More' }).then(action => {
        if (action) {
          this.openWebsite(url);
        }
      });
    };

    const getCantFindMessage = (fileNames: string[]) =>
      `CoffeeSense can't find ${fileNames.map(el => `\`${el}\``).join(' or ')} in ${projectConfig.rootFsPath}.`;
    if (!projectConfig.tsconfigPath) {
      showWarningAndLearnMore(
        getCantFindMessage(['tsconfig.json', 'jsconfig.json']),
        'https://github.com/phil294/coffeesense/blob/master/docs/guide/FAQ.md#coffeesense-can-t-find-tsconfig-json-jsconfig-json-in-xxxx-xxxxxx'
      );
    } else if (!isFileCanAccess(projectConfig.tsconfigPath)) {
      showErrorIfCantAccess('ts/js config', projectConfig.tsconfigPath);
    } else {
      if (
        !projectConfig.isExistCoffeeSenseConfig &&
        ![
          normalizeFileNameResolve(projectConfig.rootFsPath, 'tsconfig.json'),
          normalizeFileNameResolve(projectConfig.rootFsPath, 'jsconfig.json')
        ].includes(projectConfig.tsconfigPath ?? '')
      ) {
        showWarningAndLearnMore(
          `CoffeeSense found \`tsconfig.json\`/\`jsconfig.json\`, but they aren\'t in the project root.`,
          'https://github.com/phil294/coffeesense/blob/master/docs/guide/FAQ.md#coffeesense-found-xxx-but-they-aren-t-in-the-project-root'
        );
      }
    }

    if (!projectConfig.packagePath) {
      showWarningAndLearnMore(
        getCantFindMessage(['package.json']),
        'https://github.com/phil294/coffeesense/blob/master/docs/guide/FAQ.md#coffeesense-can-t-find-package-json-in-xxxx-xxxxxx'
      );
    } else if (!isFileCanAccess(projectConfig.packagePath)) {
      showErrorIfCantAccess('ts/js config', projectConfig.packagePath);
    } else {
      if (
        !projectConfig.isExistCoffeeSenseConfig &&
        normalizeFileNameResolve(projectConfig.rootFsPath, 'package.json') !== projectConfig.packagePath
      ) {
        showWarningAndLearnMore(
          `CoffeeSense found \`package.json\`/, but it isn\'t in the project root.`,
          'https://github.com/phil294/coffeesense/blob/master/docs/guide/FAQ.md#coffeesense-found-xxx-but-they-aren-t-in-the-project-root'
        );
      }
    }
  }