Skip to content

Commit

Permalink
extract remove
Browse files Browse the repository at this point in the history
  • Loading branch information
ComradeVanti committed Sep 17, 2024
1 parent 2d8f22b commit eaedd54
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 82 deletions.
106 changes: 59 additions & 47 deletions src/cli/cmd-remove.ts
Original file line number Diff line number Diff line change
@@ -1,75 +1,87 @@
import { Argument, Command } from "@commander-js/extra-typings";
import { Logger } from "npmlog";
import { removeDependenciesUsing } from "../app/remove-dependencies";
import { DomainName } from "../domain/domain-name";
import { partialApply } from "../domain/fp-utils";
import type { DebugLog } from "../domain/logging";
import { makePackageReference } from "../domain/package-reference";
import type { ReadTextFile, WriteTextFile } from "../io/fs";
import type { DebugLog } from "../domain/logging";
import { partialApply } from "../domain/fp-utils";
import { logError } from "./error-logging";
import { CmdOptions } from "./options";
import { eachValue } from "./cli-parsing";
import { logError, withErrorLogger } from "./error-logging";
import { GlobalOptions } from "./options";
import { parseEnvUsing } from "./parse-env";
import { ResultCodes } from "./result-codes";
import { mustBeDomainName } from "./validators";

/**
* The possible result codes with which the remove command can exit.
*/
export type RemoveResultCode = ResultCodes.Ok | ResultCodes.Error;
const pkgArg = new Argument("<pkg>", "Name of the package to remove").argParser(
mustBeDomainName
);

/**
* Options passed to the remove command.
*/
export type RemoveOptions = CmdOptions;

/**
* Cmd-handler for removing packages.
* @param pkgs One or multiple packages to remove.
* @param options Command options.
*/
export type RemoveCmd = (
pkgs: ReadonlyArray<DomainName>,
options: RemoveOptions
) => Promise<RemoveResultCode>;
const otherPkgsArg = new Argument(
"[otherPkgs...]",
"Names of additional packages to remove"
).argParser(eachValue(mustBeDomainName));

/**
* Makes a {@link RemoveCmd} function.
* Makes the `openupm remove` cli command with the given dependencies.
* @param readTextFile IO function for reading a text file.
* @param writeTextFile IO function for writing a text file.
* @param debugLog IO function for debug-logs.
* @param log Logger for cli output.
* @returns The command.
*/
export function makeRemoveCmd(
readTextFile: ReadTextFile,
writeTextFile: WriteTextFile,
debugLog: DebugLog,
log: Logger
): RemoveCmd {
) {
const removeDependencies = partialApply(
removeDependenciesUsing,
readTextFile,
writeTextFile,
debugLog
);

return async (pkgs, options) => {
// parse env
const env = await parseEnvUsing(log, process.env, process.cwd(), options);
return new Command("remove")
.aliases(["rm", "uninstall"])
.addArgument(pkgArg)
.addArgument(otherPkgsArg)
.description("remove package from manifest json")
.action(
withErrorLogger(
log,
async function (packageName, otherPackageNames, _, cmd) {
const globalOptions = cmd.optsWithGlobals<GlobalOptions>();
const pkgs = [packageName, ...otherPackageNames];

const removeResult = await removeDependencies(env.cwd, pkgs).promise;
if (removeResult.isErr()) {
logError(log, removeResult.error);
return ResultCodes.Error;
}
const removedPackages = removeResult.value;
// parse env
const env = await parseEnvUsing(
log,
process.env,
process.cwd(),
globalOptions
);

removedPackages.forEach((removedPackage) => {
log.notice(
"",
`Removed "${makePackageReference(
removedPackage.name,
removedPackage.version
)}".`
);
});
const removeResult = await removeDependencies(env.cwd, pkgs).promise;
if (removeResult.isErr()) {
logError(log, removeResult.error);
return process.exit(ResultCodes.Error);
}
const removedPackages = removeResult.value;

// print manifest notice
log.notice("", "please open Unity project to apply changes");
removedPackages.forEach((removedPackage) => {
log.notice(
"",
`Removed "${makePackageReference(
removedPackage.name,
removedPackage.version
)}".`
);
});

return ResultCodes.Ok;
};
// print manifest notice
log.notice("", "please open Unity project to apply changes");
}
)
);
}
39 changes: 4 additions & 35 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
searchRegistryUsing,
} from "../io/registry";
import { fetchCheckUrlExists } from "../io/www";
import { eachValue } from "./cli-parsing";
import { makeAddCmd } from "./cmd-add";
import { makeDepsCmd } from "./cmd-deps";
import { makeLoginCmd } from "./cmd-login";
Expand All @@ -23,11 +22,7 @@ import { makeSearchCmd } from "./cmd-search";
import { makeViewCmd } from "./cmd-view";
import { withErrorLogger } from "./error-logging";
import { CmdOptions } from "./options";
import {
mustBeDomainName,
mustBePackageReference,
mustBeRegistryUrl,
} from "./validators";
import { mustBePackageReference, mustBeRegistryUrl } from "./validators";

// Composition root

Expand Down Expand Up @@ -65,12 +60,6 @@ const searchCmd = makeSearchCmd(
log,
debugLogToConsole
);
const removeCmd = makeRemoveCmd(
readTextFile,
writeTextFile,
debugLogToConsole,
log
);
const viewCmd = makeViewCmd(
getRegistryPackumentUsing(registryClient, debugLogToConsole),
readTextFile,
Expand Down Expand Up @@ -115,29 +104,9 @@ program.addCommand(
)
);

program
.command("remove")
.argument("<pkg>", "Name of the package to remove", mustBeDomainName)
.argument(
"[otherPkgs...]",
"Names of additional packages to remove",
eachValue(mustBeDomainName)
)
.aliases(["rm", "uninstall"])
.description("remove package from manifest json")
.action(
withErrorLogger(
log,
async function (packageName, otherPackageNames, options) {
const packageNames = [packageName].concat(otherPackageNames);
const resultCode = await removeCmd(
packageNames,
makeCmdOptions(options)
);
process.exit(resultCode);
}
)
);
program.addCommand(
makeRemoveCmd(readTextFile, writeTextFile, debugLogToConsole, log)
);

program
.command("search")
Expand Down

0 comments on commit eaedd54

Please sign in to comment.