1
0
mirror of https://gitcode.com/github-mirrors/react-native-update-cli.git synced 2025-09-16 01:41:37 +08:00
Code Issues Packages Projects Releases Wiki Activity GitHub Gitee

check lockfile in workspaces

This commit is contained in:
sunnylqm
2025-04-15 22:39:01 +08:00
parent 91b26ebc87
commit cc6c1aae79
2 changed files with 73 additions and 13 deletions

View File

@@ -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": {

View File

@@ -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'));
}