Skip to content

Commit f3df53b

Browse files
committed
refactor: pull up partial application
Pull up creation of partially applied functions so that they a. don't pollute business code and b. are created only once
1 parent 73e6ae5 commit f3df53b

File tree

5 files changed

+70
-32
lines changed

5 files changed

+70
-32
lines changed

src/app/add-dependencies.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,12 @@ export async function addDependenciesUsing(
226226
fetchPackument
227227
);
228228

229+
const resolveDependencies = partialApply(
230+
resolveDependenciesUsing,
231+
checkUrlExists,
232+
fetchPackument
233+
);
234+
229235
async function resolveScopesFor(
230236
packageName: DomainName,
231237
verison: SemanticVersion,
@@ -234,9 +240,8 @@ export async function addDependenciesUsing(
234240
if (source === unityRegistryUrl) return {};
235241

236242
await debugLog(`fetch: ${makePackageReference(packageName, verison)}`);
237-
const dependencyGraph = await resolveDependenciesUsing(
238-
checkUrlExists,
239-
fetchPackument,
243+
244+
const dependencyGraph = await resolveDependencies(
240245
sources,
241246
packageName,
242247
verison,

src/cli/cmd-add.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Logger } from "npmlog";
33
import { addDependenciesUsing } from "../app/add-dependencies";
44
import { determineEditorVersionUsing } from "../app/determine-editor-version";
55
import { loadRegistryAuthUsing } from "../app/get-registry-auth";
6+
import { partialApply } from "../domain/fp-utils";
67
import { DebugLog } from "../domain/logging";
78
import { makePackageReference } from "../domain/package-reference";
89
import { recordEntries } from "../domain/record-utils";
@@ -59,6 +60,27 @@ export function makeAddCmd(
5960
log: Logger,
6061
debugLog: DebugLog
6162
) {
63+
const determineEditorVersion = partialApply(
64+
determineEditorVersionUsing,
65+
readTextFile,
66+
debugLog
67+
);
68+
69+
const getRegistryAuth = partialApply(
70+
loadRegistryAuthUsing,
71+
readTextFile,
72+
debugLog
73+
);
74+
75+
const addDependencies = partialApply(
76+
addDependenciesUsing,
77+
readTextFile,
78+
writeTextFile,
79+
fetchPackument,
80+
checkUrlExists,
81+
debugLog
82+
);
83+
6284
return new Command("add")
6385
.aliases(["install", "i"])
6486
.addArgument(pkgArg)
@@ -80,11 +102,7 @@ openupm add @ [otherPkgs...]`
80102

81103
const projectDirectory = options.chdir;
82104

83-
const editorVersion = await determineEditorVersionUsing(
84-
readTextFile,
85-
debugLog,
86-
projectDirectory
87-
);
105+
const editorVersion = await determineEditorVersion(projectDirectory);
88106

89107
if (typeof editorVersion === "string")
90108
log.warn(
@@ -101,18 +119,13 @@ openupm add @ [otherPkgs...]`
101119

102120
const sources = await Promise.all(
103121
(options.registry ?? [openupmRegistryUrl]).map((it) =>
104-
loadRegistryAuthUsing(readTextFile, debugLog, upmConfigPath, it)
122+
getRegistryAuth(upmConfigPath, it)
105123
)
106124
);
107125

108126
if (options.upstream) sources.push(unityRegistry);
109127

110-
const addResults = await addDependenciesUsing(
111-
readTextFile,
112-
writeTextFile,
113-
fetchPackument,
114-
checkUrlExists,
115-
debugLog,
128+
const addResults = await addDependencies(
116129
projectDirectory,
117130
typeof editorVersion === "string" ? null : editorVersion,
118131
sources,

src/cli/cmd-deps.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { loadRegistryAuthUsing } from "../app/get-registry-auth";
77
import { queryAllRegistriesLazy } from "../app/query-registries";
88
import { resolveDependenciesUsing } from "../app/resolve-dependencies";
99
import { PackumentNotFoundError } from "../domain/common-errors";
10+
import { partialApply } from "../domain/fp-utils";
1011
import { DebugLog } from "../domain/logging";
1112
import {
1213
makePackageReference,
@@ -52,6 +53,23 @@ export function makeDepsCmd(
5253
log: Logger,
5354
debugLog: DebugLog
5455
) {
56+
const getRegistryAuth = partialApply(
57+
loadRegistryAuthUsing,
58+
readTextFile,
59+
debugLog
60+
);
61+
62+
const getLatestVersion = partialApply(
63+
fetchLatestPackumentVersionUsing,
64+
fetchPackument
65+
);
66+
67+
const resolveDependencies = partialApply(
68+
resolveDependenciesUsing,
69+
checkUrlExists,
70+
fetchPackument
71+
);
72+
5573
return new Command("deps")
5674
.alias("dep")
5775
.addArgument(pkgArg)
@@ -71,9 +89,7 @@ openupm deps @`
7189
homePath,
7290
options.systemUser
7391
);
74-
const primaryRegistry = await loadRegistryAuthUsing(
75-
readTextFile,
76-
debugLog,
92+
const primaryRegistry = await getRegistryAuth(
7793
upmConfigPath,
7894
options.registry
7995
);
@@ -95,11 +111,7 @@ openupm deps @`
95111
? requestedVersion
96112
: (
97113
await queryAllRegistriesLazy(sources, (source) =>
98-
fetchLatestPackumentVersionUsing(
99-
fetchPackument,
100-
source,
101-
packageName
102-
)
114+
getLatestVersion(source, packageName)
103115
)
104116
)?.value ?? null;
105117

@@ -111,9 +123,7 @@ openupm deps @`
111123
options.deep
112124
}`
113125
);
114-
const dependencyGraph = await resolveDependenciesUsing(
115-
checkUrlExists,
116-
fetchPackument,
126+
const dependencyGraph = await resolveDependencies(
117127
sources,
118128
packageName,
119129
latestVersion,

src/cli/cmd-search.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,19 @@ export function makeSearchCmd(
3333
log: Logger,
3434
debugLog: DebugLog
3535
) {
36+
const getRegistryAuth = partialApply(
37+
loadRegistryAuthUsing,
38+
readTextFile,
39+
debugLog
40+
);
41+
3642
const searchPackages = partialApply(
3743
searchPackagesUsing,
3844
searchRegistry,
3945
fetchAllPackuments,
4046
debugLog
4147
);
48+
4249
return new Command("search")
4350
.argument("", "The keyword to search")
4451
.addOption(primaryRegistryUrlOpt)
@@ -54,9 +61,7 @@ export function makeSearchCmd(
5461
options.systemUser
5562
);
5663

57-
const primaryRegistry = await loadRegistryAuthUsing(
58-
readTextFile,
59-
debugLog,
64+
const primaryRegistry = await getRegistryAuth(
6065
upmConfigPath,
6166
options.registry
6267
);

src/cli/cmd-view.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { EOL } from "os";
44
import { loadRegistryAuthUsing } from "../app/get-registry-auth";
55
import { queryAllRegistriesLazy } from "../app/query-registries";
66
import { PackumentNotFoundError } from "../domain/common-errors";
7+
import { partialApply } from "../domain/fp-utils";
78
import type { DebugLog } from "../domain/logging";
89
import { hasVersion, splitPackageReference } from "../domain/package-reference";
910
import { unityRegistry } from "../domain/registry";
@@ -33,6 +34,12 @@ export function makeViewCmd(
3334
debugLog: DebugLog,
3435
log: Logger
3536
) {
37+
const getRegistryAuth = partialApply(
38+
loadRegistryAuthUsing,
39+
readTextFile,
40+
debugLog
41+
);
42+
3643
return new Command("view")
3744
.argument("", "Reference to a package", mustBePackageReference)
3845
.addOption(primaryRegistryUrlOpt)
@@ -49,9 +56,7 @@ export function makeViewCmd(
4956
options.systemUser
5057
);
5158

52-
const primaryRegistry = await loadRegistryAuthUsing(
53-
readTextFile,
54-
debugLog,
59+
const primaryRegistry = await getRegistryAuth(
5560
upmConfigPath,
5661
options.registry
5762
);

0 commit comments

Comments
 (0)