Skip to content

Commit a52eac9

Browse files
committed
Prevent 'yarnpkg not found' error from displaying to users.
Closes #39
1 parent 1ca22b8 commit a52eac9

File tree

2 files changed

+35
-30
lines changed

2 files changed

+35
-30
lines changed

create-react-native-app/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "create-react-native-app",
3-
"version": "0.0.5",
3+
"version": "0.0.6",
44
"description": "Create React Native apps with no build configuration.",
55
"license": "BSD-3-Clause",
66
"keywords": ["react-native", "react"],

create-react-native-app/src/index.js

+34-29
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ if (commands.length === 0) {
4040

4141
createApp(commands[0], !!argv.verbose, argv['scripts-version']).then(() => {});
4242

43+
// use yarn if it's available, otherwise use npm
44+
function shouldUseYarn() {
45+
try {
46+
const result = spawn.sync('yarnpkg', ['--version'], { stdio: 'ignore' });
47+
if (result.error || result.status !== 0) {
48+
return false;
49+
}
50+
return true;
51+
} catch (e) {
52+
return false;
53+
}
54+
}
55+
4356
async function createApp(name: string, verbose: boolean, version: ?string): Promise<void> {
4457
const root = path.resolve(name);
4558
const appName = path.basename(root);
@@ -78,45 +91,37 @@ function install(
7891
verbose: boolean,
7992
callback: (code: number, command: string, args: Array<string>) => Promise<void>
8093
): void {
81-
let args = ['add', '--dev', '--exact', '--ignore-optional', packageToInstall];
82-
const proc = spawn('yarnpkg', args, { stdio: 'inherit' });
94+
const useYarn = shouldUseYarn();
95+
let args, cmd, result;
8396

84-
let yarnExists = true;
85-
proc.on('error', function(err) {
86-
if (err.code === 'ENOENT') {
87-
yarnExists = false;
88-
}
89-
});
97+
if (useYarn) {
98+
cmd = 'yarnpkg';
99+
args = ['add'];
90100

91-
proc.on('close', function(code) {
92-
if (yarnExists) {
93-
callback(code, 'yarnpkg', args).then(
94-
() => {},
95-
e => {
96-
throw e;
97-
}
98-
);
99-
return;
101+
if (verbose) {
102+
args.push('--verbose');
100103
}
101-
// No Yarn installed, continuing with npm.
104+
105+
args = args.concat(['--dev', '--exact', '--ignore-optional', packageToInstall]);
106+
result = spawn.sync(cmd, args, { stdio: 'inherit' });
107+
} else {
102108
args = ['install'];
103109

104110
if (verbose) {
105111
args.push('--verbose');
106112
}
107-
113+
cmd = 'npm';
108114
args = args.concat(['--save-dev', '--save-exact', packageToInstall]);
109115

110-
const npmProc = spawn('npm', args, { stdio: 'inherit' });
111-
npmProc.on('close', function(code) {
112-
callback(code, 'npm', args).then(
113-
() => {},
114-
e => {
115-
throw e;
116-
}
117-
);
118-
});
119-
});
116+
result = spawn.sync(cmd, args, { stdio: 'inherit' });
117+
}
118+
119+
callback(result.status, cmd, args).then(
120+
() => {},
121+
e => {
122+
throw e;
123+
}
124+
);
120125
}
121126

122127
async function run(

0 commit comments

Comments
 (0)