# Class: Intermodular

# Hierarchy

  • Intermodular

# Properties

# Readonly config

config: DataFile

Defined in src/intermodular.ts:22

Configuration for source module in target module as a DataFile instance.


# Readonly logger

logger: Logger

Defined in src/intermodular.ts:25

Winston compatible logger.


# Readonly sourceModule

sourceModule: Module

Defined in src/intermodular.ts:16

Module instance of node module which is used as source for modification operations such as copy, update.


# Readonly targetModule

targetModule: Module

Defined in src/intermodular.ts:19

Module instance of node module which is used as target for modification operations such as copy, update.

# Methods

# areEquivalentFiles

areEquivalentFiles(sourceModuleFilePath: string, targetModuleFilePath: string): Promise‹boolean›

Defined in src/intermodular.ts:211

Returns whether given files from source module and target module are equal using method described below:

  • For data files such as JSON or YAML, returns whether they are deeply equal. (Objects does not have to have same key order.)
  • For .js and .ts files, files are transpiled and minified then copmpared. (To overcome comment and simple format changes.)
  • For other files returns whether their string content are equal.

# Example

intermodular("module-files/src/address.js", "src/address.js");
intermodular("module-files/config/.eslintrc", ".eslintrc");
intermodular("module-files/some.txt", "some.txt");

Parameters:

Name Type Default Description
sourceModuleFilePath string - is the file path relative to source module root.
targetModuleFilePath string sourceModuleFilePath is the file path relative to target module root. If not provided uses same relative path as sourceModuleFilePath.

Returns: Promise‹boolean›

whether given files are equivalent.


# command

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

Defined in src/intermodular.ts:190

Executes given command using execa.command with cwd as target module's root. Additionally adds source module's node_modules/.bin to path.

# Example

intermodular.command("ls"); // Run `ls`.
intermodular.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/intermodular.ts:191

Parameters:

Name Type
cmd string
options? ExecuteOptions‹null›

Returns: Promise‹ExecaReturnValue‹Buffer››


# copy

copy(sourcePath: string, targetPath: string, copyOptions: CopyOptions): Promise‹string[]›

Defined in src/intermodular.ts:124

Copies a file or directory from pathInSourceModule relative to source module root to pathInTargetModulerelative to target module root. The directory can have contents. Like cp -r. IMPORTANT: Note that if source is a directory it will copy everything inside of this directory, not the entire directory itself.

# Example

// Copy everything in `/path/to/project/node_modules/module-a/src/config/` to `/path/to/project/`
const copiedPaths = copySync("src/config", ".");
const copiedFiles = copySync("src/config", ".", { excludeDirFromReturn: true });

Parameters:

Name Type Default Description
sourcePath string - is source to copy from.
targetPath string sourcePath is destination to copy to. Cannot be a directory.
copyOptions CopyOptions {} -

Returns: Promise‹string[]›

copied files and directories. Directories can be optionally excluded.


# execute

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

Defined in src/intermodular.ts:154

Executes given command using execa with given arguments and options with cwd as target module's root. Applies sensible default options. Additionally adds source module's node_modules/.bin to path.

# Example

intermodular.execute("ls"); // Run `ls`.
intermodular.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/intermodular.ts:155

Parameters:

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

Returns: Promise‹ExecaReturnValue‹Buffer››

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

Defined in src/intermodular.ts:168

Executes given command using execa with given arguments and options with cwd as target module's root. Applies sensible default options. Additionally adds source module's node_modules/.bin to path.

# Example

intermodular.execute("ls"); // Run `ls`.
intermodular.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/intermodular.ts:169

Parameters:

Name Type
bin string
options? ExecuteOptions‹null›

Returns: Promise‹ExecaReturnValue‹Buffer››


# log

log(logLevel: LogLevel, message: string): void

Defined in src/intermodular.ts:40

Logs given message with required level using logger provided during object construction.

Parameters:

Name Type Description
logLevel LogLevel is the level to log message.
message string is the message to log.

Returns: void


# Static isEnvSet

isEnvSet(variable: string): boolean

Defined in src/intermodular.ts:285

Returns whether variable is set in environment variables and not empty.

Parameters:

Name Type Description
variable string is name of the environment variable to check.

Returns: boolean

whether given environment variable is set and not empty.


# Static new

new(__namedParameters: object): Promise‹Intermodular

Defined in src/intermodular.ts:242

Creates and returns Intermodular instance.

Parameters:

Default value __namedParameters: object= {}

are options

Name Type Description
commandStdio undefined | "pipe" | "ignore" | "inherit" | undefined | number | "pipe" | "ignore" | "inherit" | internal‹› | "ipc"[] -
logger undefined | Logger is Winston compatible logger or console.
overwrite undefined | false | true is whether to overwrite files by default.
source undefined | string | Module‹› is the source module or a path in source module. By default immediate parent's root dir is used. Immediate parent is the file which calls this method.
target undefined | string | Module‹› is the target module or a path in target module. By default process.env.INIT_CWD or process.env.CWD is used whichever is first available.

Returns: Promise‹Intermodular

Intermodular instance.


# Static parseEnv

parseEnvT›(variable: string, defaultValue?: T): string | number | Record‹string, any› | T | undefined

Defined in src/intermodular.ts:299

Parses and returns variable environment variable. If value is JSON object, parses using JSON5 and returns it as a JavaScript object. Otherwise returns defaultValue.

Type parameters:

T

Parameters:

Name Type Description
variable string is Name of the environment variable
defaultValue? T is value to return if no environment variable is set or is empty.

Returns: string | number | Record‹string, any› | T | undefined

environment variable (if possible as an object) or default value.