当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript promises.readFile方法代码示例

本文整理汇总了TypeScript中fs.promises.readFile方法的典型用法代码示例。如果您正苦于以下问题:TypeScript promises.readFile方法的具体用法?TypeScript promises.readFile怎么用?TypeScript promises.readFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在fs.promises的用法示例。


在下文中一共展示了promises.readFile方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: getSpecificPicture

export async function getSpecificPicture(fileName: string, thumb: boolean = false): Promise<Picture> {
  const splitted = fileName.split('.');
  const type = splitted[splitted.length - 1];
  const filePath = thumb ? `${picturePath}/thumb/${fileName}` : `${picturePath}/${fileName}`;

  try {
    return {
      fileName,
      file: await fs.readFile(path.resolve(filePath)),
      type,
    };
  } catch (e) {
    if (!thumb) {
      throw e;
    }
  }

  const image = await resize({
    src: path.resolve(`${picturePath}/${fileName}`),
    dst: path.resolve(`${picturePath}/thumb/${fileName}`),
    width: 400,
  });
  const file = await fs.readFile(path.resolve(image.path));

  return {
    fileName,
    file,
    type,
  };
}
开发者ID:marudor,项目名称:randomCats,代码行数:30,代码来源:pictures.ts

示例2: prepare_posts

async function prepare_posts() {
  const dir = `${__dirname}/posts/`;
  const folders = await readdir(dir);

  for (const folder of folders) {
    // TODO: Make parallel
    const data = await readFile(`${dir}${folder}/data.json`, "utf8");
    let post_data = JSON.parse(data);

    const post = await readFile(`${dir}${folder}/post.md`, "utf8");
    marked(post, (err, content) => {
      post_data["text"] = content;
      posts[post_data.title.replace(/ /g, "_")] = post_data;
    });
  }
}
开发者ID:hugo19941994,项目名称:hugofs,代码行数:16,代码来源:posts.ts

示例3: getDebugGeneratorContext

// Create a debugging version of our AutoRest abstraction (ideally we'd just
// launch AutoRest and parse the readme.md, but that's also nontrivial to get
// working correctly)
async function getDebugGeneratorContext(swaggerPath : string, outputPath?: string) : Promise<IGeneratorContext> {
    const dir = outputPath || path.dirname(swaggerPath);
    const settings : { [key: string]: any; } = {
        'input-file': swaggerPath,
        'output-folder': dir,
        'clear-output-folder': false,
        'azure-track2-csharp': true
    };
    const ctx = {
        async getSetting(key: string) : Promise<any> { return Promise.resolve(settings[key]); },
        async getInputUris() : Promise<string[]> { return Promise.resolve([swaggerPath]); },
        log(message: string): void { console.log(`INFORMATION: ${message}`); },
        warn(message: string): void { console.error(chalk.yellow(`WARNING: ${message}`)); },
        error(message: string): void { console.error(chalk.red(`ERROR: ${message}`)); },
        verbose(message: string): void { console.log(`VERBOSE: ${message}`); },
        async readFile(filename: string): Promise<string> {
            const data = await fs.promises.readFile(filename);
            return data.toString();
        },
        async writeFile(filename: string, content: string): Promise<void> {
            ctx.verbose(`Emitting file ${filename}`);
            filename = path.join(dir, filename);
            await fs.promises.mkdir(path.dirname(filename), { recursive: true });
            await fs.promises.writeFile(filename, content, 'utf8');
        }
    }
    return ctx;
}
开发者ID:katherinebecker,项目名称:azure-sdk-for-net,代码行数:31,代码来源:interface.ts

示例4: readOggFile

export async function readOggFile(file: string): Promise<ByteStream> {
    const buffer = await fs.readFile(file);
    const arrayBuffer = buffer.buffer;
    const byteStream = new ByteStream();

    byteStream.setData(arrayBuffer);

    return byteStream;
}
开发者ID:lo1tuma,项目名称:theorajs,代码行数:9,代码来源:files.ts

示例5: main

