import * as fs from 'fs' ; import * as path from 'path' ; import RawConfig from '../types/rawConfig/RawConfig' ; import Config from '../types/config/Config' ; import normalizePath from './normalizePath' ; import RawDependencyRule from '../types/rawConfig/RawDependencyRule' ; import RawExportRule from '../types/rawConfig/RawExportRule' ; import ConfigSet from '../types/ConfigSet' ; import ExportRule from '../types/config/ExportRule' ; import validateRawConfig from '../validation/validateRawConfig' ; import NormalizedPath from '../types/NormalizedPath' ; import DependencyRule from '../types/config/DependencyRule' ; export function loadConfigFromString ( configPath , fileContent ) { let rawConfig = JSON . parse ( fileContent ) ; if ( validateRawConfig ( rawConfig , configPath ) ) { return { path : configPath , tags : rawConfig . tags , exports : normalizeExportRules ( rawConfig . exports ) , dependencies : normalizeDependencyRules ( rawConfig . dependencies ) , imports : rawConfig . imports , } ; } return null ; } export default function loadConfig ( file , configSet ) { const configPath = normalizePath ( path . dirname ( file ) ) ; const config = loadConfigFromString ( configPath , fs . readFileSync ( file , 'utf-8' ) ) ; if ( config ) { configSet [ config . path ] = config ; } } function normalizeDependencyRules ( rules ) { if ( ! rules ) { return null ; } return rules . map ( dependency => { if ( typeof dependency == 'string' ) { return { dependency , accessibleTo : null , } ; } else { return dependency ; } } ) ; } export function normalizeExportRules ( rules ) { if ( ! rules ) { return null ; } return rules . map ( exportRule => { if ( typeof exportRule == 'string' ) { return { modules : exportRule , accessibleTo : null , } ; } else { return exportRule ; } } ) ; }