Skip to content

Commit

Permalink
Supported force-npm option for create-react-native-app. (#307)
Browse files Browse the repository at this point in the history
* Supported force-npm option for create-react-native-app.

* Supported --package-manager option which allows yarn,npm and specific path to command.
  • Loading branch information
metheglin authored and brentvatne committed Jul 17, 2017
1 parent 0c9330c commit 1609cef
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions create-react-native-app/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ const argv = minimist(process.argv.slice(2));
* --version - to print current version
* --verbose - to print npm logs during init
* --scripts-version <alternative package>
* --package-manager <package manager name or path>
* Example of valid values:
* - a specific npm version: "0.22.0-rc1"
* - a .tgz archive from npm: "https://registry.npmjs.org/react-native-scripts/-/react-native-scripts-0.20.0.tgz"
* - a package from `tasks/clean_pack.sh`: "/home/adam/create-react-native-app/react-native-scripts-0.22.0.tgz"
*/
const commands = argv._;
const cwd = process.cwd();
const packageManager = argv['package-manager'];

if (commands.length === 0) {
if (argv.version) {
Expand All @@ -40,8 +42,7 @@ if (commands.length === 0) {

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

// use yarn if it's available, otherwise use npm
function shouldUseYarn() {
function isRespondYarn() {
try {
const result = spawn.sync('yarnpkg', ['--version'], { stdio: 'ignore' });
if (result.error || result.status !== 0) {
Expand All @@ -53,6 +54,31 @@ function shouldUseYarn() {
}
}

// This decides the 'interface' of the package managing command.
// Ex: If it guesses the type of package manager as 'yarn',
// then it executes '(yarn) add' command instead of '(npm) install'.
function packageManagerType() {
const defaultType = 'npm';
const supportedTypes = ['yarn', 'npm'];

if (packageManager) {
let t = supportedTypes.find(type => {
return (packageManager.indexOf(type)>-1);
})
return t ? t : defaultType;
}

return isRespondYarn() ? 'yarn' : defaultType;
}

function packageManagerCmd() {
if ( packageManager ) {
return packageManager;
} else {
return packageManagerType() === 'yarn' ? 'yarnpkg' : 'npm';
}
}

async function createApp(name: string, verbose: boolean, version: ?string): Promise<void> {
const root = path.resolve(name);
const appName = path.basename(root);
Expand All @@ -79,6 +105,7 @@ async function createApp(name: string, verbose: boolean, version: ?string): Prom
await fse.writeFile(path.join(root, 'package.json'), JSON.stringify(packageJson, null, 2));
process.chdir(root);

console.log(`Using package manager as ${packageManagerCmd()} with ${packageManagerType()} interface.`)
console.log('Installing packages. This might take a couple minutes.');
console.log('Installing react-native-scripts...');
console.log();
Expand All @@ -91,11 +118,11 @@ function install(
verbose: boolean,
callback: (code: number, command: string, args: Array<string>) => Promise<void>
): void {
const useYarn = shouldUseYarn();
let args, cmd, result;
const type = packageManagerType();
let args, result;
let cmd = packageManagerCmd();

if (useYarn) {
cmd = 'yarnpkg';
if ( type === 'yarn' ) {
args = ['add'];

if (verbose) {
Expand All @@ -110,7 +137,6 @@ function install(
if (verbose) {
args.push('--verbose');
}
cmd = 'npm';
args = args.concat(['--save-dev', '--save-exact', packageToInstall]);

result = spawn.sync(cmd, args, { stdio: 'inherit' });
Expand Down

0 comments on commit 1609cef

Please sign in to comment.