sublime_standard_tools 0.0.15

A collection of utilities for working with Node.js projects from Rust applications
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
//! # Project Validation Implementation
//!
//! ## What
//! This file implements the `ProjectValidator` struct, providing methods to
//! validate Node.js projects regardless of their type. It checks project
//! structure, dependencies, and configuration consistency.
//!
//! ## How
//! The validator performs comprehensive checks on project structure,
//! package.json validity, package manager consistency, and dependency
//! installation status. It updates project validation status with detailed
//! error and warning information.
//!
//! ## Why
//! Project validation ensures that Node.js projects are properly configured
//! and ready for development. Consistent validation across project types
//! helps identify issues early and provides actionable feedback.

use super::Project;
use super::types::{ProjectDescriptor, ProjectInfo, ProjectValidationStatus};
use crate::error::Result;
use crate::filesystem::{AsyncFileSystem, FileSystemManager};
use package_json::PackageJson;
use std::path::Path;

/// Validates Node.js projects with comprehensive structure and configuration checks.
///
/// This struct provides methods for validating different aspects of Node.js
/// projects, including package.json validity, package manager consistency,
/// dependency installation, and project structure integrity.
///
/// # Type Parameters
///
/// * `F` - An async filesystem implementation that satisfies the `AsyncFileSystem` trait.
///   Defaults to `FileSystemManager` for standard operations.
///
/// # Examples
///
/// ```
/// use sublime_standard_tools::project::{ProjectValidator, ProjectDescriptor};
/// use sublime_standard_tools::filesystem::FileSystemManager;
///
/// let validator = ProjectValidator::new();
/// // Assume project is created elsewhere
/// // validator.validate_project(&mut project).await.unwrap();
/// ```
#[derive(Debug)]
pub struct ProjectValidator<F: AsyncFileSystem = FileSystemManager> {
    /// Filesystem implementation for file operations
    fs: F,
}

impl ProjectValidator<FileSystemManager> {
    /// Creates a new `ProjectValidator` with the default filesystem implementation.
    ///
    /// # Returns
    ///
    /// A new `ProjectValidator` instance using the `FileSystemManager`.
    ///
    /// # Examples
    ///
    /// ```
    /// use sublime_standard_tools::project::ProjectValidator;
    ///
    /// let validator = ProjectValidator::new();
    /// ```
    #[must_use]
    pub fn new() -> Self {
        Self { fs: FileSystemManager::new() }
    }
}

impl<F: AsyncFileSystem> ProjectValidator<F> {
    /// Creates a new `ProjectValidator` with a custom filesystem implementation.
    ///
    /// # Arguments
    ///
    /// * `fs` - The filesystem implementation to use
    ///
    /// # Returns
    ///
    /// A new `ProjectValidator` instance using the provided filesystem.
    ///
    /// # Examples
    ///
    /// ```
    /// use sublime_standard_tools::filesystem::FileSystemManager;
    /// use sublime_standard_tools::project::ProjectValidator;
    ///
    /// let fs = FileSystemManager::new();
    /// let validator = ProjectValidator::with_filesystem(fs);
    /// ```
    #[must_use]
    pub fn with_filesystem(fs: F) -> Self {
        Self { fs }
    }

