Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Package

This is the main class for managing npm packages on local file-system. You can do most of the stuff you do in npm cli using this class.

Events:

  • child_process: Runs whenever a child process start, passes child process object to the callback function.
export
class

Package

Hierarchy

  • EventEmitter
    • Package

Index

Constructors

constructor

  • new Package(dir: string): Package
  • Creates an instance of Package.

    Parameters

    • dir: string

    Returns Package

Properties

Private _json

_json: any = null

Private _path

_path: string

Static Private debug

debug: any = require('debug')('puckages')

Static defaultMaxListeners

defaultMaxListeners: number

Accessors

json

  • get json(): any
  • Returns the content of package.json in package directory. This field stays null when Package first initiated. If you want to use this before any other method that affects package.json (install, uninstall -with save option-, init), you need to run forceReloadJson().

    Returns any

path

  • get path(): string
  • Returns the package directory.

    Returns string

Methods

Private _pipeChildProcess

  • _pipeChildProcess(cp: ChildProcess): void
  • Pipes the configured streams to the childProcess and handles the persistence of the pipe operation.

    Parameters

    • cp: ChildProcess

    Returns void

addListener

  • addListener(event: string | symbol, listener: Function): this
  • Parameters

    • event: string | symbol
    • listener: Function

    Returns this

emit

  • emit(event: string | symbol, ...args: any[]): boolean
  • Parameters

    • event: string | symbol
    • Rest ...args: any[]

    Returns boolean

eventNames

  • eventNames(): (string | symbol)[]
  • Returns (string | symbol)[]

forceReloadJson

  • forceReloadJson(opts?: object): Promise<Package>
  • Force reload the package.json. Returns empty object when opts.returnEmptyOnErr is set to true.

    Parameters

    • Default value opts: object = { returnEmptyOnErr: false }
      • returnEmptyOnErr: boolean

    Returns Promise<Package>

getDependency

  • getDependency(dep: string): Promise<Package>
  • Get dependency as instance of Package.

    Parameters

    • dep: string

    Returns Promise<Package>

getMaxListeners

  • getMaxListeners(): number
  • Returns number

hasDependency

  • hasDependency(dep: string, dev?: boolean): Promise<boolean>
  • Check wether the package has the given dependency or not.

    Parameters

    • dep: string
    • Default value dev: boolean = false

      Check devDependencies instead of dependencies in package.json.

    Returns Promise<boolean>

init

install

  • Install new dependency.

    Parameters

    • Optional pkgs: string | string[]
    • Default value opts: InstallOptions = { save: false, dev: false, symlink: false }

    Returns Promise<Package>

listenerCount

  • listenerCount(type: string | symbol): number
  • Parameters

    • type: string | symbol

    Returns number

listeners

  • listeners(event: string | symbol): Function[]
  • Parameters

    • event: string | symbol

    Returns Function[]

on

  • on(event: string | symbol, listener: Function): this
  • Parameters

    • event: string | symbol
    • listener: Function

    Returns this

once

  • once(event: string | symbol, listener: Function): this
  • Parameters

    • event: string | symbol
    • listener: Function

    Returns this

pipe

  • pipe(stdout: any, stderr: any, persistent: boolean): Package
  • Pipe npm command output to the given stream. This process can be persistent or non-persistent. Persistent means the piping operation will be applied on the next actions too. This method should be called before the others.

    Parameters

    • stdout: any
    • stderr: any
    • persistent: boolean

    Returns Package

prependListener

  • prependListener(event: string | symbol, listener: Function): this
  • Parameters

    • event: string | symbol
    • listener: Function

    Returns this

prependOnceListener

  • prependOnceListener(event: string | symbol, listener: Function): this
  • Parameters

    • event: string | symbol
    • listener: Function

    Returns this

removeAllListeners

  • removeAllListeners(event?: string | symbol): this
  • Parameters

    • Optional event: string | symbol

    Returns this

removeListener

  • removeListener(event: string | symbol, listener: Function): this
  • Parameters

    • event: string | symbol
    • listener: Function

    Returns this

run

  • Run npm script in package directory.

    Parameters

    • Default value opts: RunOptions = { script: 'start', env: { PATH: process.env.PATH } }

    Returns ChildProcess

setMaxListeners

  • setMaxListeners(n: number): this
  • Parameters

    • n: number

    Returns this

uninstall

  • uninstall(pkgs: string | string[], save?: boolean, dev?: boolean): Promise<Package>
  • Uninstall package(s).

    Parameters

    • pkgs: string | string[]
    • Default value save: boolean = false

      Remove dependency from the dependencies list of package.json.

    • Default value dev: boolean = false

      Remove dependency from the devDependencies list of package.json.

    Returns Promise<Package>

Static create

  • create(dir: string): Promise<Package>
  • Creates writable directory and returns new instance of Package pointing to created directory.

    static

    Parameters

    • dir: string

    Returns Promise<Package>

Static getPackageDir

  • getPackageDir(pkg: string): string
  • Returns the unformatted package name.

    static

    Parameters

    • pkg: string

    Returns string

Static listenerCount

  • listenerCount(emitter: EventEmitter, event: string | symbol): number
  • Parameters

    • emitter: EventEmitter
    • event: string | symbol

    Returns number

Object literals

Private _pipe

_pipe: object

persistent

persistent: boolean = false

stderr

stderr: null = null

stdout

stdout: null = null

Generated using TypeDoc