Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Module

Class which provides information and modification methods for a module.

Hierarchy

  • Module

Index

Constructors

Private constructor

  • new Module(root: string, manager: Manager, packageData: DataFile, __namedParameters: { commandStdio: "pipe" | "ignore" | "inherit" | (undefined | number | "pipe" | "ignore" | "inherit" | internal | "ipc")[]; isTypeScript: boolean; overwrite: boolean; packageManager: "npm" | "yarn"; logger: object }): Module
  • Parameters

    • root: string
    • manager: Manager
    • packageData: DataFile
    • __namedParameters: { commandStdio: "pipe" | "ignore" | "inherit" | (undefined | number | "pipe" | "ignore" | "inherit" | internal | "ipc")[]; isTypeScript: boolean; overwrite: boolean; packageManager: "npm" | "yarn"; logger: object }
      • commandStdio: "pipe" | "ignore" | "inherit" | (undefined | number | "pipe" | "ignore" | "inherit" | internal | "ipc")[]
      • isTypeScript: boolean
      • overwrite: boolean
      • packageManager: "npm" | "yarn"
      • logger: object
        • log: function
          • log(): void
          • Returns void

    Returns Module

Properties

Private #commandStdio

#commandStdio: StdioOption

Private #logger

#logger: Logger

Private #manager

#manager: Manager

Private #overwrite

#overwrite: boolean

Readonly isTypeScript

isTypeScript: boolean

Whether module is a TypeScript project.

Readonly package

package: DataFile

DataFile instance of package.json.

Readonly packageManager

packageManager: PackageManager

Package manager of the module.

Readonly root

root: string

Absolute path of the module's root directory, where package.json is located.

Accessors

name

  • get name(): string
  • Name of the module as defined in package.json.

    Returns string

nameWithoutUser

  • get nameWithoutUser(): string
  • Name of the module without user name. For example: typescript for @microsoft/typescript.

    Returns string

Methods

Private checkFileModificationCondition

  • checkFileModificationCondition(path: string, name: string, condition?: PredicateFileOperation): Promise<boolean>
  • Parameters

    Returns Promise<boolean>

cloneWithSharedManager

  • cloneWithSharedManager(__namedParameters?: { logger: undefined | Logger; overwrite: undefined | false | true }): Module
  • Creates a new Module instance from current instance, which shares Data File Manager with current Module. Multiple instance work over same files efficiently and without collision.

    Parameters

    • Default value __namedParameters: { logger: undefined | Logger; overwrite: undefined | false | true } = {}
      • logger: undefined | Logger
      • overwrite: undefined | false | true

    Returns Module

    Module instance.

command

  • command(cmd: string, options?: ExecuteOptions): Promise<ExecaReturnValue>
  • command(cmd: string, options?: ExecuteOptions<null>): Promise<ExecaReturnValue<Buffer>>
  • Executes given command using execa.command with given options. Applies sensible default options.

    Example

    module.command("ls"); // Run `ls`.
    module.command("ls -al", { stdio: "inherit" }); // Run `ls -al`.

    Parameters

    Returns Promise<ExecaReturnValue>

    [[ExecaReturnValue]] instance.

  • Parameters

    Returns Promise<ExecaReturnValue<Buffer>>

createDirectory

  • createDirectory(path: string): Promise<void>
  • Ensures that the directory exists. If the directory structure does not exist, it is created similar to mkdir -p.

    Parameters

    • path: string

      is the path relative to module root or an absolute path.

    Returns Promise<void>

execute

  • execute(bin: string, args?: string[], options?: ExecuteOptions): Promise<ExecaReturnValue>
  • execute(bin: string, args?: string[], options?: ExecuteOptions<null>): Promise<ExecaReturnValue<Buffer>>
  • execute(bin: string, options?: ExecuteOptions): Promise<ExecaReturnValue>
  • execute(bin: string, options?: ExecuteOptions<null>): Promise<ExecaReturnValue<Buffer>>
  • Executes given command using execa with given arguments and options. Applies sensible default options.

    Example

    module.execute("ls"); // Run `ls`.
    module.execute("ls", ["-al"], { stdio: "inherit" }); // Run `ls -al`.

    Parameters

    • bin: string

      is binary file to execute.

    • Optional args: string[]

      are arguments to pass to executable.

    • Optional options: ExecuteOptions

      are passed to Execa.

    Returns Promise<ExecaReturnValue>

    [[ExecaReturnValue]] instance.

  • Parameters

    • bin: string
    • Optional args: string[]
    • Optional options: ExecuteOptions<null>

    Returns Promise<ExecaReturnValue<Buffer>>

  • Executes given command using execa with given arguments and options. Applies sensible default options.

    Example

    module.execute("ls"); // Run `ls`.
    module.execute("ls", { stdio: "inherit" }); // Run `ls`.

    Parameters

    Returns Promise<ExecaReturnValue>

    [[ExecaReturnValue]] instance.

  • Parameters

    Returns Promise<ExecaReturnValue<Buffer>>

