Skip to content

Commit a814a22

Browse files
committed
feat: Detect if the user is using npm and set import accordingly
1 parent bb3f90c commit a814a22

File tree

4 files changed

+41
-20
lines changed

4 files changed

+41
-20
lines changed

deno.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@denosaurs/typefetch",
3-
"version": "0.0.26",
3+
"version": "0.0.27",
44
"exports": {
55
".": "./main.ts"
66
},

main.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import manifest from "./deno.json" with { type: "json" };
1111

1212
export * from "./mod.ts";
1313

14-
const args = parseArgs(Deno.args, {
14+
const parseOptions = {
1515
string: [
1616
"output",
1717
"config",
@@ -28,13 +28,15 @@ const args = parseArgs(Deno.args, {
2828
alias: { "output": "o", "help": "h", "version": "V" },
2929
default: {
3030
"output": "./typefetch.d.ts",
31-
"import": "https://raw.githubusercontent.com/denosaurs/typefetch/main",
31+
"import": "__npm" in globalThis
32+
? manifest.name
33+
: "https://raw.githubusercontent.com/denosaurs/typefetch/main",
3234
"include-server-urls": true,
3335
"include-absolute-url": false,
3436
"include-relative-url": false,
3537
"experimental-urlsearchparams": false,
3638
},
37-
unknown: (arg, key) => {
39+
unknown: (arg: string, key?: string) => {
3840
if (key === undefined) return;
3941

4042
console.error(
@@ -43,7 +45,9 @@ const args = parseArgs(Deno.args, {
4345
);
4446
Deno.exit(1);
4547
},
46-
});
48+
} as const;
49+
50+
const args = parseArgs(Deno.args, parseOptions);
4751

4852
if (args.help) {
4953
// deno-fmt-ignore
@@ -52,14 +56,14 @@ if (args.help) {
5256
`Options:\n` +
5357
` -h, --help Print this help message\n` +
5458
` -V, --version Print the version of TypeFetch\n` +
55-
` -o, --output <PATH> Output file path (default: typefetch.d.ts)\n` +
59+
` -o, --output <PATH> Output file path (default: ${parseOptions.default["output"]})\n` +
5660
` --config <PATH> File path to the tsconfig.json file\n` +
57-
` --import <PATH> Import path for TypeFetch (default: https://raw.githubusercontent.com/denosaurs/typefetch/main)\n` +
61+
` --import <PATH> Import path for TypeFetch (default: ${parseOptions.default["import"]})\n` +
5862
` --base-urls <URLS> A comma separated list of custom base urls for paths to start with\n` +
59-
` --include-server-urls Include server URLs from the schema in the generated paths (default: true)\n` +
60-
` --include-absolute-url Include absolute URLs in the generated paths (default: false)\n` +
61-
` --include-relative-url Include relative URLs in the generated paths (default: false)\n` +
62-
` --experimental-urlsearchparams Enable the experimental fully typed URLSearchParams type (default: false)\n`,
63+
` --include-server-urls Include server URLs from the schema in the generated paths (default: ${parseOptions.default["include-server-urls"]})\n` +
64+
` --include-absolute-url Include absolute URLs in the generated paths (default: ${parseOptions.default["include-absolute-url"]})\n` +
65+
` --include-relative-url Include relative URLs in the generated paths (default: ${parseOptions.default["include-relative-url"]})\n` +
66+
` --experimental-urlsearchparams Enable the experimental fully typed URLSearchParams type (default: ${parseOptions.default["experimental-urlsearchparams"]})\n`,
6367
);
6468
Deno.exit(0);
6569
}

node/shims.ts

+2
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,5 @@ export function addEventListener(
2828
process.on("SIGINT", listener);
2929
process.on("uncaughtException", listener);
3030
}
31+
32+
export const __npm = true;

scripts/npm.ts

+24-9
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,35 @@ import { build, emptyDir } from "jsr:@deno/dnt";
55
await emptyDir("./npm");
66

77
await build({
8-
entryPoints: [{
9-
kind: "bin",
10-
name: "typefetch",
11-
path: "./main.ts",
12-
}],
8+
entryPoints: [
9+
{
10+
kind: "bin",
11+
name: "typefetch",
12+
path: "./main.ts",
13+
},
14+
"./types/headers.ts",
15+
"./types/json.ts",
16+
"./types/urlsearchparams.ts",
17+
],
18+
filterDiagnostic: (diagnostic) => {
19+
// Ignore excessively deep and possibly infinite type errors
20+
if (diagnostic.code === 2589) {
21+
return false;
22+
}
23+
24+
return true;
25+
},
1326
scriptModule: false,
1427
outDir: "./npm",
1528
shims: {
1629
deno: true,
1730
timers: true,
18-
custom: [{
19-
globalNames: ["addEventListener"],
20-
module: "./node/shims.ts",
21-
}],
31+
custom: [
32+
{
33+
globalNames: ["addEventListener", "__npm"],
34+
module: "./node/shims.ts",
35+
},
36+
],
2237
},
2338
test: false,
2439
importMap: "./deno.json",

0 commit comments

Comments
 (0)