Skip to content

Commit

Permalink
Split core module (#58)
Browse files Browse the repository at this point in the history
* refactor: make property required

Make version property required in dependency. This makes sense as Unity dependencies always give their dependency versions explicitly. The only reason this was set to optional was because it was not immediately initialized at one point.

* refactor: extract utility function

There are many points in the code where a package name is combined with a version using `${name}@${version}` or similar. Extracted this logic to a utility function. This process also showed that a potentially undefined version was used in some prints. Changed logic to only print version where it is defined.

* refactor: move and refactor function

Extract function for getting latest version of pkg-info to own module, so it can be grouped with other pkg-info related functions. Also change name to tryGetLatestVersion to indicate that this function may return undefined. Also refactor the function a bit for readability

a

* chore: reformat file

Just add a line break for consistency

* chore: reformat files

* refactor: move function

Move isInternalPackage function into package-name module to be closer to other related functions

* fix: incorrect type

This function assumes the name includes no version

* refactor: move function

Move manifest related functions from core to own module

* chore: clean up imports and reformat

* refactor: move functions

Move upm-config related functions to own module

* refactor: extract module

Move env related code into own module

* refactor: rename type

This type does not refer to a generic semantic version but specifically to a unity-editor version

* refactor: extract module

Extract editor-version related logic to own module

* refactor: move function

Move function to get search options into cmd-search since that is the only place where it is used

* refactor: move functions

Move functions for interacting with npm registry into client module.

* refactor: rename module
  • Loading branch information
ComradeVanti authored Nov 1, 2023
1 parent 910fe93 commit 5d9a0d5
Show file tree
Hide file tree
Showing 26 changed files with 1,185 additions and 1,104 deletions.
85 changes: 0 additions & 85 deletions src/client.ts

This file was deleted.

37 changes: 19 additions & 18 deletions src/cmd-add.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import log from "./logger";
import url from "url";
import {
compareEditorVersion,
env,
fetchPackageDependencies,
fetchPackageInfo,
getLatestVersion,
loadManifest,
parseEditorVersion,
parseEnv,
saveManifest,
} from "./core";
import { isUrlVersion } from "./utils/pkg-version";
import { splitPkgName } from "./utils/pkg-name";
import { atVersion, splitPkgName } from "./utils/pkg-name";
import { GlobalOptions, PkgName, ScopedRegistry } from "./types/global";
import { tryGetLatestVersion } from "./utils/pkg-info";
import { loadManifest, saveManifest } from "./utils/manifest";
import { env, parseEnv } from "./utils/env";

import {
compareEditorVersion,
tryParseEditorVersion,
} from "./utils/editor-version";
import { fetchPackageDependencies, fetchPackageInfo } from "./registry-client";

export type AddOptions = {
test?: boolean;
Expand Down Expand Up @@ -86,7 +84,7 @@ const _add = async function ({
// verify version
const versions = Object.keys(pkgInfo.versions);
// eslint-disable-next-line require-atomic-updates
if (!version || version == "latest") version = getLatestVersion(pkgInfo);
if (!version || version == "latest") version = tryGetLatestVersion(pkgInfo);
if (versions.filter((x) => x == version).length <= 0) {
log.warn(
"404",
Expand All @@ -106,8 +104,8 @@ const _add = async function ({
? versionInfo.unity + "." + versionInfo.unityRelease
: versionInfo.unity;
if (env.editorVersion) {
const editorVersionResult = parseEditorVersion(env.editorVersion);
const requiredEditorVersionResult = parseEditorVersion(
const editorVersionResult = tryParseEditorVersion(env.editorVersion);
const requiredEditorVersionResult = tryParseEditorVersion(
requiredEditorVersion
);
if (!editorVersionResult) {
Expand Down Expand Up @@ -165,7 +163,10 @@ const _add = async function ({
if (!depObj.resolved)
log.notice(
"suggest",
`to install ${depObj.name}@${depObj.version} or a replaceable version manually`
`to install ${atVersion(
depObj.name,
depObj.version
)} or a replaceable version manually`
);
}
});
Expand All @@ -185,15 +186,15 @@ const _add = async function ({
manifest.dependencies[name] = version;
if (!oldVersion) {
// Log the added package
log.notice("manifest", `added ${name}@${version}`);
log.notice("manifest", `added ${atVersion(name, version)}`);
dirty = true;
} else if (oldVersion != version) {
// Log the modified package version
log.notice("manifest", `modified ${name} ${oldVersion} => ${version}`);
dirty = true;
} else {
// Log the existed package
log.notice("manifest", `existed ${name}@${version}`);
log.notice("manifest", `existed ${atVersion(name, version)}`);
}
if (!isUpstreamPackage) {
// add to scopedRegistries
Expand Down
11 changes: 7 additions & 4 deletions src/cmd-deps.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import log from "./logger";
import { fetchPackageDependencies, parseEnv } from "./core";
import { splitPkgName } from "./utils/pkg-name";
import { atVersion, splitPkgName } from "./utils/pkg-name";
import { GlobalOptions, PkgName, PkgVersion } from "./types/global";
import { parseEnv } from "./utils/env";
import { fetchPackageDependencies } from "./registry-client";

export type DepsOptions = {
deep?: boolean;
Expand Down Expand Up @@ -36,13 +37,15 @@ const _deps = async function ({
});
depsValid
.filter((x) => !x.self)
.forEach((x) => log.notice("dependency", `${x.name}@${x.version}`));
.forEach((x) =>
log.notice("dependency", `${atVersion(x.name, x.version)}`)
);
depsInvalid
.filter((x) => !x.self)
.forEach((x) => {
let reason = "unknown";
if (x.reason == "package404") reason = "missing dependency";
else if (x.reason == "version404") reason = "missing dependency version";
log.warn(reason, `${x.name}@${x.version}`);
log.warn(reason, atVersion(x.name, x.version));
});
};
8 changes: 4 additions & 4 deletions src/cmd-login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import fs from "fs";
import path from "path";
import _ from "lodash";
import promptly from "promptly";
import { assertIsNpmClientError, getNpmClient } from "./client";
import { assertIsNpmClientError, getNpmClient } from "./registry-client";

import log from "./logger";

import { GlobalOptions, Registry } from "./types/global";
import {
getUpmConfigDir,
loadUpmConfig,
parseEnv,
saveUpmConfig,
} from "./core";
import { GlobalOptions, Registry } from "./types/global";
} from "./utils/upm-config";
import { parseEnv } from "./utils/env";

export type LoginOptions = {
username?: string;
Expand Down
9 changes: 5 additions & 4 deletions src/cmd-remove.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import log from "./logger";
import { env, loadManifest, parseEnv, saveManifest } from "./core";
import { splitPkgName } from "./utils/pkg-name";
import { atVersion, splitPkgName } from "./utils/pkg-name";
import { GlobalOptions, PkgName, ScopedRegistry } from "./types/global";
import { loadManifest, saveManifest } from "./utils/manifest";
import { env, parseEnv } from "./utils/env";

export type RemoveOptions = {
_global: GlobalOptions;
Expand Down Expand Up @@ -36,7 +37,7 @@ const _remove = async function (pkg: PkgName) {
const name = split.name;
let version = split.version;
if (version) {
log.warn("", `please replace '${name}@${version}' with '${name}'`);
log.warn("", `please replace '${atVersion(name, version)}' with '${name}'`);
return { code: 1, dirty };
}
// load manifest
Expand All @@ -48,7 +49,7 @@ const _remove = async function (pkg: PkgName) {
if (manifest.dependencies) {
version = manifest.dependencies[name];
if (version) {
log.notice("manifest", `removed ${name}@${version}`);
log.notice("manifest", `removed ${atVersion(name, version)}`);
delete manifest.dependencies[name];
dirty = true;
} else pkgsNotFound.push(pkg);
Expand Down
17 changes: 14 additions & 3 deletions src/cmd-search.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import npmSearch from "libnpmsearch";
import npmSearch, { Options } from "libnpmsearch";
import npmFetch from "npm-registry-fetch";
import Table from "cli-table";
import log from "./logger";
import { env, getLatestVersion, getNpmFetchOptions, parseEnv } from "./core";
import { is404Error, isHttpError } from "./utils/error-type-guards";
import * as os from "os";
import assert from "assert";
Expand All @@ -13,6 +12,8 @@ import {
PkgVersion,
Registry,
} from "./types/global";
import { tryGetLatestVersion } from "./utils/pkg-info";
import { env, parseEnv } from "./utils/env";

type DateString = string;

Expand All @@ -21,6 +22,16 @@ type TableRow = [PkgName, PkgVersion, DateString, ""];
export type SearchOptions = {
_global: GlobalOptions;
};
// Get npm fetch options
const getNpmFetchOptions = function (): Options {
const opts: Options = {
log,
registry: env.registry,
};
const auth = env.auth[env.registry];
if (auth) Object.assign(opts, auth);
return opts;
};

const searchEndpoint = async function (
keyword: string,
Expand Down Expand Up @@ -86,7 +97,7 @@ const getTable = function () {

const getTableRow = function (pkg: PkgInfo): TableRow {
const name = pkg.name;
const version = getLatestVersion(pkg);
const version = tryGetLatestVersion(pkg);
let date = "";
if (pkg.time && pkg.time.modified) date = pkg.time.modified.split("T")[0];
if (pkg.date) {
Expand Down
10 changes: 6 additions & 4 deletions src/cmd-view.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import chalk from "chalk";
import log from "./logger";
import { env, fetchPackageInfo, getLatestVersion, parseEnv } from "./core";
import assert from "assert";
import { splitPkgName } from "./utils/pkg-name";
import { atVersion, splitPkgName } from "./utils/pkg-name";
import { GlobalOptions, PkgInfo, PkgName } from "./types/global";
import { tryGetLatestVersion } from "./utils/pkg-info";
import { env, parseEnv } from "./utils/env";
import { fetchPackageInfo } from "./registry-client";

export type ViewOptions = {
_global: GlobalOptions;
Expand All @@ -16,7 +18,7 @@ export const view = async function (pkg: PkgName, options: ViewOptions) {
// parse name
const { name, version } = splitPkgName(pkg);
if (version) {
log.warn("", `please replace '${name}@${version}' with '${name}'`);
log.warn("", `please replace '${atVersion(name, version)}' with '${name}'`);
return 1;
}
// verify name
Expand All @@ -34,7 +36,7 @@ export const view = async function (pkg: PkgName, options: ViewOptions) {

const printInfo = function (pkg: PkgInfo) {
const versionCount = Object.keys(pkg.versions).length;
const ver = getLatestVersion(pkg);
const ver = tryGetLatestVersion(pkg);
assert(ver !== undefined);
const verInfo = pkg.versions[ver];
const license = verInfo.license || "proprietary or unlicensed";
Expand Down
Loading

0 comments on commit 5d9a0d5

Please sign in to comment.