Skip to content

Commit f2f3bcc

Browse files
committed
feat: rename script target to custom
1 parent 8e0874b commit f2f3bcc

File tree

5 files changed

+73
-68
lines changed

5 files changed

+73
-68
lines changed

docs/pages/build.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,17 +188,17 @@ Generates the [React Native Codegen](https://reactnative.dev/docs/the-new-archit
188188

189189
You can ensure your Codegen generated scaffold code is stable through different React Native versions by shipping it with your library. You can find more in the [React Native Official Docs](https://reactnative.dev/docs/the-new-architecture/codegen-cli#including-generated-code-into-libraries).
190190

191-
#### `script`
191+
#### `custom`
192192

193-
Allows arbitrary scripts to be called during the build process. There is no guarantee when the script is going to be called or finished. This is useful to call code generators during the build process.
193+
Define a custom build target. This is useful to call code generators during the build process.
194194

195-
##### `run`
195+
##### `script`
196196

197-
`bob` will run the command passed in the `run` option. The build process will throw and exit if the `script` target is defined without the `run` option.
197+
`bob` will run the script passed in the `script` option. The build process **will throw and exit** if the `build` target is defined without this option.
198198

199199
##### `cwd`
200200

201-
You can set the `cwd` (current working directory) option to specify where the command should be called from. This option accepts a path and will default to the path `build` was called from.
201+
You can set the `cwd` (current working directory) option to specify where the command should be called from. This option accepts **a relative path**, and it will default to the path `build` was called from.
202202

203203
##### `clean`
204204

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import buildCommonJS from './targets/commonjs';
1111
import buildModule from './targets/module';
1212
import buildTypescript from './targets/typescript';
1313
import buildCodegen from './targets/codegen';
14+
import customTarget from './targets/custom';
1415
import type { Options, Report, Target } from './types';
15-
import runScript from './targets/script';
1616

1717
type ArgName = 'target';
1818

@@ -585,8 +585,8 @@ async function buildTarget(
585585
report,
586586
});
587587
break;
588-
case 'script':
589-
await runScript({
588+
case 'custom':
589+
await customTarget({
590590
options: targetOptions,
591591
source: path.resolve(root, source),
592592
output: path.resolve(root, output, 'typescript'),
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import kleur from 'kleur';
2+
import path from 'path';
3+
import type { Input } from '../types';
4+
import { spawn } from '../utils/spawn';
5+
import dedent from 'dedent';
6+
import del from 'del';
7+
8+
type Options = Input & {
9+
options?: {
10+
script?: string;
11+
cwd?: string;
12+
clean?: string;
13+
}
14+
};
15+
16+
const NO_SCRIPT_ERROR_MESSAGE = dedent(
17+
`No script provided with the custom target.
18+
Example: ${kleur.green('{["custom", { "script": "yarn generateTypes" }}')}`
19+
)
20+
21+
export default async function customTarget({
22+
options,
23+
root,
24+
report
25+
}: Options) {
26+
if (options?.script === undefined) {
27+
report.error(
28+
NO_SCRIPT_ERROR_MESSAGE
29+
)
30+
process.exit(1)
31+
}
32+
33+
const [scriptBinary, ...scriptParams] = options.script.split(" ");
34+
if (scriptBinary === undefined) {
35+
report.error(
36+
NO_SCRIPT_ERROR_MESSAGE
37+
)
38+
process.exit(1)
39+
}
40+
41+
let cwd = root
42+
if (options.cwd) {
43+
cwd = path.resolve(root, options.cwd)
44+
}
45+
46+
if (options.clean) {
47+
report.info(
48+
`Cleaning up previous custom build at ${kleur.blue(path.relative(root, options.clean))}`
49+
);
50+
51+
await del([path.resolve(cwd, options.clean)])
52+
}
53+
54+
55+
report.info(
56+
`Running ${kleur.blue(options.script)} `
57+
);
58+
59+
await spawn(scriptBinary, scriptParams, {
60+
cwd,
61+
})
62+
63+
report.success(`Ran ${kleur.blue(options.script)} succesfully`)
64+
}

packages/react-native-builder-bob/src/targets/script.ts

Lines changed: 0 additions & 59 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export type Input = {
1313
report: Report;
1414
};
1515

16-
export type Target = 'commonjs' | 'module' | 'typescript' | 'codegen' | 'script';
16+
export type Target = 'commonjs' | 'module' | 'typescript' | 'codegen' | 'custom';
1717

1818
export type Options = {
1919
source?: string;

0 commit comments

Comments
 (0)