exists

  • exists(path: string): Promise<boolean>
  • Checks whether given path exists.

    Parameters

    • path: string

      is the path relative to module root or an absolute path.

    Returns Promise<boolean>

    whether given path exists.

getDependencyVersion

  • getDependencyVersion(moduleName: string, dependencyTypes?: string[]): string | undefined
  • Fetches a dependent module's version from given dependency types.

    Parameters

    • moduleName: string

      is the name of the module to get version of.

    • Default value dependencyTypes: string[] = ALL_DEPENDENCIES

      are array of dependency types to search module in.

    Returns string | undefined

    version of the moduleName || undefined.

hasAnyDependency

  • hasAnyDependency(moduleNames: string | string[], dependencyTypes?: string[]): boolean
  • Checks whether given module or any of the modules exist in given dependency types.

    Parameters

    • moduleNames: string | string[]

      are the name of the module to search for.

    • Default value dependencyTypes: string[] = ALL_DEPENDENCIES

      are array of dependency types to search module in.

    Returns boolean

    whether moduleName exists in one of the dependency types.

ifAnyDependency

  • ifAnyDependency<T, F>(moduleNames: string | string[]): boolean
  • ifAnyDependency<T, F>(moduleNames: string | string[], t: T): T | false
  • ifAnyDependency<T, F>(moduleNames: string | string[], t: T, f: F, dependencyTypes?: DependencyType[]): T | F
  • Checks single or multiple module's existence in any of the package.json dependencies.

    Type parameters

    • T

    • F

    Parameters

    • moduleNames: string | string[]

      are Module or modules to check whether this module has any dependency.

    Returns boolean

    trueValue if module depends on any of the moduleNames. Otherwise returns falseValue.

  • Checks single or multiple module's existence in any of the package.json dependencies.

    Type parameters

    • T

    • F

    Parameters

    • moduleNames: string | string[]
    • t: T

    Returns T | false

    trueValue if module depends on any of the moduleNames. Otherwise returns falseValue.

  • Checks single or multiple module's existence in any of the package.json dependencies.

    Type parameters

    • T

    • F

    Parameters

    • moduleNames: string | string[]
    • t: T
    • f: F
    • Optional dependencyTypes: DependencyType[]

    Returns T | F

    trueValue if module depends on any of the moduleNames. Otherwise returns falseValue.

install

  • install(packageNames?: string | string[], __namedParameters?: { type: "dependencies" | "devDependencies" | "peerDependencies" | "optionalDependencies" }): Promise<void>
  • Installs node modules using specified package manager.

    Parameters

    • Default value packageNames: string | string[] = []

      are package name or array of package names.

    • Default value __namedParameters: { type: "dependencies" | "devDependencies" | "peerDependencies" | "optionalDependencies" } = {}
      • type: "dependencies" | "devDependencies" | "peerDependencies" | "optionalDependencies"

        is the dependency type of the package. dev, peer, optional etc.

    Returns Promise<void>

isDirectory

  • isDirectory(path: string): Promise<boolean>
  • Returns whether given path is a directory.

    Parameters

    • path: string

      is the path relative to module root or an absolute path.

    Returns Promise<boolean>

    whether given path is a directory.

isEqual

  • isEqual(path: string, content: string | Record<string, any>): Promise<boolean>
  • Checks whether content of pathInModule file is equal to data by making string comparison (for strings) or deep comparison (for objects).

    Example

    const isConfigEqual = module.isEqual("config.json", { someData: 4 });
    const textEqual = module.isEqual("some.txt", "content");

    Parameters

    • path: string

      is the path relative to module root or an absolute path.

    • content: string | Record<string, any>

      is string or JavaScript object to compare to file's content.

    Returns Promise<boolean>

    whether the file is equal to given content.

pathOf

  • pathOf(...parts: string[]): string
  • Returns absolute path for given relative path to module root. If given path is an absolute path, returns it directly.

    Example

    module.pathOf("images", "photo.jpg"); // -> /path/to/root/images/photo.jpg
    module.pathOf("/usr", "bin"); // -> /usr/bin

    Parameters

    • Rest ...parts: string[]

      are path or array of path parts.

    Returns string

    absolute path to given destination.

read

  • read(path: string, options?: ManagerLoadOptions): Promise<DataFile | string | undefined>
  • Reads and if possible returns DataFile otherwise file content. If file does not exist returns undefined. If options.defaultData is true, file will be created using options.defaultData if it does not exist.

    see

    Module.readData, Module.readRaw

    throws

    if given path is a directory.

    Parameters

    • path: string

      is the path relative to module root or an absolute path.

    • Optional options: ManagerLoadOptions

      are options passed to Manager.load of edit-config. See here.

    Returns Promise<DataFile | string | undefined>

    DataFile instance, file content or undefined.

readData

  • readData(path: string, options?: ManagerLoadOptions): Promise<DataFile>
  • Reads file and creates DataFile instance using Manager.

    Parameters

    • path: string

      is the path relative to module root or an absolute path.

    • Optional options: ManagerLoadOptions

      are options passed to Manager.load of edit-config. See here.

    Returns Promise<DataFile>

