summaryrefslogtreecommitdiff
path: root/u/process
diff options
context:
space:
mode:
Diffstat (limited to 'u/process')
-rw-r--r--u/process/env.ts24
-rw-r--r--u/process/exec.ts (renamed from u/process/run.ts)25
-rw-r--r--u/process/index.ts2
3 files changed, 34 insertions, 17 deletions
diff --git a/u/process/env.ts b/u/process/env.ts
index 88fb490..f59fadf 100644
--- a/u/process/env.ts
+++ b/u/process/env.ts
@@ -1,27 +1,25 @@
-import { IOptional, Either, Optional, type IEither } from '@emprespresso/pengueno';
+import { IOptional, Either, Optional, type IEither, type ObjectFromList } from '@emprespresso/pengueno';
-export const getEnv = <V extends string>(name: string): IOptional<V> => Optional.from(<V>process.env[name]);
+// type safe environment variables
-export const getRequiredEnv = <V extends string>(name: string): IEither<Error, V> =>
- Either.fromFailable(() => getEnv<V>(name).get()).mapLeft(
+export const getEnv = (name: string): IOptional<string> => Optional.from(process.env[name]);
+
+export const getRequiredEnv = <V extends string>(name: V): IEither<Error, string> =>
+ Either.fromFailable(() => getEnv(name).get()).mapLeft(
() => new Error(`environment variable "${name}" is required D:`),
);
-type ObjectFromList<T extends ReadonlyArray<string>, V = string> = {
- [K in T extends ReadonlyArray<infer U> ? U : never]: V;
-};
-
-export const getRequiredEnvVars = <V extends string>(vars: Array<V>) => {
+export const getRequiredEnvVars = <V extends string>(vars: Array<V>): IEither<Error, ObjectFromList<typeof vars>> => {
type Environment = ObjectFromList<typeof vars>;
const emptyEnvironment = Either.right<Error, Environment>(<Environment>{});
- const addTo = (env: Environment, key: V) => (val: string) =>
+ const addTo = (env: Environment, key: V, val: string) =>
<Environment>{
...env,
[key]: val,
};
- return Either.joinRight<V, Error, Environment>(
- vars,
- (envVar: V, environment: Environment) => getRequiredEnv(envVar).mapRight(addTo(environment, envVar)),
+ return vars.reduce(
+ (environment, key) =>
+ environment.joinRight(getRequiredEnv(key), (value, environment) => addTo(environment, key, value)),
emptyEnvironment,
);
};
diff --git a/u/process/run.ts b/u/process/exec.ts
index 1d19129..a2cdbca 100644
--- a/u/process/run.ts
+++ b/u/process/exec.ts
@@ -15,11 +15,13 @@ export type Command = string[] | string;
export type StdStreams = { stdout: string; stderr: string };
export const CmdMetric = Metric.fromName('Exec').asResult();
+type Environment = Record<string, string>;
+type Options = { env?: Environment; clearEnv?: boolean };
export const getStdout = (
- c: ITraceable<Command, LogMetricTraceSupplier>,
- options: { env?: Record<string, string>; clearEnv?: boolean } = {},
+ cmd: ITraceable<Command, LogMetricTraceSupplier>,
+ options: Options = {},
): Promise<IEither<Error, string>> =>
- c
+ cmd
.flatMap(TraceUtil.withFunctionTrace(getStdout))
.flatMap((tCmd) => tCmd.traceScope(() => `Command = ${tCmd.get()}`))
.map((tCmd) => {
@@ -38,3 +40,20 @@ export const getStdout = (
)
.peek(TraceUtil.promiseify(TraceUtil.traceResultingEither(CmdMetric)))
.get();
+
+export const getStdoutMany = (
+ cmds: ITraceable<Array<Command>, LogMetricTraceSupplier>,
+ options: Options = {},
+): Promise<IEither<Error, Array<string>>> =>
+ cmds
+ .coExtend((t) => t.get())
+ .reduce(
+ async (_result, tCmd) => {
+ const result = await _result;
+ return result.joinRightAsync(
+ () => tCmd.map((cmd) => getStdout(cmd, options)).get(),
+ (stdout, pre) => pre.concat(stdout),
+ );
+ },
+ Promise.resolve(Either.right<Error, Array<string>>([])),
+ );
diff --git a/u/process/index.ts b/u/process/index.ts
index 6945a0f..2d74a5f 100644
--- a/u/process/index.ts
+++ b/u/process/index.ts
@@ -1,5 +1,5 @@
+export * from './exec.js';
export * from './env.js';
-export * from './run.js';
export * from './validate_identifier.js';
export * from './argv.js';
export * from './signals.js';