Project Checker
The Project Checker module is responsible for analyzing the project structure and verifying compatibility with the selected component library. It provides functions to check if the current directory contains a valid Vue project and to identify the project's configuration files.
API Reference
checkProject
Signature:
1function checkProject(
2 options: CheckProjectOptions
3): Promise<CheckProjectResult>;
Parameters:
options: CheckProjectOptions
- Configuration options for project checking
path?: string
- The path to the project directory (defaults to current directory)
library?: string
- The component library to check compatibility with
Returns:
Promise<CheckProjectResult>
- A promise that resolves to the check result
isVueProject: boolean
- Whether the directory contains a Vue project
hasViteConfig: boolean
- Whether the project has a Vite configuration file
hasTsConfig: boolean
- Whether the project has a TypeScript configuration file
viteConfigPath?: string
- The path to the Vite configuration file (if found)
tsConfigPath?: string
- The path to the TypeScript configuration file (if found)
packageManager: 'npm' | 'yarn' | 'pnpm'
- The detected package manager
Example:
1import { checkProject } from "ew-auto-import-tool";
2
3async function analyzeProject() {
4 const result = await checkProject({
5 path: "./my-vue-project",
6 library: "element-plus",
7 });
8
9 if (result.isVueProject) {
10 console.log("Valid Vue project detected!");
11 console.log(`Package manager: ${result.packageManager}`);
12 console.log(`Vite config: ${result.viteConfigPath}`);
13 console.log(`TS config: ${result.tsConfigPath}`);
14 } else {
15 console.log("Not a valid Vue project");
16 }
17}
18
19analyzeProject();
Implementation Details
The Project Checker performs the following checks:
-
Vue Project Detection
- Checks for the presence of
vue
in package.json dependencies
- Verifies the existence of key Vue project files
-
Configuration File Detection
- Searches for Vite configuration files (vite.config.js/ts)
- Searches for TypeScript configuration files (tsconfig.json)
-
Package Manager Detection
- Detects whether the project uses npm, yarn, or pnpm based on lock files
Source Code Analysis
The Project Checker uses the fs-extra
library to check for file existence and read package.json. It follows these steps:
- Check if the specified path exists and is a directory
- Read and parse the package.json file to verify Vue dependency
- Look for configuration files using pattern matching
- Determine the package manager by checking for lock files
1// Simplified implementation
2async function checkProject(
3 options: CheckProjectOptions
4): Promise<CheckProjectResult> {
5 const projectPath = options.path || process.cwd();
6
7 // Check if directory exists
8 if (!fs.existsSync(projectPath) || !fs.statSync(projectPath).isDirectory()) {
9 throw new Error(`Invalid project path: ${projectPath}`);
10 }
11
12 // Read package.json
13 const packageJsonPath = path.join(projectPath, "package.json");
14 const hasPackageJson = fs.existsSync(packageJsonPath);
15
16 let isVueProject = false;
17 if (hasPackageJson) {
18 const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
19 isVueProject =
20 !!packageJson.dependencies?.vue || !!packageJson.devDependencies?.vue;
21 }
22
23 // Find configuration files
24 const viteConfigPath = findViteConfig(projectPath);
25 const tsConfigPath = findTsConfig(projectPath);
26
27 // Detect package manager
28 const packageManager = detectPackageManager(projectPath);
29
30 return {
31 isVueProject,
32 hasViteConfig: !!viteConfigPath,
33 hasTsConfig: !!tsConfigPath,
34 viteConfigPath,
35 tsConfigPath,
36 packageManager,
37 };
38}
Error Handling
The Project Checker includes robust error handling to provide clear feedback when issues are encountered:
- Invalid project path errors
- Package.json parsing errors
- Missing required dependencies
These errors are propagated to the caller with descriptive messages to help users troubleshoot issues.
Related Modules