functions
Checkdependencies

CheckDependencies

Function Description

Ensures that all required dependencies for a project are present in the specified output directory. If any dependencies are missing, they are automatically installed.

Function Code

  async function checkDependencies(outputDir: string) {
  const requiredPackages = [
    'next', 'react', 'react-dom', 'nextra', 'nextra-theme-docs', 'typescript', '@types/node',
    'simple-git', 'tree-sitter', 'tree-sitter-typescript'
  ];
  const packageJsonPath = path.join(outputDir, 'package.json');
 
  if (!await fs.pathExists(packageJsonPath)) {
    const packageJsonContent = {
      name: `${path.basename(process.cwd())}-docs`,
      version: '1.0.0',
      private: true,
      scripts: {
        dev: 'next dev',
        build: 'next build',
        start: 'next start'
      },
      dependencies: {
        'next': '^13.4.0',
        'nextra': '^2.8.0',
        'nextra-theme-docs': '^2.8.0',
        'react': '^18.2.0',
        'react-dom': '^18.2.0',
        'simple-git': '^3.22.0',
        'tree-sitter': '^0.20.6',
        'tree-sitter-typescript': '^0.20.3'
      },
      devDependencies: {
        '@types/node': '^20.3.1',
        '@types/react': '^18.2.14',
        'typescript': '^5.1.3'
      }
    };
    await fs.writeFile(packageJsonPath, JSON.stringify(packageJsonContent, null, 2));
  }
 
  const packageJson = require(packageJsonPath);
  const installedPackages = new Set([
    ...Object.keys(packageJson.dependencies || {}),
    ...Object.keys(packageJson.devDependencies || {})
  ]);
 
  const missingPackages = requiredPackages.filter(pkg => !installedPackages.has(pkg));
 
  if (missingPackages.length > 0) {
    console.log(`Missing dependencies detected: ${missingPackages.join(', ')}`);
    console.log('Installing missing packages...');
    try {
      execSync(`npm install ${missingPackages.join(' ')} --save-dev`, {
        stdio: 'inherit',
        cwd: outputDir
      });
      console.log(`Successfully installed: ${missingPackages.join(', ')}`);
    } catch (error) {
      console.error('Failed to install missing packages:', error);
      throw new Error('Dependency installation failed. Please install the required packages manually.');
    }
  } else {
    console.log('All required dependencies are already installed.');
  }
}

Parameters

- `outputDir`: string - The path to the output directory where the function checks for a package.json file and required dependencies.

Return Value

  • Type: Promise<void> This function does not return a value. It ensures the dependencies are installed or throws an error if installation fails.

Usage Example

import { checkDependencies } from "./path/to/module";
 
(async () => {
  try {
    const outputDirectory = "./my-project";
    await checkDependencies(outputDirectory);
    console.log("Dependencies are up-to-date!");
  } catch (error) {
    console.error("An error occurred:", error.message);
  }
})();