Skip to content

Commit 16dace2

Browse files
committed
feat: add a run command to bob
1 parent 073e2b7 commit 16dace2

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,38 @@ Example:
192192
["aar", { "reverseJetify": true }]
193193
```
194194

195+
### Commands
196+
197+
The `bob` CLI exposes the following commands:
198+
199+
#### `init`
200+
201+
This configures an existing project to use `bob` by adding the required configuration and dependencies. This is usually run with `npx`:
202+
203+
```sh
204+
npx react-native-builder-bob@latest init
205+
```
206+
207+
#### `build`
208+
209+
This builds the project according to the configuration. This is usually run as part of the package's publishing flow, i.e. in the `prepare` or `prepack` scripts.
210+
211+
```json
212+
"scripts": {
213+
"prepare": "bob build"
214+
}
215+
```
216+
217+
#### `run`
218+
219+
This runs a script either with `npm` or `yarn` depending on the command that was used to run `bob`. If the script doesn't exist under the `scripts` section in `package.json`, then it's forwarded to `npm` or `yarn`. This is useful for using inside `package.json` scripts to avoid coupling them with a specific package manager.
220+
221+
```json
222+
"scripts": {
223+
"bootstrap": "bob run install && bob run --cwd example pods"
224+
}
225+
```
226+
195227
## FAQ
196228

197229
### Why should I compile my project with `react-native-builder-bob`?

packages/react-native-builder-bob/src/index.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import path from 'path';
2+
import os from 'os';
3+
import spawn from 'cross-spawn';
24
import fs from 'fs-extra';
35
import kleur from 'kleur';
46
import dedent from 'dedent';
@@ -427,6 +429,58 @@ yargs
427429
}
428430
}
429431
})
432+
.command(
433+
'run [script...]',
434+
'run a package.json script',
435+
{
436+
cwd: {
437+
type: 'string',
438+
describe: 'specify the working directory',
439+
},
440+
script: {
441+
type: 'array',
442+
},
443+
},
444+
async (argv) => {
445+
const execpath = process.env.npm_execpath;
446+
const cli = execpath?.split('/').pop()?.includes('yarn') ? 'yarn' : 'npm';
447+
const args = argv.script?.map((s) => String(s)) ?? [];
448+
const cwd = argv.cwd
449+
? path.resolve(process.cwd(), argv.cwd)
450+
: process.cwd();
451+
452+
const scripts = Object.keys(
453+
JSON.parse(fs.readFileSync(path.join(cwd, 'package.json'), 'utf-8'))
454+
.scripts || {}
455+
);
456+
457+
if (args[0] && scripts.includes(args[0])) {
458+
// If the script exists in package.json, then prefix with `run`
459+
// e.g. `yarn run build`
460+
args.unshift('run');
461+
}
462+
463+
const options = {
464+
cwd,
465+
env: process.env,
466+
stdio: 'inherit',
467+
encoding: 'utf-8',
468+
shell: os.type() === 'Windows_NT',
469+
} as const;
470+
471+
const dir = path.relative(process.cwd(), cwd);
472+
473+
logger.info(
474+
`Running ${kleur.cyan(`${cli} ${args.join(' ')}`.trim())}${
475+
dir ? ` at ${kleur.blue(dir)}` : ''
476+
}`
477+
);
478+
479+
const result = spawn.sync(cli, args, options);
480+
481+
process.exitCode = result.status ?? undefined;
482+
}
483+
)
430484
.demandCommand()
431485
.recommendCommands()
432486
.strict().argv;

0 commit comments

Comments
 (0)