project.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import path from 'path';
  2. import fs from 'fs';
  3. import { execSync } from 'child_process';
  4. // Added type definitions
  5. interface PackageJson {
  6. dependencies: { [key: string]: string };
  7. devDependencies?: { [key: string]: string };
  8. peerDependencies?: { [key: string]: string };
  9. [key: string]: any;
  10. }
  11. export interface ProjectInfo {
  12. projectPath: string;
  13. packageJsonPath: string;
  14. packageJson: PackageJson;
  15. flowgramVersion: string;
  16. }
  17. export function getProjectInfo(): ProjectInfo {
  18. // get nearest package.json
  19. let projectPath: string = process.cwd();
  20. while (projectPath !== '/' && !fs.existsSync(path.join(projectPath, 'package.json'))) {
  21. projectPath = path.join(projectPath, '..');
  22. }
  23. if (projectPath === '/') {
  24. throw new Error('Please run this command in a valid project');
  25. }
  26. const packageJsonPath: string = path.join(projectPath, 'package.json');
  27. const packageJson: PackageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
  28. // fixed layout or free layout
  29. const flowgramVersion: string | undefined =
  30. packageJson.dependencies['@flowgram.ai/fixed-layout-editor'] ||
  31. packageJson.dependencies['@flowgram.ai/free-layout-editor'] ||
  32. packageJson.dependencies['@flowgram.ai/editor'];
  33. if (!flowgramVersion) {
  34. throw new Error(
  35. 'Please install @flowgram.ai/fixed-layout-editor or @flowgram.ai/free-layout-editor'
  36. );
  37. }
  38. return {
  39. projectPath,
  40. packageJsonPath,
  41. packageJson,
  42. flowgramVersion, // TypeScript will ensure this is string due to the check above
  43. };
  44. }
  45. export function findRushJson(startPath: string): string | null {
  46. let currentPath: string = startPath;
  47. while (currentPath !== '/' && !fs.existsSync(path.join(currentPath, 'rush.json'))) {
  48. currentPath = path.join(currentPath, '..');
  49. }
  50. if (fs.existsSync(path.join(currentPath, 'rush.json'))) {
  51. return path.join(currentPath, 'rush.json');
  52. }
  53. return null;
  54. }
  55. export function installDependencies(packages: string[], projectInfo: ProjectInfo): void {
  56. if (fs.existsSync(path.join(projectInfo.projectPath, 'yarn.lock'))) {
  57. execSync(`yarn add ${packages.join(' ')}`, { stdio: 'inherit' });
  58. return;
  59. }
  60. if (fs.existsSync(path.join(projectInfo.projectPath, 'pnpm-lock.yaml'))) {
  61. execSync(`pnpm add ${packages.join(' ')}`, { stdio: 'inherit' });
  62. return;
  63. }
  64. // rush monorepo
  65. if (findRushJson(projectInfo.projectPath)) {
  66. execSync(`rush add ${packages.map((pkg) => `--package ${pkg}`).join(' ')}`, {
  67. stdio: 'inherit',
  68. });
  69. return;
  70. }
  71. execSync(`npm install ${packages.join(' ')}`, { stdio: 'inherit' });
  72. }