diff --git a/packages/react-native-builder-bob/src/index.ts b/packages/react-native-builder-bob/src/index.ts index 57d2e8211..4a9739e4f 100644 --- a/packages/react-native-builder-bob/src/index.ts +++ b/packages/react-native-builder-bob/src/index.ts @@ -13,13 +13,18 @@ import buildTypescript from './targets/typescript'; import buildCodegen from './targets/codegen'; import type { Options, Report, Target } from './types'; -type ArgName = 'target'; +type ArgName = 'target' | 'clean'; const args = { target: { type: 'string', description: 'The target to build', - choices: ['commonjs', 'module', 'typescript', 'codegen'] satisfies Target[], + choices: ['commonjs', 'module', 'typescript', 'codegen'] satisfies Target[], // Keep existing choices + }, + clean: { + type: 'boolean', + description: 'Clean the build directory before building', + default: true, }, } satisfies Record; @@ -507,6 +512,7 @@ yargs if (argv.target != null) { buildTarget( + argv.clean, argv.target, report, source as string, @@ -516,6 +522,7 @@ yargs } else { for (const target of options.targets!) { buildTarget( + argv.clean, target, report, source as string, @@ -530,6 +537,7 @@ yargs .strict().argv; async function buildTarget( + clean: boolean, target: Exclude[number], report: Report, source: string, @@ -544,6 +552,7 @@ async function buildTarget( switch (targetName) { case 'commonjs': await buildCommonJS({ + clean, root, source: path.resolve(root, source), output: path.resolve(root, output, 'commonjs'), @@ -554,6 +563,7 @@ async function buildTarget( break; case 'module': await buildModule({ + clean, root, source: path.resolve(root, source), output: path.resolve(root, output, 'module'), @@ -564,6 +574,7 @@ async function buildTarget( break; case 'typescript': await buildTypescript({ + clean, root, source: path.resolve(root, source), output: path.resolve(root, output, 'typescript'), @@ -573,6 +584,7 @@ async function buildTarget( break; case 'codegen': await buildCodegen({ + clean, root, source: path.resolve(root, source), output: path.resolve(root, output, 'typescript'), diff --git a/packages/react-native-builder-bob/src/targets/codegen.ts b/packages/react-native-builder-bob/src/targets/codegen.ts index cff50a9e3..bef05399c 100644 --- a/packages/react-native-builder-bob/src/targets/codegen.ts +++ b/packages/react-native-builder-bob/src/targets/codegen.ts @@ -6,14 +6,16 @@ import fs from 'fs-extra'; import path from 'path'; import del from 'del'; -type Options = Input; +type Options = Input & { + clean: boolean; +}; -export default async function build({ root, report }: Options) { +export default async function build({ clean, root, report }: Options) { const packageJsonPath = path.resolve(root, 'package.json'); const packageJson = await fs.readJson(packageJsonPath); const codegenIosPath = packageJson.codegenConfig?.outputDir?.ios; - if (codegenIosPath != null) { + if (codegenIosPath != null && clean) { report.info( `Cleaning up previous iOS codegen native code at ${kleur.blue( path.relative(root, codegenIosPath) @@ -23,7 +25,7 @@ export default async function build({ root, report }: Options) { } const codegenAndroidPath = packageJson.codegenConfig?.outputDir?.android; - if (codegenAndroidPath != null) { + if (codegenAndroidPath != null && clean) { report.info( `Cleaning up previous Android codegen native code at ${kleur.blue( path.relative(root, codegenAndroidPath) diff --git a/packages/react-native-builder-bob/src/targets/commonjs.ts b/packages/react-native-builder-bob/src/targets/commonjs.ts index b38875e5a..e8545763a 100644 --- a/packages/react-native-builder-bob/src/targets/commonjs.ts +++ b/packages/react-native-builder-bob/src/targets/commonjs.ts @@ -12,10 +12,12 @@ type Options = Input & { sourceMaps?: boolean; copyFlow?: boolean; }; + clean: boolean; exclude: string; }; export default async function build({ + clean, root, source, output, @@ -23,11 +25,13 @@ export default async function build({ options, report, }: Options) { - report.info( - `Cleaning up previous build at ${kleur.blue(path.relative(root, output))}` - ); + if (clean) { + report.info( + `Cleaning up previous build at ${kleur.blue(path.relative(root, output))}` + ); - await del([output]); + await del([output]); + } await compile({ ...options, diff --git a/packages/react-native-builder-bob/src/targets/module.ts b/packages/react-native-builder-bob/src/targets/module.ts index ff8cb0716..76d009dc0 100644 --- a/packages/react-native-builder-bob/src/targets/module.ts +++ b/packages/react-native-builder-bob/src/targets/module.ts @@ -12,10 +12,12 @@ type Options = Input & { sourceMaps?: boolean; copyFlow?: boolean; }; + clean: boolean; exclude: string; }; export default async function build({ + clean, root, source, output, @@ -23,11 +25,13 @@ export default async function build({ options, report, }: Options) { - report.info( - `Cleaning up previous build at ${kleur.blue(path.relative(root, output))}` - ); + if (clean) { + report.info( + `Cleaning up previous build at ${kleur.blue(path.relative(root, output))}` + ); - await del([output]); + await del([output]); + } await compile({ ...options, diff --git a/packages/react-native-builder-bob/src/targets/typescript.ts b/packages/react-native-builder-bob/src/targets/typescript.ts index b333ab12d..7c6e637a7 100644 --- a/packages/react-native-builder-bob/src/targets/typescript.ts +++ b/packages/react-native-builder-bob/src/targets/typescript.ts @@ -14,6 +14,7 @@ type Options = Input & { project?: string; tsc?: string; }; + clean: boolean; }; type Field = { @@ -24,17 +25,20 @@ type Field = { }; export default async function build({ - source, + clean, root, + source, output, report, options, }: Options) { - report.info( - `Cleaning up previous build at ${kleur.blue(path.relative(root, output))}` - ); + if (clean) { + report.info( + `Cleaning up previous build at ${kleur.blue(path.relative(root, output))}` + ); - await del([output]); + await del([output]); + } report.info(`Generating type definitions with ${kleur.blue('tsc')}`);