Repository: leon-ai/leon
File: scripts/setup/setup-python-packages.js (JavaScript)

Code before:
      const dotVenvPath = path.join(process.cwd(), 'bridges/python/.venv')
      const pipfileLockPath = path.join(process.cwd(), 'bridges/python/Pipfile.lock')
      const pipfileLockMtime = fs.statSync(pipfileLockPath).mtime
      const isDotVenvExist = fs.existsSync(dotVenvPath)
      const installPythonPackages = async () => {
        // Installing Python packages
        log.info('Installing Python packages from bridges/python/Pipfile...')

        await command('pipenv install --site-packages', { shell: true })
        log.success('Python packages installed')

        log.info('Installing spaCy models...')
        // Find new spaCy models:  https://github.com/explosion/spacy-models/releases
        await Promise.all([
          command('pipenv run spacy download en_core_web_trf-3.3.0 --direct', { shell: true }),
          command('pipenv run spacy download fr_core_news_md-3.3.0 --direct', { shell: true })
        ])

        log.success('spaCy models installed')
      }

      if (!isDotVenvExist) {
        await installPythonPackages()
      } else {
        const dotProjectPath = path.join(process.cwd(), 'bridges/python/.venv/.project')
        const dotProjectMtime = fs.statSync(dotProjectPath).mtime

        // Check if Python deps tree has been modified since the initial setup
        if (pipfileLockMtime > dotProjectMtime) {
          await installPythonPackages()
        } else {
          log.success('Python packages are up-to-date')
        }
      }

      resolve()
    } catch (e) {
      log.error(`Failed to install the Python packages: ${e}`)
      reject(e)
    }
  } else {
    log.error('bridges/python/Pipfile does not exist. Try to pull the project (git pull)')
    reject()
  }
})


Review comment (suggestion):
It happens that `bridges/python/.venv/.project` doesn't exist, so it throws an error when executing `fs.statSync`.
If this file doesn't exist, we install Python packages.

Code after:
      const dotVenvPath = path.join(process.cwd(), 'bridges/python/.venv')
      const pipfileLockPath = path.join(process.cwd(), 'bridges/python/Pipfile.lock')
      const pipfileLockMtime = fs.statSync(pipfileLockPath).mtime
      const isDotVenvExist = fs.existsSync(dotVenvPath)
      const installPythonPackages = async () => {
        // Installing Python packages
        log.info('Installing Python packages from bridges/python/Pipfile...')

        await command('pipenv install --site-packages', { shell: true })
        log.success('Python packages installed')

        log.info('Installing spaCy models...')
        // Find new spaCy models:  https://github.com/explosion/spacy-models/releases
        await Promise.all([
          command('pipenv run spacy download en_core_web_trf-3.3.0 --direct', { shell: true }),
          command('pipenv run spacy download fr_core_news_md-3.3.0 --direct', { shell: true })
        ])

        log.success('spaCy models installed')
      }

      if (!isDotVenvExist) {
        await installPythonPackages()
      } else {
        const dotProjectPath = path.join(process.cwd(), 'bridges/python/.venv/.project')
        if (fs.existsSync(dotProjectPath)) {
          const dotProjectMtime = fs.statSync(dotProjectPath).mtime

          // Check if Python deps tree has been modified since the initial setup
          if (pipfileLockMtime > dotProjectMtime) {
            await installPythonPackages()
          } else {
            log.success('Python packages are up-to-date')
          }
        } else {
          await installPythonPackages()
        }
      }

      resolve()
    } catch (e) {
      log.error(`Failed to install the Python packages: ${e}`)
      reject(e)
    }
  } else {
    log.error('bridges/python/Pipfile does not exist. Try to pull the project (git pull)')
    reject()
  }
})