Skip to content

Commit

Permalink
refactor: apply strict typescript checks + fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Oct 4, 2023
1 parent fcf68ad commit 9704865
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 20 deletions.
29 changes: 16 additions & 13 deletions src/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type { GitInfo } from "./types";
export async function download(
url: string,
filePath: string,
options: { headers?: Record<string, string> } = {},
options: { headers?: Record<string, string | undefined> } = {},
) {
const infoPath = filePath + ".json";
const info: { etag?: string } = JSON.parse(
Expand All @@ -29,7 +29,9 @@ export async function download(
// Already downloaded
return;
}
info.etag = etag;
if (typeof etag === "string") {
info.etag = etag;
}

const response = await sendFetch(url, { headers: options.headers });
if (response.status >= 400) {
Expand All @@ -48,23 +50,23 @@ const inputRegex =
/^(?<repo>[\w.-]+\/[\w.-]+)(?<subdir>[^#]+)?(?<ref>#[\w.-]+)?/;

export function parseGitURI(input: string): GitInfo {
const m = input.match(inputRegex)?.groups;
const m = input.match(inputRegex)?.groups || {};
return <GitInfo>{
repo: m.repo,
subdir: m.subdir || "/",
ref: m.ref ? m.ref.slice(1) : "main",
};
}

export function debug(...arguments_) {
export function debug(...args: unknown[]) {
if (process.env.DEBUG) {
console.debug("[giget]", ...arguments_);
console.debug("[giget]", ...args);
}
}

// eslint-disable-next-line no-undef
interface InternalFetchOptions extends Exclude<RequestInit, "headers"> {
headers?: Record<string, string>;
interface InternalFetchOptions extends Omit<RequestInit, "headers"> {
headers?: Record<string, string | undefined>;
agent?: Agent;
}

Expand All @@ -83,11 +85,10 @@ export async function sendFetch(
}
}

if (options?.headers) {
options.headers = normalizeHeaders(options.headers);
}

return await fetch(url, options);
return await fetch(url, {
...options,
headers: normalizeHeaders(options.headers),
});
}

export function cacheDirectory() {
Expand All @@ -96,7 +97,9 @@ export function cacheDirectory() {
: resolve(homedir(), ".cache/giget");
}

export function normalizeHeaders(headers: Record<string, string> = {}) {
export function normalizeHeaders(
headers: Record<string, string | undefined> = {},
) {
const normalized: Record<string, string> = {};
for (const [key, value] of Object.entries(headers)) {
if (!value) {
Expand Down
10 changes: 7 additions & 3 deletions src/giget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export async function downloadTemplate(
? undefined
: registryProvider(options.registry, { auth: options.auth });
let providerName: string =
options.provider || (registryProvider ? "registry" : "github");
options.provider || (registry ? "registry" : "github");
let source: string = input;
const sourceProvierMatch = input.match(sourceProtoRe);
if (sourceProvierMatch) {
Expand All @@ -66,6 +66,10 @@ export async function downloadTemplate(
);
});

if (!template) {
throw new Error(`Failed to resolve template from ${providerName}`);
}

// Sanitize name and defaultDir
template.name = (template.name || "template").replace(/[^\da-z-]/gi, "-");
template.defaultDir = (template.defaultDir || template.name).replace(
Expand All @@ -89,7 +93,7 @@ export async function downloadTemplate(

const temporaryDirectory = resolve(
cacheDirectory(),
options.provider,
providerName,
template.name,
);
const tarPath = resolve(
Expand All @@ -105,7 +109,7 @@ export async function downloadTemplate(
const s = Date.now();
await download(template.tar, tarPath, {
headers: {
authorization: options.auth ? `Bearer ${options.auth}` : undefined,
Authorization: options.auth ? `Bearer ${options.auth}` : undefined,
...normalizeHeaders(template.headers),
},
}).catch((error) => {
Expand Down
3 changes: 1 addition & 2 deletions src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ const DEFAULT_REGISTRY =

export const registryProvider = (
registryEndpoint: string = DEFAULT_REGISTRY,
options?: { auth?: string },
options: { auth?: string } = {},
) => {
options = options || {};
return <TemplateProvider>(async (input) => {
const start = Date.now();
const registryURL = `${registryEndpoint}/${input}.json`;
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface TemplateInfo {
subdir?: string;
url?: string;
defaultDir?: string;
headers?: Record<string, string>;
headers?: Record<string, string | undefined>;

// Added by giget
source?: never;
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Node",
"esModuleInterop": true
"esModuleInterop": true,
"strict": true
},
"include": ["src"]
}

0 comments on commit 9704865

Please sign in to comment.