Dependency Manager
The Dependency Manager module is responsible for installing and managing the dependencies required for auto-import functionality. It provides functions to detect the package manager used in the project and install the necessary packages.
API Reference
installDependencies
Signature:
1function installDependencies(
2 options: InstallDependenciesOptions
3): Promise<InstallDependenciesResult>;
Parameters:
options: InstallDependenciesOptions
- Configuration options for dependency installation
path?: string
- The path to the project directory (defaults to current directory)
library: string
- The component library to install
packageManager?: 'npm' | 'yarn' | 'pnpm'
- The package manager to use (auto-detected if not specified)
Returns:
Promise<InstallDependenciesResult>
- A promise that resolves to the installation result
success: boolean
- Whether the installation was successful
packageManager: 'npm' | 'yarn' | 'pnpm'
- The package manager used
installedPackages: string[]
- The list of installed packages
Example:
1import { installDependencies } from "ew-auto-import-tool";
2
3async function setupDependencies() {
4 try {
5 const result = await installDependencies({
6 path: "./my-vue-project",
7 library: "element-plus",
8 });
9
10 if (result.success) {
11 console.log(
12 `Successfully installed dependencies using ${result.packageManager}`
13 );
14 console.log("Installed packages:", result.installedPackages.join(", "));
15 } else {
16 console.error("Failed to install dependencies");
17 }
18 } catch (error) {
19 console.error("Error installing dependencies:", error);
20 }
21}
22
23setupDependencies();
Implementation Details
The Dependency Manager performs the following operations:
-
Package Manager Detection
- Detects whether the project uses npm, yarn, or pnpm based on lock files
- Falls back to npm if no package manager is detected
-
Dependency Resolution
- Determines the required packages based on the selected component library
- Always includes core packages: unplugin-auto-import, unplugin-vue-components
- Adds library-specific packages (e.g., element-plus for Element Plus)
-
Installation Process
- Executes the appropriate install command based on the detected package manager
- Handles installation errors and provides detailed feedback
Package Manager Commands
The module uses different commands depending on the detected package manager:
Package Manager |
Install Command |
npm |
npm install --save-dev <packages> |
yarn |
yarn add --dev <packages> |
pnpm |
pnpm add --save-dev <packages> |
For component libraries, the module uses the standard installation command without the --save-dev
flag, as these are runtime dependencies.
Source Code Analysis
The Dependency Manager uses Node.js child process execution to run package manager commands. It follows these steps:
- Detect or use the specified package manager
- Determine the packages to install based on the library
- Execute the installation commands
- Parse the output to determine success or failure
1// Simplified implementation
2async function installDependencies(
3 options: InstallDependenciesOptions
4): Promise<InstallDependenciesResult> {
5 const projectPath = options.path || process.cwd();
6 const packageManager =
7 options.packageManager || (await detectPackageManager(projectPath));
8
9 // Determine packages to install
10 const devDependencies = ["unplugin-auto-import", "unplugin-vue-components"];
11 const dependencies = [options.library];
12
13 // Install dev dependencies
14 const devInstallCommand = getInstallCommand(
15 packageManager,
16 devDependencies,
17 true
18 );
19 await executeCommand(devInstallCommand, projectPath);
20
21 // Install runtime dependencies
22 const runtimeInstallCommand = getInstallCommand(
23 packageManager,
24 dependencies,
25 false
26 );
27 await executeCommand(runtimeInstallCommand, projectPath);
28
29 return {
30 success: true,
31 packageManager,
32 installedPackages: [...devDependencies, ...dependencies],
33 };
34}
35
36function getInstallCommand(
37 packageManager: string,
38 packages: string[],
39 isDev: boolean
40): string {
41 const packagesStr = packages.join(" ");
42 switch (packageManager) {
43 case "npm":
44 return `npm install ${isDev ? "--save-dev" : ""} ${packagesStr}`;
45 case "yarn":
46 return `yarn add ${isDev ? "--dev" : ""} ${packagesStr}`;
47 case "pnpm":
48 return `pnpm add ${isDev ? "--save-dev" : ""} ${packagesStr}`;
49 default:
50 throw new Error(`Unsupported package manager: ${packageManager}`);
51 }
52}
Error Handling
The Dependency Manager includes robust error handling to provide clear feedback when issues are encountered:
- Package manager detection errors
- Network connectivity issues
- Package resolution failures
- Installation process errors
These errors are propagated to the caller with descriptive messages to help users troubleshoot issues.
Related Modules