generated from JoshuaKGoldberg/create-typescript-app
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from JoshuaKGoldberg/ownership
feat: add --ownership option
- Loading branch information
Showing
11 changed files
with
263 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { describe, expect, it } from "vitest"; | ||
|
||
import { assertValidOwnership } from "./assertValidOwnership.js"; | ||
|
||
describe("assertValidOwnership", () => { | ||
it("does not throw an error when ownership is undefined", () => { | ||
expect(() => assertValidOwnership(undefined)).not.toThrow(); | ||
}); | ||
|
||
it("does not throw an error when ownership contains valid ownership", () => { | ||
expect(() => assertValidOwnership(["author"])).not.toThrow(); | ||
}); | ||
|
||
it("throws an error when ownership contains invalid ownership", () => { | ||
expect(() => assertValidOwnership(["abc"])).toThrowError( | ||
`Unknown --ownership: abc (must be one of: author, maintainer, publisher)` | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { | ||
PackageOwnershipForm, | ||
packageOwnershipForms, | ||
} from "./packageOwnershipForms.js"; | ||
|
||
export function assertValidOwnership( | ||
ownership: string[] | undefined | ||
): asserts ownership is PackageOwnershipForm[] | undefined { | ||
if (ownership) { | ||
for (const ownershipForm of ownership) { | ||
if (!["author", "maintainer", "publisher"].includes(ownershipForm)) { | ||
throw new Error( | ||
`Unknown --ownership: ${ownershipForm} (must be one of: ${packageOwnershipForms.join( | ||
", " | ||
)})` | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
import { describe, expect, it } from "vitest"; | ||
|
||
import { | ||
UserPackageData, | ||
createUserPackagesFilter, | ||
} from "./createUserPackagesFilter.js"; | ||
|
||
const username = "abc123"; | ||
|
||
const createPackageData = (overrides?: Partial<UserPackageData>) => | ||
({ | ||
author: { name: "" }, | ||
date: new Date().toString(), | ||
maintainers: [], | ||
publisher: { email: "", username: "" }, | ||
...overrides, | ||
} satisfies UserPackageData); | ||
|
||
describe("createUserPackagesFilter", () => { | ||
it("filters a package when its date predates since", () => { | ||
const filter = createUserPackagesFilter({ | ||
ownership: ["author"], | ||
since: new Date(Date.now() - 10_000), | ||
username, | ||
}); | ||
|
||
const actual = filter( | ||
createPackageData({ | ||
author: { name: "", username }, | ||
date: new Date(Date.now() - 20_000).toString(), | ||
}) | ||
); | ||
|
||
expect(actual).toBe(false); | ||
}); | ||
|
||
it("allows a package when its date is after since", () => { | ||
const filter = createUserPackagesFilter({ | ||
ownership: ["author"], | ||
since: new Date(Date.now() - 20_000), | ||
username, | ||
}); | ||
|
||
const actual = filter( | ||
createPackageData({ | ||
author: { name: "", username }, | ||
date: new Date(Date.now() - 10_000).toString(), | ||
}) | ||
); | ||
|
||
expect(actual).toBe(true); | ||
}); | ||
|
||
it("filters a package when author ownership doesn't match", () => { | ||
const filter = createUserPackagesFilter({ | ||
ownership: ["author"], | ||
since: new Date(0), | ||
username, | ||
}); | ||
|
||
const actual = filter( | ||
createPackageData({ | ||
author: { name: "", username: "other" }, | ||
}) | ||
); | ||
|
||
expect(actual).toBe(false); | ||
}); | ||
|
||
it("allows a package when author ownership matches", () => { | ||
const filter = createUserPackagesFilter({ | ||
ownership: ["author"], | ||
since: new Date(0), | ||
username, | ||
}); | ||
|
||
const actual = filter( | ||
createPackageData({ | ||
author: { name: "", username }, | ||
}) | ||
); | ||
|
||
expect(actual).toBe(true); | ||
}); | ||
|
||
it("filters a package when maintainer ownership doesn't match", () => { | ||
const filter = createUserPackagesFilter({ | ||
ownership: ["maintainer"], | ||
since: new Date(0), | ||
username, | ||
}); | ||
|
||
const actual = filter( | ||
createPackageData({ | ||
maintainers: [{ email: "", username: "other" }], | ||
}) | ||
); | ||
|
||
expect(actual).toBe(false); | ||
}); | ||
|
||
it("allows a package when maintainer ownership matches", () => { | ||
const filter = createUserPackagesFilter({ | ||
ownership: ["maintainer"], | ||
since: new Date(0), | ||
username, | ||
}); | ||
|
||
const actual = filter( | ||
createPackageData({ | ||
maintainers: [{ email: "", username }], | ||
}) | ||
); | ||
|
||
expect(actual).toBe(true); | ||
}); | ||
|
||
it("filters a package when publisher ownership doesn't match", () => { | ||
const filter = createUserPackagesFilter({ | ||
ownership: ["publisher"], | ||
since: new Date(0), | ||
username, | ||
}); | ||
|
||
const actual = filter( | ||
createPackageData({ | ||
publisher: { email: "", username: "other" }, | ||
}) | ||
); | ||
|
||
expect(actual).toBe(false); | ||
}); | ||
|
||
it("allows a package when publisher ownership matches", () => { | ||
const filter = createUserPackagesFilter({ | ||
ownership: ["publisher"], | ||
since: new Date(0), | ||
username, | ||
}); | ||
|
||
const actual = filter( | ||
createPackageData({ | ||
publisher: { email: "", username }, | ||
}) | ||
); | ||
|
||
expect(actual).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { PackageData } from "npm-user-packages"; | ||
|
||
import { PackageOwnershipForm } from "./packageOwnershipForms.js"; | ||
|
||
export interface FilterSettings { | ||
ownership: PackageOwnershipForm[]; | ||
since: Date; | ||
username: string; | ||
} | ||
|
||
export type UserPackageData = Pick< | ||
PackageData, | ||
"author" | "date" | "maintainers" | "publisher" | ||
>; | ||
|
||
export function createUserPackagesFilter({ | ||
ownership, | ||
since, | ||
username, | ||
}: FilterSettings) { | ||
return (userPackage: UserPackageData) => { | ||
if (new Date(userPackage.date) < since) { | ||
return false; | ||
} | ||
|
||
if ( | ||
!ownership.some((ownershipForm) => { | ||
switch (ownershipForm) { | ||
case "author": | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
return userPackage.author?.username === username; | ||
|
||
case "maintainer": | ||
return userPackage.maintainers.some( | ||
(maintainer) => maintainer.username === username | ||
); | ||
|
||
case "publisher": | ||
return userPackage.publisher.username === username; | ||
} | ||
}) | ||
) { | ||
return false; | ||
} | ||
|
||
return true; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export type PackageOwnershipForm = "author" | "maintainer" | "publisher"; | ||
|
||
export const packageOwnershipForms = ["author", "maintainer", "publisher"]; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.