project-validation.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import * as fs from 'fs-extra';
  2. import path from 'path';
  3. /**
  4. * Checks if the current working directory is a valid Vendure project directory.
  5. * This function centralizes the project validation logic used across CLI commands.
  6. */
  7. export function isVendureProjectDirectory(): boolean {
  8. const cwd = process.cwd();
  9. const hasPackageJson = fs.existsSync(path.join(cwd, 'package.json'));
  10. const hasVendureConfig =
  11. fs.existsSync(path.join(cwd, 'vendure-config.ts')) ||
  12. fs.existsSync(path.join(cwd, 'vendure-config.js')) ||
  13. fs.existsSync(path.join(cwd, 'src/vendure-config.ts')) ||
  14. fs.existsSync(path.join(cwd, 'src/vendure-config.js'));
  15. if (hasPackageJson) {
  16. try {
  17. const packageJson = JSON.parse(fs.readFileSync(path.join(cwd, 'package.json'), 'utf-8'));
  18. const dependencies = { ...packageJson.dependencies, ...packageJson.devDependencies };
  19. const hasVendureDeps = Object.keys(dependencies).some(
  20. dep => dep.includes('@vendure/') || dep === 'vendure',
  21. );
  22. return hasVendureDeps && hasVendureConfig;
  23. } catch {
  24. return false;
  25. }
  26. }
  27. return false;
  28. }
  29. export function validateVendureProjectDirectory(): void {
  30. if (!isVendureProjectDirectory()) {
  31. throw new Error(
  32. 'Error: Not in a Vendure project directory. Please run this command from your Vendure project root.',
  33. );
  34. }
  35. }