# Class: Module

Class which provides information and modification methods for a module.

# Hierarchy

  • Module

# Properties

# Readonly isTypeScript

isTypeScript: boolean

Defined in src/module.ts:32

Whether module is a TypeScript project.


# Readonly package

package: DataFile

Defined in src/module.ts:29

DataFile instance of package.json.


# Readonly packageManager

packageManager: PackageManager

Defined in src/module.ts:26

Package manager of the module.


# Readonly root

root: string

Defined in src/module.ts:23

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

# Accessors

# name

get name(): string

Defined in src/module.ts:77

Name of the module as defined in package.json.

Returns: string


# nameWithoutUser

get nameWithoutUser(): string

Defined in src/module.ts:82

Name of the module without user name. For example: typescript for @microsoft/typescript.

Returns: string

# Methods

# cloneWithSharedManager

cloneWithSharedManager(__namedParameters: object): Module

Defined in src/module.ts:66

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: object= {}

Name Type
logger undefined | Logger
overwrite undefined | false | true

Returns: Module

Module instance.


# command

command(cmd: string, options?: ExecuteOptions): Promise‹ExecaReturnValue›

Defined in src/module.ts:408

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:

Name Type Description
cmd string is command to execute.
options? ExecuteOptions are passed to Execa.

Returns: Promise‹ExecaReturnValue›

[[ExecaReturnValue]] instance.

command(cmd: string, options?: ExecuteOptions‹null›): Promise‹ExecaReturnValue‹Buffer››

Defined in src/module.ts:409

Parameters:

Name Type
cmd string
options? ExecuteOptions‹null›

Returns: Promise‹ExecaReturnValue‹Buffer››


# createDirectory

createDirectory(path: string): Promise‹void›

Defined in src/module.ts:308

Ensures that the directory exists. If the directory structure does not exist, it is created similar to mkdir -p.

Parameters:

Name Type Description
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›

Defined in src/module.ts:364

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:

Name Type Description
bin string is binary file to execute.
args? string[] are arguments to pass to executable.
options? ExecuteOptions are passed to Execa.

Returns: Promise‹ExecaReturnValue›

[[ExecaReturnValue]] instance.

execute(bin: string, args?: string[], options?: ExecuteOptions‹null›): Promise‹ExecaReturnValue‹Buffer››

Defined in src/module.ts:365

Parameters:

Name Type
bin string
args? string[]
options? ExecuteOptions‹null›

Returns: Promise‹ExecaReturnValue‹Buffer››

execute(bin: string, options?: ExecuteOptions): Promise‹ExecaReturnValue›

Defined in src/module.ts:377

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:

Name Type Description
bin string is binary file to execute.
options? ExecuteOptions are passed to Execa.

Returns: Promise‹ExecaReturnValue›

[[ExecaReturnValue]] instance.

execute(bin: string, options?: ExecuteOptions‹null›): Promise‹ExecaReturnValue‹Buffer››

Defined in src/module.ts:378

Parameters:

Name Type
bin string
options? ExecuteOptions‹null›

Returns: Promise‹ExecaReturnValue‹Buffer››


# exists

exists(path: string): Promise‹boolean›

Defined in src/module.ts:283

Checks whether given path exists.

Parameters:

Name Type Description
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

Defined in src/module.ts:93

Fetches a dependent module's version from given dependency types.

Parameters:

Name Type Default Description
moduleName string - is the name of the module to get version of.
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

Defined in src/module.ts:105

Checks whether given module or any of the modules exist in given dependency types.

Parameters:

Name Type Default Description
moduleNames string | string[] - are the name of the module to search for.
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

ifAnyDependencyT, F›(moduleNames: string | string[]): boolean

Defined in src/module.ts:109

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

Type parameters:

T

F

Parameters:

Name Type Description
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.

ifAnyDependencyT, F›(moduleNames: string | string[], t: T): T | false

Defined in src/module.ts:110

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

Type parameters:

T

F