    /// Validates a project descriptor and updates its validation status.
    ///
    /// This method performs comprehensive validation on a project descriptor,
    /// checking various aspects of the project structure and configuration.
    /// The validation status is updated with detailed error and warning information.
    ///
    /// # Arguments
    ///
    /// * `project` - A mutable reference to the project descriptor to validate
    ///
    /// # Errors
    ///
    /// Returns an [`crate::error::Error`] if:
    /// - An I/O error occurs while reading project files
    /// - Project files cannot be parsed
    /// - The filesystem cannot be accessed
    ///
    /// # Returns
    ///
    /// * `Ok(())` - If validation completed successfully (regardless of validation result)
    /// * `Err(Error)` - If an unexpected error occurred during validation
    ///
    /// # Examples
    ///
    /// ```
    /// use sublime_standard_tools::project::{ProjectValidator, ProjectDescriptor};
    ///
    /// # async fn example(mut project: ProjectDescriptor) -> Result<(), Box<dyn std::error::Error>> {
    /// let validator = ProjectValidator::new();
    /// validator.validate_project(&mut project).await?;
    ///
    /// let info = project.as_project_info();
    /// match info.validation_status() {
    ///     sublime_standard_tools::project::ProjectValidationStatus::Valid => {
    ///         println!("Project is valid");
    ///     }
    ///     sublime_standard_tools::project::ProjectValidationStatus::Warning(warnings) => {
    ///         println!("Project has warnings: {:?}", warnings);
    ///     }
    ///     sublime_standard_tools::project::ProjectValidationStatus::Error(errors) => {
    ///         println!("Project has errors: {:?}", errors);
    ///     }
    ///     _ => {}
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn validate_project(&self, project: &mut ProjectDescriptor) -> Result<()> {
        match project {
            ProjectDescriptor::NodeJs(project) => {
                if project.is_monorepo() {
                    self.validate_monorepo_project(project).await;
                } else {
                    self.validate_simple_project(project).await;
                }
                Ok(())
            }
        }
    }

    /// Validates a simple project.
    ///
    /// This method performs validation specific to simple Node.js projects,
    /// checking package.json validity, package manager consistency, and
    /// dependency installation status.
    ///
    /// # Arguments
    ///
    /// * `project` - A mutable reference to the simple project to validate
    ///
    /// # Errors
    ///
    /// Returns an [`Error`] if:
    /// - An I/O error occurs while reading project files
    /// - Project files cannot be parsed
    /// - The filesystem cannot be accessed
    ///
    /// # Returns
    ///
    /// * `Ok(())` - If validation completed successfully
    /// * `Err(Error)` - If an unexpected error occurred during validation
    async fn validate_simple_project(&self, project: &mut Project) {
        let mut errors = Vec::new();
        let mut warnings = Vec::new();

        // Validate package.json
        self.validate_package_json(project.root(), &mut errors, &mut warnings).await;

        // Validate package manager consistency
        self.validate_package_manager_consistency(project, &mut errors, &mut warnings).await;

        // Validate node_modules and dependencies
        self.validate_dependencies(project, &mut errors, &mut warnings).await;

        // Update validation status
        let status = self.create_validation_status(errors, warnings);
        project.set_validation_status(status);
    }

    /// Validates a monorepo project.
    ///
    /// This method performs validation specific to monorepo projects,
    /// delegating to the monorepo's own validation logic while ensuring
    /// consistency with the unified validation interface.
    ///
    /// # Arguments
    ///
    /// * `monorepo` - A mutable reference to the monorepo descriptor to validate
    ///
    /// # Errors
    ///
    /// Returns an [`Error`] if:
    /// - An I/O error occurs while reading project files
    /// - Project files cannot be parsed
    /// - The filesystem cannot be accessed
    ///
    /// # Returns
    ///
    /// * `Ok(())` - If validation completed successfully
    /// * `Err(Error)` - If an unexpected error occurred during validation
    async fn validate_monorepo_project(&self, project: &mut Project) {
        let mut errors = Vec::new();
        let mut warnings = Vec::new();

        // Validate basic monorepo structure
        if project.internal_dependencies.is_empty() {
            warnings.push("Monorepo detected but no packages found".to_string());
        }

        // Validate that root directory exists
        if !self.fs.exists(project.root()).await {
            errors.push("Monorepo root directory does not exist".to_string());
            project.set_validation_status(ProjectValidationStatus::Error(errors));
            return;
        }

        // Validate each package
        for package in &project.internal_dependencies {
            if self.fs.exists(&package.absolute_path).await {
                // Check if package has its own package.json
                let package_json_path = package.absolute_path.join("package.json");
                if !self.fs.exists(&package_json_path).await {
                    warnings.push(format!("Package '{}' is missing package.json", package.name));
                }
            } else {
                errors.push(format!(
                    "Package '{}' directory does not exist at {}",
                    package.name,
                    package.absolute_path.display()
                ));
            }
        }

        // Validate workspace dependencies exist
        for package in &project.internal_dependencies {
            for dep_name in &package.workspace_dependencies {
                if !project.internal_dependencies.iter().any(|p| p.name == *dep_name) {
                    errors.push(format!(
                        "Package '{}' depends on workspace package '{}' which was not found",
                        package.name, dep_name
                    ));
                }
            }

            for dep_name in &package.workspace_dev_dependencies {
                if !project.internal_dependencies.iter().any(|p| p.name == *dep_name) {
                    warnings.push(format!(
                        "Package '{}' has dev dependency on workspace package '{}' which was not found",
                        package.name, dep_name
                    ));
                }
            }
        }

        // Validate root package.json if present
        if let Some(package_json) = project.package_json() {
            self.validate_package_json_content(package_json, &mut warnings);
        }

        // Validate package manager consistency
        if let Some(package_manager) = project.package_manager() {
            let lock_file_path = package_manager.lock_file_path();
            if !self.fs.exists(&lock_file_path).await {
                warnings.push(format!(
                    "Detected {} but lock file is missing: {}",
                    package_manager.kind().command(),
                    lock_file_path.display()
                ));
            }

            // Check for conflicting lock files
            self.check_conflicting_lock_files(
                project.root(),
                package_manager.kind(),
                &mut warnings,
            )
            .await;
        } else if project.package_json().is_some() {
            warnings.push("Package manager could not be detected".to_string());
        }

        // Create validation status and update project
        let validation_status = if !errors.is_empty() {
            ProjectValidationStatus::Error(errors)
        } else if !warnings.is_empty() {
            ProjectValidationStatus::Warning(warnings)
        } else {
            ProjectValidationStatus::Valid
        };

        // Update the project's validation status
        project.set_validation_status(validation_status);
    }

