Skip to content
Merged
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/quiet-fans-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"sv": patch
---

chore: detect package manager asynchronously
6 changes: 3 additions & 3 deletions packages/cli/commands/add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ export async function runAddCommand(
}

// prepare official addons
let workspace = createWorkspace({ cwd: options.cwd });
let workspace = await createWorkspace({ cwd: options.cwd });
const addonSetupResults = setupAddons(officialAddons, workspace);

// prompt which addons to apply
Expand Down Expand Up @@ -312,7 +312,7 @@ export async function runAddCommand(

// add inter-addon dependencies
for (const { addon } of selectedAddons) {
workspace = createWorkspace(workspace);
workspace = await createWorkspace(workspace);

const setupResult = addonSetupResults[addon.id];
const missingDependencies = setupResult.dependsOn.filter(
Expand Down Expand Up @@ -464,7 +464,7 @@ export async function runAddCommand(
}

// format modified/created files with prettier (if available)
workspace = createWorkspace(workspace);
workspace = await createWorkspace(workspace);
if (filesToFormat.length > 0 && packageManager && !!workspace.dependencyVersion('prettier')) {
const { start, stop } = p.spinner();
start('Formatting modified files');
Expand Down
10 changes: 5 additions & 5 deletions packages/cli/commands/add/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'node:path';
import * as find from 'empathic/find';
import { common, object, type AstTypes } from '@sveltejs/cli-core/js';
import { parseScript } from '@sveltejs/cli-core/parsers';
import { detectSync } from 'package-manager-detector';
import { detect } from 'package-manager-detector';
import type { OptionValues, PackageManager, Workspace } from '@sveltejs/cli-core';
import { TESTING } from '../../utils/env.ts';
import { commonFilePaths, getPackageJson, readFile } from './utils.ts';
Expand All @@ -14,11 +14,11 @@ type CreateWorkspaceOptions = {
packageManager?: PackageManager;
options?: OptionValues<any>;
};
export function createWorkspace({
export async function createWorkspace({
cwd,
options = {},
packageManager = detectSync({ cwd })?.name ?? getUserAgent() ?? 'npm'
}: CreateWorkspaceOptions): Workspace<any> {
packageManager
}: CreateWorkspaceOptions): Promise<Workspace<any>> {
const resolvedCwd = path.resolve(cwd);
const viteConfigPath = path.join(resolvedCwd, commonFilePaths.viteConfigTS);
let usesTypescript = fs.existsSync(viteConfigPath);
Expand Down Expand Up @@ -53,7 +53,7 @@ export function createWorkspace({
return {
cwd: resolvedCwd,
options,
packageManager,
packageManager: packageManager ?? (await detect({ cwd }))?.name ?? getUserAgent() ?? 'npm',
typescript: usesTypescript,
kit: dependencies['@sveltejs/kit'] ? parseKitOptions(resolvedCwd) : undefined,
dependencyVersion: (pkg) => dependencies[pkg]
Expand Down
5 changes: 3 additions & 2 deletions packages/cli/commands/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from '@sveltejs/create';
import * as common from '../utils/common.ts';
import { runAddCommand } from './add/index.ts';
import { detectSync, resolveCommand, type AgentName } from 'package-manager-detector';
import { detect, resolveCommand, type AgentName } from 'package-manager-detector';
import {
addPnpmBuildDependendencies,
getUserAgent,
Expand Down Expand Up @@ -65,7 +65,8 @@ export const create = new Command('create')
let i = 1;
const initialSteps: string[] = [];
const relative = path.relative(process.cwd(), directory);
const pm = packageManager ?? detectSync({ cwd: directory })?.name ?? getUserAgent() ?? 'npm';
const pm =
packageManager ?? (await detect({ cwd: directory }))?.name ?? getUserAgent() ?? 'npm';
if (relative !== '') {
const pathHasSpaces = relative.includes(' ');
initialSteps.push(
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/lib/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export async function installAddon<Addons extends AddonMap>({
options,
packageManager = 'npm'
}: InstallOptions<Addons>): Promise<ReturnType<typeof applyAddons>> {
const workspace = createWorkspace({ cwd, packageManager });
const workspace = await createWorkspace({ cwd, packageManager });
const addonSetupResults = setupAddons(Object.values(addons), workspace);

return await applyAddons({ addons, workspace, options, addonSetupResults });
Expand Down Expand Up @@ -62,7 +62,7 @@ export async function applyAddons({
const ordered = orderAddons(mapped, addonSetupResults);

for (const addon of ordered) {
workspace = createWorkspace({ ...workspace, options: options[addon.id] });
workspace = await createWorkspace({ ...workspace, options: options[addon.id] });

const { files, pnpmBuildDependencies } = await runAddon({
workspace,
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/utils/package-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
AGENTS,
COMMANDS,
constructCommand,
detectSync,
detect,
type AgentName
} from 'package-manager-detector';
import { parseJson } from '@sveltejs/cli-core/parsers';
Expand All @@ -19,7 +19,7 @@ agentOptions.unshift({ label: 'None', value: undefined });

type PackageManagerOptions = Array<{ value: AgentName | undefined; label: AgentName | 'None' }>;
export async function packageManagerPrompt(cwd: string): Promise<AgentName | undefined> {
const detected = detectSync({ cwd });
const detected = await detect({ cwd });
const agent = detected?.name ?? getUserAgent();

const pm = await p.select({
Expand Down