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


TypeScript promises.mkdir方法代码示例

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


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

async function note(options: Options) {
  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 publishedAt = date.toISOString()
  const text = '_Content goes here_'
  const mdx = `---\npublishedAt: ${publishedAt}\n---\n\n${text}\n`

  const dir = await uniqueNote(options.topic, date)
  await fs.promises.mkdir(dir, { recursive: true })

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

  console.log(`New note created at: ${file.replace(path.resolve(__dirname, '..'), '')}`)
}
开发者ID:jeremyboles,项目名称:jeremyboles.com,代码行数:27,代码来源:make.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: savePngDiff

export async function savePngDiff(file: string, png: PNG): Promise<string> {
    const data = PNG.sync.write(png);
    const outputDir = path.resolve(process.cwd(), './build/test-output/integration');
    const diffPath = path.resolve(outputDir, file);

    await fs.mkdir(outputDir, { recursive: true });
    await fs.writeFile(diffPath, data);

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

示例5: twitter

async function twitter(topic: string, pathname: string) {
  const [_null, username, _type, id] = pathname.split('/')

  const { data } = await axios.get('https://api.twitter.com/1.1/statuses/show.json', {
    headers: { Authorization: `Bearer ${process.env.TWITTER_BEARER_TOKEN}` },
    params: { id },
  })

  const date = dateFns.parse(data.created_at, 'eee MMM dd HH:mm:ss xxxx yyyy', new Date())

  const dir = await uniqueNote(topic, date)
  await fs.promises.mkdir(dir, { recursive: true })

  async function reducer(acc: Promise<string[]>, { media_url_https, type }: { media_url_https: string; type: string }) {
    if (type !== 'photo') return acc

    try {
      const basename = path.basename(media_url_https)
      const image = `${dir}/${basename}`
      const writer = fs.createWriteStream(image)
      const response = await axios.get(media_url_https, { responseType: 'stream' })

      await new Promise((resolve, reject) => {
        response.data.pipe(writer)
        writer.on('error', reject)
        writer.on('finish', resolve)
      })
      return [...(await acc), basename]
    } catch (e) {
      console.error(e)
      return acc
    }
  }

  if (data.entities && data.entities.media) {
    const images = await data.entities.media.reduce(reducer, Promise.resolve([]))
    console.log('Downloaded images attached to this tweet:', images)
  }

  const publishedAt = date.toISOString()
  const text = dePants(data.text)
  const url = `https://twitter.com${pathname}`
  const mdx = `---\npublishedAt: ${publishedAt}\n\ntwitter:\n  url: ${url}\n---\n\n${text}\n`

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

  console.log(`Imported tweet to: ${file.replace(path.resolve(__dirname, '..'), '')}`)
}
开发者ID:jeremyboles,项目名称:jeremyboles.com,代码行数:49,代码来源:import.ts

示例6: 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

示例7: instagram

async function instagram(topic: string, pathname: string) {
  const [_null, _p, id] = pathname.split('/')

  const { data } = await axios.get(`https://www.instagram.com${pathname}`)

  const $ = cheerio.load(data)
  const json = $('script[type="application/ld+json"]').html()

  if (!json) {
    console.error(`Photo data was not found for: ${path}`)
    process.exit(1)
    return
  }

  const schema = JSON.parse(json)

  const date = dateFns.parseISO(schema.uploadDate)

  const alt = `A photo taken in ${schema.contentLocation.name}`
  const publishedAt = date.toISOString()
  const text = dePants(schema.caption)
  const url = `https://www.instagram.com${pathname}`
  const mdx = `---\npublishedAt: ${publishedAt}\n\ninstagram:\n  url: ${url}\n---\n\n<!-- prettier-ignore-start -->\n! ![${alt}](${id}.jpg)\n! ${text}\n\n<!-- prettier-ignore-end -->\n`

  const dir = await uniqueNote(topic, date)
  await fs.promises.mkdir(dir, { recursive: true })

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

  const image = `${dir}/${id}.jpg`
  const writer = fs.createWriteStream(image)
  const response = await axios.get(`https://www.instagram.com${pathname}media?size=l`, { responseType: 'stream' })

  response.data.pipe(writer)

  writer.on('error', console.error)
  writer.on('finish', () => {
    console.log(`Imported Instagram post to: ${file.replace(path.resolve(__dirname, '..'), '')}`)
  })
}
开发者ID:jeremyboles,项目名称:jeremyboles.com,代码行数:41,代码来源:import.ts


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