import type { MaybeAsync } from '@gasket/core'; import type { SpawnOptions } from 'child_process'; export interface PackageManagerOptions { /** Name of manager, either `npm` (default) or `yarn` */ packageManager?: string; /** Target directory where `node_module` should exist */ dest?: string; } /** * Wrapper class for executing commands for a given package manager */ export class PackageManager { constructor(options: PackageManagerOptions): void; /** Name of manager, either `npm` (default) or `yarn` */ manager: string; /** Target directory where `node_module` should exist */ dest: string; /** * Executes npm in the application directory `this.dest`. This installation * can be run multiple times. * @param cmd The command that needs to be executed. * @param args Additional CLI arguments to pass to `npm`. */ exec(cmd: string, args?: Array): Promise; /** * Executes npm link in the application directory `this.dest`. * @param packages Explicit `npm` packages to link locally. */ link(packages?: Array): Promise; /** * Executes npm install in the application directory `this.dest`. This * installation can be run multiple times. * @param args Additional CLI arguments to pass to `npm`. * @public */ install(args?: Array): Promise; /** * Executes yarn or npm info, and returns parsed JSON data results. * @param args Additional CLI arguments to pass to `npm`. * @returns stdout and data * @public */ info(args?: Array): Promise<{ data: any; stdout: string }>; } export interface Signal { aborted?: boolean; addEventListener(type: 'abort', listener: () => void): void; } /** * Promise friendly wrapper to running a shell command (eg: git, npm, ls) which * passes back any { stdout, stderr } to the error thrown. * * Options can be passed to the underlying spawn. An additional `signal` option * can be passed to use AbortController, allowing processes to be killed when no * longer needed. * @param {string} cmd - Command to execute * @param {string[]} argv - Array of command arguments * @param {object} options - Spawn options * @param {AbortSignal} options.signal - Abort signal for process cancellation * @param {string} options.cwd - Current working directory * @param {boolean} debug - Enable debug output * @example * const { runShellCommand } = require('@gasket/utils'); * * async function helloWorld() { * await runShellCommand('echo', ['hello world']); * } * @example * // With timeout using AbortController * * const { runShellCommand } = require('@gasket/utils'); * const AbortController = require('abort-controller'); * * async function helloWorld() { * const controller = new AbortController(); * // abort the process after 60 seconds * const id = setTimeout(() => controller.abort(), 60000); * await runShellCommand('long-process', ['something'], { signal: controller.signal }); * clearTimeout(id); * } */ export function runShellCommand( /** Binary that is run */ cmd: string, /** Arguments passed to npm binary through spawn. */ argv?: string[], /** Options passed to npm binary through spawn */ options?: SpawnOptions, /** When present pipes std{out,err} to process.*/ debug?: boolean ): Promise<{ stdout: string }>; export interface PkgManager { pkgManager: string; cmd: string; flags: string[]; logMsg: (msg: string) => string; } export interface TargetConfig { environments: string; } export interface environments { dev; } export interface ConfigContext { /** Name of environment */ env: string; /** Name of command */ commandId?: string; /** Project root; required if using localeFile */ root?: string; /** Optional file to load relative to gasket root */ localFile?: string; } /** * Executes the appropriate npm binary with the verbatim `argv` and * `spawnWith` options provided. Passes appropriate debug flag for * npm based on process.env. * @param {string[]} argv - Precise CLI arguments to pass to `npm` * @param {SpawnOptions} spawnWith - Options for child_process.spawn */ export function PackageManager_spawnNpm( /** Precise CLI arguments to pass to `npm`. */ argv: string[], /** Options for child_process.spawn. */ spawnWith: SpawnOptions ): Promise<{ stdout: string }>; /** * Executes the appropriate yarn binary with the verbatim `argv` and * `spawnWith` options provided. Passes appropriate debug flag for * npm based on process.env. * @param {string[]} argv - Precise CLI arguments to pass to `yarn` * @param {SpawnOptions} spawnWith - Options for child_process.spawn */ export function PackageManager_spawnYarn( /** Precise CLI arguments to pass to `npm`. */ argv: string[], /** Options for child_process.spawn. */ spawnWith: SpawnOptions ): Promise<{ stdout: string }>; export function PackageManager_exec( /** The command that needs to be executed. */ cmd: string, /** Additional CLI arguments to pass to `npm`. */ args: string[] ): Promise<{ stdout: string }>; export function PackageManager_link( /** Explicit `npm` packages to link locally. */ packages: string[] ): Promise<{ stdout: string }>; export function PackageManager_install( /** Additional CLI arguments to pass to `npm`. */ args: string[] ): Promise<{ stdout: string }>; export function PackageManager_info( /** Additional CLI arguments to pass to `npm`. */ args: string[] ): Promise<{ data: any; stdout: string }>; export function warnIfOutdated(pkgName: string, currentVersion: string): MaybeAsync; export function getPackageLatestVersion(pkgName: string, options?: object): Promise; // TODO: switch @gasket/core to re-exporting this type once this change is // published and we can update its dependency to this version export type PartialRecursive = T extends object ? { [K in keyof T]?: PartialRecursive } | undefined : T | undefined; export type ConfigDefinition = T & { environments?: Record>; commands?: Record>; }; type ConfigPartial = PartialRecursive | undefined | void | unknown; export function getPotentialConfigs( config: ConfigDefinition, configContext: ConfigContext ): Array>; export function getCommandOverrides( commands: Record>, commandId: string ): Array>; export function getSubEnvironmentOverrides( env: string, environments: Record> ): Array>; export function getDevOverrides( isLocalEnv: boolean, environments: Record> ): Array>; export function getLatestVersion( pkgName: string, /** current time in milliseconds */ currentTime: number, cache: Record ): Promise; export function getLocalOverrides( isLocalEnv: boolean, root: string, localFile: string ): Generator | undefined, void, unknown>; // Normalize the config by applying any overrides for environments, commands, or local-only config file. export function applyConfigOverrides( config: ConfigDefinition, configContext: ConfigContext ): T; declare module 'diagnostics' { const diagnostics: (namespace: string) => (...args: any[]) => void; export = diagnostics; }