Skip to content

feat: add clean option for cleaning build directory #653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions packages/react-native-builder-bob/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArgName, yargs.Options>;

Expand Down Expand Up @@ -507,6 +512,7 @@ yargs

if (argv.target != null) {
buildTarget(
argv.clean,
argv.target,
report,
source as string,
Expand All @@ -516,6 +522,7 @@ yargs
} else {
for (const target of options.targets!) {
buildTarget(
argv.clean,
target,
report,
source as string,
Expand All @@ -530,6 +537,7 @@ yargs
.strict().argv;

async function buildTarget(
clean: boolean,
target: Exclude<Options['targets'], undefined>[number],
report: Report,
source: string,
Expand All @@ -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'),
Expand All @@ -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'),
Expand All @@ -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'),
Expand All @@ -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'),
Expand Down
10 changes: 6 additions & 4 deletions packages/react-native-builder-bob/src/targets/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
12 changes: 8 additions & 4 deletions packages/react-native-builder-bob/src/targets/commonjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,26 @@ type Options = Input & {
sourceMaps?: boolean;
copyFlow?: boolean;
};
clean: boolean;
exclude: string;
};

export default async function build({
clean,
root,
source,
output,
exclude,
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,
Expand Down
12 changes: 8 additions & 4 deletions packages/react-native-builder-bob/src/targets/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,26 @@ type Options = Input & {
sourceMaps?: boolean;
copyFlow?: boolean;
};
clean: boolean;
exclude: string;
};

export default async function build({
clean,
root,
source,
output,
exclude,
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,
Expand Down
14 changes: 9 additions & 5 deletions packages/react-native-builder-bob/src/targets/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Options = Input & {
project?: string;
tsc?: string;
};
clean: boolean;
};

type Field = {
Expand All @@ -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')}`);

Expand Down
Loading