import is from "is" ; import semver from "semver" ; import Immutable from "immutable" ; import PREFIX from "../constants/pluginPrefix" ; const DEFAULT_VERSION = "*" ; class PluginDependency extends Immutable . Record ( { name : String ( ) , version : String ( DEFAULT_VERSION ) , path : String ( ) , enabled : Boolean ( true ) , } , "PluginDependency" ) { getName ( ) { return this . get ( "name" ) ; } getVersion ( ) { return this . get ( "version" ) ; } getPath ( ) { return this . get ( "path" ) ; } isEnabled ( ) { return this . get ( "enabled" ) ; } return { PluginDependency } toggle ( state ? ) { if ( state === undefined ) { state = ! this . isEnabled ( ) ; } return this . set ( "enabled" , state ) ; } return { string } getNpmID ( ) { return PluginDependency . nameToNpmID ( this . getName ( ) ) ; } return { boolean } isGitDependency ( ) { return ! semver . validRange ( this . getVersion ( ) ) ; } param { string } return { Plugin | undefined } static create ( name , version , enabled ) { if ( is . undefined ( enabled ) ) { enabled = true ; } return new PluginDependency ( { name : name , version : version || DEFAULT_VERSION , enabled : Boolean ( enabled ) , } ) ; } param { string } s return { Plugin | undefined } static createFromString ( s ) { const packagePattern = /^(?<disabled>-)?(?<name>.+)$/ ; const scopedPackagePattern = /^(?<disabled>-)?(?<name>@[^/]+\/.+)$/ ; if ( packagePattern . test ( s ) && ! s . includes ( "@" ) ) { const match = s . match ( packagePattern ) ; const enabled = ! match . groups . disabled ; return new PluginDependency ( { name : match . groups . name , version : DEFAULT_VERSION , enabled : enabled , } ) ; } else if ( scopedPackagePattern . test ( s ) ) { const match = s . match ( scopedPackagePattern ) ; const enabled = ! match . groups . disabled ; return new PluginDependency ( { name : match . groups . name , version : DEFAULT_VERSION , enabled : enabled , } ) ; } else { const parts = s . split ( "@" ) ; let name = parts [ 0 ] ; const version = parts . slice ( 1 ) . join ( "@" ) ; let enabled = true ; if ( name [ 0 ] === "-" ) { enabled = false ; name = name . slice ( 1 ) ; } return new PluginDependency ( { name : name , version : version || DEFAULT_VERSION , enabled : enabled , } ) ; } } param { string } return { List < PluginDependency > } static listFromString ( s ) { const parts = s . split ( "," ) ; return PluginDependency . listFromArray ( parts ) ; } /**
     * Create a PluginDependency from an array
     */ static listFromArray ( arr ) { return Immutable . List ( arr ) . map ( ( entry ) => { if ( typeof entry === "string" ) { return PluginDependency . createFromString ( entry ) ; } else { return new PluginDependency ( { name : entry . get ( "name" ) , version : entry . get ( "version" ) , } ) ; } } ) . filter ( ( dep ) => { return Boolean ( dep . getName ( ) ) ; } ) ; } param { List < PluginDependency > } list return { Array < String > } static listToArray ( list ) { return list . map ( ( dep ) => { let result = "" ; if ( ! dep . isEnabled ( ) ) { result += "-" ; } result += dep . getName ( ) ; if ( dep . getVersion ( ) !== DEFAULT_VERSION ) { result += `@${ dep . getVersion ( ) }` ; } return result ; } ) . toJS ( ) ; } param { string } return { string } static nameToNpmID ( s ) { return PREFIX + s ; } } export default PluginDependency ;