Skip to content

Commit 07df18c

Browse files
committed
fix: addres issues
1 parent 7cc79da commit 07df18c

File tree

11 files changed

+143
-93
lines changed

11 files changed

+143
-93
lines changed

packages/create-react-native-library/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import generateExampleApp, {
1313
} from './utils/generateExampleApp';
1414
import { spawn } from './utils/spawn';
1515
import { version } from '../package.json';
16-
import { patchExampleAppCodegen } from './utils/patchExampleAppCodegen';
16+
import { addCodegenBuildScript } from './utils/addCodegenBuildScript';
1717

1818
const FALLBACK_BOB_VERSION = '0.29.0';
1919

@@ -791,7 +791,7 @@ async function create(_argv: yargs.Arguments<any>) {
791791
}
792792

793793
if (arch !== 'legacy') {
794-
patchExampleAppCodegen(folder, options.project.name);
794+
addCodegenBuildScript(folder, options.project.name);
795795
}
796796
}
797797

packages/create-react-native-library/src/utils/patchExampleAppCodegen.ts renamed to packages/create-react-native-library/src/utils/addCodegenBuildScript.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const PODSPEC_INVOKE_CODEGEN_SCRIPT = `
4040
* Codegen isn't invoked for libraries with `includesGeneratedCode` set to `true`.
4141
* This patches the example app to invoke library codegen on every app build.
4242
*/
43-
export async function patchExampleAppCodegen(
43+
export async function addCodegenBuildScript(
4444
libraryPath: string,
4545
projectName: string
4646
) {

packages/create-react-native-library/templates/native-library-mixed/react-native.config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
/**
2+
* @type {import('@react-native-community/cli-types').UserDependencyConfig}
3+
*/
14
module.exports = {
25
dependency: {
36
platforms: {
4-
/**
5-
* @type {import('@react-native-community/cli-types').AndroidDependencyParams}
6-
*/
77
android: {
88
cmakeListsPath: 'generated/jni/CMakeLists.txt',
99
},

packages/create-react-native-library/templates/native-library-new/react-native.config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
/**
2+
* @type {import('@react-native-community/cli-types').UserDependencyConfig}
3+
*/
14
module.exports = {
25
dependency: {
36
platforms: {
4-
/**
5-
* @type {import('@react-native-community/cli-types').AndroidDependencyParams}
6-
*/
77
android: {
88
cmakeListsPath: 'generated/jni/CMakeLists.txt',
99
},

packages/create-react-native-library/templates/native-view-mixed/react-native.config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
/**
2+
* @type {import('@react-native-community/cli-types').UserDependencyConfig}
3+
*/
14
module.exports = {
25
dependency: {
36
platforms: {
4-
/**
5-
* @type {import('@react-native-community/cli-types').AndroidDependencyParams}
6-
*/
77
android: {
88
cmakeListsPath: 'generated/jni/CMakeLists.txt',
99
},

packages/create-react-native-library/templates/native-view-new/react-native.config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
/**
2+
* @type {import('@react-native-community/cli-types').UserDependencyConfig}
3+
*/
14
module.exports = {
25
dependency: {
36
platforms: {
4-
/**
5-
* @type {import('@react-native-community/cli-types').AndroidDependencyParams}
6-
*/
77
android: {
88
cmakeListsPath: 'generated/jni/CMakeLists.txt',
99
},

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

Lines changed: 87 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ 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 type { Options, Target } from './types';
14+
import type { Options, Report, Target } from './types';
1515

1616
type ArgName = 'target';
1717

18-
const args: Record<ArgName, yargs.Options> = {
18+
const args = {
1919
target: {
2020
type: 'string',
2121
description: 'The target to build',
2222
choices: ['commonjs', 'module', 'typescript', 'codegen'] satisfies Target[],
2323
},
24-
};
24+
} satisfies Record<ArgName, yargs.Options>;
2525

2626
// eslint-disable-next-line import/no-commonjs, @typescript-eslint/no-var-requires
2727
const { name, version } = require('../package.json');
@@ -37,6 +37,8 @@ const explorer = cosmiconfig(name, {
3737
],
3838
});
3939

40+
const projectPackagePath = path.resolve(root, 'package.json');
41+
4042
const FLOW_PRGAMA_REGEX = /\*?\s*@(flow)\b/m;
4143

4244
yargs
@@ -54,15 +56,13 @@ yargs
5456
}
5557
}
5658

57-
const pak = path.join(root, 'package.json');
58-
59-
if (!(await fs.pathExists(pak))) {
59+
if (!(await fs.pathExists(projectPackagePath))) {
6060
logger.exit(
6161
`Couldn't find a 'package.json' file in '${root}'.\n Are you in a project folder?`
6262
);
6363
}
6464

