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

feat: include data: PackageData in estimated packages #34

Merged
merged 4 commits into from
Jun 27, 2023
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"devDependencies": {
"@types/eslint": "^8.37.0",
"@types/node": "^20.3.1",
"@types/npm-user-packages": "^3.0.1",
"@typescript-eslint/eslint-plugin": "^5.59.5",
"@typescript-eslint/parser": "^5.59.5",
"@vitest/coverage-istanbul": "^0.31.0",
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 19 additions & 6 deletions src/fakes.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import { UserPackageData } from "./createUserPackagesFilter.js";
import { PackageData } from "npm-user-packages";

export const createFakePackageData = (overrides?: Partial<UserPackageData>) =>
export const createFakePackageData = (overrides?: Partial<PackageData>) =>
({
author: { name: "" },
date: new Date().toString(),
author: {
name: "",
},
date: "",
description: "",
keywords: [],
links: {
npm: "",
},
maintainers: [],
publisher: { email: "", username: "" },
name: "",
publisher: {
email: "",
username: "",
},
scope: "",
version: "",
...overrides,
} satisfies UserPackageData);
} satisfies PackageData);
6 changes: 1 addition & 5 deletions src/getPackageEstimates.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { PackageEstimate } from "./types.js";

interface PackageEstimateData {
estimated_money: string;
lifted: boolean;
name: string;
platform: "npm";
}

export async function getPackageEstimates(
packageNames: string[]
): Promise<PackageEstimate[]> {
export async function getPackageEstimates(packageNames: string[]) {
const response = await fetch(
"https://tidelift.com/api/depci/estimate/bulk_estimates",
{
Expand Down
11 changes: 7 additions & 4 deletions src/reporters/jsonReporter.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import { describe, expect, it, vi } from "vitest";

import { createFakePackageData } from "../fakes.js";
import { EstimatedPackage } from "../types.js";
import { jsonReporter } from "./jsonReporter.js";

describe("jsonReporter", () => {
it("directly logs from JSON.stringify", () => {
const logger = vi.spyOn(console, "log").mockImplementation(() => undefined);
const packageEstimates = [
const estimatedPackages = [
{
data: createFakePackageData(),
estimatedMoney: 12.34,
lifted: false,
name: "abc123",
},
];
] satisfies EstimatedPackage[];

jsonReporter(packageEstimates);
jsonReporter(estimatedPackages);

expect(logger).toHaveBeenCalledWith(JSON.stringify(packageEstimates));
expect(logger).toHaveBeenCalledWith(JSON.stringify(estimatedPackages));
});
});
6 changes: 3 additions & 3 deletions src/reporters/jsonReporter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PackageEstimate } from "../types.js";
import { EstimatedPackage } from "../types.js";

export function jsonReporter(packageEstimates: PackageEstimate[]) {
console.log(JSON.stringify(packageEstimates));
export function jsonReporter(estimatedPackages: EstimatedPackage[]) {
console.log(JSON.stringify(estimatedPackages));
}
3 changes: 3 additions & 0 deletions src/reporters/textReporter.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import chalk from "chalk";
import { describe, expect, it, vi } from "vitest";

import { createFakePackageData } from "../fakes.js";
import { textReporter } from "./textReporter.js";

describe("textReporter", () => {
Expand All @@ -9,6 +10,7 @@ describe("textReporter", () => {

textReporter([
{
data: createFakePackageData(),
estimatedMoney: 12.34,
lifted: true,
name: "abc123",
Expand All @@ -25,6 +27,7 @@ describe("textReporter", () => {

textReporter([
{
data: createFakePackageData(),
estimatedMoney: 12.34,
lifted: false,
name: "abc123",
Expand Down
14 changes: 7 additions & 7 deletions src/reporters/textReporter.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import chalk from "chalk";

import { PackageEstimate } from "../types.js";
import { EstimatedPackage } from "../types.js";

export function textReporter(packageEstimates: PackageEstimate[]) {
for (const packageEstimate of packageEstimates) {
export function textReporter(estimatedPackages: EstimatedPackage[]) {
for (const estimatedPackage of estimatedPackages) {
const currency = new Intl.NumberFormat("en-US", {
currency: "USD",
style: "currency",
}).format(packageEstimate.estimatedMoney);
}).format(estimatedPackage.estimatedMoney);

if (packageEstimate.lifted) {
if (estimatedPackage.lifted) {
console.log(
chalk.gray(
`✅ ${packageEstimate.name} is already lifted for ${currency}/mo.`
`✅ ${estimatedPackage.name} is already lifted for ${currency}/mo.`
)
);
} else {
console.log(
[
chalk.cyan(`👉 `),
chalk.cyanBright(packageEstimate.name),
chalk.cyanBright(estimatedPackage.name),
` is not yet lifted, but is estimated for `,
chalk.cyanBright(`${currency}/mo`),
`.`,
Expand Down
4 changes: 3 additions & 1 deletion src/tideliftMeUp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ vi.mock("./getNpmWhoami.js", () => ({
},
}));

vi.mock("./getPackageEstimates.js");
vi.mock("./getPackageEstimates.js", () => ({
getPackageEstimates: () => [],
}));

describe("tideliftMeUp", () => {
it("throws an error when --username isn't provided and getNpmWhoami returns undefined", async () => {
Expand Down
14 changes: 11 additions & 3 deletions src/tideliftMeUp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createUserPackagesFilter } from "./createUserPackagesFilter.js";
import { getNpmUserPackages } from "./getNpmUserPackages.js";
import { getNpmWhoami } from "./getNpmWhoami.js";
import { getPackageEstimates } from "./getPackageEstimates.js";
import { PackageOwnership } from "./types.js";
import { EstimatedPackage, PackageOwnership } from "./types.js";

export interface TideliftMeUpSettings {
ownership?: PackageOwnership[];
Expand All @@ -14,7 +14,7 @@ export async function tideliftMeUp({
ownership = ["author", "publisher"],
since = getTwoYearsAgo(),
username,
}: TideliftMeUpSettings = {}) {
}: TideliftMeUpSettings = {}): Promise<EstimatedPackage[]> {
username ??= await getNpmWhoami();
if (!username) {
throw new Error("Either log in to npm or provide a `username`.");
Expand All @@ -23,10 +23,18 @@ export async function tideliftMeUp({
const userPackages = (await getNpmUserPackages(username)).filter(
createUserPackagesFilter({ ownership, since: new Date(since), username })
);
const userPackagesByName = Object.fromEntries(
userPackages.map((userPackage) => [userPackage.name, userPackage])
);

return await getPackageEstimates(
const packageEstimates = await getPackageEstimates(
userPackages.map((userPackage) => userPackage.name)
);

return packageEstimates.map((packageEstimate) => ({
...packageEstimate,
data: userPackagesByName[packageEstimate.name],
}));
}

function getTwoYearsAgo() {
Expand Down
5 changes: 4 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export interface PackageEstimate {
import { PackageData } from "npm-user-packages";

export interface EstimatedPackage {
data: PackageData;
estimatedMoney: number;
lifted: boolean;
name: string;
Expand Down