readRaw

  • readRaw(path: string): Promise<string>
  • Asynchronously reads the entire contents of a file using utf8 encoding.

    Parameters

    • path: string

      is the path relative to module root or an absolute path.

    Returns Promise<string>

    file contents.

relativePathOf

  • relativePathOf(...parts: string[]): string
  • Returns relative path to module root for given absolute path. If given path is a relative path, returns it directly.

    Example

    module.relativePathOf("/path/to/module/src/my-file.js"); // -> src/my-file.js
    module.relativePathOf("src/my-file.js"); // -> src/my-file.js

    Parameters

    • Rest ...parts: string[]

      are path or array of path parts.

    Returns string

    path relative to module's root.

remove

  • remove(path: string, __namedParameters?: { condition: undefined | ((fileContent?: string | DataFile) => Promise<boolean> | boolean) }): Promise<string | undefined>
  • Removes file or directory relative to module's root. Removes directory even it has files in it. If the path does not exist, silently does nothing.

    Parameters

    • path: string

      is the path relative to module root or an absolute path.

    • Default value __namedParameters: { condition: undefined | ((fileContent?: string | DataFile) => Promise<boolean> | boolean) } = {}
      • condition: undefined | ((fileContent?: string | DataFile) => Promise<boolean> | boolean)

    Returns Promise<string | undefined>

    file path relative to module root if file is removed, undefined otherwise.

removeEmptyDirs

  • removeEmptyDirs(path: string): Promise<string[]>
  • Removes empty directories recursively for given path relative to module root.

    Parameters

    • path: string

      is the path relative to module root or an absolute path.

    Returns Promise<string[]>

    array of deleted directories.

rename

  • rename(oldPath: string, newPath: string, __namedParameters?: { overwrite: boolean }): Promise<boolean>
  • Renames given path.

    Parameters

    • oldPath: string

      is the old path relative to module root or an absolute path.

    • newPath: string

      is the new path relative to module root or an absolute path.

    • Default value __namedParameters: { overwrite: boolean } = {}
      • overwrite: boolean

        is whether to allow rename operation if target path already exists. Silently ignores operation if overwrite is not allowed and target path exists.

    Returns Promise<boolean>

    whether file is renamed.

saveAll

  • saveAll(): Promise<void>
  • Saves all read data files.

    Returns Promise<void>

uninstall

  • uninstall(packageNames?: string | string[]): Promise<void>
  • Uninstalls node modules using specified package manager.

    Parameters

    • Default value packageNames: string | string[] = []

      are package name or array of package names.

    Returns Promise<void>

write

  • write(path: string, content?: object | string, __namedParameters?: { condition: undefined | ((fileContent?: string | DataFile) => Promise<boolean> | boolean); defaultFormat: "json" | "yaml"; overwrite: boolean }): Promise<string | DataFile | undefined>
  • Writes given content to file. If content is an object, it is serialized. If prettier configuration and module is available and content is formatted using prettier.

    Parameters

    • path: string

      is the path relative to module root or an absolute path.

    • Default value content: object | string = ""

      is the content to write to file.

    • Default value __namedParameters: { condition: undefined | ((fileContent?: string | DataFile) => Promise<boolean> | boolean); defaultFormat: "json" | "yaml"; overwrite: boolean } = {}
      • condition: undefined | ((fileContent?: string | DataFile) => Promise<boolean> | boolean)
      • defaultFormat: "json" | "yaml"

        is the format to be used in serialization if file does not exist and content is object.

      • overwrite: boolean

        is whether to overwrite existing file.

    Returns Promise<string | DataFile | undefined>

    written content or [[DataFile]] if file is written, undefined otherwise.

Static Private getPackageManager

  • Returns package manager type.

    Parameters

    • dir: string

      is the absolute path of directory where package.json is located.

    Returns Promise<PackageManager | undefined>

    type of package manager.

Static Private getRoot

  • getRoot(cwd?: undefined | string): Promise<string>
  • Returns root directory of the module by searching package.json from given cwd or environment variables (INIT_CWD by npm install or CWD) up.

    throws

    if root cannot be found.

    Parameters

    • Optional cwd: undefined | string

      is the path to start search from.

    Returns Promise<string>

    path of module root.

Static new

  • new(options?: { commandStdio?: StdioOption; cwd?: undefined | string; logger?: Logger; overwrite?: undefined | false | true; packageManager?: PackageManager }): Promise<Module>
  • Creates and returns a Module instance.

    Parameters

    • Default value options: { commandStdio?: StdioOption; cwd?: undefined | string; logger?: Logger; overwrite?: undefined | false | true; packageManager?: PackageManager } = {}

      are options.

      • Optional commandStdio?: StdioOption

        is the default stdio option to be used with command and execute methods.

      • Optional cwd?: undefined | string

        is starting directory to start search for module root from.

      • Optional logger?: Logger

        is Winston compatible Logger to be used when logging.

      • Optional overwrite?: undefined | false | true

        is whether to overwrite files by default.

      • Optional packageManager?: PackageManager

        is package manager used by module.

    Returns Promise<Module>

    Module instance.

Generated using TypeDoc