mirror of
https://gitcode.com/github-mirrors/react-native-update-cli.git
synced 2025-09-16 09:41:38 +08:00
check lockfile in workspaces
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "react-native-update-cli",
|
"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)",
|
"description": "command line tool for react-native-update (remote updates for react native)",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"bin": {
|
"bin": {
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
import fs from 'node:fs';
|
import fs from 'node:fs';
|
||||||
|
import path from 'node:path';
|
||||||
import { t } from './i18n';
|
import { t } from './i18n';
|
||||||
|
|
||||||
const lockFiles = [
|
const lockFiles = [
|
||||||
@@ -9,21 +10,80 @@ const lockFiles = [
|
|||||||
'bun.lock',
|
'bun.lock',
|
||||||
];
|
];
|
||||||
|
|
||||||
const existingLockFiles: string[] = [];
|
// Function to check if a package.json has a workspaces field
|
||||||
export function checkLockFiles() {
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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) {
|
for (const file of lockFiles) {
|
||||||
if (fs.existsSync(file)) {
|
const filePath = path.join(directory, file);
|
||||||
existingLockFiles.push(file);
|
if (fs.existsSync(filePath)) {
|
||||||
|
found.push(filePath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (existingLockFiles.length === 1) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (foundLockFiles.length > 1) {
|
||||||
|
// Found multiple lock files in the determined searchDir
|
||||||
console.warn(t('lockBestPractice'));
|
console.warn(t('lockBestPractice'));
|
||||||
if (existingLockFiles.length === 0) {
|
|
||||||
throw new Error(t('lockNotFound'));
|
|
||||||
}
|
|
||||||
throw new Error(
|
throw new Error(
|
||||||
t('multipleLocksFound', { lockFiles: existingLockFiles.join(', ') }),
|
t('multipleLocksFound', { lockFiles: foundLockFiles.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'));
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user