How to use @poppinss/utils - 10 common examples

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 / utils / listDirectoryFiles.ts View on Github external
export function listDirectoryFiles (
  scanDirectory: string,
  appRoot: string,
  filterFn?: CommandsListFilterFn,
): string[] {
  return fsReadAll(scanDirectory)
    .filter((name) => !name.endsWith('.json')) // remove .json files
    .map((name) => {
      const relativePath = relative(appRoot, join(scanDirectory, name))
      return slash(relativePath.startsWith('../') ? relativePath : `./${relativePath}`)
    })
    .filter((name) => {
      if (typeof (filterFn) === 'function') {
        return filterFn(name)
      }

      return Array.isArray(filterFn) ? !filterFn.includes(name) : true
    })
}
github adonisjs / adonis-lucid / src / Orm / Preloader / index.ts View on Github external
public parseRelationName (relationName: string) {
    const relations = relationName.split('.')
    const primary = relations.shift()!
    const relation = this._model.$getRelation(primary)

    /**
     * Undefined relationship
     */
    if (!relation) {
      throw new Exception(
        `${primary} is not defined as a relationship on ${this._model.name} model`,
        500,
        'E_UNDEFINED_RELATIONSHIP',
      )
    }

    return {
      primary,
      relation,
      children: relations.length ? { relationName: relations.join('') } : null,
    }
  }
github adonisjs / adonis-framework / src / Ignitor / Ace / CoreCommands.ts View on Github external
public async handle (argv: string[]) {
    this.setupApplication()
    await this.importAssembler(argv[0])

    const manifest = new this.ace.Manifest(dirname(resolveFrom(this.appRoot, '@adonisjs/assembler')))
    const kernel = new this.ace.Kernel(this.application)

    /**
     * Showing commands help
     */
    kernel.flag('help', async (value, _, command) => {
      if (!value) {
        return
      }

      kernel.printHelp(command)
      process.exit(0)
    }, { alias: 'h' })

    kernel.useManifest(manifest)
    await kernel.handle(argv)
github adonisjs / adonis-lucid / src / Database / QueryBuilder / Database.ts View on Github external
private _ensureCanPerformWrites () {
    if (this.client && this.client.mode === 'read') {
      throw new Exception('Updates and deletes cannot be performed in read mode')
    }
  }
github adonisjs / adonis-lucid / src / Orm / BaseModel / index.ts View on Github external
const Model = this.constructor as typeof BaseModel
    const relation = Model.$relations.get(key as string)

    /**
     * Ignore when relation is not defined
     */
    if (!relation) {
      return
    }

    /**
     * Reset array before invoking $pushRelated
     */
    if (MANY_RELATIONS.includes(relation.type)) {
      if (!Array.isArray(models)) {
        throw new Exception(
          `${Model.name}.${key} must be an array when setting ${relation.type} relationship`,
        )
      }
      this.$preloaded[key] = []
    }

    return this.$pushRelated(key, models)
  }
github adonisjs / adonis-lucid / src / Orm / BaseModel / index.ts View on Github external
private _ensureIsntDeleted () {
    if (this.$isDeleted) {
      throw new Exception('Cannot mutate delete model instance', 500, 'E_MODEL_DELETED')
    }
  }
github edge-js / edge / src / Loader / index.ts View on Github external
public makePath (templatePath: string): string {
    if (this._preRegistered.has(templatePath)) {
      return templatePath
    }

    const [ diskName, template ] = extractDiskAndTemplateName(templatePath)

    /**
     * Raise exception when disk name is not defined
     */
    const mountedDir = this._mountedDirs.get(diskName)
    if (!mountedDir) {
      throw new Exception(`{${diskName}} namespace is not mounted`, 500, 'E_UNMOUNTED_DISK_NAME')
    }

    return join(mountedDir, template)
  }
github adonisjs / adonis-lucid / src / Orm / Relations / HasManyThrough / index.ts View on Github external
private _validateKeys () {
    const relationRef = `${this.model.name}.${this.relationName}`

    if (!this.model.$hasColumn(this.localKey)) {
      const ref = `${this.model.name}.${this.localKey}`
      throw new Exception(
        `${ref} required by ${relationRef} relation is missing`,
        500,
        'E_MISSING_RELATED_LOCAL_KEY',
      )
    }

    if (!this.throughModel().$hasColumn(this.foreignKey)) {
      const ref = `${this.throughModel().name}.${this.foreignKey}`
      throw new Exception(
        `${ref} required by ${relationRef} relation is missing`,
        500,
        'E_MISSING_RELATED_FOREIGN_KEY',
      )
    }

    if (!this.throughModel().$hasColumn(this.throughLocalKey)) {
      const ref = `${this.throughModel().name}.${this.throughLocalKey}`
      throw new Exception(
        `${ref} required by ${relationRef} relation is missing`,
        500,
        'E_MISSING_THROUGH_LOCAL_KEY',
      )
    }

    if (!this.relatedModel().$hasColumn(this.throughForeignKey)) {
github adonisjs / adonis-lucid / src / Orm / Relations / ManyToMany / index.ts View on Github external
private _ensureRelatedModel () {
    if (!this._options.relatedModel) {
      throw new Exception(
        'Related model reference is required to construct the relationship',
        500,
        'E_MISSING_RELATED_MODEL',
      )
    }
  }
github adonisjs / adonis-lucid / src / utils / index.ts View on Github external
export function ensureRelation (
  name: string,
  relation?: T,
): relation is T {
  if (!relation) {
    throw new Exception(`Cannot process unregistered relationship ${name}`, 500)
  }

  return true
}

@poppinss/utils

Handy utilities for repetitive work

MIT
Latest version published 2 months ago

Package Health Score

81 / 100
Full package analysis