@w5s/managed-script 1.0.0-alpha.4 → 1.0.0-alpha.5
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.
- package/README.md +37 -0
- package/dist/ManagedScript-B8d632d3.js +321 -0
- package/dist/ManagedScript-B8d632d3.js.map +1 -0
- package/dist/cli.js +27 -43
- package/dist/cli.js.map +1 -1
- package/dist/{defaultContext-BthxLDX3.js → defaultContext-Xl2iA7UM.js} +38 -9
- package/dist/defaultContext-Xl2iA7UM.js.map +1 -0
- package/dist/index.d.ts +27 -54
- package/dist/index.js +23 -8
- package/dist/index.js.map +1 -1
- package/dist/type-hvsy_R0f.d.ts +103 -0
- package/dist/zx.d.ts +45 -14
- package/dist/zx.js +47 -17
- package/dist/zx.js.map +1 -1
- package/package.json +3 -2
- package/src/ManagedScript/defaultContext.ts +2 -4
- package/src/ManagedScript/execute.ts +45 -24
- package/src/ManagedScript/resolveScripts.ts +52 -5
- package/src/cli/RootCommand.ts +2 -3
- package/src/internal/Executor.ts +27 -10
- package/src/internal/Logger.ts +10 -3
- package/src/internal/PackageScriptOverride.ts +43 -0
- package/src/internal/ScriptInvocation.ts +55 -0
- package/src/internal/ScriptNameResolver.ts +5 -4
- package/src/internal/Stdio.ts +41 -0
- package/src/internal/lazy.ts +7 -0
- package/src/mergeConfig.ts +28 -0
- package/src/type/CommandContext.ts +6 -7
- package/src/type/ResolvedConfig.ts +27 -8
- package/src/type/StdOptions.ts +1 -0
- package/src/type/UserConfig.ts +38 -1
- package/src/type.ts +1 -0
- package/src/zx/ZXOptions.ts +73 -1
- package/src/zx/ZXShell.ts +18 -1
- package/src/zx/defaults.ts +6 -0
- package/src/zx/isCI.ts +26 -0
- package/src/zx/zx.ts +7 -13
- package/src/zx.ts +2 -0
- package/dist/ManagedScript-CLp9a43o.js +0 -193
- package/dist/ManagedScript-CLp9a43o.js.map +0 -1
- package/dist/defaultContext-BthxLDX3.js.map +0 -1
- package/dist/type-DQiyX1q_.d.ts +0 -53
- package/src/zx/defaultZXOptions.ts +0 -58
|
@@ -1,4 +1,36 @@
|
|
|
1
|
-
|
|
1
|
+
import { Writable } from "node:stream";
|
|
2
|
+
// src/internal/Stdio.ts
|
|
3
|
+
function createSink() {
|
|
4
|
+
return new Writable({ write(_chunk, _encoding, callback) {
|
|
5
|
+
callback();
|
|
6
|
+
} });
|
|
7
|
+
}
|
|
8
|
+
function resolveEntry(entry, fd) {
|
|
9
|
+
const parent = fd === 1 ? process.stdout : process.stderr;
|
|
10
|
+
if (entry === "inherit" || entry === fd) return parent;
|
|
11
|
+
return typeof entry === "object" && entry != null && "write" in entry && typeof entry.write === "function" ? entry : createSink();
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Helpers for Node.js {@link StdioOptions}
|
|
15
|
+
*
|
|
16
|
+
* @namespace
|
|
17
|
+
*/
|
|
18
|
+
const Stdio = {
|
|
19
|
+
/**
|
|
20
|
+
* Resolve the writable streams (stdout is index 1, stderr is index 2) described by `stdio`.
|
|
21
|
+
*
|
|
22
|
+
* @param stdio
|
|
23
|
+
*/
|
|
24
|
+
resolve(stdio) {
|
|
25
|
+
return typeof stdio === "string" ? {
|
|
26
|
+
stderr: resolveEntry(stdio, 2),
|
|
27
|
+
stdout: resolveEntry(stdio, 1)
|
|
28
|
+
} : {
|
|
29
|
+
stderr: resolveEntry(stdio[2], 2),
|
|
30
|
+
stdout: resolveEntry(stdio[1], 1)
|
|
31
|
+
};
|
|
32
|
+
} };
|
|
33
|
+
// src/ManagedScriptEnv.ts
|
|
2
34
|
/**
|
|
3
35
|
* Enum for managed script environment variables.
|
|
4
36
|
*
|
|
@@ -30,10 +62,9 @@ const ManagedScriptEnv = Object.freeze({
|
|
|
30
62
|
*/
|
|
31
63
|
Name: "MANAGED_SCRIPT_NAME"
|
|
32
64
|
});
|
|
33
|
-
|
|
34
|
-
//#region src/ManagedScript/defaultContext.ts
|
|
65
|
+
// src/ManagedScript/defaultContext.ts
|
|
35
66
|
function defaultContext(context) {
|
|
36
|
-
const { cli = {}, color, cwd, dryRun, env = process.env, logLevel,
|
|
67
|
+
const { cli = {}, color, cwd, dryRun, env = process.env, logLevel, stdio = "inherit" } = context ?? {};
|
|
37
68
|
const envLogLevel = env[ManagedScriptEnv.LogLevel];
|
|
38
69
|
return {
|
|
39
70
|
cli,
|
|
@@ -42,8 +73,7 @@ function defaultContext(context) {
|
|
|
42
73
|
dryRun: dryRun ?? cli.dryRun ?? false,
|
|
43
74
|
env,
|
|
44
75
|
logLevel: logLevel ?? cli.logLevel ?? (isLogLevel(envLogLevel) ? envLogLevel : "info"),
|
|
45
|
-
|
|
46
|
-
stdout
|
|
76
|
+
stdio
|
|
47
77
|
};
|
|
48
78
|
}
|
|
49
79
|
const knownLogLevels = /* @__PURE__ */ new Set([
|
|
@@ -56,7 +86,6 @@ const knownLogLevels = /* @__PURE__ */ new Set([
|
|
|
56
86
|
function isLogLevel(value) {
|
|
57
87
|
return knownLogLevels.has(value);
|
|
58
88
|
}
|
|
59
|
-
|
|
60
|
-
export { ManagedScriptEnv as n, defaultContext as t };
|
|
89
|
+
export { ManagedScriptEnv as n, Stdio as r, defaultContext as t };
|
|
61
90
|
|
|
62
|
-
//# sourceMappingURL=defaultContext-
|
|
91
|
+
//# sourceMappingURL=defaultContext-Xl2iA7UM.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defaultContext-Xl2iA7UM.js","names":[],"sources":["../src/internal/Stdio.ts","../src/ManagedScriptEnv.ts","../src/ManagedScript/defaultContext.ts"],"sourcesContent":["import type { StdioOptions } from 'node:child_process';\n\nimport { Writable } from 'node:stream';\n\nexport interface StdioStreams {\n readonly stderr: NodeJS.WritableStream;\n readonly stdout: NodeJS.WritableStream;\n}\n\nfunction createSink(): NodeJS.WritableStream {\n return new Writable({\n write(_chunk, _encoding, callback) {\n callback();\n },\n });\n}\n\nfunction resolveEntry(entry: StdioOptions[number] | undefined, fd: 1 | 2): NodeJS.WritableStream {\n const parent = fd === 1 ? process.stdout : process.stderr;\n if (entry === 'inherit' || entry === fd) {\n return parent;\n }\n // `ignore`, `pipe`, `ipc`, other file descriptors and unset entries have no stream to write to\n return (typeof entry === 'object' && entry != null && 'write' in entry && typeof entry.write === 'function') ? entry as unknown as NodeJS.WritableStream : createSink();\n}\n\n/**\n * Helpers for Node.js {@link StdioOptions}\n *\n * @namespace\n */\nexport const Stdio = {\n /**\n * Resolve the writable streams (stdout is index 1, stderr is index 2) described by `stdio`.\n *\n * @param stdio\n */\n resolve(stdio: StdioOptions): StdioStreams {\n return typeof stdio === 'string' ? { stderr: resolveEntry(stdio, 2), stdout: resolveEntry(stdio, 1) } : { stderr: resolveEntry(stdio[2], 2), stdout: resolveEntry(stdio[1], 1) };\n },\n};\n","/**\n * Enum for managed script environment variables.\n *\n * @enum\n */\nexport const ManagedScriptEnv = Object.freeze({\n /**\n * The color mode used for messages.\n */\n Color: 'MANAGED_SCRIPT_COLOR',\n\n /**\n * The directory where the managed script configuration is located.\n */\n ConfigDir: 'MANAGED_SCRIPT_CONFIG_DIR',\n\n /**\n * The file where the managed script configuration is located.\n */\n ConfigFile: 'MANAGED_SCRIPT_CONFIG_FILE',\n\n /**\n * The invocation directory used to resolve configuration and spawn the script.\n */\n Cwd: 'MANAGED_SCRIPT_CWD',\n\n /**\n * The resolved log level.\n */\n LogLevel: 'MANAGED_SCRIPT_LOGLEVEL',\n\n /**\n * The name of the managed script.\n */\n Name: 'MANAGED_SCRIPT_NAME',\n});\nexport type ManagedScriptEnv = typeof ManagedScriptEnv[keyof typeof ManagedScriptEnv];\n","import type { LogLevel } from '../internal/Logger.js';\nimport type { CommandContext } from '../type.js';\n\nimport { ManagedScriptEnv } from '../ManagedScriptEnv.js';\n\nexport function defaultContext(context: Partial<CommandContext> | undefined): CommandContext {\n const {\n cli = {},\n color,\n cwd,\n dryRun,\n env = process.env,\n logLevel,\n stdio = 'inherit',\n } = context ?? {};\n const envLogLevel = env[ManagedScriptEnv.LogLevel];\n return {\n cli,\n color: color ?? cli.color ?? 'auto',\n cwd: cwd ?? cli.cwd ?? env[ManagedScriptEnv.Cwd] ?? process.cwd(),\n dryRun: dryRun ?? cli.dryRun ?? false,\n env,\n logLevel: logLevel ?? cli.logLevel ?? (isLogLevel(envLogLevel) ? envLogLevel : 'info'),\n stdio,\n };\n}\n\nconst knownLogLevels = new Set<LogLevel>(['debug', 'error', 'info', 'silent', 'warn']);\n\nfunction isLogLevel(value: string | undefined): value is LogLevel {\n return knownLogLevels.has(value as LogLevel);\n}\n"],"mappings":";;AASA,SAAS,aAAoC;CAC3C,OAAO,IAAI,SAAS,EAClB,MAAM,QAAQ,WAAW,UAAU;EACjC,SAAS;CACX,EACF,CAAC;AACH;AAEA,SAAS,aAAa,OAAyC,IAAkC;CAC/F,MAAM,SAAS,OAAO,IAAI,QAAQ,SAAS,QAAQ;CACnD,IAAI,UAAU,aAAa,UAAU,IACnC,OAAO;CAGT,OAAQ,OAAO,UAAU,YAAY,SAAS,QAAQ,WAAW,SAAS,OAAO,MAAM,UAAU,aAAc,QAA4C,WAAW;AACxK;;;;;;AAOA,MAAa,QAAQ;;;;;;AAMnB,QAAQ,OAAmC;CACzC,OAAO,OAAO,UAAU,WAAW;EAAE,QAAQ,aAAa,OAAO,CAAC;EAAG,QAAQ,aAAa,OAAO,CAAC;CAAE,IAAI;EAAE,QAAQ,aAAa,MAAM,IAAI,CAAC;EAAG,QAAQ,aAAa,MAAM,IAAI,CAAC;CAAE;AACjL,EACF;;;;;;;ACnCA,MAAa,mBAAmB,OAAO,OAAO;;;;CAI5C,OAAO;;;;CAKP,WAAW;;;;CAKX,YAAY;;;;CAKZ,KAAK;;;;CAKL,UAAU;;;;CAKV,MAAM;AACR,CAAC;;AC9BD,SAAgB,eAAe,SAA8D;CAC3F,MAAM,EACJ,MAAM,CAAC,GACP,OACA,KACA,QACA,MAAM,QAAQ,KACd,UACA,QAAQ,cACN,WAAW,CAAC;CAChB,MAAM,cAAc,IAAI,iBAAiB;CACzC,OAAO;EACL;EACA,OAAO,SAAS,IAAI,SAAS;EAC7B,KAAK,OAAO,IAAI,OAAO,IAAI,iBAAiB,QAAQ,QAAQ,IAAI;EAChE,QAAQ,UAAU,IAAI,UAAU;EAChC;EACA,UAAU,YAAY,IAAI,aAAa,WAAW,WAAW,IAAI,cAAc;EAC/E;CACF;AACF;AAEA,MAAM,iCAAiB,IAAI,IAAc;CAAC;CAAS;CAAS;CAAQ;CAAU;AAAM,CAAC;AAErF,SAAS,WAAW,OAA8C;CAChE,OAAO,eAAe,IAAI,KAAiB;AAC7C"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
//#region src/defineConfig.d.ts
|
|
1
|
+
import { a as RunnerScript$1, c as UserConfig, n as CommandContext, o as ScriptDefinition, r as StdioOptions, s as ShellScript$1, t as ManagedScriptCommand } from "./type-hvsy_R0f.js";
|
|
3
2
|
/**
|
|
4
3
|
* Define the configuration for the managed script.
|
|
5
4
|
*
|
|
@@ -12,53 +11,39 @@ import { n as UserConfig, t as CommandContext } from "./type-DQiyX1q_.js";
|
|
|
12
11
|
* ```
|
|
13
12
|
*/
|
|
14
13
|
export declare const defineConfig: import("c12").DefineConfig<UserConfig, import("c12").ConfigLayerMeta>;
|
|
15
|
-
//#endregion
|
|
16
|
-
//#region src/type/Command.d.ts
|
|
17
|
-
export type ManagedScriptCommand = ManagedScriptCommand.RunScript;
|
|
18
|
-
export declare namespace ManagedScriptCommand {
|
|
19
|
-
interface Base<TName extends string, TParameters extends object> {
|
|
20
|
-
/**
|
|
21
|
-
* Name of the command.
|
|
22
|
-
*/
|
|
23
|
-
readonly _: TName;
|
|
24
|
-
/**
|
|
25
|
-
* Context in which the command is executed.
|
|
26
|
-
*/
|
|
27
|
-
readonly context: CommandContext;
|
|
28
|
-
/**
|
|
29
|
-
* Parameters provided to the command.
|
|
30
|
-
*/
|
|
31
|
-
readonly parameters: Readonly<TParameters>;
|
|
32
|
-
}
|
|
33
|
-
type Parameters<T extends Base<string, object>> = Omit<T, '_'>;
|
|
34
|
-
interface RunScript extends Base<'RunScript', {
|
|
35
|
-
scriptArgs?: ReadonlyArray<string>;
|
|
36
|
-
scriptName: string | undefined;
|
|
37
|
-
}> {}
|
|
38
|
-
}
|
|
39
|
-
export declare const ManagedScriptCommand: Readonly<{
|
|
40
|
-
RunScript: (args: ManagedScriptCommand.Parameters<ManagedScriptCommand.RunScript>) => ManagedScriptCommand.RunScript;
|
|
41
|
-
}>;
|
|
42
|
-
//#endregion
|
|
43
|
-
//#region src/type/ResolvedConfig.d.ts
|
|
44
14
|
export interface ResolvedConfig {
|
|
45
|
-
readonly scripts: Record<string,
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
15
|
+
readonly scripts: Record<string, ResolvedConfig.Script>;
|
|
16
|
+
}
|
|
17
|
+
export declare namespace ResolvedConfig {
|
|
18
|
+
/**
|
|
19
|
+
* Direct executable invocation. Extra CLI arguments are appended to {@link args}.
|
|
20
|
+
*/
|
|
21
|
+
export interface RunnerScript extends Origin {
|
|
22
|
+
readonly args: ReadonlyArray<string>;
|
|
23
|
+
readonly run?: undefined;
|
|
24
|
+
readonly runner: string;
|
|
25
|
+
}
|
|
26
|
+
export type Script = RunnerScript | ShellScript;
|
|
27
|
+
/**
|
|
28
|
+
* Shell command. Extra CLI arguments are appended to the command string.
|
|
29
|
+
*/
|
|
30
|
+
export interface ShellScript extends Origin {
|
|
31
|
+
readonly args?: undefined;
|
|
32
|
+
readonly run: string;
|
|
33
|
+
readonly runner?: undefined;
|
|
34
|
+
}
|
|
35
|
+
interface Origin {
|
|
50
36
|
/**
|
|
51
|
-
*
|
|
37
|
+
* Directory of the configuration file that defined this script.
|
|
52
38
|
*/
|
|
53
39
|
readonly configDir: string | undefined;
|
|
54
40
|
/**
|
|
55
|
-
*
|
|
41
|
+
* File path of the configuration file that defined this script.
|
|
56
42
|
*/
|
|
57
43
|
readonly configFile: string | undefined;
|
|
58
|
-
}
|
|
44
|
+
}
|
|
45
|
+
export {};
|
|
59
46
|
}
|
|
60
|
-
//#endregion
|
|
61
|
-
//#region src/ManagedScript/execute.d.ts
|
|
62
47
|
declare const handlers: {
|
|
63
48
|
RunScript: (command: ManagedScriptCommand.RunScript) => Promise<number>;
|
|
64
49
|
};
|
|
@@ -78,19 +63,13 @@ type ExecuteCommandParameters<T extends ManagedScriptCommand> = Omit<Parameters<
|
|
|
78
63
|
* @param command
|
|
79
64
|
*/
|
|
80
65
|
declare function execute<T extends ManagedScriptCommand>(command: ExecuteCommand<T>): Promise<Awaited<ReturnType<typeof handlers[T['_']]>>>;
|
|
81
|
-
//#endregion
|
|
82
|
-
//#region src/ManagedScript/runScript.d.ts
|
|
83
66
|
declare function runScript(options: ExecuteCommandParameters<ManagedScriptCommand.RunScript>): Promise<number>;
|
|
84
|
-
//#endregion
|
|
85
|
-
//#region src/ManagedScript.d.ts
|
|
86
67
|
/**
|
|
87
68
|
* @namespace
|
|
88
69
|
*/
|
|
89
70
|
export declare const ManagedScript: Readonly<{
|
|
90
71
|
runScript: typeof runScript;
|
|
91
72
|
}>;
|
|
92
|
-
//#endregion
|
|
93
|
-
//#region src/ManagedScriptEnv.d.ts
|
|
94
73
|
/**
|
|
95
74
|
* Enum for managed script environment variables.
|
|
96
75
|
*
|
|
@@ -123,8 +102,6 @@ export declare const ManagedScriptEnv: Readonly<{
|
|
|
123
102
|
Name: "MANAGED_SCRIPT_NAME";
|
|
124
103
|
}>;
|
|
125
104
|
export type ManagedScriptEnv = typeof ManagedScriptEnv[keyof typeof ManagedScriptEnv];
|
|
126
|
-
//#endregion
|
|
127
|
-
//#region src/mergeConfig.d.ts
|
|
128
105
|
/**
|
|
129
106
|
* Merge two managed-script configs using the same heuristic as Vite and Vitest:
|
|
130
107
|
* later values win, plain objects merge recursively, and arrays concatenate.
|
|
@@ -140,8 +117,6 @@ export type ManagedScriptEnv = typeof ManagedScriptEnv[keyof typeof ManagedScrip
|
|
|
140
117
|
* ```
|
|
141
118
|
*/
|
|
142
119
|
export declare function mergeConfig(defaults: UserConfig, overrides: UserConfig): UserConfig;
|
|
143
|
-
//#endregion
|
|
144
|
-
//#region src/meta.d.ts
|
|
145
120
|
export declare const meta: Readonly<{
|
|
146
121
|
binaryLabel: "W5S Managed Script";
|
|
147
122
|
binaryName: "managed-script";
|
|
@@ -150,6 +125,4 @@ export declare const meta: Readonly<{
|
|
|
150
125
|
name: string;
|
|
151
126
|
version: string;
|
|
152
127
|
}>;
|
|
153
|
-
|
|
154
|
-
export { CommandContext, UserConfig };
|
|
155
|
-
//# sourceMappingURL=index.d.ts.map
|
|
128
|
+
export { CommandContext, ManagedScriptCommand, RunnerScript$1 as RunnerScript, ScriptDefinition, ShellScript$1 as ShellScript, type StdioOptions, UserConfig };
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { n as meta, t as ManagedScript } from "./ManagedScript-CLp9a43o.js";
|
|
2
|
-
import { n as ManagedScriptEnv } from "./defaultContext-BthxLDX3.js";
|
|
3
1
|
import { createDefineConfig } from "c12";
|
|
4
|
-
|
|
2
|
+
import { n as meta, t as ManagedScript } from "./ManagedScript-B8d632d3.js";
|
|
3
|
+
import { n as ManagedScriptEnv } from "./defaultContext-Xl2iA7UM.js";
|
|
4
|
+
// src/defineConfig.ts
|
|
5
5
|
/**
|
|
6
6
|
* Define the configuration for the managed script.
|
|
7
7
|
*
|
|
@@ -14,8 +14,7 @@ import { createDefineConfig } from "c12";
|
|
|
14
14
|
* ```
|
|
15
15
|
*/
|
|
16
16
|
const defineConfig = createDefineConfig();
|
|
17
|
-
|
|
18
|
-
//#region src/mergeConfig.ts
|
|
17
|
+
// src/mergeConfig.ts
|
|
19
18
|
const DANGEROUS_KEYS = /* @__PURE__ */ new Set([
|
|
20
19
|
"__proto__",
|
|
21
20
|
"constructor",
|
|
@@ -52,6 +51,10 @@ function mergeConfigRecursively(defaults, overrides) {
|
|
|
52
51
|
merged[key] = value;
|
|
53
52
|
continue;
|
|
54
53
|
}
|
|
54
|
+
if (key === "scripts" && isObject(existing) && isObject(value)) {
|
|
55
|
+
merged[key] = replaceEntries(existing, value);
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
55
58
|
if (Array.isArray(existing) || Array.isArray(value)) {
|
|
56
59
|
merged[key] = [...toArray(existing), ...toArray(value)];
|
|
57
60
|
continue;
|
|
@@ -64,16 +67,28 @@ function mergeConfigRecursively(defaults, overrides) {
|
|
|
64
67
|
}
|
|
65
68
|
return merged;
|
|
66
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* Script definitions replace each other. A runner's `args` array is not concatenated with the previous one.
|
|
72
|
+
*
|
|
73
|
+
* @param defaults
|
|
74
|
+
* @param overrides
|
|
75
|
+
*/
|
|
76
|
+
function replaceEntries(defaults, overrides) {
|
|
77
|
+
const merged = { ...defaults };
|
|
78
|
+
for (const [key, value] of Object.entries(overrides)) {
|
|
79
|
+
if (DANGEROUS_KEYS.has(key) || value == null) continue;
|
|
80
|
+
merged[key] = value;
|
|
81
|
+
}
|
|
82
|
+
return merged;
|
|
83
|
+
}
|
|
67
84
|
function toArray(target) {
|
|
68
85
|
return Array.isArray(target) ? target : [target];
|
|
69
86
|
}
|
|
70
|
-
|
|
71
|
-
//#region src/type/Command.ts
|
|
87
|
+
// src/type/Command.ts
|
|
72
88
|
const ManagedScriptCommand = Object.freeze({ RunScript: (args) => ({
|
|
73
89
|
_: "RunScript",
|
|
74
90
|
...args
|
|
75
91
|
}) });
|
|
76
|
-
//#endregion
|
|
77
92
|
export { ManagedScript, ManagedScriptCommand, ManagedScriptEnv, defineConfig, mergeConfig, meta };
|
|
78
93
|
|
|
79
94
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/defineConfig.ts","../src/mergeConfig.ts","../src/type/Command.ts"],"sourcesContent":["import { createDefineConfig } from 'c12';\n\nimport type { UserConfig } from './type/UserConfig.js';\n\n/**\n * Define the configuration for the managed script.\n *\n * @example\n * ```ts\n * // managed-script.config.ts or @acme/managed-script-config/index.ts\n * export default defineConfig({\n * // your configuration here\n * });\n * ```\n */\nexport const defineConfig = createDefineConfig<UserConfig>();\n","import type { UserConfig } from './type/UserConfig.js';\n\nconst DANGEROUS_KEYS = new Set(['__proto__', 'constructor', 'prototype']);\n\n/**\n * Merge two managed-script configs using the same heuristic as Vite and Vitest:\n * later values win, plain objects merge recursively, and arrays concatenate.\n *\n * @param defaults\n * @param overrides\n * @example\n * ```ts\n * export default defineConfig(mergeConfig(\n * { scripts: { build: 'echo base', test: 'echo test' } },\n * { scripts: { build: 'echo override' } },\n * ));\n * ```\n */\nexport function mergeConfig(defaults: UserConfig, overrides: UserConfig): UserConfig {\n if (typeof defaults === 'function' || typeof overrides === 'function') {\n throw new TypeError('Cannot merge config in form of callback');\n }\n\n return mergeConfigRecursively({ ...defaults }, { ...overrides });\n}\n\nfunction isObject(value: unknown): value is Record<string, unknown> {\n return Object.prototype.toString.call(value) === '[object Object]';\n}\n\nfunction mergeConfigRecursively(\n defaults: Record<string, unknown>,\n overrides: Record<string, unknown>,\n): Record<string, unknown> {\n const merged: Record<string, unknown> = { ...defaults };\n\n for (const [key, value] of Object.entries(overrides)) {\n if (DANGEROUS_KEYS.has(key)) {\n continue;\n }\n\n if (value == null) {\n continue;\n }\n\n const existing = merged[key];\n if (existing == null) {\n merged[key] = value;\n continue;\n }\n\n if (Array.isArray(existing) || Array.isArray(value)) {\n merged[key] = [...toArray(existing), ...toArray(value)];\n continue;\n }\n\n if (isObject(existing) && isObject(value)) {\n merged[key] = mergeConfigRecursively(existing, value);\n continue;\n }\n\n merged[key] = value;\n }\n\n return merged;\n}\n\nfunction toArray<T>(target: Array<T> | T): Array<T> {\n return Array.isArray(target) ? target : [target];\n}\n","import type { CommandContext } from './CommandContext.js';\n\nexport type ManagedScriptCommand =\n | ManagedScriptCommand.RunScript;\n\nexport namespace ManagedScriptCommand {\n export interface Base<TName extends string, TParameters extends object> {\n /**\n * Name of the command.\n */\n readonly _: TName;\n\n /**\n * Context in which the command is executed.\n */\n readonly context: CommandContext;\n\n /**\n * Parameters provided to the command.\n */\n readonly parameters: Readonly<TParameters>;\n }\n\n export type Parameters<T extends Base<string, object>> = Omit<T, '_'>;\n\n export interface RunScript extends Base<'RunScript', {\n scriptArgs?: ReadonlyArray<string>;\n scriptName: string | undefined;\n }> {}\n}\n\nexport const ManagedScriptCommand = Object.freeze({\n RunScript: (args: ManagedScriptCommand.Parameters<ManagedScriptCommand.RunScript>): ManagedScriptCommand.RunScript => ({\n _: 'RunScript' as const,\n ...args,\n }),\n});\n"],"mappings":";;;;;;;;;;;;;;;AAeA,MAAa,eAAe,mBAA+B
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/defineConfig.ts","../src/mergeConfig.ts","../src/type/Command.ts"],"sourcesContent":["import { createDefineConfig } from 'c12';\n\nimport type { UserConfig } from './type/UserConfig.js';\n\n/**\n * Define the configuration for the managed script.\n *\n * @example\n * ```ts\n * // managed-script.config.ts or @acme/managed-script-config/index.ts\n * export default defineConfig({\n * // your configuration here\n * });\n * ```\n */\nexport const defineConfig = createDefineConfig<UserConfig>();\n","import type { UserConfig } from './type/UserConfig.js';\n\nconst DANGEROUS_KEYS = new Set(['__proto__', 'constructor', 'prototype']);\n\n/**\n * Merge two managed-script configs using the same heuristic as Vite and Vitest:\n * later values win, plain objects merge recursively, and arrays concatenate.\n *\n * @param defaults\n * @param overrides\n * @example\n * ```ts\n * export default defineConfig(mergeConfig(\n * { scripts: { build: 'echo base', test: 'echo test' } },\n * { scripts: { build: 'echo override' } },\n * ));\n * ```\n */\nexport function mergeConfig(defaults: UserConfig, overrides: UserConfig): UserConfig {\n if (typeof defaults === 'function' || typeof overrides === 'function') {\n throw new TypeError('Cannot merge config in form of callback');\n }\n\n return mergeConfigRecursively({ ...defaults }, { ...overrides });\n}\n\nfunction isObject(value: unknown): value is Record<string, unknown> {\n return Object.prototype.toString.call(value) === '[object Object]';\n}\n\nfunction mergeConfigRecursively(\n defaults: Record<string, unknown>,\n overrides: Record<string, unknown>,\n): Record<string, unknown> {\n const merged: Record<string, unknown> = { ...defaults };\n\n for (const [key, value] of Object.entries(overrides)) {\n if (DANGEROUS_KEYS.has(key)) {\n continue;\n }\n\n if (value == null) {\n continue;\n }\n\n const existing = merged[key];\n if (existing == null) {\n merged[key] = value;\n continue;\n }\n\n if (key === 'scripts' && isObject(existing) && isObject(value)) {\n merged[key] = replaceEntries(existing, value);\n continue;\n }\n\n if (Array.isArray(existing) || Array.isArray(value)) {\n merged[key] = [...toArray(existing), ...toArray(value)];\n continue;\n }\n\n if (isObject(existing) && isObject(value)) {\n merged[key] = mergeConfigRecursively(existing, value);\n continue;\n }\n\n merged[key] = value;\n }\n\n return merged;\n}\n\n/**\n * Script definitions replace each other. A runner's `args` array is not concatenated with the previous one.\n *\n * @param defaults\n * @param overrides\n */\nfunction replaceEntries(\n defaults: Record<string, unknown>,\n overrides: Record<string, unknown>,\n): Record<string, unknown> {\n const merged: Record<string, unknown> = { ...defaults };\n\n for (const [key, value] of Object.entries(overrides)) {\n if (DANGEROUS_KEYS.has(key) || value == null) {\n continue;\n }\n\n merged[key] = value;\n }\n\n return merged;\n}\n\nfunction toArray<T>(target: Array<T> | T): Array<T> {\n return Array.isArray(target) ? target : [target];\n}\n","import type { CommandContext } from './CommandContext.js';\n\nexport type ManagedScriptCommand =\n | ManagedScriptCommand.RunScript;\n\nexport namespace ManagedScriptCommand {\n export interface Base<TName extends string, TParameters extends object> {\n /**\n * Name of the command.\n */\n readonly _: TName;\n\n /**\n * Context in which the command is executed.\n */\n readonly context: CommandContext;\n\n /**\n * Parameters provided to the command.\n */\n readonly parameters: Readonly<TParameters>;\n }\n\n export type Parameters<T extends Base<string, object>> = Omit<T, '_'>;\n\n export interface RunScript extends Base<'RunScript', {\n scriptArgs?: ReadonlyArray<string>;\n scriptName: string | undefined;\n }> {}\n}\n\nexport const ManagedScriptCommand = Object.freeze({\n RunScript: (args: ManagedScriptCommand.Parameters<ManagedScriptCommand.RunScript>): ManagedScriptCommand.RunScript => ({\n _: 'RunScript' as const,\n ...args,\n }),\n});\n"],"mappings":";;;;;;;;;;;;;;;AAeA,MAAa,eAAe,mBAA+B;;ACb3D,MAAM,iCAAiB,IAAI,IAAI;CAAC;CAAa;CAAe;AAAW,CAAC;;;;;;;;;;;;;;;AAgBxE,SAAgB,YAAY,UAAsB,WAAmC;CACnF,IAAI,OAAO,aAAa,cAAc,OAAO,cAAc,YACzD,MAAM,IAAI,UAAU,yCAAyC;CAG/D,OAAO,uBAAuB,EAAE,GAAG,SAAS,GAAG,EAAE,GAAG,UAAU,CAAC;AACjE;AAEA,SAAS,SAAS,OAAkD;CAClE,OAAO,OAAO,UAAU,SAAS,KAAK,KAAK,MAAM;AACnD;AAEA,SAAS,uBACP,UACA,WACyB;CACzB,MAAM,SAAkC,EAAE,GAAG,SAAS;CAEtD,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,SAAS,GAAG;EACpD,IAAI,eAAe,IAAI,GAAG,GACxB;EAGF,IAAI,SAAS,MACX;EAGF,MAAM,WAAW,OAAO;EACxB,IAAI,YAAY,MAAM;GACpB,OAAO,OAAO;GACd;EACF;EAEA,IAAI,QAAQ,aAAa,SAAS,QAAQ,KAAK,SAAS,KAAK,GAAG;GAC9D,OAAO,OAAO,eAAe,UAAU,KAAK;GAC5C;EACF;EAEA,IAAI,MAAM,QAAQ,QAAQ,KAAK,MAAM,QAAQ,KAAK,GAAG;GACnD,OAAO,OAAO,CAAC,GAAG,QAAQ,QAAQ,GAAG,GAAG,QAAQ,KAAK,CAAC;GACtD;EACF;EAEA,IAAI,SAAS,QAAQ,KAAK,SAAS,KAAK,GAAG;GACzC,OAAO,OAAO,uBAAuB,UAAU,KAAK;GACpD;EACF;EAEA,OAAO,OAAO;CAChB;CAEA,OAAO;AACT;;;;;;;AAQA,SAAS,eACP,UACA,WACyB;CACzB,MAAM,SAAkC,EAAE,GAAG,SAAS;CAEtD,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,SAAS,GAAG;EACpD,IAAI,eAAe,IAAI,GAAG,KAAK,SAAS,MACtC;EAGF,OAAO,OAAO;CAChB;CAEA,OAAO;AACT;AAEA,SAAS,QAAW,QAAgC;CAClD,OAAO,MAAM,QAAQ,MAAM,IAAI,SAAS,CAAC,MAAM;AACjD;;AClEA,MAAa,uBAAuB,OAAO,OAAO,EAChD,YAAY,UAA2G;CACrH,GAAG;CACH,GAAG;AACL,GACF,CAAC"}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { InputConfig } from "c12";
|
|
2
|
+
import { StdioOptions } from "node:child_process";
|
|
3
|
+
/**
|
|
4
|
+
* Run an executable directly, in the same way a lefthook script uses `runner` and `args`.
|
|
5
|
+
*
|
|
6
|
+
* `runner` is the program, such as `node` or `bash`. `args` are its arguments.
|
|
7
|
+
* Arguments passed to the managed-script CLI are appended after `args`.
|
|
8
|
+
* The process is spawned directly, so the shell does not parse the arguments.
|
|
9
|
+
*/
|
|
10
|
+
interface RunnerScript {
|
|
11
|
+
/**
|
|
12
|
+
* Arguments passed to {@link RunnerScript.runner}.
|
|
13
|
+
*/
|
|
14
|
+
readonly args?: ReadonlyArray<string> | undefined;
|
|
15
|
+
readonly run?: undefined;
|
|
16
|
+
/**
|
|
17
|
+
* Executable name or path. A multi-word command belongs in {@link RunnerScript.args}:
|
|
18
|
+
* `{ runner: "go", args: ["run", "main.go"] }`.
|
|
19
|
+
*/
|
|
20
|
+
readonly runner: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* A shell command string (shorthand for `{ run }`), a {@link ShellScript} or a direct {@link RunnerScript}.
|
|
24
|
+
*/
|
|
25
|
+
type ScriptDefinition = RunnerScript | ShellScript | string;
|
|
26
|
+
/**
|
|
27
|
+
* Run a shell command, in the same way as a lefthook `run`.
|
|
28
|
+
* Arguments passed to the managed-script CLI are appended (shell-quoted).
|
|
29
|
+
*/
|
|
30
|
+
interface ShellScript {
|
|
31
|
+
readonly args?: undefined;
|
|
32
|
+
readonly run: string;
|
|
33
|
+
readonly runner?: undefined;
|
|
34
|
+
}
|
|
35
|
+
interface UserConfig extends InputConfig<{
|
|
36
|
+
readonly scripts?: Record<string, ScriptDefinition> | undefined;
|
|
37
|
+
}> {}
|
|
38
|
+
type ColorMode = 'always' | 'auto' | 'never';
|
|
39
|
+
type LogLevel = 'debug' | 'error' | 'info' | 'silent' | 'warn';
|
|
40
|
+
interface CommandContext {
|
|
41
|
+
/**
|
|
42
|
+
* Command-line arguments passed to the script.
|
|
43
|
+
*/
|
|
44
|
+
readonly cli: {
|
|
45
|
+
color?: ColorMode | undefined;
|
|
46
|
+
cwd?: string | undefined;
|
|
47
|
+
dryRun?: boolean | undefined;
|
|
48
|
+
logLevel?: LogLevel | undefined;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Color mode used for status messages.
|
|
52
|
+
*/
|
|
53
|
+
readonly color: ColorMode;
|
|
54
|
+
/**
|
|
55
|
+
* Current working directory of the script.
|
|
56
|
+
*/
|
|
57
|
+
readonly cwd: string;
|
|
58
|
+
/**
|
|
59
|
+
* When `true`, the resolved command is printed instead of being executed.
|
|
60
|
+
*/
|
|
61
|
+
readonly dryRun: boolean;
|
|
62
|
+
/**
|
|
63
|
+
* Environment variables available to the script.
|
|
64
|
+
*/
|
|
65
|
+
readonly env: NodeJS.ProcessEnv;
|
|
66
|
+
/**
|
|
67
|
+
* Resolved log level used to filter status messages.
|
|
68
|
+
*/
|
|
69
|
+
readonly logLevel: LogLevel;
|
|
70
|
+
/**
|
|
71
|
+
* Standard I/O configuration. Index `1` is used to write standard output (resolved command)
|
|
72
|
+
* and index `2` to write status messages (stderr).
|
|
73
|
+
*
|
|
74
|
+
* @default 'inherit'
|
|
75
|
+
*/
|
|
76
|
+
readonly stdio: StdioOptions;
|
|
77
|
+
}
|
|
78
|
+
type ManagedScriptCommand = ManagedScriptCommand.RunScript;
|
|
79
|
+
declare namespace ManagedScriptCommand {
|
|
80
|
+
interface Base<TName extends string, TParameters extends object> {
|
|
81
|
+
/**
|
|
82
|
+
* Name of the command.
|
|
83
|
+
*/
|
|
84
|
+
readonly _: TName;
|
|
85
|
+
/**
|
|
86
|
+
* Context in which the command is executed.
|
|
87
|
+
*/
|
|
88
|
+
readonly context: CommandContext;
|
|
89
|
+
/**
|
|
90
|
+
* Parameters provided to the command.
|
|
91
|
+
*/
|
|
92
|
+
readonly parameters: Readonly<TParameters>;
|
|
93
|
+
}
|
|
94
|
+
type Parameters<T extends Base<string, object>> = Omit<T, '_'>;
|
|
95
|
+
interface RunScript extends Base<'RunScript', {
|
|
96
|
+
scriptArgs?: ReadonlyArray<string>;
|
|
97
|
+
scriptName: string | undefined;
|
|
98
|
+
}> {}
|
|
99
|
+
}
|
|
100
|
+
declare const ManagedScriptCommand: Readonly<{
|
|
101
|
+
RunScript: (args: ManagedScriptCommand.Parameters<ManagedScriptCommand.RunScript>) => ManagedScriptCommand.RunScript;
|
|
102
|
+
}>;
|
|
103
|
+
export { RunnerScript as a, UserConfig as c, LogLevel as i, CommandContext as n, ScriptDefinition as o, StdioOptions as r, ShellScript as s, ManagedScriptCommand as t };
|
package/dist/zx.d.ts
CHANGED
|
@@ -1,6 +1,45 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import { Options as ZXDefaultOptions, ProcessOutput, ProcessPromise } from "zx";
|
|
2
|
+
import { i as LogLevel, n as CommandContext, r as StdioOptions } from "./type-hvsy_R0f.js";
|
|
3
|
+
interface ZXOptions extends ZXDefaultOptions {
|
|
4
|
+
/**
|
|
5
|
+
*
|
|
6
|
+
*/
|
|
7
|
+
logLevel?: LogLevel | undefined;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* @namespace
|
|
11
|
+
*/
|
|
12
|
+
declare const ZXOptions: {
|
|
13
|
+
create(options: Partial<ZXOptions>): Partial<ZXOptions>;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Shell interface with some extensions
|
|
17
|
+
*/
|
|
18
|
+
interface ZXShell<S = false, R = S extends true ? ProcessOutput : ProcessPromise> {
|
|
19
|
+
(pieces: TemplateStringsArray, ...args: Array<any>): R;
|
|
20
|
+
<O extends Partial<ZXOptions> = Partial<ZXOptions>, R = O extends {
|
|
21
|
+
sync: true;
|
|
22
|
+
} ? ZXShell<true> : ZXShell>(opts: O): R;
|
|
23
|
+
sync: {
|
|
24
|
+
(pieces: TemplateStringsArray, ...args: Array<any>): ProcessOutput;
|
|
25
|
+
(opts: Partial<Omit<ZXOptions, 'sync'>>): ZXShell<true>;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Default ZX options for the managed script environment.
|
|
30
|
+
*/
|
|
31
|
+
export declare const defaults: Partial<ZXOptions>;
|
|
32
|
+
/**
|
|
33
|
+
* Determines if the current environment is a CI (Continuous Integration) environment.
|
|
34
|
+
* Checks the `CI` and `CONTINUOUS_INTEGRATION` environment variables to determine if the current environment is a CI environment.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* const ci = isCI();
|
|
39
|
+
* ```
|
|
40
|
+
* @returns `true` if the current environment is a CI environment, otherwise `false`
|
|
41
|
+
*/
|
|
42
|
+
export declare const isCI: () => boolean;
|
|
4
43
|
export interface CreateZXOptions {
|
|
5
44
|
/**
|
|
6
45
|
* Working directory for spawned commands. Defaults to `MANAGED_SCRIPT_CWD`, then `process.cwd()`.
|
|
@@ -16,13 +55,9 @@ export interface CreateZXOptions {
|
|
|
16
55
|
*/
|
|
17
56
|
readonly logLevel?: CommandContext['logLevel'] | undefined;
|
|
18
57
|
/**
|
|
19
|
-
*
|
|
20
|
-
*/
|
|
21
|
-
readonly stderr?: NodeJS.WritableStream | undefined;
|
|
22
|
-
/**
|
|
23
|
-
* Stream used to write instrumented command logs at the info and debug levels.
|
|
58
|
+
* Standard I/O used to write instrumented command logs (index `1` for command lines and output, index `2` for the rest).
|
|
24
59
|
*/
|
|
25
|
-
readonly
|
|
60
|
+
readonly stdio?: StdioOptions | undefined;
|
|
26
61
|
}
|
|
27
62
|
/**
|
|
28
63
|
* Creates a zx `$` preset configured from managed-script context and environment variables.
|
|
@@ -30,8 +65,6 @@ export interface CreateZXOptions {
|
|
|
30
65
|
* @param options
|
|
31
66
|
*/
|
|
32
67
|
export declare function zx(options?: CreateZXOptions): ZXShell;
|
|
33
|
-
//#endregion
|
|
34
|
-
//#region src/zx.d.ts
|
|
35
68
|
/**
|
|
36
69
|
* Default `$` preset for scripts launched by managed-script.
|
|
37
70
|
* Reads `MANAGED_SCRIPT_*` from the process environment at import time.
|
|
@@ -43,6 +76,4 @@ export declare function zx(options?: CreateZXOptions): ZXShell;
|
|
|
43
76
|
* ```
|
|
44
77
|
*/
|
|
45
78
|
export declare const $: ZXShell;
|
|
46
|
-
|
|
47
|
-
export type { ZXOptions, ZXShell };
|
|
48
|
-
//# sourceMappingURL=zx.d.ts.map
|
|
79
|
+
export type { ZXDefaultOptions, ZXOptions, ZXShell };
|
package/dist/zx.js
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
import { t as defaultContext } from "./defaultContext-BthxLDX3.js";
|
|
2
1
|
import { $ as $$1, chalk, log } from "zx";
|
|
3
2
|
import { stripVTControlCharacters } from "node:util";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
import { r as Stdio, t as defaultContext } from "./defaultContext-Xl2iA7UM.js";
|
|
4
|
+
import { bool, cleanEnv } from "envalid";
|
|
5
|
+
// src/zx/ZXOptions.ts
|
|
6
|
+
/**
|
|
7
|
+
* @namespace
|
|
8
|
+
*/
|
|
9
|
+
const ZXOptions = { create(options) {
|
|
10
|
+
const { cwd: optionsCwd, env: optionsEnv, logLevel: optionsLogLevel, stdio: optionsStdio, ...optionsRest } = options;
|
|
7
11
|
const env = optionsEnv == null ? process.env : {
|
|
8
12
|
...process.env,
|
|
9
13
|
...optionsEnv
|
|
@@ -12,14 +16,14 @@ function defaultZXOptions(options) {
|
|
|
12
16
|
env,
|
|
13
17
|
...optionsCwd == null ? {} : { cwd: optionsCwd },
|
|
14
18
|
...optionsLogLevel == null ? {} : { logLevel: optionsLogLevel },
|
|
15
|
-
...
|
|
16
|
-
...optionsStdout == null ? {} : { stdout: optionsStdout }
|
|
19
|
+
...optionsStdio == null ? {} : { stdio: optionsStdio }
|
|
17
20
|
});
|
|
21
|
+
const { stderr, stdout } = Stdio.resolve(context.stdio);
|
|
18
22
|
return {
|
|
19
23
|
cwd: context.cwd,
|
|
20
24
|
env: context.env,
|
|
21
25
|
log: (entry) => {
|
|
22
|
-
const target = entry.kind === "cmd" || entry.kind === "stdout" ?
|
|
26
|
+
const target = entry.kind === "cmd" || entry.kind === "stdout" ? stdout : stderr;
|
|
23
27
|
const colorEnabled = context.color === "always" || context.color === "auto" && autoColor(context.env, target);
|
|
24
28
|
const previousOutput = log.output;
|
|
25
29
|
const previousLevel = chalk.level;
|
|
@@ -42,14 +46,13 @@ function defaultZXOptions(options) {
|
|
|
42
46
|
verbose: context.logLevel === "debug",
|
|
43
47
|
...optionsRest
|
|
44
48
|
};
|
|
45
|
-
}
|
|
49
|
+
} };
|
|
46
50
|
function autoColor(env, stream) {
|
|
47
51
|
if (env["NO_COLOR"] != null && env["NO_COLOR"] !== "") return false;
|
|
48
52
|
const force = env["FORCE_COLOR"];
|
|
49
53
|
return force == null ? stream.isTTY === true : force !== "0" && force !== "false";
|
|
50
54
|
}
|
|
51
|
-
|
|
52
|
-
//#region src/zx/zx.ts
|
|
55
|
+
// src/zx/zx.ts
|
|
53
56
|
/**
|
|
54
57
|
* Creates a zx `$` preset configured from managed-script context and environment variables.
|
|
55
58
|
*
|
|
@@ -60,16 +63,44 @@ function zx(options = {}) {
|
|
|
60
63
|
...process.env,
|
|
61
64
|
...options.env
|
|
62
65
|
};
|
|
63
|
-
return $$1(
|
|
66
|
+
return $$1(ZXOptions.create({
|
|
64
67
|
env,
|
|
65
68
|
...options.cwd == null ? {} : { cwd: options.cwd },
|
|
66
69
|
...options.logLevel == null ? {} : { logLevel: options.logLevel },
|
|
67
|
-
...options.
|
|
68
|
-
...options.stdout == null ? {} : { stdout: options.stdout }
|
|
70
|
+
...options.stdio == null ? {} : { stdio: options.stdio }
|
|
69
71
|
}));
|
|
70
72
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
+
// src/zx/defaults.ts
|
|
74
|
+
/**
|
|
75
|
+
* Default ZX options for the managed script environment.
|
|
76
|
+
*/
|
|
77
|
+
const defaults = ZXOptions.create({});
|
|
78
|
+
// src/internal/lazy.ts
|
|
79
|
+
function lazy(fn) {
|
|
80
|
+
let cached;
|
|
81
|
+
return () => {
|
|
82
|
+
cached ??= fn();
|
|
83
|
+
return cached;
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
// src/zx/isCI.ts
|
|
87
|
+
const envSchema = {
|
|
88
|
+
CI: bool({ default: false }),
|
|
89
|
+
CONTINUOUS_INTEGRATION: bool({ default: false })
|
|
90
|
+
};
|
|
91
|
+
const getEnvClean = lazy(() => cleanEnv(process.env, envSchema, { reporter: null }));
|
|
92
|
+
/**
|
|
93
|
+
* Determines if the current environment is a CI (Continuous Integration) environment.
|
|
94
|
+
* Checks the `CI` and `CONTINUOUS_INTEGRATION` environment variables to determine if the current environment is a CI environment.
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```ts
|
|
98
|
+
* const ci = isCI();
|
|
99
|
+
* ```
|
|
100
|
+
* @returns `true` if the current environment is a CI environment, otherwise `false`
|
|
101
|
+
*/
|
|
102
|
+
const isCI = lazy(() => getEnvClean().CI || getEnvClean().CONTINUOUS_INTEGRATION);
|
|
103
|
+
// src/zx.ts
|
|
73
104
|
/**
|
|
74
105
|
* Default `$` preset for scripts launched by managed-script.
|
|
75
106
|
* Reads `MANAGED_SCRIPT_*` from the process environment at import time.
|
|
@@ -81,7 +112,6 @@ function zx(options = {}) {
|
|
|
81
112
|
* ```
|
|
82
113
|
*/
|
|
83
114
|
const $ = zx();
|
|
84
|
-
|
|
85
|
-
export { $, zx };
|
|
115
|
+
export { $, defaults, isCI, zx };
|
|
86
116
|
|
|
87
117
|
//# sourceMappingURL=zx.js.map
|
package/dist/zx.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zx.js","names":["zxLog","
|
|
1
|
+
{"version":3,"file":"zx.js","names":["zxLog","zxFn"],"sources":["../src/zx/ZXOptions.ts","../src/zx/zx.ts","../src/zx/defaults.ts","../src/internal/lazy.ts","../src/zx/isCI.ts","../src/zx.ts"],"sourcesContent":["import { stripVTControlCharacters } from 'node:util';\nimport { chalk, log as zxLog } from 'zx';\n\nimport type { LogLevel } from '../internal/Logger.js';\nimport type { ZXDefaultOptions } from '../zx.js';\n\nimport { Stdio } from '../internal/Stdio.js';\nimport { defaultContext } from '../ManagedScript/defaultContext.js';\n\nexport type { Options as ZXDefaultOptions } from 'zx';\n\nexport interface ZXOptions extends ZXDefaultOptions {\n /**\n *\n */\n logLevel?: LogLevel | undefined;\n}\n\n/**\n * @namespace\n */\nexport const ZXOptions = {\n create(options: Partial<ZXOptions>): Partial<ZXOptions> {\n const { cwd: optionsCwd, env: optionsEnv, logLevel: optionsLogLevel, stdio: optionsStdio, ...optionsRest } = options;\n const env = optionsEnv == null ? process.env : { ...process.env, ...optionsEnv };\n const context = defaultContext({\n env,\n ...(optionsCwd == null ? {} : { cwd: optionsCwd }),\n ...(optionsLogLevel == null ? {} : { logLevel: optionsLogLevel }),\n ...(optionsStdio == null ? {} : { stdio: optionsStdio }),\n });\n const { stderr, stdout } = Stdio.resolve(context.stdio);\n\n return {\n cwd: context.cwd,\n env: context.env,\n log: (entry) => {\n const target = (entry.kind === 'cmd' || entry.kind === 'stdout' ? stdout : stderr) as NodeJS.WriteStream;\n const colorEnabled = context.color === 'always' || (context.color === 'auto' && autoColor(context.env, target));\n const previousOutput = zxLog.output;\n const previousLevel = chalk.level;\n zxLog.output = colorEnabled\n ? target\n : ({ write: (chunk: string) => target.write(stripVTControlCharacters(chunk)) } as unknown as NodeJS.WriteStream);\n if (colorEnabled && !chalk.level) {\n chalk.level = 1;\n } else if (!colorEnabled) {\n chalk.level = 0;\n }\n try {\n zxLog(entry.kind === 'cmd' && (context.logLevel === 'info' || context.logLevel === 'debug') ? { ...entry, verbose: true } : entry);\n } finally {\n // eslint-disable-next-line ts/no-non-null-assertion\n zxLog.output = previousOutput!;\n chalk.level = previousLevel;\n }\n },\n preferLocal: true,\n quiet: context.logLevel === 'silent',\n stdio: 'inherit',\n verbose: context.logLevel === 'debug',\n ...optionsRest,\n };\n },\n};\n\nfunction autoColor(env: NodeJS.ProcessEnv, stream: NodeJS.WriteStream): boolean {\n if (env['NO_COLOR'] != null && env['NO_COLOR'] !== '') {\n return false;\n }\n const force = env['FORCE_COLOR'];\n return force == null ? stream.isTTY === true : force !== '0' && force !== 'false';\n}\n","import { $ as zxFn } from 'zx';\n\nimport type { CommandContext, StdioOptions } from '../type.js';\nimport type { ZXShell } from './ZXShell.js';\n\nimport { ZXOptions } from './ZXOptions.js';\n\nexport interface CreateZXOptions {\n /**\n * Working directory for spawned commands. Defaults to `MANAGED_SCRIPT_CWD`, then `process.cwd()`.\n */\n readonly cwd?: string | undefined;\n\n /**\n * Environment variables passed to spawned commands. Merged over `process.env`.\n */\n readonly env?: NodeJS.ProcessEnv | undefined;\n\n /**\n * Log level used to derive zx `verbose` / `quiet` and command instrumentation.\n * Defaults to `MANAGED_SCRIPT_LOGLEVEL`, then `info`.\n */\n readonly logLevel?: CommandContext['logLevel'] | undefined;\n\n /**\n * Standard I/O used to write instrumented command logs (index `1` for command lines and output, index `2` for the rest).\n */\n readonly stdio?: StdioOptions | undefined;\n}\n\n/**\n * Creates a zx `$` preset configured from managed-script context and environment variables.\n *\n * @param options\n */\nexport function zx(options: CreateZXOptions = {}): ZXShell {\n const env = options.env == null ? process.env : { ...process.env, ...options.env };\n\n return zxFn(ZXOptions.create({\n env,\n ...(options.cwd == null ? {} : { cwd: options.cwd }),\n ...(options.logLevel == null ? {} : { logLevel: options.logLevel }),\n ...(options.stdio == null ? {} : { stdio: options.stdio }),\n }));\n}\n","import { ZXOptions } from './ZXOptions.js';\n\n/**\n * Default ZX options for the managed script environment.\n */\nexport const defaults = ZXOptions.create({});\n","export function lazy<T>(fn: () => T): () => T {\n let cached: T | undefined;\n return () => {\n cached ??= fn();\n return cached;\n };\n}\n","import { bool, cleanEnv } from 'envalid';\n\nimport { lazy } from '../internal/lazy.js';\n\nconst envSchema = {\n CI: bool({ default: false }),\n CONTINUOUS_INTEGRATION: bool({ default: false }),\n};\n\nconst getEnvClean = lazy(() =>\n cleanEnv(process.env, envSchema, {\n reporter: null,\n }),\n);\n\n/**\n * Determines if the current environment is a CI (Continuous Integration) environment.\n * Checks the `CI` and `CONTINUOUS_INTEGRATION` environment variables to determine if the current environment is a CI environment.\n *\n * @example\n * ```ts\n * const ci = isCI();\n * ```\n * @returns `true` if the current environment is a CI environment, otherwise `false`\n */\nexport const isCI = lazy(() => getEnvClean().CI || getEnvClean().CONTINUOUS_INTEGRATION);\n","import type { ZXShell } from './zx/ZXShell.js';\n\nimport { zx } from './zx/zx.js';\n\nexport * from './zx/defaults.js';\nexport * from './zx/isCI.js';\nexport * from './zx/zx.js';\nexport type * from './zx/ZXOptions.js';\nexport type * from './zx/ZXShell.js';\n\n/**\n * Default `$` preset for scripts launched by managed-script.\n * Reads `MANAGED_SCRIPT_*` from the process environment at import time.\n *\n * @example\n * ```ts\n * import { $ } from '@w5s/managed-script/zx';\n * await $`echo Hello, world!`;\n * ```\n */\nexport const $: ZXShell = zx();\n"],"mappings":";;;;;;;;AAqBA,MAAa,YAAY,EACvB,OAAO,SAAiD;CACtD,MAAM,EAAE,KAAK,YAAY,KAAK,YAAY,UAAU,iBAAiB,OAAO,cAAc,GAAG,gBAAgB;CAC7G,MAAM,MAAM,cAAc,OAAO,QAAQ,MAAM;EAAE,GAAG,QAAQ;EAAK,GAAG;CAAW;CAC/E,MAAM,UAAU,eAAe;EAC7B;EACA,GAAI,cAAc,OAAO,CAAC,IAAI,EAAE,KAAK,WAAW;EAChD,GAAI,mBAAmB,OAAO,CAAC,IAAI,EAAE,UAAU,gBAAgB;EAC/D,GAAI,gBAAgB,OAAO,CAAC,IAAI,EAAE,OAAO,aAAa;CACxD,CAAC;CACD,MAAM,EAAE,QAAQ,WAAW,MAAM,QAAQ,QAAQ,KAAK;CAEtD,OAAO;EACL,KAAK,QAAQ;EACb,KAAK,QAAQ;EACb,MAAM,UAAU;GACd,MAAM,SAAU,MAAM,SAAS,SAAS,MAAM,SAAS,WAAW,SAAS;GAC3E,MAAM,eAAe,QAAQ,UAAU,YAAa,QAAQ,UAAU,UAAU,UAAU,QAAQ,KAAK,MAAM;GAC7G,MAAM,iBAAiBA,IAAM;GAC7B,MAAM,gBAAgB,MAAM;GAC5BA,IAAM,SAAS,eACX,SACC,EAAE,QAAQ,UAAkB,OAAO,MAAM,yBAAyB,KAAK,CAAC,EAAE;GAC/E,IAAI,gBAAgB,CAAC,MAAM,OACzB,MAAM,QAAQ;QACT,IAAI,CAAC,cACV,MAAM,QAAQ;GAEhB,IAAI;IACFA,IAAM,MAAM,SAAS,UAAU,QAAQ,aAAa,UAAU,QAAQ,aAAa,WAAW;KAAE,GAAG;KAAO,SAAS;IAAK,IAAI,KAAK;GACnI,UAAU;IAERA,IAAM,SAAS;IACf,MAAM,QAAQ;GAChB;EACF;EACA,aAAa;EACb,OAAO,QAAQ,aAAa;EAC5B,OAAO;EACP,SAAS,QAAQ,aAAa;EAC9B,GAAG;CACL;AACF,EACF;AAEA,SAAS,UAAU,KAAwB,QAAqC;CAC9E,IAAI,IAAI,eAAe,QAAQ,IAAI,gBAAgB,IACjD,OAAO;CAET,MAAM,QAAQ,IAAI;CAClB,OAAO,SAAS,OAAO,OAAO,UAAU,OAAO,UAAU,OAAO,UAAU;AAC5E;;;;;;;ACrCA,SAAgB,GAAG,UAA2B,CAAC,GAAY;CACzD,MAAM,MAAM,QAAQ,OAAO,OAAO,QAAQ,MAAM;EAAE,GAAG,QAAQ;EAAK,GAAG,QAAQ;CAAI;CAEjF,OAAOC,IAAK,UAAU,OAAO;EAC3B;EACA,GAAI,QAAQ,OAAO,OAAO,CAAC,IAAI,EAAE,KAAK,QAAQ,IAAI;EAClD,GAAI,QAAQ,YAAY,OAAO,CAAC,IAAI,EAAE,UAAU,QAAQ,SAAS;EACjE,GAAI,QAAQ,SAAS,OAAO,CAAC,IAAI,EAAE,OAAO,QAAQ,MAAM;CAC1D,CAAC,CAAC;AACJ;;;;;ACvCA,MAAa,WAAW,UAAU,OAAO,CAAC,CAAC;;ACL3C,SAAgB,KAAQ,IAAsB;CAC5C,IAAI;CACJ,aAAa;EACX,WAAW,GAAG;EACd,OAAO;CACT;AACF;;ACFA,MAAM,YAAY;CAChB,IAAI,KAAK,EAAE,SAAS,MAAM,CAAC;CAC3B,wBAAwB,KAAK,EAAE,SAAS,MAAM,CAAC;AACjD;AAEA,MAAM,cAAc,WAClB,SAAS,QAAQ,KAAK,WAAW,EAC/B,UAAU,KACZ,CAAC,CACH;;;;;;;;;;;AAYA,MAAa,OAAO,WAAW,YAAY,CAAC,CAAC,MAAM,YAAY,CAAC,CAAC,sBAAsB;;;;;;;;;;;;ACLvF,MAAa,IAAa,GAAG"}
|