Skip to content

Commit

Permalink
extract non-cli to root main
Browse files Browse the repository at this point in the history
  • Loading branch information
ComradeVanti committed Sep 17, 2024
1 parent 8b2909d commit a11e237
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 108 deletions.
189 changes: 82 additions & 107 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { createCommand } from "@commander-js/extra-typings";
import RegClient from "another-npm-registry-client";
import npmlog from "npmlog";
import type { Logger } from "npmlog";
import pkginfo from "pkginfo";
import updateNotifier from "update-notifier";
import pkg from "../../package.json";
import { DebugLog } from "../domain/logging";
import type { DebugLog } from "../domain/logging";
import { getHomePathFromEnv } from "../domain/special-paths";
import { readTextFile, writeTextFile } from "../io/fs";
import {
getAllRegistryPackumentsUsing,
getAuthTokenUsing,
getRegistryPackumentUsing,
searchRegistryUsing,
type GetAllRegistryPackuments,
type GetAuthToken,
type GetRegistryPackument,
type SearchRegistry,
} from "../io/registry";
import { fetchCheckUrlExists } from "../io/www";
import { makeAddCmd } from "./cmd-add";
Expand All @@ -22,112 +19,90 @@ import { makeSearchCmd } from "./cmd-search";
import { makeViewCmd } from "./cmd-view";
import { mustBeRegistryUrl } from "./validators";

// Composition root

const log = npmlog;

/**
* {@link DebugLog} function which uses {@link npmlog} to print logs to
* the console.
* Makes the openupm cli app with the given dependencies.
* @param fetchPackument IO function for fetching registry packuments.
* @param searchRegistry IO function for using the search API on a registry.
* @param fetchAllPackuments IO function for getting all packuments from
* a registry.
* @param getAuthToken IO function for getting a users auth token from a
* registry.
* @param log Logger for printing output.
* @param debugLog IO function for printing debug logs.
* @returns Root command.
*/
const debugLogToConsole: DebugLog = async function (message, context) {
const contextMessage =
context !== undefined
? `\n${
context instanceof Error
? context.toString()
: JSON.stringify(context, null, 2)
}`
: "";
return log.verbose("", `${message}${contextMessage}`);
};

const homePath = getHomePathFromEnv(process.env);
const registryClient = new RegClient({ log });
const fetchPackument = getRegistryPackumentUsing(
registryClient,
debugLogToConsole
);
const searchRegistry = searchRegistryUsing(debugLogToConsole);
const fetchAllPackuments = getAllRegistryPackumentsUsing(debugLogToConsole);

// update-notifier
pkginfo(module);
const notifier = updateNotifier({ pkg });
notifier.notify();

const program = createCommand()
.version(module.exports.version)
.option("-c, --chdir <path>", "change the working directory")
.option("-r, --registry <url>", "specify registry url", mustBeRegistryUrl)
.option("-v, --verbose", "output extra debugging")
.option("--system-user", "auth for Windows system user")
.option("--no-upstream", "don't use upstream unity registry")
.option("--no-color", "disable color");
export function makeOpenupmCli(
fetchPackument: GetRegistryPackument,
searchRegistry: SearchRegistry,
fetchAllPackuments: GetAllRegistryPackuments,
getAuthToken: GetAuthToken,
log: Logger,
debugLog: DebugLog
) {
const homePath = getHomePathFromEnv(process.env);

program.addCommand(
makeAddCmd(
fetchCheckUrlExists,
fetchPackument,
readTextFile,
writeTextFile,
log,
debugLogToConsole
)
);
pkginfo(module);
const program = createCommand()
.version(module.exports.version)
.option("-c, --chdir <path>", "change the working directory")
.option("-r, --registry <url>", "specify registry url", mustBeRegistryUrl)
.option("-v, --verbose", "output extra debugging")
.option("--system-user", "auth for Windows system user")
.option("--no-upstream", "don't use upstream unity registry")
.option("--no-color", "disable color");

program.addCommand(
makeRemoveCmd(readTextFile, writeTextFile, debugLogToConsole, log)
);
program.addCommand(
makeAddCmd(
fetchCheckUrlExists,
fetchPackument,
readTextFile,
writeTextFile,
log,
debugLog
)
);

program.addCommand(
makeSearchCmd(
readTextFile,
searchRegistry,
fetchAllPackuments,
log,
debugLogToConsole
)
);
program.addCommand(makeRemoveCmd(readTextFile, writeTextFile, debugLog, log));

program.addCommand(
makeViewCmd(
getRegistryPackumentUsing(registryClient, debugLogToConsole),
readTextFile,
debugLogToConsole,
log
)
);
program.addCommand(
makeSearchCmd(
readTextFile,
searchRegistry,
fetchAllPackuments,
log,
debugLog
)
);

program.addCommand(
makeDepsCmd(
readTextFile,
fetchPackument,
fetchCheckUrlExists,
log,
debugLogToConsole
)
);
program.addCommand(makeViewCmd(fetchPackument, readTextFile, debugLog, log));

program.addCommand(
makeLoginCmd(
homePath,
getAuthTokenUsing(registryClient, debugLogToConsole),
readTextFile,
writeTextFile,
debugLogToConsole,
log
)
);
program.addCommand(
makeDepsCmd(
readTextFile,
fetchPackument,
fetchCheckUrlExists,
log,
debugLog
)
);

// prompt for invalid command
program.on("command:*", function () {
log.warn("", `unknown command: ${program.args.join(" ")}`);
log.warn("", "see --help for a list of available commands");
process.exit(1);
});
program.addCommand(
makeLoginCmd(
homePath,
getAuthToken,
readTextFile,
writeTextFile,
debugLog,
log
)
);

program.parse(process.argv);
// prompt for invalid command
program.on("command:*", function () {
log.warn("", `unknown command: ${program.args.join(" ")}`);
log.warn("", "see --help for a list of available commands");
process.exit(1);
});

// print help if no command is given
if (!program.args.length) program.help();
return program;
}
59 changes: 58 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,60 @@
#!/usr/bin/env node

import "./cli";
import RegClient from "another-npm-registry-client";
import npmlog from "npmlog";
import updateNotifier from "update-notifier";
import pkg from "../package.json";
import { makeOpenupmCli } from "./cli";
import type { DebugLog } from "./domain/logging";
import {
getAllRegistryPackumentsUsing,
getAuthTokenUsing,
getRegistryPackumentUsing,
searchRegistryUsing,
} from "./io/registry";

// Composition root

const log = npmlog;

const debugLogToConsole: DebugLog = async function (message, context) {
const contextMessage =
context !== undefined
? `\n${
context instanceof Error
? context.toString()
: JSON.stringify(context, null, 2)
}`
: "";
return log.verbose("", `${message}${contextMessage}`);
};

const registryClient = new RegClient({ log });
const fetchPackument = getRegistryPackumentUsing(
registryClient,
debugLogToConsole
);
const searchRegistry = searchRegistryUsing(debugLogToConsole);
const fetchAllPackuments = getAllRegistryPackumentsUsing(debugLogToConsole);
const getAuthToken = getAuthTokenUsing(registryClient, debugLogToConsole);

const openupmCli = makeOpenupmCli(
fetchPackument,
searchRegistry,
fetchAllPackuments,
getAuthToken,
log,
debugLogToConsole
);

// Update in case of update

const notifier = updateNotifier({ pkg });
notifier.notify();

// Run app

openupmCli.parse(process.argv);

// print help if no command is given
if (openupmCli.args.length === 0) openupmCli.help();

0 comments on commit a11e237

Please sign in to comment.