Skip to content

Commit aa3d845

Browse files
author
Rishabh Karnad
committed
Display react-native-cli version on initialisation for non-expo projects
1 parent 92f4dc2 commit aa3d845

File tree

4 files changed

+28
-18
lines changed

4 files changed

+28
-18
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"chalk": "^2.4.2",
3737
"commander": "^2.20.0",
3838
"ora": "^3.4.0",
39-
"prompt": "^1.0.0"
39+
"prompt": "^1.0.0",
40+
"semver-regex": "^3.1.0"
4041
}
4142
}

src/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,16 @@ program
3636
);
3737
}
3838
} else {
39-
if (!validationObjects.getReactNativeCLIifAvailable()) {
39+
const reactNativeCLIVersion = validationObjects.getReactNativeCLIifAvailable();
40+
if (!reactNativeCLIVersion) {
4041
terminateTheProcess(
41-
"Please globally install react-native-cli dependency"
42+
"Please globally install react-native-cli"
4243
);
4344
return;
45+
} else {
46+
console.log(
47+
chalk.cyan("Using globally installed react-native-cli " + reactNativeCLIVersion + "\n"),
48+
);
4449
}
4550
}
4651
const isProjectNameValidResponse = validationObjects.isProjectNameValid(

src/utils/validation.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const chalk = require("chalk");
22
const execSync = require("child_process").execSync;
3+
const semverRegex = require('semver-regex');
4+
35
const constantObjects = require("./constants");
46

57
function isProjectNameValidForCrna(projectName) {
@@ -73,26 +75,23 @@ function getCrnaVersionIfAvailable() {
7375
}
7476

7577
function getReactNativeCLIifAvailable() {
76-
let crnaVersion = null;
77-
try {
78+
// Get package version command and discard stderr on *nix systems
79+
const processCommand = process.platform.startsWith("win")
80+
? `${constantObjects.rnPackageName} --version`
81+
: `${constantObjects.rnPackageName} --version 2>/dev/null`
82+
83+
try {
7884
// execSync returns a Buffer -> convert to string
79-
if (process.platform.startsWith("win")) {
80-
crnaVersion = (
81-
execSync(`${constantObjects.rnPackageName} --version`).toString() ||
82-
""
83-
).trim();
84-
} else {
85-
crnaVersion = (
86-
execSync(
87-
`${constantObjects.rnPackageName} --version 2>/dev/null`
88-
).toString() || ""
89-
).trim();
90-
}
85+
const commandResult = (execSync(processCommand).toString() || "").trim();
86+
const regexMatches = commandResult.match(semverRegex());
87+
const packageSemver = regexMatches.length > 0
88+
? regexMatches[0] // longest first match
89+
: "";
90+
return packageSemver;
9191
} catch (error) {
9292
console.log(chalk.red("Error In Getting React Native Package Version"));
9393
return null;
9494
}
95-
return crnaVersion;
9695
}
9796

9897
module.exports = {

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,11 @@ rimraf@2.x.x:
288288
dependencies:
289289
glob "^7.1.3"
290290

291+
semver-regex@^3.1.0:
292+
version "3.1.0"
293+
resolved "https://registry.yarnpkg.com/semver-regex/-/semver-regex-3.1.0.tgz#23368cb577bf2668e78bf9c3e8e519fc029ca069"
294+
integrity sha512-xJqZonbAohJmpogzq1Mx7DB4DT3LO+sG5J4i/rcRyZ527dcUqsTVb5T0vM5gwOBf3KO0v7i94EpbdvwSukJyAQ==
295+
291296
signal-exit@^3.0.2:
292297
version "3.0.2"
293298
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d"

0 commit comments

Comments
 (0)