import * as fs from 'fs' ; import * as path from 'path' ; import * as paths from './paths' ; namespace Is { export function string ( value ) { return typeof value === 'string' || value instanceof String ; } export function number ( value ) { return typeof value === 'number' || value instanceof Number ; } } class PackageJson { static read ( filename ) { try { if ( fs . existsSync ( filename ) ) { let content = new PackageJson ( filename , JSON . parse ( fs . readFileSync ( filename , { encoding : 'utf8' } ) ) ) ; if ( Is . string ( content . name ) ) { return content ; } } } catch { } return undefined ; } public $fileName : string ; public $location : string ; public name : string ; public main : string ; public typings : string ; public version ? : string ; public repository ? : { type : string ; url : string ; } ; private constructor ( fileName , json ) { this . $fileName = paths . normalizeSeparator ( fileName ) ; this . $location = path . posix . dirname ( this . $fileName ) ; this . name = json . name ; this . version = json . version ; this . repository = json . repository ; if ( Is . string ( json . main ) ) { this . main = path . posix . normalize ( paths . normalizeSeparator ( paths . removeExtension ( json . main ) ) ) ; } else { this . main = 'index' ; } if ( Is . string ( json . typings ) ) { this . typings = path . posix . normalize ( paths . normalizeSeparator ( paths . removeExtension ( json . typings ) ) ) ; } else { this . typings = 'index' ; } } public hasVersion ( ) { return Is . string ( this . version ) ; } public hasRepository ( ) { return this . repository !== undefined && Is . string ( this . repository . url ) && Is . string ( this . repository . url ) ; } } export default PackageJson ;