    /// Validates package.json file existence and format.
    ///
    /// # Arguments
    ///
    /// * `root` - The root directory of the project
    /// * `errors` - Vector to collect validation errors
    /// * `warnings` - Vector to collect validation warnings
    ///
    /// # Errors
    ///
    /// Returns an [`Error`] if filesystem operations fail.
    async fn validate_package_json(
        &self,
        root: &Path,
        errors: &mut Vec<String>,
        warnings: &mut Vec<String>,
    ) {
        let package_json_path = root.join("package.json");

        if !self.fs.exists(&package_json_path).await {
            errors.push("Missing package.json file".to_string());
            return;
        }

        // Try to parse package.json
        match self.fs.read_file_string(&package_json_path).await {
            Ok(content) => {
                if let Err(e) = serde_json::from_str::<PackageJson>(&content) {
                    errors.push(format!("Invalid package.json format: {e}"));
                } else {
                    // Parse successful - check for common issues
                    if let Ok(package_json) = serde_json::from_str::<PackageJson>(&content) {
                        self.validate_package_json_content(&package_json, warnings);
                    }
                }
            }
            Err(e) => {
                errors.push(format!("Failed to read package.json: {e}"));
            }
        }
    }

    /// Validates package.json content for common issues.
    ///
    /// # Arguments
    ///
    /// * `package_json` - The parsed package.json content
    /// * `warnings` - Vector to collect validation warnings
    #[allow(clippy::unused_self)]
    fn validate_package_json_content(
        &self,
        package_json: &PackageJson,
        warnings: &mut Vec<String>,
    ) {
        // Check for empty or default name
        if package_json.name.is_empty() {
            warnings.push("Package name is empty".to_string());
        }

        // Check for default version
        if package_json.version == "1.0.0" {
            warnings.push("Package is using default version (1.0.0)".to_string());
        }

        // Check for missing description
        if package_json.description.is_none() {
            warnings.push("Package description is missing".to_string());
        }

        // Check for missing license
        if package_json.license.is_none() {
            warnings.push("Package license is missing".to_string());
        }
    }

