From cc6c1aae790542144479c1ffbc4afb1d62947888 Mon Sep 17 00:00:00 2001 From: sunnylqm Date: Tue, 15 Apr 2025 22:39:01 +0800 Subject: [PATCH] check lockfile in workspaces --- package.json | 2 +- src/utils/check-lockfile.ts | 84 +++++++++++++++++++++++++++++++------ 2 files changed, 73 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index 61962ba..8334e9d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-update-cli", - "version": "1.43.6", + "version": "1.44.0", "description": "command line tool for react-native-update (remote updates for react native)", "main": "index.js", "bin": { diff --git a/src/utils/check-lockfile.ts b/src/utils/check-lockfile.ts index 5dbe86b..c0bc0ad 100644 --- a/src/utils/check-lockfile.ts +++ b/src/utils/check-lockfile.ts @@ -1,4 +1,5 @@ import fs from 'node:fs'; +import path from 'node:path'; import { t } from './i18n'; const lockFiles = [ @@ -9,21 +10,80 @@ const lockFiles = [ 'bun.lock', ]; -const existingLockFiles: string[] = []; -export function checkLockFiles() { - for (const file of lockFiles) { - if (fs.existsSync(file)) { - existingLockFiles.push(file); +// Function to check if a package.json has a workspaces field +function hasWorkspaces(dir: string): boolean { + const pkgPath = path.join(dir, 'package.json'); + if (fs.existsSync(pkgPath)) { + try { + const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8')); + return !!pkg.workspaces; + } catch (e) { + // Ignore parsing errors } } - if (existingLockFiles.length === 1) { + return false; +} + +// Helper function to find lock files in a specific directory +function findLockFilesInDir(directory: string): string[] { + const found: string[] = []; + for (const file of lockFiles) { + const filePath = path.join(directory, file); + if (fs.existsSync(filePath)) { + found.push(filePath); + } + } + return found; +} + +export function checkLockFiles() { + const cwd = process.cwd(); + let searchDir = cwd; + let foundLockFiles = findLockFilesInDir(searchDir); + + // If no lock file in cwd, try to find monorepo root and check there + if (foundLockFiles.length === 0) { + // Search upwards for package.json with workspaces + let currentDir = path.dirname(cwd); // Start searching from parent + let projectRootDir: string | null = null; + + while (true) { + if (hasWorkspaces(currentDir)) { + projectRootDir = currentDir; + break; + } + const parentDir = path.dirname(currentDir); + if (parentDir === currentDir) { + // Reached the filesystem root + break; + } + currentDir = parentDir; + } + + // If a potential root was found, switch search directory and re-check + if (projectRootDir) { + searchDir = projectRootDir; + foundLockFiles = findLockFilesInDir(searchDir); + } + // If no projectRootDir found, foundLockFiles remains empty and searchDir remains cwd + } + + // Handle results based on findings in the final searchDir + if (foundLockFiles.length === 1) { + // Successfully found one lock file in the determined searchDir return; } - console.warn(t('lockBestPractice')); - if (existingLockFiles.length === 0) { - throw new Error(t('lockNotFound')); + + if (foundLockFiles.length > 1) { + // Found multiple lock files in the determined searchDir + console.warn(t('lockBestPractice')); + throw new Error( + t('multipleLocksFound', { lockFiles: foundLockFiles.join(', ') }), + ); } - throw new Error( - t('multipleLocksFound', { lockFiles: existingLockFiles.join(', ') }), - ); + + // If we reach here, foundLockFiles.length === 0 + console.warn(t('lockBestPractice')); + // Warn instead of throwing an error if no lock file is found + console.warn(t('lockNotFound')); }