async function main() {
  const root = new Root();
  const { resolvePath } = root;
  const numDirectoriesToStrip = 2;
  let initialOrigin: string|null;
  root.resolvePath = (origin, target) => {
    if (!origin) {
      initialOrigin = target;
      for (let i = 0; i <= numDirectoriesToStrip; i++) {
        initialOrigin = path.dirname(initialOrigin);
      }
      return resolvePath(origin, target);
    }
    return path.resolve(initialOrigin!, target);
  };
  const traceProto = await root.load(process.argv[2]);
  const Trace = traceProto.lookupType("Trace");
  const payload = await fs.promises.readFile(process.argv[3]);
  const msg = Trace.decode(payload).toJSON();
  const output = {
    traceEvents: msg.packet
      .filter((packet: any) => !!packet.chromeEvents)
      .map((packet: any) => packet.chromeEvents.traceEvents)
      .map((traceEvents: any) => traceEvents.map((e: any) => {

        const bind_id = (e.flags & (TRACE_EVENT_FLAG_FLOW_IN |
          TRACE_EVENT_FLAG_FLOW_OUT)) ? e.bindId : undefined;
        const scope = (e.flags & TRACE_EVENT_FLAG_HAS_ID) && e.scope ?
            e.scope : undefined;

        return {
          pid: e.processId,
          tid: e.threadId,
          ts: parseIntOrThrow(e.timestamp),
          tts: parseIntOrThrow(e.threadTimestamp),
          ph: String.fromCodePoint(e.phase),
          cat: e.categoryGroupName,
          name: e.name,
          dur: parseIntOrThrow(e.duration),
          tdur: parseIntOrThrow(e.threadDuration),
          bind_id: bind_id,
          flow_in: e.flags & TRACE_EVENT_FLAG_FLOW_IN ? true : undefined,
          flow_out: e.flags & TRACE_EVENT_FLAG_FLOW_OUT ? true : undefined,
          scope: scope,
          id: (e.flags & TRACE_EVENT_FLAG_HAS_ID) ?
              uint64AsHexString(e.id) : undefined,
          args: (e.args || []).reduce((js_args: any, proto_arg: any) => {
            js_args[proto_arg.name] = parseArgValue(proto_arg);
            return js_args;
          }, {})
        };
      }))
      .reduce(flatten, [])
  };
  await fs.promises.writeFile(process.argv[4], JSON.stringify(output, null, 2));
}
开发者ID:v8,项目名称:v8,代码行数:56,代码来源:proto-to-json.ts

示例6: getAll

export async function getAll() {
  const hash = await getHash();
  const zipPath = path.resolve(picturePath, `${hash}.zip`);

  try {
    return fs.readFile(zipPath);
  } catch (e) {
    return null;
  }
}
开发者ID:marudor,项目名称:randomCats,代码行数:10,代码来源:pictures.ts

示例7: it

        it('should natively handle cpg1200 re-coding', async () => {
            const input = await fs.promises.readFile('test/examples/cpg1200.rtf');
            const result = await process(input, {
                outputMode: 'buffer-default-cpg'
            });

            expect(result.warnings).to.be.an('array').of.length(1);
            expect(result.decodings).to.be.an('array').of.length(2);

            expect(result.asBuffer.toString('utf16le')).to.eql(`Plain text body: ! < > " ' € œ ¤ ´ ¼ ½ 𠜎 𩶘 😀\r\n`);
        });
开发者ID:mazira,项目名称:rtf-parser,代码行数:11,代码来源:de-encapsulate.test.ts

示例8: getMostRecentChangelogEntry

async function getMostRecentChangelogEntry(): Promise<string> {
    return fs
        .readFile(path.join(__dirname, 'CHANGELOG.md'), { encoding: 'utf-8' })
        .then(contents => {
            const entry = /(^## .*?)\n^## /ms.exec(contents);
            if (!entry || !entry[1]) {
                throw new Error('Error parsing last changelog entry');
            }
            return entry[1];
        })
        .then(entry =>
            entry.replace(/^## (\S+)/, '= $1 =').replace(/^-/gm, '*'),
        );
}
开发者ID:dsifford,项目名称:academic-bloggers-toolkit,代码行数:14,代码来源:webpack.config.ts

示例9: readPng

export async function readPng(file: string): Promise<PNG> {
    const data = await fs.readFile(path.resolve(process.cwd(), file));
    return PNG.sync.read(data);
}
开发者ID:lo1tuma,项目名称:theorajs,代码行数:4,代码来源:files.ts

示例10:

 return p.then(() => fs.readFile(zipPath));
开发者ID:marudor,项目名称:randomCats,代码行数:1,代码来源:pictures.ts


注:本文中的fs.promises.readFile方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。