Skip to content

Commit

Permalink
style: format with prettier v3
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Oct 4, 2023
1 parent 082f95b commit 9fdb553
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 37 deletions.
4 changes: 1 addition & 3 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"extends": [
"eslint-config-unjs"
],
"extends": ["eslint-config-unjs"],
"rules": {
"unicorn/prefer-module": 0
}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ You can define additional [custom registry](#custom-registry) providers using `r
import { registryProvider } from "giget";

const themes = registryProvider(
"https://raw.githubusercontent.com/unjs/giget/main/templates"
"https://raw.githubusercontent.com/unjs/giget/main/templates",
);

const { source, dir } = await downloadRepo("themes:test", {
Expand Down
4 changes: 1 addition & 3 deletions renovate.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{
"extends": [
"github>unjs/renovate-config"
]
"extends": ["github>unjs/renovate-config"]
}
10 changes: 5 additions & 5 deletions src/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import type { GitInfo } from "./types";
export async function download(
url: string,
filePath: string,
options: { headers?: Record<string, string> } = {}
options: { headers?: Record<string, string> } = {},
) {
const infoPath = filePath + ".json";
const info: { etag?: string } = JSON.parse(
await readFile(infoPath, "utf8").catch(() => "{}")
await readFile(infoPath, "utf8").catch(() => "{}"),
);
// eslint-disable-next-line unicorn/no-useless-undefined
const headResponse = await sendFetch(url, {
Expand All @@ -34,7 +34,7 @@ export async function download(
const response = await sendFetch(url, { headers: options.headers });
if (response.status >= 400) {
throw new Error(
`Failed to download ${url}: ${response.status} ${response.statusText}`
`Failed to download ${url}: ${response.status} ${response.statusText}`,
);
}

Expand Down Expand Up @@ -70,7 +70,7 @@ interface InternalFetchOptions extends Exclude<RequestInit, "headers"> {

export async function sendFetch(
url: string,
options: InternalFetchOptions = {}
options: InternalFetchOptions = {},
) {
if (!options.agent) {
const proxyEnv =
Expand Down Expand Up @@ -123,7 +123,7 @@ export function startShell(cwd: string) {
cwd = resolve(cwd);
const shell = currentShell();
console.info(
`(experimental) Opening shell in ${relative(process.cwd(), cwd)}...`
`(experimental) Opening shell in ${relative(process.cwd(), cwd)}...`,
);
spawnSync(shell, [], {
cwd,
Expand Down
6 changes: 3 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async function main() {

if (!input || arguments_.help || arguments_.h) {
console.error(
"Usage: npx giget@latest <input> [<dir>] [--force] [--force-clean] [--offline] [--prefer-offline] [--shell] [--registry] [--no-registry] [--verbose] [--cwd] [--auth]"
"Usage: npx giget@latest <input> [<dir>] [--force] [--force-clean] [--offline] [--prefer-offline] [--shell] [--registry] [--no-registry] [--verbose] [--cwd] [--auth]",
);
process.exit(1);
}
Expand All @@ -46,8 +46,8 @@ async function main() {

console.log(
`✨ Successfully cloned ${cyan(r.name || r.url)} to ${cyan(
relative(process.cwd(), r.dir)
)}\n`
relative(process.cwd(), r.dir),
)}\n`,
);

if (arguments_.shell) {
Expand Down
20 changes: 10 additions & 10 deletions src/giget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ export type DownloadTemplateResult = Omit<TemplateInfo, "dir" | "source"> & {

export async function downloadTemplate(
input: string,
options: DownloadTemplateOptions = {}
options: DownloadTemplateOptions = {},
): Promise<DownloadTemplateResult> {
options = defu(
{
registry: process.env.GIGET_REGISTRY,
auth: process.env.GIGET_AUTH,
},
options
options,
);

const registry =
options.registry !== false
? registryProvider(options.registry, { auth: options.auth })
: undefined;
options.registry === false
? undefined
: registryProvider(options.registry, { auth: options.auth });
let providerName: string =
options.provider || (registryProvider ? "registry" : "github");
let source: string = input;
Expand All @@ -62,15 +62,15 @@ export async function downloadTemplate(
.then(() => provider(source, { auth: options.auth }))
.catch((error) => {
throw new Error(
`Failed to download template from ${providerName}: ${error.message}`
`Failed to download template from ${providerName}: ${error.message}`,
);
});

// Sanitize name and defaultDir
template.name = (template.name || "template").replace(/[^\da-z-]/gi, "-");
template.defaultDir = (template.defaultDir || template.name).replace(
/[^\da-z-]/gi,
"-"
"-",
);

const cwd = resolve(options.cwd || ".");
Expand All @@ -90,11 +90,11 @@ export async function downloadTemplate(
const temporaryDirectory = resolve(
cacheDirectory(),
options.provider,
template.name
template.name,
);
const tarPath = resolve(
temporaryDirectory,
(template.version || template.name) + ".tar.gz"
(template.version || template.name) + ".tar.gz",
);

if (options.preferOffline && existsSync(tarPath)) {
Expand All @@ -121,7 +121,7 @@ export async function downloadTemplate(

if (!existsSync(tarPath)) {
throw new Error(
`Tarball not found: ${tarPath} (offline: ${options.offline})`
`Tarball not found: ${tarPath} (offline: ${options.offline})`,
);
}

Expand Down
8 changes: 4 additions & 4 deletions src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const DEFAULT_REGISTRY =

export const registryProvider = (
registryEndpoint: string = DEFAULT_REGISTRY,
options?: { auth?: string }
options?: { auth?: string },
) => {
options = options || {};
return <TemplateProvider>(async (input) => {
Expand All @@ -21,19 +21,19 @@ export const registryProvider = (
});
if (result.status >= 400) {
throw new Error(
`Failed to download ${input} template info from ${registryURL}: ${result.status} ${result.statusText}`
`Failed to download ${input} template info from ${registryURL}: ${result.status} ${result.statusText}`,
);
}
const info = (await result.json()) as TemplateInfo;
if (!info.tar || !info.name) {
throw new Error(
`Invalid template info from ${registryURL}. name or tar fields are missing!`
`Invalid template info from ${registryURL}. name or tar fields are missing!`,
);
}
debug(
`Fetched ${input} template info from ${registryURL} in ${
Date.now() - start
}ms`
}ms`,
);
return info;
});
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ export interface TemplateInfo {

export type TemplateProvider = (
input: string,
options: { auth?: string }
options: { auth?: string },
) => TemplateInfo | Promise<TemplateInfo> | null;
2 changes: 1 addition & 1 deletion test/getgit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("downloadTemplate", () => {
await mkdir(destinationDirectory).catch(() => {});
await writeFile(resolve(destinationDirectory, "test.txt"), "test");
await expect(
downloadTemplate("gh:unjs/template", { dir: destinationDirectory })
downloadTemplate("gh:unjs/template", { dir: destinationDirectory }),
).rejects.toThrow("already exists");
});
});
4 changes: 1 addition & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,5 @@
"moduleResolution": "Node",
"esModuleInterop": true
},
"include": [
"src"
]
"include": ["src"]
}
6 changes: 3 additions & 3 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
coverage: {
reporter: ["text", "clover", "json"]
}
}
reporter: ["text", "clover", "json"],
},
},
});

0 comments on commit 9fdb553

Please sign in to comment.