Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: bump package manager detector #1646

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/blue-cougars-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"shadcn-svelte": patch
---

update `package-manager-detector` version
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"cross-env": "^7.0.3",
"get-tsconfig": "^4.7.3",
"ignore": "^5.3.1",
"package-manager-detector": "^0.1.2",
"package-manager-detector": "^0.2.8",
"sisteransi": "^1.0.5",
"tsup": "^8.0.0",
"type-fest": "^3.13.1",
Expand Down
12 changes: 8 additions & 4 deletions packages/cli/src/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import color from "chalk";
import { Command } from "commander";
import { execa } from "execa";
import * as v from "valibot";
import { resolveCommand } from "package-manager-detector";
import { type Config, getConfig } from "../utils/get-config.js";
import { getEnvProxy } from "../utils/get-env-proxy.js";
import { ConfigError, error, handleError } from "../utils/errors.js";
Expand Down Expand Up @@ -244,14 +245,17 @@ async function runAdd(cwd: string, config: Config, options: AddOptions) {
}

// Install dependencies.
const commands = await detectPM(cwd, options.deps);
if (commands) {
const [pm, add] = commands.add.split(" ") as [string, string];
const pm = await detectPM(cwd, options.deps);
if (pm) {
const add = resolveCommand(pm, "add", ["-D", ...dependencies]);
if (!add) {
throw error(`Could not detect a package manager in ${cwd}.`);
}
tasks.push({
title: `${highlight(pm)}: Installing dependencies`,
enabled: dependencies.size > 0,
async task() {
await execa(pm, [add, "-D", ...dependencies], {
await execa(add.command, add.args, {
cwd,
});
return `Dependencies installed with ${highlight(pm)}`;
Expand Down
12 changes: 8 additions & 4 deletions packages/cli/src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import color from "chalk";
import * as v from "valibot";
import { Command, Option } from "commander";
import { execa } from "execa";
import { resolveCommand } from "package-manager-detector";
import * as cliConfig from "../utils/get-config.js";
import type { Config } from "../utils/get-config.js";
import { error, handleError } from "../utils/errors.js";
Expand Down Expand Up @@ -363,14 +364,17 @@ export async function runInit(cwd: string, config: Config, options: InitOptions)
});

// Install dependencies.
const commands = await detectPM(cwd, options.deps);
if (commands) {
const [pm, add] = commands.add.split(" ") as [string, string];
const pm = await detectPM(cwd, options.deps);
if (pm) {
const add = resolveCommand(pm, "add", ["-D", ...PROJECT_DEPENDENCIES]);
if (!add) {
throw error(`Could not detect a package manager in ${cwd}.`);
}
tasks.push({
title: `${highlight(pm)}: Installing dependencies`,
enabled: options.deps,
async task() {
await execa(pm, [add, "-D", ...PROJECT_DEPENDENCIES], {
await execa(add.command, add.args, {
cwd,
});
return `Dependencies installed with ${highlight(pm)}`;
Expand Down
12 changes: 8 additions & 4 deletions packages/cli/src/commands/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import color from "chalk";
import { Command } from "commander";
import { execa } from "execa";
import * as v from "valibot";
import { resolveCommand } from "package-manager-detector";
import { type Config, getConfig } from "../utils/get-config.js";
import { error, handleError } from "../utils/errors.js";
import { fetchTree, getItemTargetPath, getRegistryIndex, resolveTree } from "../utils/registry";
Expand Down Expand Up @@ -225,14 +226,17 @@ async function runUpdate(cwd: string, config: Config, options: UpdateOptions) {
}

// Install dependencies.
const commands = await detectPM(cwd, true);
if (commands) {
const [pm, add] = commands.add.split(" ") as [string, string];
const pm = await detectPM(cwd, true);
if (pm) {
const add = resolveCommand(pm, "add", ["-D", ...dependencies]);
if (!add) {
throw error(`Could not detect a package manager in ${cwd}.`);
}
tasks.push({
title: `${highlight(pm)}: Installing dependencies`,
enabled: dependencies.size > 0,
async task() {
await execa(pm, [add, "-D", ...dependencies], {
await execa(add.command, add.args, {
cwd,
});
return `Dependencies installed with ${highlight(pm)}`;
Expand Down
14 changes: 8 additions & 6 deletions packages/cli/src/utils/auto-detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import fs from "node:fs";
import path from "node:path";
import ignore, { type Ignore } from "ignore";
import { type TsConfigResult, getTsconfig } from "get-tsconfig";
import { detect } from "package-manager-detector";
import { AGENTS, type Agent, COMMANDS } from "package-manager-detector/agents";
import { AGENTS, type Agent, detect } from "package-manager-detector";
import * as p from "./prompts.js";
import { cancel } from "./prompt-helpers.js";

Expand Down Expand Up @@ -98,9 +97,12 @@ export function detectLanguage(cwd: string): DetectLanguageResult | undefined {

type Options = Array<{ value: Agent | undefined; label: Agent | "None" }>;
export async function detectPM(cwd: string, prompt: boolean) {
let { agent } = await detect({ cwd });

if (agent === undefined && prompt) {
let agent: Agent | undefined;
const detectResult = await detect({ cwd });
if (detectResult != null) {
agent = detectResult.agent;
}
if (detectResult === null && prompt) {
const options: Options = AGENTS.filter((agent) => !agent.includes("@")).map((pm) => ({
value: pm,
label: pm,
Expand All @@ -118,5 +120,5 @@ export async function detectPM(cwd: string, prompt: boolean) {
agent = res;
}

return agent ? COMMANDS[agent] : undefined;
return agent;
}
15 changes: 9 additions & 6 deletions packages/cli/src/utils/sveltekit.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import fs from "node:fs";
import path from "node:path";
import { execa } from "execa";
import { detect } from "package-manager-detector";
import { COMMANDS } from "package-manager-detector/agents";
import { detect, resolveCommand } from "package-manager-detector";
import { loadProjectPackageInfo } from "./get-package-info.js";
import { log } from "./prompts.js";

// if it's a SvelteKit project, run `svelte-kit sync` if the `.svelte-kit` dir is missing
export async function syncSvelteKit(cwd: string) {
const isSvelteKit = isUsingSvelteKit(cwd);
if (isSvelteKit) {
// we'll exit early since syncing is rather slow
if (fs.existsSync(path.join(cwd, ".svelte-kit"))) return;

const { agent } = await detect({ cwd });
const [pm] = COMMANDS[agent ?? "npm"].agent.split(" ") as [string];
await execa(pm === "npm" ? "npx" : pm, ["svelte-kit", "sync"], {
const detectResult = await detect({ cwd });
const cmd = resolveCommand(detectResult?.agent ?? "npm", "execute", ["svelte-kit", "sync"]);
if (!cmd) {
log.warn(`Could not detect a package manager in ${cwd} to sync svelte-kit.`);
return;
}
await execa(cmd.command, cmd.args, {
cwd,
});
}
Expand Down
22 changes: 11 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sites/docs/src/lib/registry/default/ui/sheet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const sheetVariants = tv({
top: "inset-x-0 top-0 border-b",
bottom: "inset-x-0 bottom-0 border-t",
left: "inset-y-0 left-0 h-full w-3/4 border-r sm:max-w-sm",
right: "inset-y-0 right-0 h-full w-3/4 border-l sm:max-w-sm",
right: "inset-y-0 right-0 h-full w-3/4 border-l sm:max-w-sm",
},
},
defaultVariants: {
Expand Down
2 changes: 1 addition & 1 deletion sites/docs/src/lib/registry/new-york/ui/sheet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const sheetVariants = tv({
base: "bg-background fixed z-50 gap-4 p-6 shadow-lg",
variants: {
side: {
top: "inset-x-0 top-0 border-b ",
top: "inset-x-0 top-0 border-b",
bottom: "inset-x-0 bottom-0 border-t",
left: "inset-y-0 left-0 h-full w-3/4 border-r sm:max-w-sm",
right: "inset-y-0 right-0 h-full w-3/4 border-l sm:max-w-sm",
Expand Down
Loading