Skip to content

Commit

Permalink
fix: init when there's a package.json in folder (#1980)
Browse files Browse the repository at this point in the history
* fix: init when there's a package.json in folder

* test: update snapshots

---------

Co-authored-by: szymonrybczak <szymon.rybczak@gmail.com>
  • Loading branch information
thymikee and szymonrybczak committed Jun 22, 2023
1 parent a1cc798 commit 170171a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion __e2e__/__snapshots__/config.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`shows up current config without unnecessary output 1`] = `
{
"root": "<<REPLACED_ROOT>>/TestProject",
"reactNativePath": "<<REPLACED_ROOT>>/TestProject/node_modules/react-native",
"reactNativeVersion": "0.71",
"reactNativeVersion": "0.72",
"dependencies": {},
"commands": [
{
Expand Down
40 changes: 22 additions & 18 deletions packages/cli-config/src/loadConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,25 @@ function getDependencyConfig(
) as DependencyConfig;
}

// Try our best to figure out what version of React Native we're running. This is
// currently being used to get our deeplinks working, so it's only worried with
// the major and minor version.
function getReactNativeVersion(reactNativePath: string) {
try {
let semver = version.current(reactNativePath);
if (semver) {
// Retain only these version, since they correspond with our documentation.
return `${semver.major}.${semver.minor}`;
}
} catch (e) {
// If we don't seem to be in a well formed project, give up quietly.
if (!(e instanceof UnknownProjectError)) {
throw e;
}
}
return 'unknown';
}

/**
* Loads CLI configuration
*/
Expand All @@ -67,7 +86,9 @@ function loadConfig(projectRoot: string = findProjectRoot()): Config {
? path.resolve(projectRoot, userConfig.reactNativePath)
: resolveReactNativePath(projectRoot);
},
reactNativeVersion: 'unknown',
get reactNativeVersion() {
return getReactNativeVersion(initialConfig.reactNativePath);
},
dependencies: userConfig.dependencies,
commands: userConfig.commands,
healthChecks: [],
Expand All @@ -92,22 +113,6 @@ function loadConfig(projectRoot: string = findProjectRoot()): Config {
},
};

// Try our best to figure out what version of React Native we're running. This is
// currently being used to get our deeplinks working, so it's only worried with
// the major and minor version.
try {
let semver = version.current(initialConfig.reactNativePath);
if (semver) {
// Retain only these version, since they correspond with our documentation.
initialConfig.reactNativeVersion = `${semver.major}.${semver.minor}`;
}
} catch (e) {
// If we don't seem to be in a well formed project, give up quietly.
if (!(e instanceof UnknownProjectError)) {
throw e;
}
}

const finalConfig = Array.from(
new Set([
...Object.keys(userConfig.dependencies),
Expand All @@ -117,7 +122,6 @@ function loadConfig(projectRoot: string = findProjectRoot()): Config {
const localDependencyRoot =
userConfig.dependencies[dependencyName] &&
userConfig.dependencies[dependencyName].root;

try {
let root =
localDependencyRoot ||
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-tools/src/releaseChecker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export function current(projectRoot: string): SemVer | undefined {
if (found) {
return found;
}
} catch (_) {
} catch {
throw new UnknownProjectError(projectRoot);
}
return undefined;
Expand Down

0 comments on commit 170171a

Please sign in to comment.