Skip to content

Commit 4584aa9

Browse files
Merge pull request #2 from siguici/refactor/cli-rework
♻️ Refactor: Use Panam for the dependency manager
2 parents ee04b9f + f1da873 commit 4584aa9

5 files changed

Lines changed: 17 additions & 30 deletions

File tree

src/commands/help/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Program } from "../../core.ts";
2-
import { getPmRunCommand } from "../../utils/package-manager.ts";
2+
import pm from "panam/pm";
33

44
type HelpArgs = { _: string[] };
55
type HelpInput = Record<string, never>;
@@ -32,7 +32,7 @@ export class HelpProgram extends Program<HelpArgs, HelpInput> {
3232
}
3333

3434
protected async execute(_input: HelpInput): Promise<number> {
35-
const pmRun = getPmRunCommand();
35+
const pmRun = pm.runCommand();
3636
console.log("Available commands:\n");
3737
for (const cmd of COMMAND_LIST) {
3838
console.log(` ${cmd.name.padEnd(18)} ${cmd.description}`);

src/create-qwik/background-install.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { copyFileSync, mkdirSync, renameSync, rmSync } from "node:fs";
22
import { join, resolve } from "node:path";
3-
import { $ } from "panam/executor";
4-
import { getPackageManagerName } from "../utils/package-manager.ts";
3+
import pm from "panam/pm";
54

65
/**
76
* Result object for a background dependency install.
@@ -43,14 +42,12 @@ export function backgroundInstallDeps(baseAppDir: string, outDir: string): Backg
4342
mkdirSync(tmpInstallDir, { recursive: true });
4443
copyFileSync(join(baseAppDir, "package.json"), join(tmpInstallDir, "package.json"));
4544

46-
const pm = getPackageManagerName();
47-
4845
// Start background install
49-
const proc = $(pm, ["install"], { cwd: tmpInstallDir });
46+
const proc = pm.install({ cwd: tmpInstallDir });
5047

5148
// Track success state
5249
let success: boolean | undefined = undefined;
53-
proc.result.then((r: { status: boolean }) => {
50+
proc.then((r: { status: boolean }) => {
5451
success = r.status;
5552
});
5653

@@ -60,14 +57,12 @@ export function backgroundInstallDeps(baseAppDir: string, outDir: string): Backg
6057
},
6158

6259
async abort(): Promise<void> {
63-
await proc.abort();
6460
rmSync(tmpInstallDir, { recursive: true, force: true });
6561
},
6662

6763
async complete(destDir: string): Promise<boolean> {
68-
const result = await proc.result;
69-
if (!result.status) {
70-
rmSync(tmpInstallDir, { recursive: true, force: true });
64+
if (!this.success) {
65+
this.abort();
7166
return false;
7267
}
7368

src/create-qwik/run-interactive.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { bye } from "../console.ts";
66
import { getRandomJoke } from "../commands/joke/jokes.ts";
77
import { installDeps } from "../integrations/update-app.ts";
88
import { loadAppStarters } from "../integrations/load-app-starters.ts";
9-
import { getPackageManagerName } from "../utils/package-manager.ts";
9+
import { name as detectedPm } from "panam/pm";
1010
import { backgroundInstallDeps } from "./background-install.ts";
1111
import { createApp } from "./create-app.ts";
1212
import { initGitRepo } from "./git-init.ts";
@@ -100,7 +100,6 @@ export async function runCreateInteractiveCli(): Promise<void> {
100100
// -------------------------------------------------------------------------
101101
// Prompt 4: Package manager selection
102102
// -------------------------------------------------------------------------
103-
const detectedPm = getPackageManagerName();
104103
const pmAnswer = await select({
105104
message: "Which package manager do you prefer?",
106105
options: (["npm", "pnpm", "yarn", "bun"] as const).map((pm) => {

src/create-qwik/run-non-interactive.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { resolve } from "node:path";
44
import yargs from "yargs";
55
import { outro } from "../console.ts";
66
import { installDeps } from "../integrations/update-app.ts";
7-
import { getPackageManagerName } from "../utils/package-manager.ts";
7+
import pm from "panam/pm";
88
import { createApp } from "./create-app.ts";
99
import { initGitRepo } from "./git-init.ts";
1010
import { loadAppStarters } from "../integrations/load-app-starters.ts";
@@ -71,7 +71,7 @@ export async function runCreateCli(args: string[]): Promise<void> {
7171
await createApp({
7272
appId: template,
7373
outDir: resolvedOutDir,
74-
pkgManager: getPackageManagerName(),
74+
pkgManager: pm.name,
7575
});
7676

7777
// Initialize git repo with initial commit (CRQW-13)
@@ -82,9 +82,8 @@ export async function runCreateCli(args: string[]): Promise<void> {
8282
await installDeps(resolvedOutDir);
8383
}
8484

85-
const pm = getPackageManagerName();
8685
outro(
87-
`Project created at ${resolvedOutDir}\n\nNext steps:\n cd ${resolvedOutDir}\n ${pm} install\n ${pm} run dev`,
86+
`Project created at ${resolvedOutDir}\n\nNext steps:\n cd ${resolvedOutDir}\n ${pm.name} install\n ${pm.runCommand()} dev`,
8887
);
8988

9089
process.exit(0);

src/integrations/update-app.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
22
import { dirname, join } from "node:path";
3-
import { spawnSync } from "node:child_process";
4-
import { install } from "panam";
3+
import pm from "panam/pm";
54
import type { IntegrationData } from "../types.ts";
6-
import { getPackageManagerName } from "../utils/package-manager.ts";
75

86
/**
97
* Merge scripts, dependencies, and devDependencies from an integration's
@@ -88,23 +86,19 @@ export function integrationHasDeps(integration: IntegrationData): boolean {
8886
* Install dependencies in the given working directory using panam.
8987
*/
9088
export async function installDeps(cwd: string): Promise<void> {
91-
await install({ cwd });
89+
await pm.install({ cwd });
9290
}
9391

9492
/**
9593
* Run a postInstall command in the given working directory.
9694
* Uses npx for npm, or the package manager name directly for pnpm/yarn/bun.
9795
*/
9896
export async function runPostInstall(postInstallCmd: string, cwd: string): Promise<void> {
99-
const parts = postInstallCmd.split(" ");
100-
const [command, ...args] = parts;
97+
const [command] = postInstallCmd.split(" ");
10198
if (!command) return;
10299

103-
const pm = getPackageManagerName();
104-
const executor = pm === "npm" ? "npx" : pm;
105-
106-
const result = spawnSync(executor, [command, ...args], { cwd, stdio: "inherit" });
107-
if (result.status !== 0) {
108-
throw new Error(`Post-install command failed: ${postInstallCmd} (exit code ${result.status})`);
100+
const result = await pm.x(postInstallCmd, { cwd });
101+
if (!result.status) {
102+
throw new Error(`Post-install command failed: ${postInstallCmd}`);
109103
}
110104
}

0 commit comments

Comments
 (0)