Skip to content

New: Add loadDependencyFiles utility for scanning files across modules #95

@taylortom

Description

@taylortom

Summary

Multiple modules independently implement the same pattern: iterate app.dependencies, glob for files in each module's rootDir, and optionally parse the JSON results. This should be extracted into a shared utility function in core.

Current duplication

The following modules all implement this pattern separately:

Module Pattern File
ErrorsModule glob('errors/*.json', { cwd: d.rootDir }) adapt-authoring-errors/lib/ErrorsModule.js
LangModule glob('lang/*.json', { cwd: d.rootDir }) adapt-authoring-lang/lib/LangModule.js
JsonSchemaModule glob('schema/*.schema.json', { cwd: d.rootDir }) adapt-authoring-jsonschema/lib/JsonSchemaModule.js
ConfigModule direct read of conf/config.schema.json per dep adapt-authoring-config/lib/ConfigModule.js
Doc plugins various file reads per dep adapt-authoring-docs

Each follows the same shape:

await Promise.all(Object.values(this.app.dependencies).map(async d => {
  const files = await glob('some/pattern', { cwd: d.rootDir, absolute: true })
  await Promise.all(files.map(async f => {
    const contents = JSON.parse(await fs.readFile(f))
    // ...
  }))
}))

Proposed API

A standalone utility function exported from core:

import { loadDependencyFiles } from 'adapt-authoring-core'

// Uses App.instance.dependencies by default, returns file paths grouped by module
const files = await loadDependencyFiles('errors/*.json')

// With JSON parsing — returns parsed content grouped by module
const parsed = await loadDependencyFiles('errors/*.json', { parse: true })

// Override dependencies (for testing or static doc builds)
const parsed = await loadDependencyFiles('errors/*.json', {
  parse: true,
  dependencies: customDeps
})

Key design decisions:

  • Standalone function (not an instance method) for easy unit testing
  • Defaults to App.instance.dependencies when no dependencies option is provided
  • parse option (default false) handles the JSON read-and-parse step that every consumer currently does manually
  • dependencies option allows callers to pass custom dependency lists, which is needed for unit tests and for the docs module's static build (see adapt-security/adapt-authoring-docs)

Motivation

This utility is also a prerequisite for refactoring the docs module to build documentation without a running app instance. The static doc builder needs to perform the same file scanning that runtime modules do, and a shared utility avoids duplicating the logic a sixth time.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions