@w5s/managed-script 1.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/README.md +121 -0
  2. package/dist/ManagedScript-DIacNHCc.js +149 -0
  3. package/dist/ManagedScript-DIacNHCc.js.map +1 -0
  4. package/dist/cli.d.ts +1 -0
  5. package/dist/cli.js +2808 -0
  6. package/dist/cli.js.map +1 -0
  7. package/dist/defaultContext-D_QWP9aF.js +98 -0
  8. package/dist/defaultContext-D_QWP9aF.js.map +1 -0
  9. package/dist/index.d.ts +155 -0
  10. package/dist/index.js +79 -0
  11. package/dist/index.js.map +1 -0
  12. package/dist/type-DQiyX1q_.d.ts +53 -0
  13. package/dist/zx.d.ts +48 -0
  14. package/dist/zx.js +76 -0
  15. package/dist/zx.js.map +1 -0
  16. package/package.json +53 -0
  17. package/src/ManagedScript/defaultContext.ts +34 -0
  18. package/src/ManagedScript/execute.ts +98 -0
  19. package/src/ManagedScript/resolveScripts.ts +25 -0
  20. package/src/ManagedScript/runScript.ts +9 -0
  21. package/src/ManagedScript.ts +8 -0
  22. package/src/ManagedScriptEnv.ts +37 -0
  23. package/src/cli/RootCommand.ts +88 -0
  24. package/src/cli/runCLI.ts +27 -0
  25. package/src/cli.ts +4 -0
  26. package/src/defineConfig.ts +16 -0
  27. package/src/index.ts +6 -0
  28. package/src/internal/ConfigLoader.ts +33 -0
  29. package/src/internal/Executor.ts +27 -0
  30. package/src/internal/Logger.ts +58 -0
  31. package/src/internal/ScriptNameResolver.ts +17 -0
  32. package/src/mergeConfig.ts +70 -0
  33. package/src/meta.ts +12 -0
  34. package/src/type/Command.ts +37 -0
  35. package/src/type/CommandContext.ts +48 -0
  36. package/src/type/ResolvedConfig.ts +18 -0
  37. package/src/type/UserConfig.ts +5 -0
  38. package/src/type.ts +4 -0
  39. package/src/zx/ZXOptions.ts +1 -0
  40. package/src/zx/ZXShell.ts +1 -0
  41. package/src/zx/defaultZXOptions.ts +37 -0
  42. package/src/zx/zx.ts +51 -0
  43. package/src/zx.ts +19 -0
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Enum for managed script environment variables.
3
+ *
4
+ * @enum
5
+ */
6
+ export const ManagedScriptEnv = Object.freeze({
7
+ /**
8
+ * The color mode used for messages.
9
+ */
10
+ Color: 'MANAGED_SCRIPT_COLOR',
11
+
12
+ /**
13
+ * The directory where the managed script configuration is located.
14
+ */
15
+ ConfigDir: 'MANAGED_SCRIPT_CONFIG_DIR',
16
+
17
+ /**
18
+ * The file where the managed script configuration is located.
19
+ */
20
+ ConfigFile: 'MANAGED_SCRIPT_CONFIG_FILE',
21
+
22
+ /**
23
+ * The invocation directory used to resolve configuration and spawn the script.
24
+ */
25
+ Cwd: 'MANAGED_SCRIPT_CWD',
26
+
27
+ /**
28
+ * The resolved log level.
29
+ */
30
+ LogLevel: 'MANAGED_SCRIPT_LOGLEVEL',
31
+
32
+ /**
33
+ * The name of the managed script.
34
+ */
35
+ Name: 'MANAGED_SCRIPT_NAME',
36
+ });
37
+ export type ManagedScriptEnv = typeof ManagedScriptEnv[keyof typeof ManagedScriptEnv];
@@ -0,0 +1,88 @@
1
+ import { Command, Option } from 'clipanion';
2
+ import { isEnum } from 'typanion';
3
+
4
+ import type { ColorMode, LogLevel } from '../internal/Logger.js';
5
+
6
+ import { ManagedScript } from '../ManagedScript.js';
7
+
8
+ const isLogLevel = isEnum(['debug', 'error', 'info', 'silent', 'warn'] as const);
9
+
10
+ const isColorModes = isEnum(['always', 'auto', 'never'] as const);
11
+
12
+ export class RootCommand extends Command {
13
+ static override paths = [Command.Default];
14
+ static override usage = Command.Usage({
15
+ description: 'Run a script from the managed-script configuration.',
16
+ details: 'Resolves the script name from the first positional argument, MANAGED_SCRIPT_NAME or npm_lifecycle_event, then executes it. Use --dry-run,-n to print the resolved command without executing it. Use --loglevel, --silent,-s or --verbose,-v to control status messages, forwarded to the script as MANAGED_SCRIPT_LOGLEVEL. Use --color[=WHEN] or --no-color to control ANSI colors (WHEN: auto, always, never).',
17
+ });
18
+
19
+ readonly color = Option.String('--color', {
20
+ description: 'Control ANSI colors (auto, always, never). Bare --color means always.',
21
+ required: false,
22
+ tolerateBoolean: true,
23
+ validator: isColorModes,
24
+ });
25
+
26
+ readonly cwd = Option.String('--cwd', {
27
+ description: 'Working directory used to resolve configuration and run the script.',
28
+ required: false,
29
+ });
30
+
31
+ readonly dryRun = Option.Boolean('--dry-run,-n', false, {
32
+ description: 'Print the resolved command instead of executing it.',
33
+ });
34
+
35
+ readonly loglevel = Option.String('--loglevel', {
36
+ description: 'Set the log level (silent, error, warn, info, debug). Default: info.',
37
+ required: false,
38
+ validator: isLogLevel,
39
+ });
40
+
41
+ readonly scriptArgs = Option.Rest();
42
+
43
+ readonly silent = Option.Boolean('--silent,-s', false, {
44
+ description: 'Alias for --loglevel silent.',
45
+ });
46
+
47
+ readonly verbose = Option.Boolean('--verbose,-v', false, {
48
+ description: 'Alias for --loglevel debug.',
49
+ });
50
+
51
+ async execute(): Promise<number> {
52
+ const [scriptName, ...scriptArgs] = this.scriptArgs;
53
+ let color: ColorMode | undefined;
54
+ if (this.color === true) {
55
+ color = 'always';
56
+ } else if (this.color === false) {
57
+ color = 'never';
58
+ } else if (this.color != null) {
59
+ color = this.color;
60
+ }
61
+ let logLevel: LogLevel | undefined;
62
+ if (this.silent) {
63
+ logLevel = 'silent';
64
+ } else if (this.verbose) {
65
+ logLevel = 'debug';
66
+ } else if (this.loglevel != null) {
67
+ logLevel = this.loglevel;
68
+ }
69
+
70
+ try {
71
+ await ManagedScript.runScript({
72
+ context: {
73
+ cli: { color, cwd: this.cwd, dryRun: this.dryRun, logLevel },
74
+ stderr: this.context.stderr,
75
+ stdout: this.context.stdout,
76
+ },
77
+ parameters: {
78
+ scriptArgs,
79
+ scriptName,
80
+ },
81
+ });
82
+ return 0;
83
+ } catch (error) {
84
+ this.context.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
85
+ return 1;
86
+ }
87
+ }
88
+ }
@@ -0,0 +1,27 @@
1
+ import { Builtins, Cli } from 'clipanion';
2
+
3
+ import { meta } from '../meta.js';
4
+ import { RootCommand } from './RootCommand.js';
5
+
6
+ export async function runCLI(): Promise<void> {
7
+ const cli = new Cli({
8
+ binaryLabel: meta.binaryLabel,
9
+ binaryName: meta.binaryName,
10
+ binaryVersion: meta.binaryVersion,
11
+ });
12
+
13
+ cli.register(RootCommand);
14
+ cli.register(Builtins.HelpCommand);
15
+ cli.register(Builtins.VersionCommand);
16
+
17
+ try {
18
+ await cli.runExit(process.argv.slice(2), {
19
+ stderr: process.stderr,
20
+ stdin: process.stdin,
21
+ stdout: process.stdout,
22
+ });
23
+ } catch (error) {
24
+ console.error(error instanceof Error ? error.message : error);
25
+ process.exitCode = 1;
26
+ }
27
+ }
package/src/cli.ts ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env node
2
+ import { runCLI } from './cli/runCLI.js';
3
+
4
+ void runCLI();
@@ -0,0 +1,16 @@
1
+ import { createDefineConfig } from 'c12';
2
+
3
+ import type { UserConfig } from './type/UserConfig.js';
4
+
5
+ /**
6
+ * Define the configuration for the managed script.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * // managed-script.config.ts or @acme/managed-script-config/index.ts
11
+ * export default defineConfig({
12
+ * // your configuration here
13
+ * });
14
+ * ```
15
+ */
16
+ export const defineConfig = createDefineConfig<UserConfig>();
package/src/index.ts ADDED
@@ -0,0 +1,6 @@
1
+ export * from './defineConfig.js';
2
+ export { ManagedScript } from './ManagedScript.js';
3
+ export { ManagedScriptEnv } from './ManagedScriptEnv.js';
4
+ export * from './mergeConfig.js';
5
+ export * from './meta.js';
6
+ export * from './type.js';
@@ -0,0 +1,33 @@
1
+ import type { ConfigLayer } from 'c12';
2
+
3
+ import { loadConfig } from 'c12';
4
+
5
+ import type { UserConfig } from '../type/UserConfig.js';
6
+
7
+ import { meta } from '../meta.js';
8
+
9
+ export interface ConfigLoaderOptions {
10
+ readonly cwd: string;
11
+ }
12
+
13
+ export interface ConfigLoaderResult {
14
+ readonly config: UserConfig;
15
+ readonly configFile: string | undefined;
16
+ readonly layers: Array<ConfigLayer<UserConfig>>;
17
+ }
18
+
19
+ export const ConfigLoader = {
20
+ async load(options: ConfigLoaderOptions): Promise<ConfigLoaderResult> {
21
+ const result = await loadConfig<UserConfig>({
22
+ cwd: options.cwd,
23
+ name: meta.binaryName,
24
+ packageJson: [meta.binaryName],
25
+ });
26
+
27
+ return {
28
+ config: result.config,
29
+ configFile: result.configFile ?? undefined,
30
+ layers: result.layers ?? [],
31
+ };
32
+ },
33
+ };
@@ -0,0 +1,27 @@
1
+ import { spawn } from 'node:child_process';
2
+
3
+ export interface ExecutorRunOptions {
4
+ readonly cwd?: string | undefined;
5
+ readonly env?: NodeJS.ProcessEnv | undefined;
6
+ }
7
+
8
+ /**
9
+ * Executes a command via the system shell, inheriting stdio and forwarding its exit code.
10
+ */
11
+ export const Executor = {
12
+ async run(command: string, options: ExecutorRunOptions = {}): Promise<number> {
13
+ return new Promise((resolve, reject) => {
14
+ const child = spawn(command, {
15
+ cwd: options.cwd,
16
+ env: options.env == null ? process.env : { ...process.env, ...options.env },
17
+ shell: true,
18
+ stdio: 'inherit',
19
+ });
20
+
21
+ child.on('error', reject);
22
+ child.on('close', (code, signal) => {
23
+ resolve(code ?? (signal == null ? 1 : 128));
24
+ });
25
+ });
26
+ },
27
+ };
@@ -0,0 +1,58 @@
1
+ export type ColorMode = 'always' | 'auto' | 'never';
2
+ export type LogLevel = 'debug' | 'error' | 'info' | 'silent' | 'warn';
3
+
4
+ /**
5
+ * Rank order used to compare log levels, higher means more verbose.
6
+ */
7
+ const logLevelRank: Record<LogLevel, number> = {
8
+ debug: 4,
9
+ error: 1,
10
+ info: 3,
11
+ silent: 0,
12
+ warn: 2,
13
+ };
14
+
15
+ export interface CreateLoggerOptions {
16
+ readonly color?: ColorMode;
17
+ readonly level: LogLevel;
18
+ readonly stderr: NodeJS.WritableStream;
19
+ readonly stdout: NodeJS.WritableStream;
20
+ }
21
+
22
+ export interface Logger {
23
+ debug(message: string): void;
24
+ error(message: string): void;
25
+ info(message: string): void;
26
+ warn(message: string): void;
27
+ }
28
+
29
+ /**
30
+ * Creates a logger writing to `stream`, filtering out messages above the configured `level`.
31
+ */
32
+ export const Logger = {
33
+ create({ color = 'auto', level, stderr, stdout }: CreateLoggerOptions): Logger {
34
+ const write = (messageLevel: LogLevel, message: string) => {
35
+ if (!(logLevelRank[messageLevel] <= logLevelRank[level])) {
36
+ return;
37
+ }
38
+ // write to stderr for warnings and errors
39
+ const outputStream = logLevelRank[messageLevel] <= logLevelRank.warn ? stderr : stdout;
40
+ const shouldColor = color === 'always' || (color === 'auto' && 'isTTY' in outputStream && outputStream.isTTY === true);
41
+ const colorCode = {
42
+ debug: '\u{1B}[36m',
43
+ error: '\u{1B}[31m',
44
+ info: '\u{1B}[32m',
45
+ silent: '',
46
+ warn: '\u{1B}[33m',
47
+ }[messageLevel];
48
+ outputStream.write(`${shouldColor ? `${colorCode}${message}\u{1B}[0m` : message}\n`);
49
+ };
50
+
51
+ return {
52
+ debug: (message) => write('debug', message),
53
+ error: (message) => write('error', message),
54
+ info: (message) => write('info', message),
55
+ warn: (message) => write('warn', message),
56
+ };
57
+ },
58
+ };
@@ -0,0 +1,17 @@
1
+ import { ManagedScriptEnv } from '../ManagedScriptEnv.js';
2
+
3
+ export interface ScriptNameResolverOptions {
4
+ readonly env: NodeJS.ProcessEnv;
5
+ readonly scriptName: string | undefined;
6
+ }
7
+
8
+ /**
9
+ * Resolves the script name to run.
10
+ *
11
+ * Priority: first positional CLI argument > `MANAGED_SCRIPT_NAME` env var > `npm_lifecycle_event` env var.
12
+ */
13
+ export const ScriptNameResolver = {
14
+ resolve({ env, scriptName }: ScriptNameResolverOptions): string | undefined {
15
+ return scriptName ?? env[ManagedScriptEnv.Name] ?? env['npm_lifecycle_event'];
16
+ },
17
+ };
@@ -0,0 +1,70 @@
1
+ import type { UserConfig } from './type/UserConfig.js';
2
+
3
+ const DANGEROUS_KEYS = new Set(['__proto__', 'constructor', 'prototype']);
4
+
5
+ /**
6
+ * Merge two managed-script configs using the same heuristic as Vite and Vitest:
7
+ * later values win, plain objects merge recursively, and arrays concatenate.
8
+ *
9
+ * @param defaults
10
+ * @param overrides
11
+ * @example
12
+ * ```ts
13
+ * export default defineConfig(mergeConfig(
14
+ * { scripts: { build: 'echo base', test: 'echo test' } },
15
+ * { scripts: { build: 'echo override' } },
16
+ * ));
17
+ * ```
18
+ */
19
+ export function mergeConfig(defaults: UserConfig, overrides: UserConfig): UserConfig {
20
+ if (typeof defaults === 'function' || typeof overrides === 'function') {
21
+ throw new TypeError('Cannot merge config in form of callback');
22
+ }
23
+
24
+ return mergeConfigRecursively({ ...defaults }, { ...overrides });
25
+ }
26
+
27
+ function isObject(value: unknown): value is Record<string, unknown> {
28
+ return Object.prototype.toString.call(value) === '[object Object]';
29
+ }
30
+
31
+ function mergeConfigRecursively(
32
+ defaults: Record<string, unknown>,
33
+ overrides: Record<string, unknown>,
34
+ ): Record<string, unknown> {
35
+ const merged: Record<string, unknown> = { ...defaults };
36
+
37
+ for (const [key, value] of Object.entries(overrides)) {
38
+ if (DANGEROUS_KEYS.has(key)) {
39
+ continue;
40
+ }
41
+
42
+ if (value == null) {
43
+ continue;
44
+ }
45
+
46
+ const existing = merged[key];
47
+ if (existing == null) {
48
+ merged[key] = value;
49
+ continue;
50
+ }
51
+
52
+ if (Array.isArray(existing) || Array.isArray(value)) {
53
+ merged[key] = [...toArray(existing), ...toArray(value)];
54
+ continue;
55
+ }
56
+
57
+ if (isObject(existing) && isObject(value)) {
58
+ merged[key] = mergeConfigRecursively(existing, value);
59
+ continue;
60
+ }
61
+
62
+ merged[key] = value;
63
+ }
64
+
65
+ return merged;
66
+ }
67
+
68
+ function toArray<T>(target: Array<T> | T): Array<T> {
69
+ return Array.isArray(target) ? target : [target];
70
+ }
package/src/meta.ts ADDED
@@ -0,0 +1,12 @@
1
+ export const meta = Object.freeze({
2
+ binaryLabel: 'W5S Managed Script',
3
+ binaryName: 'managed-script',
4
+ // @ts-ignore - these variables are injected at build time
5
+ binaryVersion: (typeof __PACKAGE_VERSION__ === 'undefined' ? '' : __PACKAGE_VERSION__) as string,
6
+ // @ts-ignore - these variables are injected at build time
7
+ buildNumber: 1, // (typeof __PACKAGE_BUILD_NUMBER__ === 'undefined' ? 0 : __PACKAGE_BUILD_NUMBER__) as number,
8
+ // @ts-ignore - these variables are injected at build time
9
+ name: (typeof __PACKAGE_NAME__ === 'undefined' ? '' : __PACKAGE_NAME__) as string,
10
+ // @ts-ignore - these variables are injected at build time
11
+ version: (typeof __PACKAGE_VERSION__ === 'undefined' ? '' : __PACKAGE_VERSION__) as string,
12
+ });
@@ -0,0 +1,37 @@
1
+ import type { CommandContext } from './CommandContext.js';
2
+
3
+ export type ManagedScriptCommand =
4
+ | ManagedScriptCommand.RunScript;
5
+
6
+ export namespace ManagedScriptCommand {
7
+ export interface Base<TName extends string, TParameters extends object> {
8
+ /**
9
+ * Name of the command.
10
+ */
11
+ readonly _: TName;
12
+
13
+ /**
14
+ * Context in which the command is executed.
15
+ */
16
+ readonly context: CommandContext;
17
+
18
+ /**
19
+ * Parameters provided to the command.
20
+ */
21
+ readonly parameters: Readonly<TParameters>;
22
+ }
23
+
24
+ export type Parameters<T extends Base<string, object>> = Omit<T, '_'>;
25
+
26
+ export interface RunScript extends Base<'RunScript', {
27
+ scriptArgs?: ReadonlyArray<string>;
28
+ scriptName: string | undefined;
29
+ }> {}
30
+ }
31
+
32
+ export const ManagedScriptCommand = Object.freeze({
33
+ RunScript: (args: ManagedScriptCommand.Parameters<ManagedScriptCommand.RunScript>): ManagedScriptCommand.RunScript => ({
34
+ _: 'RunScript' as const,
35
+ ...args,
36
+ }),
37
+ });
@@ -0,0 +1,48 @@
1
+ import type { ColorMode, LogLevel } from '../internal/Logger.js';
2
+
3
+ export interface CommandContext {
4
+ /**
5
+ * Command-line arguments passed to the script.
6
+ */
7
+ readonly cli: {
8
+ color?: ColorMode | undefined;
9
+ cwd?: string | undefined;
10
+ dryRun?: boolean | undefined;
11
+ logLevel?: LogLevel | undefined;
12
+ };
13
+
14
+ /**
15
+ * Color mode used for status messages.
16
+ */
17
+ readonly color: ColorMode;
18
+
19
+ /**
20
+ * Current working directory of the script.
21
+ */
22
+ readonly cwd: string;
23
+
24
+ /**
25
+ * When `true`, the resolved command is printed instead of being executed.
26
+ */
27
+ readonly dryRun: boolean;
28
+
29
+ /**
30
+ * Environment variables available to the script.
31
+ */
32
+ readonly env: NodeJS.ProcessEnv;
33
+
34
+ /**
35
+ * Resolved log level used to filter status messages.
36
+ */
37
+ readonly logLevel: LogLevel;
38
+
39
+ /**
40
+ * Stream used to write error log messages.
41
+ */
42
+ readonly stderr: NodeJS.WritableStream;
43
+
44
+ /**
45
+ * Stream used to write standard log messages.
46
+ */
47
+ readonly stdout: NodeJS.WritableStream;
48
+ }
@@ -0,0 +1,18 @@
1
+ export interface ResolvedConfig {
2
+ readonly scripts: Record<string, {
3
+ /**
4
+ * Command to execute
5
+ */
6
+ readonly command: string;
7
+
8
+ /**
9
+ * File path of the configuration file that defined this script
10
+ */
11
+ readonly configDir: string | undefined;
12
+
13
+ /**
14
+ * Directory of the configuration file that defined this script
15
+ */
16
+ readonly configFile: string | undefined;
17
+ }>;
18
+ }
@@ -0,0 +1,5 @@
1
+ import type { InputConfig } from 'c12';
2
+
3
+ export interface UserConfig extends InputConfig<{
4
+ readonly scripts?: Record<string, string> | undefined;
5
+ }> {}
package/src/type.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from './type/Command.js';
2
+ export * from './type/CommandContext.js';
3
+ export * from './type/ResolvedConfig.js';
4
+ export * from './type/UserConfig.js';
@@ -0,0 +1 @@
1
+ export type { Options as ZXOptions } from 'zx';
@@ -0,0 +1 @@
1
+ export type { Shell as ZXShell } from 'zx';
@@ -0,0 +1,37 @@
1
+ import { log as zxLog } from 'zx';
2
+
3
+ import type { LogLevel } from '../internal/Logger.js';
4
+ import type { ZXOptions } from './ZXOptions.js';
5
+
6
+ import { Logger } from '../internal/Logger.js';
7
+ import { defaultContext } from '../ManagedScript/defaultContext.js';
8
+
9
+ export function defaultZXOptions(options: Partial<ZXOptions> & { logLevel?: LogLevel | undefined; stderr?: NodeJS.WritableStream | undefined; stdout?: NodeJS.WritableStream | undefined }): Partial<ZXOptions> {
10
+ const { cwd: optionsCwd, env: optionsEnv, logLevel: optionsLogLevel, stderr: optionsStderr, stdout: optionsStdout, ...optionsRest } = options;
11
+ const env = optionsEnv == null ? process.env : { ...process.env, ...optionsEnv };
12
+ const context = defaultContext({
13
+ env,
14
+ ...(optionsCwd == null ? {} : { cwd: optionsCwd }),
15
+ ...(optionsLogLevel == null ? {} : { logLevel: optionsLogLevel }),
16
+ ...(optionsStderr == null ? {} : { stderr: optionsStderr }),
17
+ ...(optionsStdout == null ? {} : { stdout: optionsStdout }),
18
+ });
19
+ const logger = Logger.create({ color: context.color, level: context.logLevel, stderr: context.stderr, stdout: context.stdout });
20
+
21
+ return {
22
+ cwd: context.cwd,
23
+ env: context.env,
24
+ log: (entry) => {
25
+ if (entry.kind === 'cmd') {
26
+ logger.info(entry.cmd);
27
+ return;
28
+ }
29
+ zxLog(entry);
30
+ },
31
+ preferLocal: true,
32
+ quiet: context.logLevel === 'silent',
33
+ stdio: 'inherit',
34
+ verbose: context.logLevel === 'debug',
35
+ ...optionsRest,
36
+ };
37
+ }
package/src/zx/zx.ts ADDED
@@ -0,0 +1,51 @@
1
+ import { $ as zxDefault } from 'zx';
2
+
3
+ import type { CommandContext } from '../type.js';
4
+ import type { ZXShell } from './ZXShell.js';
5
+
6
+ import { defaultZXOptions } from './defaultZXOptions.js';
7
+
8
+ export interface CreateZXOptions {
9
+ /**
10
+ * Working directory for spawned commands. Defaults to `MANAGED_SCRIPT_CWD`, then `process.cwd()`.
11
+ */
12
+ readonly cwd?: string | undefined;
13
+
14
+ /**
15
+ * Environment variables passed to spawned commands. Merged over `process.env`.
16
+ */
17
+ readonly env?: NodeJS.ProcessEnv | undefined;
18
+
19
+ /**
20
+ * Log level used to derive zx `verbose` / `quiet` and command instrumentation.
21
+ * Defaults to `MANAGED_SCRIPT_LOGLEVEL`, then `info`.
22
+ */
23
+ readonly logLevel?: CommandContext['logLevel'] | undefined;
24
+
25
+ /**
26
+ * Stream used to write instrumented command logs at the error and warn levels.
27
+ */
28
+ readonly stderr?: NodeJS.WritableStream | undefined;
29
+
30
+ /**
31
+ * Stream used to write instrumented command logs at the info and debug levels.
32
+ */
33
+ readonly stdout?: NodeJS.WritableStream | undefined;
34
+ }
35
+
36
+ /**
37
+ * Creates a zx `$` preset configured from managed-script context and environment variables.
38
+ *
39
+ * @param options
40
+ */
41
+ export function zx(options: CreateZXOptions = {}): ZXShell {
42
+ const env = options.env == null ? process.env : { ...process.env, ...options.env };
43
+
44
+ return zxDefault(defaultZXOptions({
45
+ env,
46
+ ...(options.cwd == null ? {} : { cwd: options.cwd }),
47
+ ...(options.logLevel == null ? {} : { logLevel: options.logLevel }),
48
+ ...(options.stderr == null ? {} : { stderr: options.stderr }),
49
+ ...(options.stdout == null ? {} : { stdout: options.stdout }),
50
+ }));
51
+ }
package/src/zx.ts ADDED
@@ -0,0 +1,19 @@
1
+ import type { ZXShell } from './zx/ZXShell.js';
2
+
3
+ import { zx } from './zx/zx.js';
4
+
5
+ export * from './zx/zx.js';
6
+ export type * from './zx/ZXOptions.js';
7
+ export type * from './zx/ZXShell.js';
8
+
9
+ /**
10
+ * Default `$` preset for scripts launched by managed-script.
11
+ * Reads `MANAGED_SCRIPT_*` from the process environment at import time.
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * import { $ } from '@w5s/managed-script/zx';
16
+ * await $`echo Hello, world!`;
17
+ * ```
18
+ */
19
+ export const $: ZXShell = zx();