How to use the util.promisify function in util

To help you get started, we’ve selected a few util examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github rajeshdavidbabu / Node-Clone-S3-Bucket / lib / helpers / file_ops.js View on Github external
// @ts-check

'use strict';

const fs = require('graceful-fs');
const { promisify } = require('util');

const mkdirPromise = promisify(fs.mkdir);

module.exports = () => ({
  createDirectoryAsync(dirPath) {
    return mkdirPromise(dirPath, { recursive: true });
  },

  _getDirPathFromFilePath(filePath) {
    let filePathArr = filePath.split('/');
    filePathArr.pop();
    return filePathArr.join('/');
  },

  getWriteStream(filePath) {
    // extract dir path from file path.
    const dirPath = this._getDirPathFromFilePath(filePath);
    return this.createDirectoryAsync(dirPath)
github destinygg / chat-bot / lib / services / reddit-vote.js View on Github external
const childProcess = require('child_process');
const util = require('util');
const readFile = util.promisify(require('fs').readFile);
const writeFile = util.promisify(require('fs').writeFile);

function writeJsonToFile(path, jsonToWrite, logger) {
  const fileContent = JSON.stringify(jsonToWrite);
  return writeFile(path, fileContent, 'utf8')
    .then(() => true)
    .catch(err => {
      logger.error(`Error while writing reddit file. Path: ${path} Err: ${err}`);
      return new Error('did not write');
    });
}

class RedditVote {
  constructor(config, logger) {
    // config properties
    this.scriptPath = config.scriptPath;
    this.threadFilePath =
github avaragado / contentful-backup / src / lib / synctoken.js View on Github external
// @flow

import path from 'path';
import fs from 'fs';
import { promisify } from 'util';

import mkdirp from 'mkdirp';

const mkdir = promisify(mkdirp);
const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);
const stat = promisify(fs.stat);

const relpathToken = 'nextSyncToken.txt';
const abspathTokenForDir = dir => path.resolve(dir, relpathToken);

type LoaderResult = {|
    nextSyncToken: ?string,
    lastSyncDate: ?Date,
|};

type Loader = (dir: string) => Promise;

const load: Loader = async (dir) => {
    const abspath = abspathTokenForDir(dir);

    try {
        const nextSyncToken = await readFile(abspath, { encoding: 'utf-8' });
github bobylito / playlistFinder / scrapper / lib / playlists.js View on Github external
const path = require("path");
const promisify = require("util").promisify;
const readdir = promisify(require("fs").readdir);
const readFile = promisify(require("fs").readFile);
const MongoClient = require("mongodb").MongoClient;

const FOLDER = path.join("playlists");

module.exports = {
  closeDB,
  connectToDB,
  deduplicatePlaylists,
  readAllPlaylists,
  writePlaylist,
  getPlaylist,
  getAllPlaylists,
};

async function readAllPlaylists() {
  const datasets = await readdir(FOLDER);
github cdr / node-browser / src / server / fs.ts View on Github external
public unlink(path: fs.PathLike): Promise {
    return promisify(fs.unlink)(path)
  }
github microsoft / BotFramework-Composer / Composer / packages / server / src / models / storage / localDiskStorage.ts View on Github external
import fs from 'fs';
import { promisify } from 'util';

import glob from 'globby';

import { IFileStorage, Stat, MakeDirectoryOptions } from './interface';

const stat = promisify(fs.stat);
const readFile = promisify(fs.readFile);
const readDir = promisify(fs.readdir);
const writeFile = promisify(fs.writeFile);
const removeFile = promisify(fs.unlink);
const mkDir = promisify(fs.mkdir);
const copyFile = promisify(fs.copyFile);
const rename = promisify(fs.rename);

export class LocalDiskStorage implements IFileStorage {
  async stat(path: string): Promise {
    try {
      const fstat = await stat(path);
      return {
        isDir: fstat.isDirectory(),
        isFile: fstat.isFile(),
        lastModified: fstat.ctime.toString(),
        size: fstat.isFile() ? fstat.size.toString() : '',
      };
    } catch (error) {
      throw error;
github gjuchault / hyperalfred / index.js View on Github external
const fs = require('graceful-fs')
const { promisify } = require('util')
const { resolve } = require('path')
const { homedir } = require('os')

const readFile = promisify(fs.readFile)
const writeFile = promisify(fs.writeFile)
const access = promisify(fs.access)

const commandFile = resolve(homedir(), '.hyper_plugins', 'hyperalfred.txt')

async function loadCommand() {
  try {
    const hasCommand = await access(commandFile, fs.constants.R_OK | fs.constants.W_OK)

    let command = await readFile(commandFile, 'utf8')

    command = command.trim()

    if (command.length === 0) {
      return null
    }
github thonatos / shorturl-egg / app / controller / home.js View on Github external
'use strict';

const util = require('util');
const QRCode = require('qrcode');
const toDataURL = util.promisify(QRCode.toDataURL);

module.exports = app => {
  class HomeController extends app.Controller {
    * index() {
      yield this.ctx.render('rct/build/index.html');
    }

    * redirect() {
      const { ctx, service } = this;
      const hash = ctx.params.hash;
      const record = yield service.shorten.expand(hash);
      if (!record) {
        const err = new Error('no record found');
        err.status = 404;
        throw err;
      }
github gilmarsquinelato / i18n-manager / main / pluginManager.ts View on Github external
import * as path from 'path';
import * as util from 'util';
import * as fs from 'fs';
import { ParsedFile } from '../common/types';
import { getLocaleFromText } from '../common/language';
import getPlugins, { Plugin } from './plugins';


const readFilePromise = util.promisify(fs.readFile);
const writeFilePromise = util.promisify(fs.writeFile);

export const getAvailablePlugins = (): Plugin[] => getPlugins();

export const getParsedFiles = async (files: string[]): Promise => {
  const parsedFiles: ParsedFile[] = [];
  for (let i = 0; i < files.length; ++i) {
    const parsedFile = await openFile(files[i]);
    if (parsedFile) {
      parsedFiles.push(parsedFile);
    }
  }

  return parsedFiles;
};

export const openFile = async (filePath: string): Promise => {