/**
 * Copyright (c) 2016-present, Facebook, Inc.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */ import * as AlgebraicType from './algebraic-type' ; import * as Either from './either' ; import * as Error from './error' ; import * as File from './file' ; import * as Maybe from './maybe' ; import * as requireJsPlugin from './js/require-plugin/require-plugin' ; import * as ObjectSpec from './object-spec' ; function requiredModuleForPath ( absolutePath ) { try { return requireJsPlugin . requirePlugin ( File . getAbsolutePathString ( absolutePath ) ) ; } catch ( e ) { return null ; } } function requirePlugin < T > ( absolutePath , loadPlugin ) { const requiredModule = requiredModuleForPath ( absolutePath ) ; return Maybe . match ( function just ( value ) : Either . Either < Error . Error [ ] , T | null > { return Either . Right < Error . Error [ ] , T | null > ( loadPlugin ( value ) ) ; } , function nothing ( ) : Either . Either < Error . Error [ ] , T | null > { return Either . Left < Error . Error [ ] , T | null > ( [ Error . Error ( 'Plugin registered at ' + File . getAbsolutePathString ( absolutePath ) + ' does not exist' ) , ] ) ; } , requiredModule ) ; } function valueObjectPluginFromModule ( module ) { return module . createPlugin !== undefined ? module . createPlugin ( ) : null ; } function algebraicTypePluginFromModule ( module ) { return module . createAlgebraicTypePlugin !== undefined ? module . createAlgebraicTypePlugin ( ) : null ; } export function requireObjectSpecPlugin ( absolutePath ) { return requirePlugin < ObjectSpec . Plugin > ( absolutePath , valueObjectPluginFromModule ) ; } export function requireAlgebraicTypePlugin ( absolutePath ) { return requirePlugin < AlgebraicType . Plugin > ( absolutePath , algebraicTypePluginFromModule ) ; }