Skip to content

Commit 4e674d2

Browse files
committed
refactor(lib): reduce lint complexity in path normalization
1 parent c832cad commit 4e674d2

2 files changed

Lines changed: 59 additions & 35 deletions

File tree

packages/lib/src/core/command-builders.ts

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -114,41 +114,57 @@ type PathConfig = {
114114
readonly outDir: string
115115
}
116116

117+
type DefaultPathConfig = {
118+
readonly dockerGitPath: string
119+
readonly authorizedKeysPath: string
120+
readonly envGlobalPath: string
121+
readonly envProjectPath: string
122+
readonly codexAuthPath: string
123+
}
124+
125+
const resolveNormalizedSecretsRoot = (value: string | undefined): string | undefined => {
126+
const trimmed = value?.trim() ?? ""
127+
return trimmed.length === 0 ? undefined : normalizeSecretsRoot(trimmed)
128+
}
129+
130+
const buildDefaultPathConfig = (
131+
normalizedSecretsRoot: string | undefined,
132+
projectSlug: string
133+
): DefaultPathConfig =>
134+
normalizedSecretsRoot === undefined
135+
? {
136+
dockerGitPath: defaultTemplateConfig.dockerGitPath,
137+
authorizedKeysPath: defaultTemplateConfig.authorizedKeysPath,
138+
envGlobalPath: defaultTemplateConfig.envGlobalPath,
139+
envProjectPath: defaultTemplateConfig.envProjectPath,
140+
codexAuthPath: defaultTemplateConfig.codexAuthPath
141+
}
142+
: {
143+
dockerGitPath: normalizedSecretsRoot,
144+
authorizedKeysPath: `${normalizedSecretsRoot}/authorized_keys`,
145+
envGlobalPath: `${normalizedSecretsRoot}/global.env`,
146+
envProjectPath: `${normalizedSecretsRoot}/${projectSlug}.env`,
147+
codexAuthPath: `${normalizedSecretsRoot}/codex`
148+
}
149+
117150
const resolvePaths = (
118151
raw: RawOptions,
119152
projectSlug: string,
120153
repoPath: string
121154
): Either.Either<PathConfig, ParseError> =>
122155
Either.gen(function*(_) {
123-
const secretsRoot = raw.secretsRoot?.trim()
124-
const normalizedSecretsRoot = secretsRoot === undefined || secretsRoot.length === 0
125-
? undefined
126-
: normalizeSecretsRoot(secretsRoot)
127-
const defaultAuthorizedKeysPath = normalizedSecretsRoot === undefined
128-
? defaultTemplateConfig.authorizedKeysPath
129-
: `${normalizedSecretsRoot}/authorized_keys`
130-
const defaultDockerGitPath = normalizedSecretsRoot === undefined
131-
? defaultTemplateConfig.dockerGitPath
132-
: normalizedSecretsRoot
133-
const defaultEnvGlobalPath = normalizedSecretsRoot === undefined
134-
? defaultTemplateConfig.envGlobalPath
135-
: `${normalizedSecretsRoot}/global.env`
136-
const defaultEnvProjectPath = normalizedSecretsRoot === undefined
137-
? defaultTemplateConfig.envProjectPath
138-
: `${normalizedSecretsRoot}/${projectSlug}.env`
139-
const defaultCodexAuthPath = normalizedSecretsRoot === undefined
140-
? defaultTemplateConfig.codexAuthPath
141-
: `${normalizedSecretsRoot}/codex`
142-
const dockerGitPath = defaultDockerGitPath
156+
const normalizedSecretsRoot = resolveNormalizedSecretsRoot(raw.secretsRoot)
157+
const defaults = buildDefaultPathConfig(normalizedSecretsRoot, projectSlug)
158+
const dockerGitPath = defaults.dockerGitPath
143159
const authorizedKeysPath = yield* _(
144-
nonEmpty("--authorized-keys", raw.authorizedKeysPath, defaultAuthorizedKeysPath)
160+
nonEmpty("--authorized-keys", raw.authorizedKeysPath, defaults.authorizedKeysPath)
145161
)
146-
const envGlobalPath = yield* _(nonEmpty("--env-global", raw.envGlobalPath, defaultEnvGlobalPath))
162+
const envGlobalPath = yield* _(nonEmpty("--env-global", raw.envGlobalPath, defaults.envGlobalPath))
147163
const envProjectPath = yield* _(
148-
nonEmpty("--env-project", raw.envProjectPath, defaultEnvProjectPath)
164+
nonEmpty("--env-project", raw.envProjectPath, defaults.envProjectPath)
149165
)
150166
const codexAuthPath = yield* _(
151-
nonEmpty("--codex-auth", raw.codexAuthPath, defaultCodexAuthPath)
167+
nonEmpty("--codex-auth", raw.codexAuthPath, defaults.codexAuthPath)
152168
)
153169
const codexSharedAuthPath = codexAuthPath
154170
const codexHome = yield* _(nonEmpty("--codex-home", raw.codexHome, defaultTemplateConfig.codexHome))

packages/lib/src/usecases/state-normalize.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,28 @@ const isLegacyDockerGitRelativePath = (value: string): boolean => {
2121
const shouldNormalizePath = (path: Path.Path, value: string): boolean =>
2222
path.isAbsolute(value) || isLegacyDockerGitRelativePath(value)
2323

24+
const withFallback = (value: string, fallback: string): string =>
25+
value.length > 0 ? value : fallback
26+
27+
const pathFieldsForNormalization = (template: TemplateConfig): ReadonlyArray<string> => [
28+
template.dockerGitPath,
29+
template.authorizedKeysPath,
30+
template.envGlobalPath,
31+
template.envProjectPath,
32+
template.codexAuthPath,
33+
template.codexSharedAuthPath
34+
]
35+
36+
const hasLegacyTemplatePaths = (path: Path.Path, template: TemplateConfig): boolean =>
37+
pathFieldsForNormalization(template).some((value) => shouldNormalizePath(path, value))
38+
2439
const normalizeTemplateConfig = (
2540
path: Path.Path,
2641
projectsRoot: string,
2742
projectDir: string,
2843
template: TemplateConfig
2944
): TemplateConfig | null => {
30-
const needs = shouldNormalizePath(path, template.dockerGitPath) ||
31-
shouldNormalizePath(path, template.authorizedKeysPath) ||
32-
shouldNormalizePath(path, template.envGlobalPath) ||
33-
shouldNormalizePath(path, template.envProjectPath) ||
34-
shouldNormalizePath(path, template.codexAuthPath) ||
35-
shouldNormalizePath(path, template.codexSharedAuthPath)
36-
37-
if (!needs) {
45+
if (!hasLegacyTemplatePaths(path, template)) {
3846
return null
3947
}
4048

@@ -51,12 +59,12 @@ const normalizeTemplateConfig = (
5159

5260
return {
5361
...template,
54-
dockerGitPath: dockerGitRel.length > 0 ? dockerGitRel : "./.docker-git",
55-
authorizedKeysPath: authorizedKeysRel.length > 0 ? authorizedKeysRel : "./authorized_keys",
62+
dockerGitPath: withFallback(dockerGitRel, "./.docker-git"),
63+
authorizedKeysPath: withFallback(authorizedKeysRel, "./authorized_keys"),
5664
envGlobalPath,
5765
envProjectPath,
5866
codexAuthPath,
59-
codexSharedAuthPath: codexSharedRel.length > 0 ? codexSharedRel : "./.orch/auth/codex"
67+
codexSharedAuthPath: withFallback(codexSharedRel, "./.orch/auth/codex")
6068
}
6169
}
6270

0 commit comments

Comments
 (0)