Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: shrink cli index #398

Merged
merged 11 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
214 changes: 115 additions & 99 deletions src/cli/cmd-add.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,51 @@
import { Argument, Command, Option } from "@commander-js/extra-typings";
import { Logger } from "npmlog";
import { addDependenciesUsing } from "../app/add-dependencies";
import { determineEditorVersionUsing } from "../app/determine-editor-version";
import { loadRegistryAuthUsing } from "../app/get-registry-auth";
import { DebugLog } from "../domain/logging";
import {
makePackageReference,
PackageReference,
} from "../domain/package-reference";
import { makePackageReference } from "../domain/package-reference";
import { recordEntries } from "../domain/record-utils";
import { getHomePathFromEnv } from "../domain/special-paths";
import { getUserUpmConfigPathFor } from "../domain/upm-config";
import type { ReadTextFile, WriteTextFile } from "../io/fs";
import type { GetRegistryPackument } from "../io/registry";
import type { CheckUrlExists } from "../io/www";
import { CmdOptions } from "./options";
import { eachValue } from "./cli-parsing";
import { withErrorLogger } from "./error-logging";
import type { GlobalOptions } from "./options";
import { parseEnvUsing } from "./parse-env";
import { ResultCodes } from "./result-codes";
import { mustBePackageReference } from "./validators";

/**
* Options passed to the add command.
*/
export type AddOptions = CmdOptions<{
/**
* Whether to also add the packages to testables.
*/
test?: boolean;
/**
* Whether to run with force. This will add packages even if validation
* was not possible.
*/
force?: boolean;
}>;
const pkgArg = new Argument(
"<pkg>",
"Reference to the package that should be added"
).argParser(mustBePackageReference);

/**
* The different command result codes for the add command.
*/
export type AddResultCode = ResultCodes.Ok | ResultCodes.Error;
const otherPkgsArg = new Argument(
"[otherPkgs...]",
"References to additional packages that should be added"
).argParser(eachValue(mustBePackageReference));

/**
* Cmd-handler for adding packages.
* @param pkgs One or multiple references to packages to add.
* @param options Options specifying how to add the packages.
*/
type AddCmd = (
pkgs: PackageReference | PackageReference[],
options: AddOptions
) => Promise<AddResultCode>;
const addTestableOpt = new Option(
"-t, --test",
"add package as testable"
).default(false);

const forceOpt = new Option(
"-f, --force",
"force add package if missing deps or editor version is not qualified"
).default(false);

/**
* Makes a {@link AddCmd} function.
* Makes the `openupm add` cli command with the given dependencies.
* @param checkUrlExists IO function to check whether a url exists.
* @param fetchPackument IO function for fetching a packument.
* @param readTextFile IO function for reading a text file.
* @param writeTextFile IO function for writing a text file.
* @param log Logger for cli output.
* @param debugLog IO function for debug-logs.
* @returns The command.
*/
export function makeAddCmd(
checkUrlExists: CheckUrlExists,
Expand All @@ -57,78 +54,97 @@ export function makeAddCmd(
writeTextFile: WriteTextFile,
log: Logger,
debugLog: DebugLog
): AddCmd {
return async (pkgs, options) => {
if (!Array.isArray(pkgs)) pkgs = [pkgs];
) {
return new Command("add")
.aliases(["install", "i"])
.addArgument(pkgArg)
.addArgument(otherPkgsArg)
.addOption(addTestableOpt)
.addOption(forceOpt)
.description(
`add package to manifest json
openupm add <pkg> [otherPkgs...]
openupm add <pkg>@<version> [otherPkgs...]`
)
.action(
withErrorLogger(log, async function (pkg, otherPkgs, addOptions, cmd) {
const globalOptions = cmd.optsWithGlobals<GlobalOptions>();

// parse env
const env = await parseEnvUsing(log, process.env, process.cwd(), options);
const pkgs = [pkg].concat(otherPkgs);

const editorVersion = await determineEditorVersionUsing(
readTextFile,
debugLog,
env.cwd
);
// parse env
const env = await parseEnvUsing(
log,
process.env,
process.cwd(),
globalOptions
);

if (typeof editorVersion === "string")
log.warn(
"editor.version",
`${editorVersion} is unknown, the editor version check is disabled`
);
const editorVersion = await determineEditorVersionUsing(
readTextFile,
debugLog,
env.cwd
);

const projectDirectory = env.cwd;
if (typeof editorVersion === "string")
log.warn(
"editor.version",
`${editorVersion} is unknown, the editor version check is disabled`
);

const homePath = getHomePathFromEnv(process.env);
const upmConfigPath = getUserUpmConfigPathFor(
process.env,
homePath,
env.systemUser
);
const projectDirectory = env.cwd;

const primaryRegistry = await loadRegistryAuthUsing(
readTextFile,
debugLog,
upmConfigPath,
env.primaryRegistryUrl
);
const homePath = getHomePathFromEnv(process.env);
const upmConfigPath = getUserUpmConfigPathFor(
process.env,
homePath,
env.systemUser
);

const addResults = await addDependenciesUsing(
readTextFile,
writeTextFile,
fetchPackument,
checkUrlExists,
debugLog,
projectDirectory,
typeof editorVersion === "string" ? null : editorVersion,
primaryRegistry,
env.upstream,
options.force === true,
options.test === true,
pkgs
);
const primaryRegistry = await loadRegistryAuthUsing(
readTextFile,
debugLog,
upmConfigPath,
env.primaryRegistryUrl
);

recordEntries(addResults)
.map(([packageName, addResult]) => {
switch (addResult.type) {
case "added":
return `added ${makePackageReference(
packageName,
addResult.version
)}`;
case "upgraded":
return `modified ${packageName} ${addResult.fromVersion} => ${addResult.toVersion}`;
case "noChange":
return `existed ${makePackageReference(
packageName,
addResult.version
)}`;
}
})
.forEach((message) => {
log.notice("", message);
});
const addResults = await addDependenciesUsing(
readTextFile,
writeTextFile,
fetchPackument,
checkUrlExists,
debugLog,
projectDirectory,
typeof editorVersion === "string" ? null : editorVersion,
primaryRegistry,
env.upstream,
addOptions.force,
addOptions.test,
pkgs
);

log.notice("", "please open Unity project to apply changes.");
return ResultCodes.Ok;
};
recordEntries(addResults)
.map(([packageName, addResult]) => {
switch (addResult.type) {
case "added":
return `added ${makePackageReference(
packageName,
addResult.version
)}`;
case "upgraded":
return `modified ${packageName} ${addResult.fromVersion} => ${addResult.toVersion}`;
case "noChange":
return `existed ${makePackageReference(
packageName,
addResult.version
)}`;
}
})
.forEach((message) => {
log.notice("", message);
});

log.notice("", "please open Unity project to apply changes.");
})
);
}
Loading