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

cli changes #1235

Closed
wants to merge 3 commits into from
Closed
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
27 changes: 16 additions & 11 deletions packages/cli/src/commands/add.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { existsSync, promises as fs } from "node:fs";
import { EOL } from "node:os";
import path from "node:path";
import process from "node:process";
import color from "chalk";
import { Command } from "commander";
import { execa } from "execa";
import { Command } from "commander";
import color from "chalk";
import * as v from "valibot";
import { handleDependencies } from "../utils/dependencies.js";
import { ConfigError, error, handleError } from "../utils/errors.js";
import { type Config, getConfig } from "../utils/get-config.js";
import { getEnvProxy } from "../utils/get-env-proxy.js";
import { getPackageManager } from "../utils/get-package-manager.js";
import { ConfigError, error, handleError } from "../utils/errors.js";
import { intro, prettifyList } from "../utils/prompt-helpers.js";
import * as p from "../utils/prompts.js";
import {
fetchTree,
getItemTargetPath,
Expand All @@ -17,8 +20,6 @@ import {
resolveTree,
} from "../utils/registry";
import { transformImports } from "../utils/transformers.js";
import * as p from "../utils/prompts.js";
import { intro, prettifyList } from "../utils/prompt-helpers.js";

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

Expand Down Expand Up @@ -244,15 +245,19 @@ async function runAdd(cwd: string, config: Config, options: AddOptions) {
}

// Install dependencies.
const { packageManager, dependencies: missingDeps } = await handleDependencies(
cwd,
dependencies
);

tasks.push({
title: "Installing package dependencies",
enabled: dependencies.size > 0,
title: `${highlight(packageManager)} Installing package dependencies`,
enabled: missingDeps.length > 0,
async task() {
const packageManager = await getPackageManager(cwd);
await execa(packageManager, ["add", ...dependencies], {
await execa(packageManager, ["add", ...missingDeps], {
cwd,
});
return "Dependencies installed";
return `Dependencies installed:${EOL}\t${color.gray(prettifyList(missingDeps))}`;
},
});

Expand Down
33 changes: 19 additions & 14 deletions packages/cli/src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { existsSync, promises as fs } from "node:fs";
import { EOL } from "node:os";
import path from "node:path";
import process from "node:process";
import { execa } from "execa";
import { Command, Option } from "commander";
import color from "chalk";
import * as v from "valibot";
import { Command, Option } from "commander";
import { execa } from "execa";
import * as cliConfig from "../utils/get-config.js";
import type { Config } from "../utils/get-config.js";
import { getPackageManager } from "../utils/get-package-manager.js";
import { type DetectLanguageResult, detectConfigs, detectLanguage } from "../utils/auto-detect.js";
import { handleDependencies } from "../utils/dependencies.js";
import { error, handleError } from "../utils/errors.js";
import { getBaseColors, getRegistryBaseColor, getStyles } from "../utils/registry";
import * as templates from "../utils/templates.js";
import * as p from "../utils/prompts.js";
import type { Config } from "../utils/get-config.js";
import * as cliConfig from "../utils/get-config.js";
import { intro, prettifyList } from "../utils/prompt-helpers.js";
import * as p from "../utils/prompts.js";
import { getBaseColors, getRegistryBaseColor, getStyles } from "../utils/registry";
import { resolveImport } from "../utils/resolve-imports.js";
import { syncSvelteKit } from "../utils/sveltekit.js";
import { type DetectLanguageResult, detectConfigs, detectLanguage } from "../utils/auto-detect.js";
import * as templates from "../utils/templates.js";

const PROJECT_DEPENDENCIES = ["tailwind-variants", "clsx", "tailwind-merge"] as const;
const highlight = (...args: unknown[]) => color.bold.cyan(...args);
Expand Down Expand Up @@ -360,15 +361,19 @@ export async function runInit(cwd: string, config: Config, options: InitOptions)

// Install dependencies.
if (options.deps) {
const { packageManager, dependencies: missingDeps } = await handleDependencies(
cwd,
new Set(PROJECT_DEPENDENCIES)
);

tasks.push({
title: "Installing dependencies",
title: `${highlight(packageManager)} Installing package dependencies`,
enabled: missingDeps.length > 0,
async task() {
const packageManager = await getPackageManager(cwd);

await execa(packageManager, ["add", ...PROJECT_DEPENDENCIES], {
await execa(packageManager, ["add", ...missingDeps], {
cwd,
});
return "Dependencies installed";
return `Dependencies installed:${EOL}\t${color.gray(prettifyList(missingDeps))}`;
},
});
}
Expand Down
29 changes: 17 additions & 12 deletions packages/cli/src/commands/update.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { existsSync, promises as fs } from "node:fs";
import { EOL } from "node:os";
import path from "node:path";
import process from "node:process";
import color from "chalk";
import { Command } from "commander";
import { execa } from "execa";
import { Command } from "commander";
import color from "chalk";
import * as v from "valibot";
import { type Config, getConfig } from "../utils/get-config.js";
import { getPackageManager } from "../utils/get-package-manager.js";
import { handleDependencies } from "../utils/dependencies.js";
import { error, handleError } from "../utils/errors.js";
import { type Config, getConfig } from "../utils/get-config.js";
import { getEnvProxy } from "../utils/get-env-proxy.js";
import { intro, prettifyList } from "../utils/prompt-helpers.js";
import * as p from "../utils/prompts.js";
import { fetchTree, getItemTargetPath, getRegistryIndex, resolveTree } from "../utils/registry";
import { UTILS, UTILS_JS } from "../utils/templates.js";
import { transformImports } from "../utils/transformers.js";
import * as p from "../utils/prompts.js";
import { intro, prettifyList } from "../utils/prompt-helpers.js";
import { getEnvProxy } from "../utils/get-env-proxy.js";

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

Expand Down Expand Up @@ -225,15 +226,19 @@ async function runUpdate(cwd: string, config: Config, options: UpdateOptions) {
}

// Install dependencies.
const { packageManager, dependencies: missingDeps } = await handleDependencies(
cwd,
dependencies
);

tasks.push({
title: "Installing package dependencies",
enabled: dependencies.size > 0,
title: `${highlight(packageManager)} Installing package dependencies`,
enabled: missingDeps.length > 0,
async task() {
const packageManager = await getPackageManager(cwd);
await execa(packageManager, ["add", ...dependencies], {
await execa(packageManager, ["add", ...missingDeps], {
cwd,
});
return "Dependencies installed";
return `Dependencies installed:${EOL}\t${color.gray(prettifyList(missingDeps))}`;
},
});

Expand Down
29 changes: 29 additions & 0 deletions packages/cli/src/utils/dependencies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import fs from "node:fs";
import { findUp } from "find-up";
import type { PackageJson } from "type-fest";
import { getPackageManager } from "./get-package-manager";

export async function handleDependencies(targetDir: string, dependencies: Set<string>) {
let missingDependencies: undefined | Set<string>;
const packageManager = await getPackageManager(targetDir);
const packageJsonPath = await findUp("package.json", { cwd: targetDir });

// read `dependencies` from package.json
if (packageJsonPath && fs.existsSync(packageJsonPath)) {
try {
const pkg = JSON.parse(fs.readFileSync(packageJsonPath, "utf8")) as PackageJson;
missingDependencies = new Set<string>();

dependencies.forEach((dep) => {
if (!pkg.dependencies?.[dep] && !pkg.devDependencies?.[dep]) {
missingDependencies?.add(dep);
}
});
} catch {}
}

return {
packageManager,
dependencies: Array.from(missingDependencies || dependencies),
};
}