import * as path from 'path' ; import * as semver from 'semver' ; import { get } from 'lodash' ; import { fileExists } from './filesys' ; param { string } packageJsonPath returns export function createDependencyMap ( packageFile ) { let deps = get ( packageFile , `dependencies` , { } ) ; let devDeps = get ( packageFile , `devDependencies` , { } ) ; let dependencyMap = { deps : toDependencies ( deps ) , devDeps : toDependencies ( devDeps ) } ; return dependencyMap ; } ; param { { } } object returns export function toDependencies ( object ) { return Object . keys ( object ) . map ( packageName => ( { name : packageName , version : object [ packageName ] } ) ) ; } ; param { string } version returns export function parseVersion ( version ) { if ( /^[~^]/ . test ( version ) ) { return version . substring ( 1 ) ; } return extractVersionFromUrl ( version ) ; } ; param { string } version returns export function extractVersionFromUrl ( version ) { const regex = /(?:git|ssh|https?|git@[-\w.]+):(\/\/)?(.*?)(\.git)(\/?|\#[-\d\w._]+?)$/ ; if ( regex . test ( version ) ) { var i = version . indexOf ( '.git#v' ) ; if ( i > 0 ) { return version . substring ( i + 6 ) ; } else { return 'Unknown' ; } } return version ; } param { IDependency } dep returns export function dependencyToPackageDescriptor ( dep ) { let packageDescriptor = { name : dep . name , definedVersion : extractVersionFromUrl ( dep . version ) , parsedDefinedVersion : parseVersion ( dep . version ) , installedVersion : null , installed : false , locked : semver . clean ( extractVersionFromUrl ( dep . version ) ) !== null } ; return packageDescriptor ; } ; param { IDependencyMap } depMap returns export function dependencyMapToPackageDescriptorMap ( depMap ) { let { deps , devDeps } = depMap ; let map = { deps : deps . map ( dependencyToPackageDescriptor ) , devDeps : devDeps . map ( dependencyToPackageDescriptor ) } ; return map ; } ; param { IPackageDescriptor } descriptor param { string } modulesPath returns export function packageDescriptorCheckInstall ( descriptor , modulesPath ) { const packagePath = path . resolve ( modulesPath , descriptor . name , 'package.json' ) ; const installed = fileExists ( packagePath ) ; if ( ! installed ) { return descriptor ; } const pkg = require ( packagePath ) ; descriptor . installed = true ; descriptor . installedVersion = pkg . version ; return descriptor ; } ; param { IPackageDescriptorMap } map param { string } modulesPath returns export function packageDescriptorMapCheckInstall ( map , modulesPath ) { let { deps , devDeps } = map ; let newMap = { deps : deps . map ( m => packageDescriptorCheckInstall ( m , modulesPath ) ) , devDeps : devDeps . map ( m => packageDescriptorCheckInstall ( m , modulesPath ) ) , } ; return newMap ; } ; param { string } version param { string } expected returns export function isMatchingVersion ( version , expected ) { return version === expected ; } ;