@tahminator/pipeline 1.0.62 → 1.0.64

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 CHANGED
@@ -252,6 +252,7 @@ await frontendClient.uploadTestCoverage();
252
252
  Assorted helper class used by clients and useful as an exported class to pipelines.
253
253
 
254
254
  ```ts
255
+ // ----- strings -----
255
256
  const githubPrivateKey = await Utils.decodeBase64EncodedString(
256
257
  process.env.GH_PRIVATE_KEY_B64!,
257
258
  );
@@ -262,9 +263,42 @@ if (await Utils.isCmdAvailable("gh")) {
262
263
  console.log(Utils.Colors.green(`gh is installed (${shortId})`));
263
264
  }
264
265
 
265
- if (!Utils.SemVer.validate("1.2.3")) throw new Error("invalid version");
266
+ // ----- required -----
267
+ const token = Utils.required(process.env.GITHUB_TOKEN); // will throw if null or undefined
268
+
269
+ // ----- wait / polling -----
270
+ const reachable = await Utils.waitUntil({
271
+ predicate: async () => await Utils.isCmdAvailable("docker"),
272
+ attempts: 10,
273
+ intervalMs: 500,
274
+ });
275
+
276
+ if (!reachable) throw new Error("docker unavailable");
277
+
278
+ // ----- debug -----
279
+ if (Utils.Log.isDebug) {
280
+ console.log(Utils.Colors.dim("debug logging enabled"));
281
+ }
266
282
  ```
267
283
 
284
+ Available Utils APIs:
285
+
286
+ - `Utils.decodeBase64EncodedString(s: string): string`
287
+ - `Utils.generateShortId(len = 7): string`
288
+ - `Utils.isCmdAvailable(cmd: string): Promise<boolean>`
289
+ - `Utils.waitUntil({ predicate, attempts?, intervalMs? }): Promise<boolean>`
290
+ - `Utils.required<T>(value: T | null | undefined): T`
291
+ - `Utils.SemVer.validate(version: string | semver.SemVer): boolean`
292
+ - `Utils.Log.isDebug: boolean` (`true` when `DEBUG=true`)
293
+ - `Utils.Colors.<formatter>(s: string): string`
294
+
295
+ `Utils.Colors` formatters:
296
+
297
+ - `black`, `red`, `green`, `yellow`, `blue`, `magenta`, `cyan`, `white`, `gray`
298
+ - `brightRed`, `brightGreen`, `brightYellow`, `brightBlue`, `brightMagenta`, `brightCyan`, `brightWhite`
299
+ - `pink`, `orange`
300
+ - `bold`, `dim`, `italic`, `underline`
301
+
268
302
  ### `EnvClient`
269
303
 
270
304
  Load environment variables from encrypted files and automatically mask them in GitHub Actions logs.
@@ -1,8 +1,17 @@
1
1
  import { EnvClientStrategy, type EnvClientReadOpts } from "./types";
2
+ export type EnvClientOpts = {
3
+ /**
4
+ * Skip GitHub Actions secret masking. Defaults to `false`.
5
+ *
6
+ * **NOTE**: Be very careful if you set this to `true`!
7
+ */
8
+ skipMasking?: boolean;
9
+ };
2
10
  export declare class EnvClient {
3
11
  private readonly strategy;
12
+ private readonly opts;
4
13
  private constructor();
5
- static create(strategy: EnvClientStrategy): EnvClient;
14
+ static create(strategy: EnvClientStrategy, opts?: EnvClientOpts): EnvClient;
6
15
  readFromEnv(fileName: string, opts?: EnvClientReadOpts): Promise<Record<string, string>>;
7
16
  private maskAndReturnEnv;
8
17
  }
@@ -2,23 +2,28 @@ import { SopsEnvClientStrategy, GitCryptEnvClientStrategy, } from "./strategy";
2
2
  import { EnvClientStrategy } from "./types";
3
3
  export class EnvClient {
4
4
  strategy;
5
- constructor(strategy) {
5
+ opts;
6
+ constructor(strategy, opts) {
6
7
  this.strategy = strategy;
8
+ this.opts = opts;
7
9
  }
8
- static create(strategy) {
10
+ static create(strategy, opts = {
11
+ skipMasking: false,
12
+ }) {
9
13
  switch (strategy) {
10
14
  case EnvClientStrategy.SOPS:
11
- return new this(new SopsEnvClientStrategy());
15
+ return new this(new SopsEnvClientStrategy(), opts);
12
16
  case EnvClientStrategy.GIT_CRYPT:
13
- return new this(new GitCryptEnvClientStrategy());
17
+ return new this(new GitCryptEnvClientStrategy(), opts);
14
18
  }
15
19
  }
16
20
  async readFromEnv(fileName, opts) {
17
21
  return this.maskAndReturnEnv(await this.strategy.readFromEnv(fileName, opts));
18
22
  }
19
23
  maskAndReturnEnv(envs) {
20
- if (process.env.CI !== "true") {
21
- return Object.fromEntries(envs);
24
+ const r = Object.fromEntries(envs);
25
+ if (this.opts.skipMasking) {
26
+ return r;
22
27
  }
23
28
  for (const [varName, value] of envs.entries()) {
24
29
  if (value === "true" || value === "false" || value === "") {
@@ -38,6 +43,6 @@ export class EnvClient {
38
43
  }
39
44
  }
40
45
  }
41
- return Object.fromEntries(envs);
46
+ return r;
42
47
  }
43
48
  }
@@ -4,6 +4,7 @@ import { Colors } from "./colors";
4
4
  import { Log } from "./log";
5
5
  import { SemVer } from "./semver";
6
6
  import { generateShortId } from "./short";
7
+ import { required } from "./string";
7
8
  import { waitUntil } from "./wait";
8
9
  export declare class Utils {
9
10
  static Colors: typeof Colors;
@@ -13,4 +14,5 @@ export declare class Utils {
13
14
  static isCmdAvailable(...args: Parameters<typeof isCmdAvailable>): Promise<boolean>;
14
15
  static decodeBase64EncodedString(...args: Parameters<typeof decodeBase64EncodedString>): Promise<string>;
15
16
  static waitUntil(...args: Parameters<typeof waitUntil>): Promise<boolean>;
17
+ static required<T>(...args: Parameters<typeof required<T>>): T;
16
18
  }
@@ -4,6 +4,7 @@ import { Colors } from "./colors";
4
4
  import { Log } from "./log";
5
5
  import { SemVer } from "./semver";
6
6
  import { generateShortId } from "./short";
7
+ import { required } from "./string";
7
8
  import { waitUntil } from "./wait";
8
9
  export class Utils {
9
10
  // hoist
@@ -22,4 +23,7 @@ export class Utils {
22
23
  static async waitUntil(...args) {
23
24
  return waitUntil(...args);
24
25
  }
26
+ static required(...args) {
27
+ return required(...args);
28
+ }
25
29
  }
@@ -0,0 +1 @@
1
+ export declare function required<T>(value: T | null | undefined): T;
@@ -0,0 +1,6 @@
1
+ export function required(value) {
2
+ if (value == null) {
3
+ throw new Error("Expected a value but received null or undefined");
4
+ }
5
+ return value;
6
+ }
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "type": "module",
4
4
  "author": "Tahmid Ahmed",
5
5
  "description": "A collection of Bun shell scripts that can be re-used in various CICD pipelines.",
6
- "version": "1.0.62",
6
+ "version": "1.0.64",
7
7
  "repository": {
8
8
  "url": "git+https://github.com/tahminator/pipeline.git"
9
9
  },