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

merge wip to main #403

Merged
merged 3 commits into from
Sep 28, 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
639 changes: 49 additions & 590 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
"dependencies": {
"@commander-js/extra-typings": "^9.5.0",
"@iarna/toml": "^2.2.5",
"another-npm-registry-client": "^8.7.0",
"chalk": "^4.1.2",
"cli-table": "^0.3.11",
"commander": "^9.5.0",
Expand Down
6 changes: 3 additions & 3 deletions src/app/get-registry-auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NpmAuth } from "another-npm-registry-client";
import type { AuthOptions } from "npm-registry-fetch";
import { decodeBase64 } from "../domain/base64";
import { partialApply } from "../domain/fp-utils";
import { DebugLog } from "../domain/logging";
Expand Down Expand Up @@ -31,7 +31,7 @@ export function isNonAuthUrl(url: RegistryUrl): boolean {
* @returns The converted auth object.
* @throws {Error} If auth contained bad base64 string.
*/
export function importNpmAuth(entry: UpmConfigEntry): NpmAuth {
export function importNpmAuth(entry: UpmConfigEntry): AuthOptions {
// Basic auth
if ("_auth" in entry) {
const decoded = decodeBase64(entry._auth);
Expand Down Expand Up @@ -61,7 +61,7 @@ export function importNpmAuth(entry: UpmConfigEntry): NpmAuth {
export function tryGetAuthEntry(
upmConfig: UpmConfig,
url: RegistryUrl
): NpmAuth | null {
): AuthOptions | null {
const entry =
upmConfig.npmAuth?.[url] ?? upmConfig.npmAuth?.[url + "/"] ?? null;
if (entry === null) {
Expand Down
6 changes: 3 additions & 3 deletions src/app/login.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NpmAuth } from "another-npm-registry-client";
import type { AuthOptions } from "npm-registry-fetch";
import { partialApply } from "../domain/fp-utils";
import { DebugLog } from "../domain/logging";
import { RegistryUrl } from "../domain/registry-url";
Expand Down Expand Up @@ -51,15 +51,15 @@ export async function loginUsing(
);

if (authMode === "basic") {
const auth: NpmAuth = { username, password, email, alwaysAuth };
const auth: AuthOptions = { username, password, email, alwaysAuth };
return await putRegistryAuth(configPath, registry, auth);
}

// npm login
const token = await getAuthToken(registry, username, email, password);
await debugLog(`npm login successful`);

const auth: NpmAuth = { token, email, alwaysAuth };
const auth: AuthOptions = { token, email, alwaysAuth };
await putRegistryAuth(configPath, registry, auth);

const npmrcPath = await putNpmAuthToken(homePath, registry, token);
Expand Down
8 changes: 4 additions & 4 deletions src/app/put-registry-auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NpmAuth } from "another-npm-registry-client";
import type { AuthOptions } from "npm-registry-fetch";
import { encodeBase64 } from "../domain/base64";
import { partialApply } from "../domain/fp-utils";
import { RegistryUrl } from "../domain/registry-url";
Expand All @@ -10,7 +10,7 @@ import { saveUpmConfigFileUsing } from "./write-upm-config";

function mergeEntries(
oldEntry: UpmConfigEntry | null,
newEntry: NpmAuth
newEntry: AuthOptions
): UpmConfigEntry {
const alwaysAuth = newEntry.alwaysAuth ?? oldEntry?.alwaysAuth;

Expand Down Expand Up @@ -41,7 +41,7 @@ function mergeEntries(
export function putRegistryAuthIntoUpmConfig(
currentContent: UpmConfig | null,
registry: RegistryUrl,
auth: NpmAuth
auth: AuthOptions
): UpmConfig {
const oldEntries = currentContent?.npmAuth ?? {};
// Search the entry both with and without trailing slash
Expand Down Expand Up @@ -70,7 +70,7 @@ export async function putRegistryAuthUsing(
writeTextFile: WriteTextFile,
configPath: string,
registry: RegistryUrl,
auth: NpmAuth
auth: AuthOptions
): Promise<void> {
const loadUpmConfig = partialApply(loadUpmConfigUsing, readTextFile);
const saveUpmConfig = partialApply(saveUpmConfigFileUsing, writeTextFile);
Expand Down
5 changes: 2 additions & 3 deletions src/domain/registry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { NpmAuth } from "another-npm-registry-client";
import npmFetch from "npm-registry-fetch";
import npmFetch, { type AuthOptions } from "npm-registry-fetch";
import { RegistryUrl, unityRegistryUrl } from "./registry-url";

/**
Expand All @@ -14,7 +13,7 @@ export type Registry = Readonly<{
* The authentication information used for this registry. Null if the registry
* does not require authentication.
*/
auth: NpmAuth | null;
auth: AuthOptions | null;
}>;

/**
Expand Down
7 changes: 1 addition & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env node

import RegClient from "another-npm-registry-client";
import npmlog from "npmlog";
import updateNotifier from "update-notifier";
import pkg from "../package.json";
Expand Down Expand Up @@ -29,11 +28,7 @@ const debugLogToConsole: DebugLog = async function (message, context) {
return log.verbose("", `${message}${contextMessage}`);
};

const registryClient = new RegClient({ log });
const fetchPackument = getRegistryPackumentUsing(
registryClient,
debugLogToConsole
);
const fetchPackument = getRegistryPackumentUsing(debugLogToConsole);
const searchRegistry = searchRegistryUsing(debugLogToConsole);
const fetchAllPackuments = getAllRegistryPackumentsUsing(debugLogToConsole);
const getAuthToken = getAuthTokenUsing(debugLogToConsole);
Expand Down
26 changes: 9 additions & 17 deletions src/io/registry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type RegClient from "another-npm-registry-client";
import npmSearch from "libnpmsearch";
import { loginCouch } from "npm-profile";
import npmFetch from "npm-registry-fetch";
Expand Down Expand Up @@ -129,26 +128,19 @@ export type GetRegistryPackument = (
* from a remote npm registry.
*/
export function getRegistryPackumentUsing(
registryClient: RegClient.Instance,
debugLog: DebugLog
): GetRegistryPackument {
return (registry, name) => {
return async (registry, name) => {
const url = `${registry.url}/${name}`;
return new Promise<UnityPackument | null>((resolve, reject) => {
return registryClient.get(
url,
{ auth: registry.auth || undefined },
(error, packument) => {
if (error !== null) {
assertIsHttpError(error);
if (error.statusCode === 404) resolve(null);
else reject(error);
} else resolve(packument);
}
);
}).catch(async (error) => {
try {
const json = await npmFetch.json(url);
// TODO: Validate object
return json as UnityPackument;
} catch (error) {
assertIsHttpError(error);
if (error.statusCode === 404) return null;
await debugLog("Fetching a packument failed.", error);
throw makeRegistryInteractionError(error, registry.url);
});
}
};
}
51 changes: 0 additions & 51 deletions src/types/another-npm-registry-client.d.ts

This file was deleted.

6 changes: 1 addition & 5 deletions test/integration/app/add-dependencies.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import RegClient from "another-npm-registry-client";
import nock from "nock";
import {
addDependenciesUsing,
Expand Down Expand Up @@ -81,10 +80,7 @@ describe("add dependencies", () => {
const someProjectDir = "/users/some-user/projects/SomeProject";

function makeDependencies() {
const fetchPackument = getRegistryPackumentUsing(
new RegClient(),
noopLogger
);
const fetchPackument = getRegistryPackumentUsing(noopLogger);

const log = makeMockLogger();

Expand Down
4 changes: 1 addition & 3 deletions test/integration/app/resolve-dependencies.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import RegClient from "another-npm-registry-client";
import nock from "nock";
import { resolveDependenciesUsing } from "../../../src/app/resolve-dependencies";
import { PackumentNotFoundError } from "../../../src/domain/common-errors";
Expand Down Expand Up @@ -32,11 +31,10 @@ describe("dependency resolving", () => {
const someVersion = SemanticVersion.parse("1.0.0");
const otherVersion = SemanticVersion.parse("2.0.0");

const registryClient = new RegClient();
const resolveDependencies = partialApply(
resolveDependenciesUsing,
fetchCheckUrlExists,
getRegistryPackumentUsing(registryClient, noopLogger)
getRegistryPackumentUsing(noopLogger)
);

afterEach(() => {
Expand Down
38 changes: 0 additions & 38 deletions test/integration/io/registry-client.mock.ts

This file was deleted.

66 changes: 0 additions & 66 deletions test/integration/io/registry.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import type RegClient from "another-npm-registry-client";
import { default as npmSearch, default as search } from "libnpmsearch";
import npmFetch from "npm-registry-fetch";
import { DomainName } from "../../../src/domain/domain-name";
import { noopLogger } from "../../../src/domain/logging";
import {
HttpErrorLike,
RegistryAuthenticationError,
} from "../../../src/io/common-errors";
import {
getAllRegistryPackumentsUsing,
getRegistryPackumentUsing,
searchRegistryUsing,
} from "../../../src/io/registry";
import { buildPackument } from "../../common/data-packument";
import { someRegistry } from "../../common/data-registry";
import { mockRegClientGetResult } from "./registry-client.mock";

jest.mock("npm-registry-fetch");
jest.mock("libnpmsearch");
Expand Down Expand Up @@ -109,65 +104,4 @@ describe("registry io", () => {
expect(actual).toEqual(expected);
});
});

describe("fetch", () => {
const packageA = DomainName.parse("package-a");

function makeDependencies() {
const regClient: jest.Mocked<RegClient.Instance> = {
adduser: jest.fn(),
get: jest.fn(),
};

const fetchRegistryPackument = getRegistryPackumentUsing(
regClient,
noopLogger
);
return { fetchRegistryPackument, regClient } as const;
}
it("should get existing packument", async () => {
// TODO: Use prop test
const packument = buildPackument(packageA);
const { fetchRegistryPackument, regClient } = makeDependencies();
mockRegClientGetResult(regClient, null, packument);

const actual = await fetchRegistryPackument(someRegistry, packageA);

expect(actual).toEqual(packument);
});

it("should not find unknown packument", async () => {
const { fetchRegistryPackument, regClient } = makeDependencies();
mockRegClientGetResult(
regClient,
{
message: "not found",
name: "FakeError",
statusCode: 404,
},
null
);

const actual = await fetchRegistryPackument(someRegistry, packageA);

expect(actual).toBeNull();
});

it("should fail for errors", async () => {
const { fetchRegistryPackument, regClient } = makeDependencies();
mockRegClientGetResult(
regClient,
{
message: "Unauthorized",
name: "FakeError",
statusCode: 401,
},
null
);

await expect(
fetchRegistryPackument(someRegistry, packageA)
).rejects.toBeInstanceOf(Error);
});
});
});
Loading