@ygrishajev/jig 1.0.4 → 1.0.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.
@@ -5,6 +5,7 @@ import path from 'node:path';
5
5
  import { parseEnv } from 'node:util';
6
6
  import { simpleGit } from 'simple-git';
7
7
  import writeFileAtomic from 'write-file-atomic';
8
+ import { z } from 'zod';
8
9
  import { ConfigInvalid, messageOf } from '../../domain/errors.js';
9
10
  import { refuseSymlinks } from '../find-symlink.js';
10
11
  import { onLocalState } from './local-state.js';
@@ -17,12 +18,23 @@ export function readCredentialsFile(repoRoot) {
17
18
  if (!existsSync(file))
18
19
  return Result.ok({});
19
20
  return Result.try({
20
- try: () => parseEnv(readFileSync(file, 'utf-8')),
21
+ try: () => readFileSync(file, 'utf-8'),
21
22
  catch: (cause) => new ConfigInvalid({ message: `Cannot read ${CREDENTIALS_FILE}: ${messageOf(cause)}` }),
22
- });
23
+ }).andThen(parseCredentials);
23
24
  }
24
25
  /** Credentials the operator keeps for this repository, relative to its root. */
25
26
  export const CREDENTIALS_FILE = `${JIG_DIR}/.env`;
27
+ /** Parser and schema diagnostics can quote the file, so the error names only the file. */
28
+ function parseCredentials(text) {
29
+ const unreadableCredentials = () => new ConfigInvalid({ message: `Cannot parse ${CREDENTIALS_FILE} as a dotenv file.` });
30
+ return Result.try({ try: () => parseEnv(text), catch: unreadableCredentials }).andThen((parsed) => {
31
+ const credentials = credentialsSchema.safeParse(parsed);
32
+ return credentials.success
33
+ ? Result.ok(credentials.data)
34
+ : Result.err(unreadableCredentials());
35
+ });
36
+ }
37
+ const credentialsSchema = z.record(z.string(), z.string());
26
38
  /** Sets one credential, keeping the rest of the file, readable only by the operator. */
27
39
  export async function saveCredential(repoRoot, name, value) {
28
40
  const directory = path.join(repoRoot, JIG_DIR);
@@ -42,14 +42,23 @@ function runDocker(process, message) {
42
42
  }
43
43
  /** An image's labels, `undefined` when docker says the image is not there, or why docker could not say. */
44
44
  export function classifyInspect(image, outcome) {
45
- if (outcome.exitCode === 0) {
46
- return Result.ok(imageLabelsSchema.parse(JSON.parse(outcome.stdout ?? '')) ?? {});
47
- }
45
+ if (outcome.exitCode === 0)
46
+ return parseLabels(image, outcome.stdout ?? '');
48
47
  if (!didNotStart(outcome) && NO_SUCH_IMAGE.test(outcome.stderr ?? ''))
49
48
  return Result.ok(undefined);
50
49
  const output = dockerFailure(outcome);
51
50
  return Result.err(new ImageBuildFailed({ message: `Could not ask docker about ${image}: ${output}`, output }));
52
51
  }
52
+ function parseLabels(image, stdout) {
53
+ const unreadableLabels = () => new ImageBuildFailed({
54
+ message: `Docker returned labels for ${image} that jig cannot read: ${stdout}`,
55
+ output: stdout,
56
+ });
57
+ return Result.try({ try: () => JSON.parse(stdout), catch: unreadableLabels }).andThen((json) => {
58
+ const labels = imageLabelsSchema.safeParse(json);
59
+ return labels.success ? Result.ok(labels.data ?? {}) : Result.err(unreadableLabels());
60
+ });
61
+ }
53
62
  /** `docker image inspect` prints `null` for an image built without labels. */
54
63
  const imageLabelsSchema = z.record(z.string()).nullable();
55
64
  function didNotStart(outcome) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ygrishajev/jig",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "Agentic SDLC pipeline orchestrator",
5
5
  "license": "MIT",
6
6
  "type": "module",