I have a custom dev.js script that runs my React Native Android app using react-native run-android.
I want to be able to run:
npm run dev gradle-offline
…and have it automatically pass --offline to Gradle (via React Native CLI).
Current script
const { spawn } = require('child_process');
const os = require('os');
const isGradleOffline = process.argv.includes('gradle-offline');
if (os.type() === 'Linux' || os.type() === 'Darwin') {
spawn(
`ENVFILE=/envs/.env.dev react-native run-android --active-arch-only --appIdSuffix debug ${isGradleOffline ? '-- --offline' : ''}`,
[],
{ shell: true, stdio: 'inherit' }
);
} else if (os.type() === 'Windows_NT') {
spawn(
`SET ENVFILE=/envs/.env.dev && react-native run-android --active-arch-only --appIdSuffix debug ${isGradleOffline ? '-- --offline' : ''}`,
[],
{ shell: true, stdio: 'inherit' }
);
} else {
throw new Error(`Unsupported OS found: ${os.type()}`);
}
Problem
When I run:
npm run dev gradle-offline
I’m not sure if:
process.argvis the correct way to detect the flag-- --offlineis being forwarded correctly to GradleThis is the correct way to structure arguments for
spawn
What I want
A reliable way to:
Detect
gradle-offlinefrom:npm run dev gradle-offlinePass
--offlinecorrectly to Gradle via:react-native run-android -- --offlineMake it work cross-platform (Windows + macOS/Linux)
Expected behavior
npm run dev→ normal buildnpm run dev gradle-offline→ Gradle runs with--offline
Question
What is the correct way to structure this script so that:
arguments from npm are reliably read
Gradle flags are properly forwarded
and this works with cached files consistently when the offline mode is on