Skip to content

Commit f47036c

Browse files
committed
chore: detect package manager asynchronously
1 parent 601d4c8 commit f47036c

File tree

5 files changed

+15
-14
lines changed

5 files changed

+15
-14
lines changed

packages/cli/commands/add/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ export async function runAddCommand(
280280
}
281281

282282
// prepare official addons
283-
let workspace = createWorkspace({ cwd: options.cwd });
283+
let workspace = await createWorkspace({ cwd: options.cwd });
284284
const addonSetupResults = setupAddons(officialAddons, workspace);
285285

286286
// prompt which addons to apply
@@ -312,7 +312,7 @@ export async function runAddCommand(
312312

313313
// add inter-addon dependencies
314314
for (const { addon } of selectedAddons) {
315-
workspace = createWorkspace(workspace);
315+
workspace = await createWorkspace(workspace);
316316

317317
const setupResult = addonSetupResults[addon.id];
318318
const missingDependencies = setupResult.dependsOn.filter(
@@ -464,7 +464,7 @@ export async function runAddCommand(
464464
}
465465

466466
// format modified/created files with prettier (if available)
467-
workspace = createWorkspace(workspace);
467+
workspace = await createWorkspace(workspace);
468468
if (filesToFormat.length > 0 && packageManager && !!workspace.dependencyVersion('prettier')) {
469469
const { start, stop } = p.spinner();
470470
start('Formatting modified files');

packages/cli/commands/add/workspace.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from 'node:path';
33
import * as find from 'empathic/find';
44
import { common, object, type AstTypes } from '@sveltejs/cli-core/js';
55
import { parseScript } from '@sveltejs/cli-core/parsers';
6-
import { detectSync } from 'package-manager-detector';
6+
import { detect } from 'package-manager-detector';
77
import type { OptionValues, PackageManager, Workspace } from '@sveltejs/cli-core';
88
import { TESTING } from '../../utils/env.ts';
99
import { commonFilePaths, getPackageJson, readFile } from './utils.ts';
@@ -14,11 +14,11 @@ type CreateWorkspaceOptions = {
1414
packageManager?: PackageManager;
1515
options?: OptionValues<any>;
1616
};
17-
export function createWorkspace({
17+
export async function createWorkspace({
1818
cwd,
1919
options = {},
20-
packageManager = detectSync({ cwd })?.name ?? getUserAgent() ?? 'npm'
21-
}: CreateWorkspaceOptions): Workspace<any> {
20+
packageManager
21+
}: CreateWorkspaceOptions): Promise<Workspace<any>> {
2222
const resolvedCwd = path.resolve(cwd);
2323
const viteConfigPath = path.join(resolvedCwd, commonFilePaths.viteConfigTS);
2424
let usesTypescript = fs.existsSync(viteConfigPath);
@@ -53,7 +53,7 @@ export function createWorkspace({
5353
return {
5454
cwd: resolvedCwd,
5555
options,
56-
packageManager,
56+
packageManager: packageManager ?? (await detect({ cwd }))?.name ?? getUserAgent() ?? 'npm',
5757
typescript: usesTypescript,
5858
kit: dependencies['@sveltejs/kit'] ? parseKitOptions(resolvedCwd) : undefined,
5959
dependencyVersion: (pkg) => dependencies[pkg]

packages/cli/commands/create.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
} from '@sveltejs/create';
1414
import * as common from '../utils/common.ts';
1515
import { runAddCommand } from './add/index.ts';
16-
import { detectSync, resolveCommand, type AgentName } from 'package-manager-detector';
16+
import { detect, resolveCommand, type AgentName } from 'package-manager-detector';
1717
import {
1818
addPnpmBuildDependendencies,
1919
getUserAgent,
@@ -65,7 +65,8 @@ export const create = new Command('create')
6565
let i = 1;
6666
const initialSteps: string[] = [];
6767
const relative = path.relative(process.cwd(), directory);
68-
const pm = packageManager ?? detectSync({ cwd: directory })?.name ?? getUserAgent() ?? 'npm';
68+
const pm =
69+
packageManager ?? (await detect({ cwd: directory }))?.name ?? getUserAgent() ?? 'npm';
6970
if (relative !== '') {
7071
const pathHasSpaces = relative.includes(' ');
7172
initialSteps.push(

packages/cli/lib/install.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export async function installAddon<Addons extends AddonMap>({
3434
options,
3535
packageManager = 'npm'
3636
}: InstallOptions<Addons>): Promise<ReturnType<typeof applyAddons>> {
37-
const workspace = createWorkspace({ cwd, packageManager });
37+
const workspace = await createWorkspace({ cwd, packageManager });
3838
const addonSetupResults = setupAddons(Object.values(addons), workspace);
3939

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

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

6767
const { files, pnpmBuildDependencies } = await runAddon({
6868
workspace,

packages/cli/utils/package-manager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
AGENTS,
99
COMMANDS,
1010
constructCommand,
11-
detectSync,
11+
detect,
1212
type AgentName
1313
} from 'package-manager-detector';
1414
import { parseJson } from '@sveltejs/cli-core/parsers';
@@ -19,7 +19,7 @@ agentOptions.unshift({ label: 'None', value: undefined });
1919

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

2525
const pm = await p.select({

0 commit comments

Comments
 (0)