Skip to content

Commit cc6c1aa

Browse files
committed
check lockfile in workspaces
1 parent 91b26eb commit cc6c1aa

File tree

2 files changed

+72
-12
lines changed

2 files changed

+72
-12
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-update-cli",
3-
"version": "1.43.6",
3+
"version": "1.44.0",
44
"description": "command line tool for react-native-update (remote updates for react native)",
55
"main": "index.js",
66
"bin": {

src/utils/check-lockfile.ts

Lines changed: 71 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from 'node:fs';
2+
import path from 'node:path';
23
import { t } from './i18n';
34

45
const lockFiles = [
@@ -9,21 +10,80 @@ const lockFiles = [
910
'bun.lock',
1011
];
1112

12-
const existingLockFiles: string[] = [];
13-
export function checkLockFiles() {
13+
// Function to check if a package.json has a workspaces field
14+
function hasWorkspaces(dir: string): boolean {
15+
const pkgPath = path.join(dir, 'package.json');
16+
if (fs.existsSync(pkgPath)) {
17+
try {
18+
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
19+
return !!pkg.workspaces;
20+
} catch (e) {
21+
// Ignore parsing errors
22+
}
23+
}
24+
return false;
25+
}
26+
27+
// Helper function to find lock files in a specific directory
28+
function findLockFilesInDir(directory: string): string[] {
29+
const found: string[] = [];
1430
for (const file of lockFiles) {
15-
if (fs.existsSync(file)) {
16-
existingLockFiles.push(file);
31+
const filePath = path.join(directory, file);
32+
if (fs.existsSync(filePath)) {
33+
found.push(filePath);
1734
}
1835
}
19-
if (existingLockFiles.length === 1) {
36+
return found;
37+
}
38+
39+
export function checkLockFiles() {
40+
const cwd = process.cwd();
41+
let searchDir = cwd;
42+
let foundLockFiles = findLockFilesInDir(searchDir);
43+
44+
// If no lock file in cwd, try to find monorepo root and check there
45+
if (foundLockFiles.length === 0) {
46+
// Search upwards for package.json with workspaces
47+
let currentDir = path.dirname(cwd); // Start searching from parent
48+
let projectRootDir: string | null = null;
49+
50+
while (true) {
51+
if (hasWorkspaces(currentDir)) {
52+
projectRootDir = currentDir;
53+
break;
54+
}
55+
const parentDir = path.dirname(currentDir);
56+
if (parentDir === currentDir) {
57+
// Reached the filesystem root
58+
break;
59+
}
60+
currentDir = parentDir;
61+
}
62+
63+
// If a potential root was found, switch search directory and re-check
64+
if (projectRootDir) {
65+
searchDir = projectRootDir;
66+
foundLockFiles = findLockFilesInDir(searchDir);
67+
}
68+
// If no projectRootDir found, foundLockFiles remains empty and searchDir remains cwd
69+
}
70+
71+
// Handle results based on findings in the final searchDir
72+
if (foundLockFiles.length === 1) {
73+
// Successfully found one lock file in the determined searchDir
2074
return;
2175
}
22-
console.warn(t('lockBestPractice'));
23-
if (existingLockFiles.length === 0) {
24-
throw new Error(t('lockNotFound'));
76+
77+
if (foundLockFiles.length > 1) {
78+
// Found multiple lock files in the determined searchDir
79+
console.warn(t('lockBestPractice'));
80+
throw new Error(
81+
t('multipleLocksFound', { lockFiles: foundLockFiles.join(', ') }),
82+
);
2583
}
26-
throw new Error(
27-
t('multipleLocksFound', { lockFiles: existingLockFiles.join(', ') }),
28-
);
84+
85+
// If we reach here, foundLockFiles.length === 0
86+
console.warn(t('lockBestPractice'));
87+
// Warn instead of throwing an error if no lock file is found
88+
console.warn(t('lockNotFound'));
2989
}

0 commit comments

Comments
 (0)