Parameters:

Name Type
moduleNames string | string[]
t T

Returns: T | false

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

ifAnyDependencyT, F›(moduleNames: string | string[], t: T, f: F, dependencyTypes?: DependencyType[]): T | F

Defined in src/module.ts:111

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

Type parameters:

T

F

Parameters:

Name Type
moduleNames string | string[]
t T
f F
dependencyTypes? DependencyType[]

Returns: T | F

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


# install

install(packageNames: string | string[], __namedParameters: object): Promise‹void›

Defined in src/module.ts:435

Installs node modules using specified package manager.

Parameters:

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

are package name or array of package names.

Default value __namedParameters: object= {}

Name Type Default Description
type "dependencies" | "devDependencies" | "peerDependencies" | "optionalDependencies" "dependencies" as DependencyType is the dependency type of the package. dev, peer, optional etc.

Returns: Promise‹void›


# isDirectory

isDirectory(path: string): Promise‹boolean›

Defined in src/module.ts:293

Returns whether given path is a directory.

Parameters:

Name Type Description
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›

Defined in src/module.ts:347

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:

Name Type Description
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

Defined in src/module.ts:135

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:

Name Type Description
...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›

Defined in src/module.ts:185

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:

Name Type Description
path string is the path relative to module root or an absolute path.
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›

Defined in src/module.ts:171

Reads file and creates DataFile instance using Manager.

Parameters:

Name Type Description
path string is the path relative to module root or an absolute path.
options? ManagerLoadOptions are options passed to Manager.load of edit-config. See here.

Returns: Promise‹DataFile›


# readRaw

readRaw(path: string): Promise‹string›

Defined in src/module.ts:161

Asynchronously reads the entire contents of a file using utf8 encoding.

Parameters:

Name Type Description
path string is the path relative to module root or an absolute path.

Returns: Promise‹string›

file contents.


# relativePathOf

relativePathOf(...parts: string[]): string

Defined in src/module.ts:150

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:

Name Type Description
...parts string[] are path or array of path parts.

Returns: string

path relative to module's root.


# remove

remove(path: string, __namedParameters: object): Promise‹string | undefined›

Defined in src/module.ts:258

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: object= {}

Name Type
condition undefined | function

Returns: Promise‹string | undefined›

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


# removeEmptyDirs

removeEmptyDirs(path: string): Promise‹string[]›

Defined in src/module.ts:271

Removes empty directories recursively for given path relative to module root.

Parameters:

Name Type Description
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: object): Promise‹boolean›

Defined in src/module.ts:320

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: object= {}

Name Type Default Description
overwrite boolean this.#overwrite 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›

Defined in src/module.ts:424

Saves all read data files.

Returns: Promise‹void›


# uninstall

uninstall(packageNames: string | string[]): Promise‹void›

Defined in src/module.ts:455

Uninstalls node modules using specified package manager.

Parameters:

Name Type Default Description
packageNames string | string[] [] are package name or array of package names.

Returns: Promise‹void›


# write

write(path: string, content: object | string, __namedParameters: object): Promise‹string | DataFile | undefined›

Defined in src/module.ts:222

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: object= {}

Name Type Default Description
condition undefined | function - -
defaultFormat "json" | "yaml" "json" is the format to be used in serialization if file does not exist and content is object.
overwrite boolean this.#overwrite is whether to overwrite existing file.

Returns: Promise‹string | DataFile | undefined›

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


# Static new

new(options: object): Promise‹Module

Defined in src/module.ts:482

Creates and returns a Module instance.

Parameters:

Default value options: object= {}

are options.

Name Type Description
commandStdio? StdioOption is the default stdio option to be used with command and execute methods.
cwd? undefined | string is starting directory to start search for module root from.
logger? Logger is Winston compatible Logger to be used when logging.
overwrite? undefined | false | true is whether to overwrite files by default.
packageManager? PackageManager is package manager used by module.

Returns: Promise‹Module

Module instance.