Skip to content

Commit

Permalink
feat: add _generatedBy to package.json for arethetypeswrong (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Nov 14, 2023
1 parent f20bd68 commit 3d9e465
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
9 changes: 9 additions & 0 deletions lib/package_json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Deno.test("single entrypoint", () => {
scripts: {
test: "node test_runner.js",
},
_generatedBy: "dnt@dev",
});

assertEquals(
Expand Down Expand Up @@ -114,6 +115,7 @@ Deno.test("single entrypoint", () => {
},
},
},
_generatedBy: "dnt@dev",
},
);

Expand Down Expand Up @@ -145,6 +147,7 @@ Deno.test("single entrypoint", () => {
require: undefined,
},
},
_generatedBy: "dnt@dev",
},
);

Expand All @@ -167,6 +170,7 @@ Deno.test("single entrypoint", () => {
"@types/node": versions.nodeTypes,
},
scripts: undefined,
_generatedBy: "dnt@dev",
},
);

Expand Down Expand Up @@ -196,6 +200,7 @@ Deno.test("single entrypoint", () => {
require: undefined,
},
},
_generatedBy: "dnt@dev",
},
);

Expand Down Expand Up @@ -228,6 +233,7 @@ Deno.test("single entrypoint", () => {
require: undefined,
},
},
_generatedBy: "dnt@dev",
},
);
});
Expand Down Expand Up @@ -348,6 +354,7 @@ Deno.test("multiple entrypoints", () => {
},
},
scripts: undefined,
_generatedBy: "dnt@dev",
});
});

Expand Down Expand Up @@ -417,6 +424,7 @@ Deno.test("binary entrypoints", () => {
},
},
scripts: undefined,
_generatedBy: "dnt@dev",
});
});

Expand Down Expand Up @@ -505,6 +513,7 @@ Deno.test("peer dependencies", () => {
"test-dep": "0.1.0",
"@deno/shim-deno": "~0.1.0",
},
_generatedBy: "dnt@dev",
},
null,
2,
Expand Down
9 changes: 8 additions & 1 deletion lib/package_json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import type { EntryPoint, ShimOptions } from "../mod.ts";
import { TransformOutput } from "../transform.ts";
import { PackageJson } from "./types.ts";
import { getDntVersion } from "./utils.ts";

export interface GetPackageJsonOptions {
transformOutput: TransformOutput;
Expand Down Expand Up @@ -150,6 +151,7 @@ export function getPackageJson({
peerDependencies,
devDependencies,
}),
_generatedBy: `dnt@${getDntVersion()}`,
};
return sortObject(final);

Expand Down Expand Up @@ -203,7 +205,12 @@ function sortObject(obj: Record<string, unknown>) {
"exports",
"scripts",
];
const lowPrecedence = ["dependencies", "peerDependencies", "devDependencies"];
const lowPrecedence = [
"dependencies",
"peerDependencies",
"devDependencies",
"_generatedBy",
];
const sortedObj: Record<string, unknown> = {};
const finalEntries: Record<string, unknown> = {};
for (const key of highPrecedence) {
Expand Down
11 changes: 10 additions & 1 deletion lib/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { path } from "./mod.deps.ts";
import { assertEquals, assertRejects } from "./test.deps.ts";
import { runCommand, valueToUrl } from "./utils.ts";
import { getDntVersion, runCommand, valueToUrl } from "./utils.ts";

Deno.test({
name: "should error when command doesn't exist",
Expand Down Expand Up @@ -30,3 +30,12 @@ Deno.test("valueToUrl", () => {
);
assertEquals(valueToUrl("file:///test"), "file:///test");
});

Deno.test("getDntVersion", () => {
assertEquals(getDntVersion("https://deno.land/x/dnt@0.1.0/mod.ts"), "0.1.0");
assertEquals(
getDntVersion("https://deno.land/x/dnt@20.21.22/mod.ts"),
"20.21.22",
);
assertEquals(getDntVersion("file:///test/mod.ts"), "dev");
});
4 changes: 4 additions & 0 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,7 @@ export function valueToUrl(value: string) {
return path.toFileUrl(path.resolve(value)).toString();
}
}

export function getDntVersion(url = import.meta.url) {
return /\/dnt@([0-9]+\.[0-9]+\.[0-9]+)\//.exec(url)?.[1] ?? "dev";
}
8 changes: 8 additions & 0 deletions tests/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ Deno.test("should build test project - basic", async () => {
"@deno/shim-deno": versions.denoShim,
"@deno/sham-weakref": versions.weakRefSham,
},
_generatedBy: "dnt@dev",
});
assertEquals(
output.npmIgnore,
Expand Down Expand Up @@ -161,6 +162,7 @@ Deno.test("should build test project without esm", async () => {
"@deno/shim-deno": versions.denoShim,
"@deno/sham-weakref": versions.weakRefSham,
},
_generatedBy: "dnt@dev",
});
assertEquals(
output.npmIgnore,
Expand Down Expand Up @@ -216,6 +218,7 @@ Deno.test("should build with all options off", async () => {
devDependencies: {
"@types/node": versions.nodeTypes,
},
_generatedBy: "dnt@dev",
});

output.assertNotExists("script/mod.js");
Expand Down Expand Up @@ -297,6 +300,7 @@ Deno.test("should build test project with declarations inline by default", async
picocolors: versions.picocolors,
"@deno/shim-deno": versions.denoShim,
},
_generatedBy: "dnt@dev",
});
});
}
Expand Down Expand Up @@ -333,6 +337,7 @@ Deno.test("should build bin project", async () => {
picocolors: versions.picocolors,
"@deno/shim-deno": versions.denoShim,
},
_generatedBy: "dnt@dev",
});
const expectedText = "#!/usr/bin/env node\n";
assertEquals(
Expand Down Expand Up @@ -431,6 +436,7 @@ Deno.test("not error for TLA when not using CommonJS", async () => {
picocolors: versions.picocolors,
"@deno/shim-deno": versions.denoShim,
},
_generatedBy: "dnt@dev",
});
});
});
Expand Down Expand Up @@ -549,6 +555,7 @@ Deno.test("should build with package mappings", async () => {
picocolors: versions.picocolors,
"@deno/shim-deno": versions.denoShim,
},
_generatedBy: "dnt@dev",
});
assertEquals(
output.npmIgnore,
Expand Down Expand Up @@ -619,6 +626,7 @@ Deno.test("should build with peer dependencies in mappings", async () => {
picocolors: versions.picocolors,
"@deno/shim-deno": versions.denoShim,
},
_generatedBy: "dnt@dev",
});
});
});
Expand Down

0 comments on commit 3d9e465

Please sign in to comment.