Skip to content

Commit

Permalink
next: Upgrade package-manager-detector to v0.2.2 (#1319)
Browse files Browse the repository at this point in the history
Co-authored-by: Hunter Johnston <johnstonhuntera@gmail.com>
  • Loading branch information
ieedan and huntabyte authored Oct 25, 2024
1 parent e78c887 commit a148777
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 497 deletions.
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.2",
"sisteransi": "^1.0.5",
"tsup": "^8.0.0",
"type-fest": "^3.13.1",
Expand Down
10 changes: 6 additions & 4 deletions packages/cli/src/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { intro, prettifyList } from "../utils/prompt-helpers.js";
import * as p from "../utils/prompts.js";
import * as registry from "../utils/registry/index.js";
import { transformImports } from "../utils/transformers.js";
import { resolveCommand } from "package-manager-detector/commands";

const highlight = (...args: unknown[]) => color.bold.cyan(...args);

Expand Down Expand Up @@ -266,14 +267,15 @@ async function runAdd(cwd: string, config: cliConfig.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
10 changes: 6 additions & 4 deletions packages/cli/src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import * as registry from "../utils/registry/index.js";
import { resolveImport } from "../utils/resolve-imports.js";
import { syncSvelteKit } from "../utils/sveltekit.js";
import * as templates from "../utils/templates.js";
import { resolveCommand } from "package-manager-detector/commands";

const PROJECT_DEPENDENCIES = [
"tailwind-variants",
Expand Down Expand Up @@ -410,14 +411,15 @@ 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
10 changes: 6 additions & 4 deletions packages/cli/src/commands/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as p from "../utils/prompts.js";
import * as registry from "../utils/registry/index.js";
import { UTILS, UTILS_JS } from "../utils/templates.js";
import { transformImports } from "../utils/transformers.js";
import { resolveCommand } from "package-manager-detector/commands";

const highlight = (msg: string) => color.bold.cyan(msg);

Expand Down Expand Up @@ -230,14 +231,15 @@ async function runUpdate(cwd: string, config: cliConfig.Config, options: UpdateO
}

// 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
16 changes: 10 additions & 6 deletions packages/cli/src/utils/auto-detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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 } from "package-manager-detector";
import * as p from "./prompts.js";
import { cancel } from "./prompt-helpers.js";

Expand Down Expand Up @@ -97,10 +97,14 @@ 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) {
export async function detectPM(cwd: string, prompt: boolean): Promise<Agent | undefined> {
const detectResult = await detect({ cwd });

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

return agent ? COMMANDS[agent] : undefined;
return agent;
}
7 changes: 5 additions & 2 deletions packages/cli/src/utils/get-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ export async function resolveConfigPaths(cwd: string, config: RawConfig) {
);
}

const utilsPath = resolveImport(config.aliases.utils, pathAliases);
const componentsPath = resolveImport(config.aliases.components, pathAliases);
let utilsPath = resolveImport(config.aliases.utils, pathAliases);
let componentsPath = resolveImport(config.aliases.components, pathAliases);
const hooksPath = resolveImport(config.aliases.hooks, pathAliases);
const uiPath = resolveImport(config.aliases.ui, pathAliases);

Expand All @@ -126,6 +126,9 @@ export async function resolveConfigPaths(cwd: string, config: RawConfig) {
if (utilsPath === undefined) throw aliasError("utils", config.aliases.utils);
if (componentsPath === undefined) throw aliasError("components", config.aliases.components);

utilsPath = path.normalize(utilsPath);
componentsPath = path.normalize(componentsPath);

return v.parse(configSchema, {
...config,
resolvedPaths: {
Expand Down
18 changes: 11 additions & 7 deletions packages/cli/src/utils/sveltekit.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
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";

// if it's a SvelteKit project, run `svelte-kit sync` if the `.svelte-kit` dir is missing
Expand All @@ -12,11 +11,16 @@ export async function syncSvelteKit(cwd: string) {
// 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"], {
cwd,
});
let agent = await detect({ cwd });

agent ??= { agent: "npm", name: "npm" };

const cmd = resolveCommand(agent.agent, "execute", ["svelte-kit", "sync"]);
if (cmd) {
await execa(cmd.command, cmd.args, {
cwd,
});
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion packages/cli/test/utils/get-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ describe("getConfig", () => {
});

it("handles cases where the project uses jsconfig.json", async () => {
expect(await getConf("config-jsconfig")).toEqual({
const config = await getConf("config-jsconfig");

expect(config).toEqual({
style: "new-york",
tailwind: {
config: "tailwind.config.js",
Expand Down
Loading

0 comments on commit a148777

Please sign in to comment.