antongolub/nestjs-esm-fix

View on GitHub
src/main/js/index.js

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
import { globby } from 'globby'
import { extname } from 'node:path'
import { patchFile } from './patch.js'

export const defaults = {
  openapiComplexTypes: true,
  openapiMeta: true,
  openapiVar: true,
  dirnameVar: true,
  importify: true,
  requireMain: true,
  redocTpl: true,
}

export const fix = async ({ target, cwd = process.cwd(), ..._opts }) => {
  if (!target) {
    throw new Error('target is required')
  }

  const opts = { ...defaults, ..._opts }
  const pattern = target.includes('*')
    ? target
    : extname(target)
    ? target
    : `${target}/**/*`
  const files = await globby(pattern, { onlyFiles: true, absolute: true, cwd })

  await Promise.all(files.map((file) => patchFile(file, opts)))
}