How to use the @poppinss/utils.esmRequire function in @poppinss/utils

To help you get started, we’ve selected a few @poppinss/utils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github adonisjs / ace / src / Manifest / index.ts View on Github external
public lookupCommands (
    commandPath: string,
  ): { command: CommandConstructorContract, commandPath: string }[] {
    /**
     * Absolute paths are not allowed when looking up commands to be saved
     * inside the manifest file. This is required, since one can accidentally
     * generate absolute paths on their local machine and then trying to
     * execute commands on a server
     */
    if (isAbsolute(commandPath)) {
      throw new Error('Absolute path to commands are not allowed, since manifest file needs to be portable')
    }

    const absPath = resolveFrom(this.basePath, commandPath)
    const commandOrCommandPaths = esmRequire(absPath)

    /**
     * The command exports an array of subpaths. Only one level
     * of subpaths are allowed.
     */
    if (Array.isArray(commandOrCommandPaths)) {
      return commandOrCommandPaths.map((commandOrCommandPath) => {
        return this.loadCommand(commandOrCommandPath)
      })
    }

    return [this.loadCommand(commandPath)]
  }
github adonisjs / ace / src / Manifest / index.ts View on Github external
public loadCommand (
    commandPath: string,
  ): { command: CommandConstructorContract, commandPath: string } {
    const absPath = resolveFrom(this.basePath, commandPath)
    const command = esmRequire(absPath)

    if (!command.name) {
      throw CommandValidationException.invalidManifestExport(commandPath)
    }

    command.$boot()
    return {
      command,
      commandPath,
    }
  }
github adonisjs / adonis-framework / src / Ignitor / Ace / CoreCommands.ts View on Github external
private setupApplication () {
    let rcContents = {}

    try {
      rcContents = esmRequire(join(this.appRoot, '.adonisrc.json'))
    } catch (error) {
      if (isMissingModuleError(error)) {
        throw new AceRuntimeException('Make sure the project root has ".adonisrc.json"')
      }
      throw error
    }

    this.application = new Application(this.appRoot, new Ioc(), rcContents, {})
  }
github adonisjs / adonis-framework / src / Ignitor / Bootstrapper / index.ts View on Github external
const adonisCorePkgFile = findPkg(join(__dirname, '..', '..')).next().value
    const appPkgFile = findPkg(this.appRoot).next().value

    const pkgFile = {
      name: appPkgFile ? appPkgFile.name : 'adonis',
      version: appPkgFile ? appPkgFile.version : '0.0.0',
      adonisVersion: adonisCorePkgFile!.version,
    }

    /**
     * Loading `.adonisrc.json` file with custom error handling when
     * the file is missing
     */
    let rcContents = {}
    try {
      rcContents = esmRequire(join(this.appRoot, '.adonisrc.json'))
    } catch (error) {
      if (isMissingModuleError(error)) {
        throw new Error('Make sure the project root has ".adonisrc.json"')
      }
      throw error
    }

    /**
     * Setting up the application and binding it to the container as well. This makes
     * it's way to the container even before the providers starts registering
     * themselves.
     */
    this.application = new Application(this.appRoot, ioc, rcContents, pkgFile)
    this.registrar = new Registrar(ioc, this.appRoot)

    ioc.singleton('Adonis/Core/Application', () => this.application)
github adonisjs / adonis-framework / src / utils / index.ts View on Github external
export function optionalRequire (filePath: string, optional = false): any | null {
  try {
    return esmRequire(filePath)
  } catch (error) {
    if (isMissingModuleError(error) && optional) {
      return null
    }

    throw error
  }
}
github adonisjs / adonis-lucid / src / Migrator / MigrationSource.ts View on Github external
}).map((file) => {
          return {
            absPath: join(path, file),
            name: join(directoryPath, file.replace(RegExp(`${extname(file)}$`), '')),
            source: esmRequire(join(path, file)),
          }
        }))
      })
github adonisjs / fold / src / Registrar / index.ts View on Github external
private loadProvider (providerPath: string) {
    providerPath = this.basePath ? resolveFrom(this.basePath, providerPath) : providerPath
    const provider = esmRequire(providerPath)

    if (typeof (provider) !== 'function') {
      throw new Error(`Make sure export default the provider from ${providerPath}`)
    }

    return new provider(this.ioc)
  }

@poppinss/utils

Handy utilities for repetitive work

MIT
Latest version published 2 months ago

Package Health Score

81 / 100
Full package analysis