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


TypeScript promises.access方法代码示例

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


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

示例1: topic

async function topic(options: Options) {
  if (!options.title) {
    console.error('No title given!')
    process.exit(1)
    return
  }

  const topics = path.resolve(__dirname, `../src/topics`)
  const slug = options.slug || makeSlug(options.title)

  const dir = path.join(topics, slug)

  try {
    await fs.promises.access(dir, fs.constants.F_OK)
    console.error(`The topic '${options.title}' already exists`)
    return
  } catch (error) {
    const mdx = `---\ntitle: ${options.title}\ndescription: This is a starter description\n${
      options.topic ? `\nparent: ${options.topic}\n` : ''
    }\ncover:\n  image: full.jpg\n  caption: Starter caption.\n---\n\nSome starter content`

    await fs.promises.mkdir(dir, { recursive: true })

    const file = `${dir}/index.mdx`
    await fs.promises.writeFile(file, mdx)

    console.log(`Created a new topic: ${file.replace(path.resolve(__dirname, '..'), '')}`)
  }
}
开发者ID:jeremyboles,项目名称:jeremyboles.com,代码行数:29,代码来源:make.ts

示例2: initZip

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

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

示例3: entry

async function entry(options: Options) {
  if (!options.title) {
    console.error('No title given!')
    process.exit(1)
    return
  }

  if (!options.topic) {
    console.error('No topic given!')
    process.exit(1)
    return
  }

  if (!(await topicExists(options.topic))) {
    console.error(`The topic "${options.topic}" does not exists`)
    process.exit(1)
    return
  }

  const date = new Date()

  const dir = path.resolve(
    __dirname,
    `../src/entries/${options.topic}/${dateFns.format(date, 'yyyy-MM-dd')}-${makeSlug(options.title, '-')}`
  )
  try {
    await fs.promises.access(dir, fs.constants.F_OK)
    console.error(`A entry already named '${options.title}' already exists for today`)
    return
  } catch (error) {
    const publishedAt = date.toISOString()
    const text = '_Content goes here_'
    const mdx = `---\ntitle: ${
      options.title
    }\npublishedAt: ${publishedAt}\n\ncover:\n  image: full.jpg\n  caption: Starter caption.\n---\n\n${text}\n`

    await fs.promises.mkdir(dir, { recursive: true })

    const file = `${dir}/index.mdx`
    await fs.promises.writeFile(file, mdx)

    console.log(`Created a new entry: ${file.replace(path.resolve(__dirname, '..'), '')}`)
  }
}
开发者ID:jeremyboles,项目名称:jeremyboles.com,代码行数:44,代码来源:make.ts

示例4: Random

import { resize } from 'easyimage';
import archiver from 'archiver';
import config from './config';
import crypto from 'crypto';
import path from 'path';

type Picture = {
  fileName: string;
  file: Buffer;
  type: string;
};

const random = new Random(nodeCrypto);
const picturePath = config.picturePath;

fs.access(picturePath).catch((e: Error) => {
  throw new Error(`${picturePath} has to exist! ${e.toString()}`);
});

async function getAvailablePictures() {
  const rawavailablePictures = await fs.readdir(path.resolve(picturePath));
  const availablePictures = rawavailablePictures.filter(
    fileName => fileName.includes('.') && !fileName.endsWith('.zip')
  );

  return availablePictures;
}

export async function getPictureFileName() {
  const availablePictures = await getAvailablePictures();
  const index = random.integer(0, availablePictures.length - 1);
开发者ID:marudor,项目名称:randomCats,代码行数:31,代码来源:pictures.ts


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