Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Intermodular

Hierarchy

  • Intermodular

Index

Constructors

Private constructor

  • Parameters

    • sourceModule: Module
    • targetModule: Module
    • config: DataFile
    • Default value logger: Logger = { log: () => {} }

    Returns Intermodular

Properties

Readonly config

config: DataFile

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

Readonly logger

logger: Logger

Winston compatible logger.

Readonly sourceModule

sourceModule: Module

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

Readonly targetModule

targetModule: Module

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>
  • 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

    • sourceModuleFilePath: string

      is the file path relative to source module root.

    • Default value 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>
  • command(cmd: string, options?: ExecuteOptions<null>): Promise<ExecaReturnValue<Buffer>>
  • 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

    Returns Promise<ExecaReturnValue>

    [[ExecaReturnValue]] instance.

  • Parameters

    Returns Promise<ExecaReturnValue<Buffer>>

copy

  • copy(sourcePath: string, targetPath?: string, copyOptions?: CopyOptions): Promise<string[]>
  • 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

    • sourcePath: string

      is source to copy from.

    • Default value targetPath: string = sourcePath

      is destination to copy to. Cannot be a directory.

    • Default value copyOptions: CopyOptions = {}

    Returns Promise<string[]>

    copied files and directories. Directories can be optionally excluded.

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 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

    • 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 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

    Returns Promise<ExecaReturnValue>

    [[ExecaReturnValue]] instance.

  • Parameters

    Returns Promise<ExecaReturnValue<Buffer>>

Private getCopyFilterAndLogFunctions

  • getCopyFilterAndLogFunctions(__namedParameters: { excludeDirFromReturn: undefined | false | true; filter: undefined | ((fullSourcePath: string, fullTargetPath: string, isSourceDirectory: boolean, isTargetDirectory: boolean, sourceContent?: string | DataFile, targetContent?: string | DataFile) => boolean | Promise<boolean>); overwrite: undefined | false | true }): [CopyFilterAsync, (e?: Error) => void, () => string[]]
  • Creates;

    1. An augmented filter function for given fs-extra copy filter function.
    2. A log function.

    Parameters

    • __namedParameters: { excludeDirFromReturn: undefined | false | true; filter: undefined | ((fullSourcePath: string, fullTargetPath: string, isSourceDirectory: boolean, isTargetDirectory: boolean, sourceContent?: string | DataFile, targetContent?: string | DataFile) => boolean | Promise<boolean>); overwrite: undefined | false | true }
      • excludeDirFromReturn: undefined | false | true

        is wether to return copied files only. If this is true, directories are excluded from return value.

      • filter: undefined | ((fullSourcePath: string, fullTargetPath: string, isSourceDirectory: boolean, isTargetDirectory: boolean, sourceContent?: string | DataFile, targetContent?: string | DataFile) => boolean | Promise<boolean>)

        intermodular copy filter function.

      • overwrite: undefined | false | true

        is whether overwirte option is passed to fs-extra copy function.

    Returns [CopyFilterAsync, (e?: Error) => void, () => string[]]

    fs-extra copy compatible filter function and log function and function to get copied paths.

log

  • log(logLevel: LogLevel, message: string): void
  • Logs given message with required level using logger provided during object construction.

    Parameters

    • logLevel: LogLevel

      is the level to log message.

    • message: string

      is the message to log.

    Returns void

Static Private assertSourceAndTarget

  • assertSourceAndTarget(source: any, target: any): void
  • Throws if source or target file are undefined.

    Parameters

    • source: any
    • target: any

    Returns void

Static isEnvSet

  • isEnvSet(variable: string): boolean
  • Returns whether variable is set in environment variables and not empty.

    Parameters

    • 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?: { commandStdio: undefined | "pipe" | "ignore" | "inherit" | (undefined | number | "pipe" | "ignore" | "inherit" | internal | "ipc")[]; logger: undefined | Logger; overwrite: undefined | false | true; source: undefined | string | Module; target: undefined | string | Module }): Promise<Intermodular>
  • Creates and returns Intermodular instance.

    Parameters

    • Default value __namedParameters: { commandStdio: undefined | "pipe" | "ignore" | "inherit" | (undefined | number | "pipe" | "ignore" | "inherit" | internal | "ipc")[]; logger: undefined | Logger; overwrite: undefined | false | true; source: undefined | string | Module; target: undefined | string | Module } = {}

      are options

      • 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

  • parseEnv<T>(variable: string, defaultValue?: T): string | number | Record<string, any> | T | undefined
  • 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

    • variable: string

      is Name of the environment variable

    • Optional 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.

Generated using TypeDoc