Skip to content

Commit

Permalink
feat: move bob codegen to bob build --target codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
atlj committed Jul 5, 2024
1 parent 481f4d7 commit 70dc19f
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ if (isNewArchitectureEnabled()) {
// Since our library doesn't invoke codegen automatically we need to do it here.
tasks.register('invokeLibraryCodegen', Exec) {
workingDir "$rootDir/../../"
commandLine "yarn", "codegen"
commandLine "npx", "bob", "build", "--target codegen"
}
preBuild.dependsOn invokeLibraryCodegen
}`;
Expand All @@ -21,14 +21,14 @@ const XCODE_INVOKE_CODEGEN_ACTION = `
ActionType = "Xcode.IDEStandardExecutionActionsCore.ExecutionActionType.ShellScriptAction">
<ActionContent
title = "Invoke Codegen"
scriptText = "cd &quot;$WORKSPACE_PATH/../../../&quot; &amp;&amp; yarn codegen&#10;">
scriptText = "cd &quot;$WORKSPACE_PATH/../../../&quot; &amp;&amp; npx bob build --target codegen&#10;">
</ActionContent>
</ExecutionAction>
</PreActions>`;

const PODSPEC_INVOKE_CODEGEN_SCRIPT = `
pre_install do |installer|
system("cd ../../ && yarn codegen")
system("cd ../../ && npx bob build --target codegen")
end
`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,7 @@
<% } else { -%>
"clean": "del-cli lib",
<% } -%>
<% if (project.arch !== 'legacy') { -%>
"codegen": "bob codegen",
"prepare": "bob build && yarn codegen",
<% } else { -%>
"prepare": "bob build",
<% } -%>
"release": "release-it"
},
"keywords": [
Expand Down
57 changes: 26 additions & 31 deletions packages/react-native-builder-bob/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,18 @@ import * as logger from './utils/logger';
import buildCommonJS from './targets/commonjs';
import buildModule from './targets/module';
import buildTypescript from './targets/typescript';
import type { Options } from './types';
import { patchCodegen } from './utils/patchCodegen';
import spawn from 'cross-spawn';
import buildCodegen from './targets/codegen';
import type { Options, Target } from './types';

type ArgName = 'target';

const args: Record<ArgName, yargs.Options> = {
target: {
type: 'string',
description: 'The target to build',
choices: ['commonjs', 'module', 'typescript', 'codegen'] satisfies Target[],
},
};

// eslint-disable-next-line import/no-commonjs, @typescript-eslint/no-var-requires
const { name, version } = require('../package.json');
Expand Down Expand Up @@ -362,7 +371,7 @@ yargs
`)
);
})
.command('build', 'build files for publishing', {}, async (argv) => {
.command('build', 'build files for publishing', args, async (argv) => {
const result = explorer.search();

if (!result?.config) {
Expand Down Expand Up @@ -415,6 +424,11 @@ yargs
};

for (const target of options.targets!) {
const targetArg = argv.target;
if (targetArg && target !== targetArg) {
continue;
}

const targetName = Array.isArray(target) ? target[0] : target;
const targetOptions = Array.isArray(target) ? target[1] : undefined;

Expand Down Expand Up @@ -450,38 +464,19 @@ yargs
report,
});
break;
case 'codegen':
await buildCodegen({
root,
source: path.resolve(root, source as string),
output: path.resolve(root, output as string, 'typescript'),
report,
});
break;
default:
logger.exit(`Invalid target ${kleur.blue(targetName)}.`);
}
}
})
.command(
'codegen',
'generate codegen from typescript specs',
{},
async () => {
const packageJsonPath = path.resolve(root, 'package.json');
if (!(await fs.pathExists(packageJsonPath))) {
logger.exit(
`Couldn't find a 'package.json' file in '${root}'. Are you in a project folder?`
);
}

spawn.sync('npx', ['react-native', 'codegen'], {
stdio: 'inherit',
});

patchCodegen(root);

console.log(
dedent`
${kleur.green('Codegen patched successfully!')}
${kleur.yellow('Good luck!')}
`
);
}
)
.demandCommand()
.recommendCommands()
.strict().argv;
42 changes: 42 additions & 0 deletions packages/react-native-builder-bob/src/targets/codegen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import path from 'path';
import fs from 'fs-extra';
import spawn from 'cross-spawn';
import type { Input } from '../types';
import { patchCodegen } from '../utils/patchCodegen';

type Options = Input;

export default async function build({ root, report }: Options) {
try {
const packageJsonPath = path.resolve(root, 'package.json');
if (!(await fs.pathExists(packageJsonPath))) {
throw new Error(
`Couldn't find a 'package.json' file in '${root}'. Are you in a project folder?`
);
}

spawn.sync('npx', ['react-native', 'codegen'], {
stdio: 'inherit',
});

patchCodegen(root);

report.success('Codegen patched successfully!');
} catch (e: unknown) {
if (e != null && typeof e === 'object') {
if ('stdout' in e && e.stdout != null) {
report.error(
`Errors found while generating codegen files:\n${e.stdout.toString()}`
);
} else if ('message' in e && typeof e.message === 'string') {
report.error(e.message);
} else {
throw e;
}
} else {
throw e;
}

throw new Error('Failed generate codegen files.');
}
}
2 changes: 1 addition & 1 deletion packages/react-native-builder-bob/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type Input = {
report: Report;
};

export type Target = 'commonjs' | 'module' | 'typescript';
export type Target = 'commonjs' | 'module' | 'typescript' | 'codegen';

export type Options = {
source?: string;
Expand Down
6 changes: 3 additions & 3 deletions packages/react-native-builder-bob/src/utils/patchCodegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ export async function patchCodegen(projectPath: string) {
packageJson.codegenConfig.outputDir.android;
if (!codegenAndroidPath) {
throw new Error(
'[Builder Bob] You need to define codegenConfig.outputDir.android in your package.json'
'You need to define codegenConfig.outputDir.android in your package.json'
);
}
codegenAndroidPath = path.resolve(projectPath, codegenAndroidPath);

if (!(await fs.pathExists(codegenAndroidPath))) {
throw new Error(
`[Builder Bob] Could not find ${codegenAndroidPath}. Make sure you are in the correct directory and react-native codegen works properly.`
`Could not find ${codegenAndroidPath}. Make sure you are in the correct directory and react-native codegen works properly.`
);
}

const codegenJavaPackageName: string | undefined =
packageJson.codegenConfig.android.javaPackageName;
if (!codegenJavaPackageName) {
throw new Error(
'[Builder Bob] You need to define codegenConfig.android.javaPackageName in your package.json'
'You need to define codegenConfig.android.javaPackageName in your package.json'
);
}

Expand Down

0 comments on commit 70dc19f

Please sign in to comment.