65-
const pkg = JSON.parse(await fs.readFile(pak, 'utf-8'));
65+
const pkg = JSON.parse(await fs.readFile(projectPackagePath, 'utf-8'));
6666
const result = await explorer.search();
6767

6868
if (result?.config && pkg.devDependencies && name in pkg.devDependencies) {
@@ -407,7 +407,7 @@ yargs
407407
pkg.eslintIgnore.push(`${output}/`);
408408
}
409409

410-
await fs.writeJSON(pak, pkg, {
410+
await fs.writeJSON(projectPackagePath, pkg, {
411411
spaces: 2,
412412
});
413413

@@ -447,7 +447,13 @@ yargs
447447
`)
448448
);
449449
})
450-
.command('build', 'build files for publishing', {}, async (argv) => {
450+
.command('build', 'build files for publishing', args, async (argv) => {
451+
if (!(await fs.pathExists(projectPackagePath))) {
452+
throw new Error(
453+
`Couldn't find a 'package.json' file in '${root}'. Are you in a project folder?`
454+
);
455+
}
456+
451457
const result = await explorer.search();
452458

453459
if (!result?.config) {
@@ -499,60 +505,81 @@ yargs
499505
success: logger.success,
500506
};
501507

502-
for (const target of options.targets!) {
503-
const targetArg = argv.target;
504-
if (targetArg && target !== targetArg) {
505-
continue;
506-
}
507-
508-
const targetName = Array.isArray(target) ? target[0] : target;
509-
const targetOptions = Array.isArray(target) ? target[1] : undefined;
510-
511-
report.info(`Building target ${kleur.blue(targetName)}`);
512-
513-
switch (targetName) {
514-
case 'commonjs':
515-
await buildCommonJS({
516-
root,
517-
source: path.resolve(root, source as string),
518-
output: path.resolve(root, output as string, 'commonjs'),
519-
exclude,
520-
options: targetOptions,
521-
report,
522-
});
523-
break;
524-
case 'module':
525-
await buildModule({
526-
root,
527-
source: path.resolve(root, source as string),
528-
output: path.resolve(root, output as string, 'module'),
529-
exclude,
530-
options: targetOptions,
531-
report,
532-
});
533-
break;
534-
case 'typescript':
535-
await buildTypescript({
536-
root,
537-
source: path.resolve(root, source as string),
538-
output: path.resolve(root, output as string, 'typescript'),
539-
options: targetOptions,
540-
report,
541-
});
542-
break;
543-
case 'codegen':
544-
await buildCodegen({
545-
root,
546-
source: path.resolve(root, source as string),
547-
output: path.resolve(root, output as string, 'typescript'),
548-
report,
549-
});
550-
break;
551-
default:
552-
logger.exit(`Invalid target ${kleur.blue(targetName)}.`);
508+
if (argv.target != null) {
509+
buildTarget(
510+
argv.target,
511+
report,
512+
source as string,
513+
output as string,
514+
exclude
515+
);
516+
} else {
517+
for (const target of options.targets!) {
518+
buildTarget(
519+
target,
520+
report,
521+
source as string,
522+
output as string,
523+
exclude
524+
);
553525
}
554526
}
555527
})
556528
.demandCommand()
557529
.recommendCommands()
558530
.strict().argv;
531+
532+
async function buildTarget(
533+
target: Exclude<Options['targets'], undefined>[number],
534+
report: Report,
535+
source: string,
536+
output: string,
537+
exclude: string
538+
) {
539+
const targetName = Array.isArray(target) ? target[0] : target;
540+
const targetOptions = Array.isArray(target) ? target[1] : undefined;
541+
542+
report.info(`Building target ${kleur.blue(targetName)}`);
543+
544+
switch (targetName) {
545+
case 'commonjs':
546+
await buildCommonJS({
547+
root,
548+
source: path.resolve(root, source),
549+
output: path.resolve(root, output, 'commonjs'),
550+
exclude,
551+
options: targetOptions,
552+
report,
553+
});
554+
break;
555+
case 'module':
556+
await buildModule({
557+
root,
558+
source: path.resolve(root, source),
559+
output: path.resolve(root, output, 'module'),
560+
exclude,
561+
options: targetOptions,
562+
report,
563+
});
564+
break;
565+
case 'typescript':
566+
await buildTypescript({
567+
root,
568+
source: path.resolve(root, source),
569+
output: path.resolve(root, output, 'typescript'),
570+
options: targetOptions,
571+
report,
572+
});
573+
break;
574+
case 'codegen':
575+
await buildCodegen({
576+
root,
577+
source: path.resolve(root, source),
578+
output: path.resolve(root, output, 'typescript'),
579+
report,
580+
});
581+
break;
582+
default:
583+
logger.exit(`Invalid target ${kleur.blue(targetName)}.`);
584+
}
585+
}
Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,18 @@
1-
import path from 'path';
2-
import fs from 'fs-extra';
3-
import spawn from 'cross-spawn';
41
import type { Input } from '../types';
52
import { patchCodegen } from '../utils/patchCodegen';
3+
import { spawn } from '../utils/spawn';
64

