All files / src loader.ts

100% Statements 27/27
100% Branches 8/8
100% Functions 4/4
100% Lines 27/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 821x 1x 1x   1x           4x 1x                                             1x 1x 1x 3x             1x 10x 10x   10x 10x 10x 10x 10x     10x 5x     5x 1x               4x 4x       4x 1x       4x      
import * as fs from 'fs'
import * as mage from 'mage'
import * as path from 'path'
 
import { crash } from './errors'
 
/**
 * Throw only if the error is not "file/folder not found"
 */
function throwIfNotFileNotFoundError(error: NodeJS.ErrnoException) {
  if (error.code !== 'ENOENT') {
    throw error
  }
}
 
/**
 * Load topics from each module's 'topics' folder
 *
 * This function is a helper you will use in your project's
 * `lib/archivist/index.js` file, as follow:
 *
 * ```typescript
 * import { loadTopicsFromModules } from 'mage-validator'
 *
 * loadTopicsFromModules(exports)
 * ```
 *
 * This will:
 *
 *   - Find all your projetc's modules
 *   - For each module folders, check if there is a `topics` folder
 *   - When a `topics` folder is found, require each file in it
 *   - Add the content of the require to exports[fileNameWithoutJSExtension]
 */
export function loadTopicsFromModules(exports: any) {
  const modules = mage.listModules()
  for (const moduleName of modules) {
    loadTopicsFromModule(exports, moduleName)
  }
}
 
/**
 * Load topics defined in a single module
 */
export function loadTopicsFromModule(archivistExports: any, moduleName: string) {
  const modulePath = mage.getModulePath(moduleName)
  const moduleTopicsPath = path.join(modulePath, 'topics')
 
  try {
    fs.readdirSync(moduleTopicsPath).forEach(function (topicFileName) {
      const topicPath = path.join(moduleTopicsPath, topicFileName)
      const topicPathInfo = path.parse(topicPath)
      const topicName = topicPathInfo.name
 
      // Skip all files but TypeScript source files
      if (topicPathInfo.ext !== '.ts') {
        return
      }
 
      if (archivistExports[topicName]) {
        throw crash('Topic is already defined!', {
          alreadySetByModule: archivistExports[topicName]._module,
          module: moduleName,
          topic: topicName,
        })
      }
 
      // Add topic to the export of lib/archivist/index.ts
      const topic = archivistExports[topicName] = require(topicPath).default
      topic._module = moduleName
 
      // No explicit class name defined; we assign the name of the file as
      // the class name used internally within the topic's methods
      if (topic.getClassName().substring(0, 8) === 'default_') {
        topic.setClassName(topicName)
      }
    })
  } catch (error) {
    throwIfNotFileNotFoundError(error)
  }
}