rsnext/examples/cms-agilitycms/lib/dependancies.ts
Tim Neutkens ed81a14922
Enable @typescript-eslint/no-use-before-define for examples dir (#39469)
Found that this rule was added but all options are set to `false` so it doesn't do anything. Started with enabling it for examples to ensure minimal breaking of existing PRs.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
2022-08-10 16:30:36 +00:00

51 lines
1.4 KiB
TypeScript

import { convertPascalToKebabCase } from './utils'
const path = require('path')
const userComponentsPath = path.resolve('./components')
const libComponentsPath = path.resolve('./lib/components')
const requireComponent = (name) => {
let Component = null
try {
//check the user path first (must be relative paths)
Component = require(`../components/${name}.tsx`).default
} catch {}
if (!Component)
try {
//fallback to lib path (must be relative paths)
Component = require(`./components/${name}.tsx`).default
} catch {}
return Component
}
//Bug: when dynamic imports are used within the module, it doest not get outputted server-side
//let AgilityModule = dynamic(() => import ('../components/' + m.moduleName));
export const requireComponentDependancyByName = (name) => {
let pascalCaseName = name
let kebabCaseName = convertPascalToKebabCase(name)
let Component = null
try {
Component = requireComponent(kebabCaseName)
} catch {}
if (!Component) {
try {
Component = requireComponent(pascalCaseName)
} catch {}
}
if (!Component) {
// eslint-disable-next-line no-throw-literal
throw `Could not find a component with the name ${name}. Tried searching:
${userComponentsPath}/${kebabCaseName}.tsx',
${libComponentsPath}/${kebabCaseName}.tsx',
${userComponentsPath}/${pascalCaseName}.tsx',
${libComponentsPath}/${pascalCaseName}.tsx'.`
}
return Component
}