Skip to content

Commit

Permalink
feat: Detect if the user is using npm and set import accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
eliassjogreen committed Sep 24, 2024
1 parent bb3f90c commit a814a22
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 20 deletions.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@denosaurs/typefetch",
"version": "0.0.26",
"version": "0.0.27",
"exports": {
".": "./main.ts"
},
Expand Down
24 changes: 14 additions & 10 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import manifest from "./deno.json" with { type: "json" };

export * from "./mod.ts";

const args = parseArgs(Deno.args, {
const parseOptions = {
string: [
"output",
"config",
Expand All @@ -28,13 +28,15 @@ const args = parseArgs(Deno.args, {
alias: { "output": "o", "help": "h", "version": "V" },
default: {
"output": "./typefetch.d.ts",
"import": "https://raw.githubusercontent.com/denosaurs/typefetch/main",
"import": "__npm" in globalThis
? manifest.name
: "https://raw.githubusercontent.com/denosaurs/typefetch/main",
"include-server-urls": true,
"include-absolute-url": false,
"include-relative-url": false,
"experimental-urlsearchparams": false,
},
unknown: (arg, key) => {
unknown: (arg: string, key?: string) => {
if (key === undefined) return;

console.error(
Expand All @@ -43,7 +45,9 @@ const args = parseArgs(Deno.args, {
);
Deno.exit(1);
},
});
} as const;

const args = parseArgs(Deno.args, parseOptions);

if (args.help) {
// deno-fmt-ignore
Expand All @@ -52,14 +56,14 @@ if (args.help) {
`Options:\n` +
` -h, --help Print this help message\n` +
` -V, --version Print the version of TypeFetch\n` +
` -o, --output <PATH> Output file path (default: typefetch.d.ts)\n` +
` -o, --output <PATH> Output file path (default: ${parseOptions.default["output"]})\n` +
` --config <PATH> File path to the tsconfig.json file\n` +
` --import <PATH> Import path for TypeFetch (default: https://raw.githubusercontent.com/denosaurs/typefetch/main)\n` +
` --import <PATH> Import path for TypeFetch (default: ${parseOptions.default["import"]})\n` +
` --base-urls <URLS> A comma separated list of custom base urls for paths to start with\n` +
` --include-server-urls Include server URLs from the schema in the generated paths (default: true)\n` +
` --include-absolute-url Include absolute URLs in the generated paths (default: false)\n` +
` --include-relative-url Include relative URLs in the generated paths (default: false)\n` +
` --experimental-urlsearchparams Enable the experimental fully typed URLSearchParams type (default: false)\n`,
` --include-server-urls Include server URLs from the schema in the generated paths (default: ${parseOptions.default["include-server-urls"]})\n` +
` --include-absolute-url Include absolute URLs in the generated paths (default: ${parseOptions.default["include-absolute-url"]})\n` +
` --include-relative-url Include relative URLs in the generated paths (default: ${parseOptions.default["include-relative-url"]})\n` +
` --experimental-urlsearchparams Enable the experimental fully typed URLSearchParams type (default: ${parseOptions.default["experimental-urlsearchparams"]})\n`,
);
Deno.exit(0);
}
Expand Down
2 changes: 2 additions & 0 deletions node/shims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ export function addEventListener(
process.on("SIGINT", listener);
process.on("uncaughtException", listener);
}

export const __npm = true;
33 changes: 24 additions & 9 deletions scripts/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,35 @@ import { build, emptyDir } from "jsr:@deno/dnt";
await emptyDir("./npm");

await build({
entryPoints: [{
kind: "bin",
name: "typefetch",
path: "./main.ts",
}],
entryPoints: [
{
kind: "bin",
name: "typefetch",
path: "./main.ts",
},
"./types/headers.ts",
"./types/json.ts",
"./types/urlsearchparams.ts",
],
filterDiagnostic: (diagnostic) => {
// Ignore excessively deep and possibly infinite type errors
if (diagnostic.code === 2589) {
return false;
}

return true;
},
scriptModule: false,
outDir: "./npm",
shims: {
deno: true,
timers: true,
custom: [{
globalNames: ["addEventListener"],
module: "./node/shims.ts",
}],
custom: [
{
globalNames: ["addEventListener", "__npm"],
module: "./node/shims.ts",
},
],
},
test: false,
importMap: "./deno.json",
Expand Down

0 comments on commit a814a22

Please sign in to comment.