75
type Options = Input;
86

97
export default async function build({ root, report }: Options) {
108
try {
11-
const packageJsonPath = path.resolve(root, 'package.json');
12-
if (!(await fs.pathExists(packageJsonPath))) {
13-
throw new Error(
14-
`Couldn't find a 'package.json' file in '${root}'. Are you in a project folder?`
15-
);
16-
}
17-
18-
spawn.sync('npx', ['react-native', 'codegen'], {
19-
stdio: 'inherit',
9+
await spawn('npx', ['react-native', 'codegen'], {
10+
stdio: 'ignore',
2011
});
2112

2213
patchCodegen(root);
2314

24-
report.success('Codegen patched successfully!');
15+
report.success('Generated native code with codegen');
2516
} catch (e: unknown) {
2617
if (e != null && typeof e === 'object') {
2718
if ('stdout' in e && e.stdout != null) {
@@ -37,6 +28,6 @@ export default async function build({ root, report }: Options) {
3728
throw e;
3829
}
3930

40-
throw new Error('Failed generate codegen files.');
31+
throw new Error('Failed generate the codegen files.');
4132
}
4233
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ export type Target = 'commonjs' | 'module' | 'typescript' | 'codegen';
1818
export type Options = {
1919
source?: string;
2020
output?: string;
21-
targets?: (Target | [Target, object])[];
21+
targets?: (Target | [target: Target, options: object])[];
2222
exclude?: string;
2323
};

packages/react-native-builder-bob/src/utils/patchCodegen.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import fs from 'fs-extra';
22
import path from 'path';
33

4+
const CODEGEN_DOCS =
5+
'https://github.com/reactwg/react-native-new-architecture/blob/main/docs/enable-libraries-prerequisites.md#configure-codegen';
6+
47
/**
58
* Currently, running react-native codegen generates java files with package name `com.facebook.fbreact.specs`.
69
* This is a known issue in react-native itself.
@@ -17,22 +20,22 @@ export async function patchCodegen(projectPath: string) {
1720
packageJson.codegenConfig.outputDir.android;
1821
if (!codegenAndroidPath) {
1922
throw new Error(
20-
'You need to define codegenConfig.outputDir.android in your package.json'
23+
`Your package.json doesn't contain codegenConfig.outputDir.android. Please see ${CODEGEN_DOCS}`
2124
);
2225
}
2326
codegenAndroidPath = path.resolve(projectPath, codegenAndroidPath);
2427

2528
if (!(await fs.pathExists(codegenAndroidPath))) {
2629
throw new Error(
27-
`Could not find ${codegenAndroidPath}. Make sure you are in the correct directory and react-native codegen works properly.`
30+
`The codegen android path defined in your package.json: ${codegenAndroidPath} doesnt' exist.`
2831
);
2932
}
3033

3134
const codegenJavaPackageName: string | undefined =
3235
packageJson.codegenConfig.android.javaPackageName;
3336
if (!codegenJavaPackageName) {
3437
throw new Error(
35-
'You need to define codegenConfig.android.javaPackageName in your package.json'
38+
`Your package.json doesn't contain codegenConfig.android.javaPackageName. Please see ${CODEGEN_DOCS}`
3639
);
3740
}
3841

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import crossSpawn from 'cross-spawn';
2+
3+
export const spawn = async (...args: Parameters<typeof crossSpawn>) => {
4+
return new Promise<string>((resolve, reject) => {
5+
const child = crossSpawn(...args);
6+
7+
let stdout = '';
8+
let stderr = '';
9+
10+
child.stdout?.setEncoding('utf8');
11+
child.stdout?.on('data', (data) => {
12+
stdout += data;
13+
});
14+
15+
child.stderr?.setEncoding('utf8');
16+
child.stderr?.on('data', (data) => {
17+
stderr += data;
18+
});
19+
20+
child.once('error', reject);
21+
child.once('close', (code) => {
22+
if (code === 0) {
23+
resolve(stdout.trim());
24+
} else {
25+
reject(new Error(stderr.trim()));
26+
}
27+
});
28+
});
29+
};

0 commit comments

Comments
 (0)