From 4a6eef2dc70880a38f1508287ca975c9c16092c3 Mon Sep 17 00:00:00 2001 From: Ramon Brullo Date: Wed, 20 Dec 2023 16:47:55 +0100 Subject: [PATCH] feat: drop namespace logic (#67) * feat: drop namespace logic * refactor: remove unsued code This was only used in namespace logic and is no longer required --- src/cmd-add.ts | 3 +-- src/cmd-remove.ts | 2 -- src/types/ip-address.ts | 15 --------------- src/utils/env.ts | 13 ------------- test/data-pkg-manifest.ts | 6 ++---- test/test-cmd-remove.ts | 11 ++--------- test/test-env.ts | 8 -------- test/test-ip-address.ts | 28 ---------------------------- 8 files changed, 5 insertions(+), 81 deletions(-) delete mode 100644 src/types/ip-address.ts delete mode 100644 test/test-ip-address.ts diff --git a/src/cmd-add.ts b/src/cmd-add.ts index fa511bbd..227fb6e4 100644 --- a/src/cmd-add.ts +++ b/src/cmd-add.ts @@ -9,7 +9,7 @@ import { tryParseEditorVersion, } from "./types/editor-version"; import { fetchPackageDependencies, fetchPackageInfo } from "./registry-client"; -import { DomainName, isDomainName } from "./types/domain-name"; +import { DomainName } from "./types/domain-name"; import { SemanticVersion } from "./types/semantic-version"; import { packageReference, @@ -206,7 +206,6 @@ export const add = async function ( } // apply pkgsInScope const scopesSet = new Set(entry.scopes || []); - if (isDomainName(env.namespace)) pkgsInScope.push(env.namespace); pkgsInScope.forEach((name) => { if (!scopesSet.has(name)) { scopesSet.add(name); diff --git a/src/cmd-remove.ts b/src/cmd-remove.ts index 74149cfd..2c87b3ed 100644 --- a/src/cmd-remove.ts +++ b/src/cmd-remove.ts @@ -1,7 +1,6 @@ import log from "./logger"; import { loadManifest, saveManifest } from "./utils/pkg-manifest-io"; import { parseEnv } from "./utils/env"; -import { isDomainName } from "./types/domain-name"; import { packageReference, PackageReference, @@ -56,7 +55,6 @@ export const remove = async function ( if (hasScope(entry, name)) { removeScope(entry, name); const scopesSet = new Set(entry.scopes); - if (isDomainName(env.namespace)) scopesSet.add(env.namespace); entry.scopes = Array.from(scopesSet).sort(); dirty = true; } diff --git a/src/types/ip-address.ts b/src/types/ip-address.ts deleted file mode 100644 index 5a9de148..00000000 --- a/src/types/ip-address.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { Brand } from "ts-brand"; -import net from "node:net"; - -/** - * A string that is either a valid v4 or v6 ip-address - */ -export type IpAddress = Brand; - -/** - * Checks if a string is valid {@link IpAddress} - * @param s The string - */ -export function isIpAddress(s: string): s is IpAddress { - return net.isIPv4(s) || net.isIPv6(s); -} diff --git a/src/utils/env.ts b/src/utils/env.ts index a331d199..57cf093e 100644 --- a/src/utils/env.ts +++ b/src/utils/env.ts @@ -4,18 +4,11 @@ import { getUpmConfigDir, loadUpmConfig } from "./upm-config-io"; import path from "path"; import fs from "fs"; import yaml from "yaml"; -import { IpAddress, isIpAddress } from "../types/ip-address"; -import { - DomainName, - namespaceFor, - openUpmReverseDomainName, -} from "../types/domain-name"; import { coerceRegistryUrl, RegistryUrl, registryUrl, } from "../types/registry-url"; -import url from "url"; import { decodeBasicAuth, isBasicAuth, @@ -36,7 +29,6 @@ export type Env = { upstream: boolean; upstreamRegistry: Registry; registry: Registry; - namespace: DomainName | IpAddress; editorVersion: string | null; }; @@ -52,7 +44,6 @@ export const parseEnv = async function ( auth: null, }; env.cwd = ""; - env.namespace = openUpmReverseDomainName; env.upstream = true; env.upstreamRegistry = { url: registryUrl("https://packages.unity.com"), @@ -90,10 +81,6 @@ export const parseEnv = async function ( url: coerceRegistryUrl(options._global.registry), auth: null, }; - // TODO: Check hostname for null - const hostname = url.parse(env.registry.url).hostname as string; - if (isIpAddress(hostname)) env.namespace = hostname; - else env.namespace = namespaceFor(hostname); } function tryToNpmAuth(upmAuth: UpmAuth): NpmAuth | null { diff --git a/test/data-pkg-manifest.ts b/test/data-pkg-manifest.ts index 20151b8f..beeb9445 100644 --- a/test/data-pkg-manifest.ts +++ b/test/data-pkg-manifest.ts @@ -1,5 +1,5 @@ import assert from "assert"; -import { domainName, isDomainName } from "../src/types/domain-name"; +import { isDomainName } from "../src/types/domain-name"; import { exampleRegistryUrl } from "./mock-registry"; import { isSemanticVersion } from "../src/types/semantic-version"; import { addScope, scopedRegistry } from "../src/types/scoped-registry"; @@ -29,9 +29,7 @@ class PkgManifestBuilder { if (this.manifest.scopedRegistries === undefined) this.manifest.scopedRegistries = [ - scopedRegistry("example.com", exampleRegistryUrl, [ - domainName("com.example"), - ]), + scopedRegistry("example.com", exampleRegistryUrl), ]; const registry = this.manifest.scopedRegistries![0]; diff --git a/test/test-cmd-remove.ts b/test/test-cmd-remove.ts index 8eff7b3a..8ff4907b 100644 --- a/test/test-cmd-remove.ts +++ b/test/test-cmd-remove.ts @@ -1,9 +1,6 @@ import "should"; import { remove } from "../src/cmd-remove"; -import { - exampleRegistryReverseDomain, - exampleRegistryUrl, -} from "./mock-registry"; +import { exampleRegistryUrl } from "./mock-registry"; import { createWorkDir, removeWorkDir } from "./mock-work-dir"; import { attachMockConsole, MockConsole } from "./mock-console"; import { @@ -54,10 +51,7 @@ describe("cmd-remove.ts", function () { retCode.should.equal(0); const manifest = shouldHaveManifest(workDir); shouldNotHaveDependency(manifest, packageA); - shouldHaveRegistryWithScopes(manifest, [ - exampleRegistryReverseDomain, - packageB, - ]); + shouldHaveRegistryWithScopes(manifest, [packageB]); mockConsole.hasLineIncluding("out", "removed ").should.be.ok(); mockConsole.hasLineIncluding("out", "open Unity").should.be.ok(); }); @@ -102,7 +96,6 @@ describe("cmd-remove.ts", function () { const manifest = shouldHaveManifest(workDir); shouldNotHaveDependency(manifest, packageA); shouldNotHaveDependency(manifest, packageB); - shouldHaveRegistryWithScopes(manifest, [exampleRegistryReverseDomain]); mockConsole .hasLineIncluding("out", "removed com.example.package-a") .should.be.ok(); diff --git a/test/test-env.ts b/test/test-env.ts index d3be0014..a71d6cd5 100644 --- a/test/test-env.ts +++ b/test/test-env.ts @@ -35,7 +35,6 @@ describe("env", function () { env!.registry.url.should.equal("https://package.openupm.com"); env!.upstream.should.be.ok(); env!.upstreamRegistry.url.should.equal("https://packages.unity.com"); - env!.namespace.should.equal("com.openupm"); env!.cwd.should.equal(""); (env!.editorVersion === null).should.be.ok(); }); @@ -74,7 +73,6 @@ describe("env", function () { ); should(env).not.be.null(); env!.registry.url.should.be.equal("https://registry.npmjs.org"); - env!.namespace.should.be.equal("org.npmjs"); }); it("custom registry with splash", async function () { const env = await parseEnv( @@ -83,7 +81,6 @@ describe("env", function () { ); should(env).not.be.null(); env!.registry.url.should.be.equal("https://registry.npmjs.org"); - env!.namespace.should.be.equal("org.npmjs"); }); it("custom registry with extra path", async function () { const env = await parseEnv( @@ -96,7 +93,6 @@ describe("env", function () { ); should(env).not.be.null(); env!.registry.url.should.be.equal("https://registry.npmjs.org/some"); - env!.namespace.should.be.equal("org.npmjs"); }); it("custom registry with extra path and splash", async function () { const env = await parseEnv( @@ -109,7 +105,6 @@ describe("env", function () { ); should(env).not.be.null(); env!.registry.url.should.be.equal("https://registry.npmjs.org/some"); - env!.namespace.should.be.equal("org.npmjs"); }); it("custom registry without http", async function () { const env = await parseEnv( @@ -118,7 +113,6 @@ describe("env", function () { ); should(env).not.be.null(); env!.registry.url.should.be.equal("http://registry.npmjs.org"); - env!.namespace.should.be.equal("org.npmjs"); }); it("custom registry with ipv4+port", async function () { const env = await parseEnv( @@ -127,7 +121,6 @@ describe("env", function () { ); should(env).not.be.null(); env!.registry.url.should.be.equal("http://127.0.0.1:4873"); - env!.namespace.should.be.equal("127.0.0.1"); }); it("custom registry with ipv6+port", async function () { const env = await parseEnv( @@ -138,7 +131,6 @@ describe("env", function () { ); should(env).not.be.null(); env!.registry.url.should.be.equal("http://[1:2:3:4:5:6:7:8]:4873"); - env!.namespace.should.be.equal("1:2:3:4:5:6:7:8"); }); it("upstream", async function () { const env = await parseEnv({ _global: { upstream: false } }, false); diff --git a/test/test-ip-address.ts b/test/test-ip-address.ts deleted file mode 100644 index 49e28d4b..00000000 --- a/test/test-ip-address.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { describe } from "mocha"; -import { isIpAddress } from "../src/types/ip-address"; -import should from "should"; - -describe("ip-address", function () { - describe("validate", function () { - [ - "10.20.30.40", - "64.233.160.0", - "2001:0db8:85a3:0000:0000:8a2e:0370:7334", - ].forEach((s) => - it(`"${s}" should be ip-address`, () => should(isIpAddress(s)).be.true()) - ); - - [ - "", - " ", - "hello", - // Missing 4th segment - "64.233.160", - // Deleted some colons - "2001:0db8:85a30000:0000:8a2e0370:7334", - ].forEach((s) => - it(`"${s}" should not be ip-address`, () => - should(isIpAddress(s)).be.false()) - ); - }); -});