    /// Validates package manager consistency.
    ///
    /// # Arguments
    ///
    /// * `project` - The simple project to validate
    /// * `errors` - Vector to collect validation errors
    /// * `warnings` - Vector to collect validation warnings
    ///
    /// # Errors
    ///
    /// Returns an [`Error`] if filesystem operations fail.
    async fn validate_package_manager_consistency(
        &self,
        project: &Project,
        _errors: &mut [String],
        warnings: &mut Vec<String>,
    ) {
        if let Some(package_manager) = project.package_manager() {
            let lock_file_path = package_manager.lock_file_path();

            if !self.fs.exists(&lock_file_path).await {
                warnings.push(format!(
                    "Detected {} but lock file is missing: {}",
                    package_manager.kind().command(),
                    lock_file_path.display()
                ));
            }

            // Check for conflicting lock files
            self.check_conflicting_lock_files(project.root(), package_manager.kind(), warnings)
                .await;
        } else if project.package_json().is_some() {
            warnings.push("Package manager could not be detected".to_string());
        }
    }

    /// Checks for conflicting package manager lock files.
    ///
    /// # Arguments
    ///
    /// * `root` - The root directory of the project
    /// * `detected_kind` - The detected package manager kind
    /// * `warnings` - Vector to collect validation warnings
    ///
    /// # Errors
    ///
    /// Returns an [`Error`] if filesystem operations fail.
    async fn check_conflicting_lock_files(
        &self,
        root: &Path,
        detected_kind: crate::node::PackageManagerKind,
        warnings: &mut Vec<String>,
    ) {
        use crate::node::PackageManagerKind;

        let lock_files = [
            (PackageManagerKind::Npm, "package-lock.json"),
            (PackageManagerKind::Yarn, "yarn.lock"),
            (PackageManagerKind::Pnpm, "pnpm-lock.yaml"),
            (PackageManagerKind::Bun, "bun.lockb"),
        ];

        for (kind, lock_file) in &lock_files {
            if *kind != detected_kind {
                let lock_path = root.join(lock_file);
                if self.fs.exists(&lock_path).await {
                    warnings.push(format!(
                        "Conflicting lock file found: {} (detected: {})",
                        lock_file,
                        detected_kind.command()
                    ));
                }
            }
        }
    }

    /// Validates dependencies and node_modules.
    ///
    /// # Arguments
    ///
    /// * `project` - The simple project to validate
    /// * `errors` - Vector to collect validation errors
    /// * `warnings` - Vector to collect validation warnings
    ///
    /// # Errors
    ///
    /// Returns an [`Error`] if filesystem operations fail.
    async fn validate_dependencies(
        &self,
        project: &Project,
        _errors: &mut [String],
        warnings: &mut Vec<String>,
    ) {
        if let Some(package_json) = project.package_json() {
            let has_dependencies = package_json.dependencies.is_some()
                || package_json.dev_dependencies.is_some()
                || package_json.peer_dependencies.is_some();

            if has_dependencies {
                let node_modules_path = project.root().join("node_modules");

                if self.fs.exists(&node_modules_path).await {
                    // Check if node_modules is actually a directory by trying to read it
                    // Using filesystem abstraction instead of direct std::fs for consistency
                    match self.fs.read_dir(&node_modules_path).await {
                        Ok(_) => {
                            // Successfully read as directory - it's valid
                        }
                        Err(_) => {
                            // Could not read as directory - either doesn't exist or is not a directory
                            warnings
                                .push("Could not check node_modules directory status".to_string());
                        }
                    }
                } else {
                    warnings.push(
                        "Dependencies declared but node_modules directory is missing".to_string(),
                    );
                }
            }
        }
    }

    /// Creates a validation status from collected errors and warnings.
    ///
    /// # Arguments
    ///
    /// * `errors` - Vector of validation errors
    /// * `warnings` - Vector of validation warnings
    ///
    /// # Returns
    ///
    /// The appropriate `ProjectValidationStatus` based on the errors and warnings.
    #[allow(clippy::unused_self)]
    fn create_validation_status(
        &self,
        errors: Vec<String>,
        warnings: Vec<String>,
    ) -> ProjectValidationStatus {
        if !errors.is_empty() {
            ProjectValidationStatus::Error(errors)
        } else if !warnings.is_empty() {
            ProjectValidationStatus::Warning(warnings)
        } else {
            ProjectValidationStatus::Valid
        }
    }
}

impl<F: AsyncFileSystem> Default for ProjectValidator<F>
where
    F: Default,
{
    fn default() -> Self {
        Self { fs: F::default() }
    }
}