How to use the fs-extra.readFile function in fs-extra

To help you get started, we’ve selected a few fs-extra 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 neo-one-suite / neo-one / packages / neo-one-server-plugin / src / Config.ts View on Github external
private async getConfig({ config = this.defaultConfig }: { readonly config?: TConfig } = {}): Promise {
    let contents;
    try {
      contents = await fs.readFile(this.configPath, 'utf8');
    } catch (error) {
      if (error.code === 'ENOENT') {
        return this.update({ config });
      }

      throw error;
    }

    const currentConfig = JSON.parse(contents);
    await this.validate(currentConfig);

    if (config !== undefined) {
      // tslint:disable-next-line no-any
      return (SeamlessImmutable as any).merge(config, currentConfig, {
        deep: true,
      });
github addyosmani / critical / lib / file-helper.js View on Github external
.then(data => {
            // Src can either be absolute or relative to opts.base
            if (opts.src !== path.resolve(data) && !isExternal(opts.src)) {
                file.path = path.join(opts.base, opts.src);
            } else {
                file.path = path.relative(process.cwd(), data);
            }

            return fs.readFile(file.path).then(contents => {
                file.contents = contents;
                return file;
            });
        });
}
github laurent22 / joplin / Tools / tool-utils.js View on Github external
toolUtils.insertContentIntoFile = async function(filePath, markerOpen, markerClose, contentToInsert) {
	const fs = require('fs-extra');
	let content = await fs.readFile(filePath, 'utf-8');
	// [^]* matches any character including new lines
	const regex = new RegExp(`${markerOpen}[^]*?${markerClose}`);
	content = content.replace(regex, markerOpen + contentToInsert + markerClose);
	await fs.writeFile(filePath, content);
};
github expo / expo-cli / packages / xdl / src / tools / FsCache.ts View on Github external
async getAsync(): Promise {
    let mtime: Date;
    try {
      const stats = await fs.stat(this.filename);
      mtime = stats.mtime;
    } catch (e) {
      try {
        await fs.mkdirp(getCacheDir());

        if (this.bootstrapFile) {
          const bootstrapContents = (await fs.readFile(this.bootstrapFile)).toString();

          await fs.writeFile(this.filename, bootstrapContents, 'utf8');
        }
      } catch (e) {
        // intentional no-op
      }
      mtime = new Date(1989, 10, 19);
    }

    let fromCache: T | null = null;
    let failedRefresh = null;

    // if mtime + ttl >= now, attempt to fetch the value, otherwise read from disk
    if (new Date().getTime() - mtime.getTime() > this.ttlMilliseconds) {
      try {
        fromCache = await this.refresher();
github gridsome / gridsome / gridsome / lib / workers / __tests__ / image-processor.spec.js View on Github external
return Promise.all(assets.map(async ({ filePath, src, hash }) => {
    const imageOptions = processQueue.images.createImageOptions(options)
    const filename = processQueue.images.createFileName(filePath, imageOptions, hash)
    const cachePath = path.join(imageCacheDir, filename)
    const destPath = path.join(context, src)

    const buffer = await fs.readFile(destPath)

    return { filePath, destPath, cachePath, buffer }
  }))
}
github MeasureOSS / Measure / lib / runtime_auth.js View on Github external
return new Promise((resolve, reject) => {
        fs.readFile("php/authlist.php", {encoding: "utf-8"}, (err, data) => {
            var authList = auths.map(a => '"' + a.name + '" => "' + a.php_verifier + '"').join(",");
            data = data.replace("$auth_list = array();", "$auth_list = array(" + authList + ");")
            const outputFile = path.join(options.userConfig.output_directory, "authlist.php");
            fs.writeFile(outputFile, data, {encoding: "utf8"}, err => {
                if (err) {
                    return reject(NICE_ERRORS.COULD_NOT_WRITE_API(err, outputFile));
                }
                return resolve(options);
            });
        })
    });
}
github expo / expo-cli / packages / xdl / src / credentials / AndroidCredentials.ts View on Github external
export async function logKeystoreHashes(keystoreInfo: KeystoreInfo, linePrefix: string = '') {
  const { keystorePath, keystorePassword, keyAlias } = keystoreInfo;
  const certFile = `${keystorePath}.cer`;
  try {
    await exportCertBinary(keystoreInfo, certFile);
    const data = await fs.readFile(certFile);
    const googleHash = crypto
      .createHash('sha1')
      .update(data)
      .digest('hex')
      .toUpperCase();
    const googleHash256 = crypto
      .createHash('sha256')
      .update(data)
      .digest('hex')
      .toUpperCase();
    const fbHash = crypto
      .createHash('sha1')
      .update(data)
      .digest('base64');
    log.info(
      `${linePrefix}Google Certificate Fingerprint:     ${googleHash.replace(
github dennisreimann / uiengine / packages / adapter-css / src / theme.js View on Github external
async function extractCustomProperties (filePath) {
  const contents = await readFile(filePath, 'utf-8')
  const root = postcss.parse(contents, { from: filePath })
  const customProps = {}

  root.walkDecls(node => {
    const { prop, value } = node
    if (prop.startsWith('--')) {
      const prev = node.prev()
      const comment = prev && prev.type === 'comment' ? prev.text : undefined
      const { variable, fallbacks } = extractVarDefinition(value)

      customProps[prop] = {
        prop,
        value,
        comment,
        variable,
        fallbacks
github idyll-lang / idyll / packages / idyll-cli / bin / cmds / publish.js View on Github external
async function getProjectToken (tokenPath, config) {
  var token;
  try {
    token = await fs.readFile(tokenPath, { encoding: 'utf-8' });
  } catch (err) {
    let deployment = await request.post({
      url: urljoin(IDYLL_PUB_API, 'create'),
      body: {
        name: config.name
      },
      json: true
    });
    token = deployment.token;
    await fs.writeFile(tokenPath, token, { encoding: 'utf-8' });
  }
  return token;
}