Skip to content

Commit

Permalink
fix: addres issues
Browse files Browse the repository at this point in the history
  • Loading branch information
atlj committed Aug 11, 2024
1 parent 7cc79da commit 07df18c
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 93 deletions.
4 changes: 2 additions & 2 deletions packages/create-react-native-library/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import generateExampleApp, {
} from './utils/generateExampleApp';
import { spawn } from './utils/spawn';
import { version } from '../package.json';
import { patchExampleAppCodegen } from './utils/patchExampleAppCodegen';
import { addCodegenBuildScript } from './utils/addCodegenBuildScript';

const FALLBACK_BOB_VERSION = '0.29.0';

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

if (arch !== 'legacy') {
patchExampleAppCodegen(folder, options.project.name);
addCodegenBuildScript(folder, options.project.name);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const PODSPEC_INVOKE_CODEGEN_SCRIPT = `
* Codegen isn't invoked for libraries with `includesGeneratedCode` set to `true`.
* This patches the example app to invoke library codegen on every app build.
*/
export async function patchExampleAppCodegen(
export async function addCodegenBuildScript(
libraryPath: string,
projectName: string
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* @type {import('@react-native-community/cli-types').UserDependencyConfig}
*/
module.exports = {
dependency: {
platforms: {
/**
* @type {import('@react-native-community/cli-types').AndroidDependencyParams}
*/
android: {
cmakeListsPath: 'generated/jni/CMakeLists.txt',
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* @type {import('@react-native-community/cli-types').UserDependencyConfig}
*/
module.exports = {
dependency: {
platforms: {
/**
* @type {import('@react-native-community/cli-types').AndroidDependencyParams}
*/
android: {
cmakeListsPath: 'generated/jni/CMakeLists.txt',
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* @type {import('@react-native-community/cli-types').UserDependencyConfig}
*/
module.exports = {
dependency: {
platforms: {
/**
* @type {import('@react-native-community/cli-types').AndroidDependencyParams}
*/
android: {
cmakeListsPath: 'generated/jni/CMakeLists.txt',
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* @type {import('@react-native-community/cli-types').UserDependencyConfig}
*/
module.exports = {
dependency: {
platforms: {
/**
* @type {import('@react-native-community/cli-types').AndroidDependencyParams}
*/
android: {
cmakeListsPath: 'generated/jni/CMakeLists.txt',
},
Expand Down
147 changes: 87 additions & 60 deletions packages/react-native-builder-bob/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import buildCommonJS from './targets/commonjs';
import buildModule from './targets/module';
import buildTypescript from './targets/typescript';
import buildCodegen from './targets/codegen';
import type { Options, Target } from './types';
import type { Options, Report, Target } from './types';

type ArgName = 'target';

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

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

const projectPackagePath = path.resolve(root, 'package.json');

const FLOW_PRGAMA_REGEX = /\*?\s*@(flow)\b/m;

yargs
Expand All @@ -54,15 +56,13 @@ yargs
}
}

const pak = path.join(root, 'package.json');

if (!(await fs.pathExists(pak))) {
if (!(await fs.pathExists(projectPackagePath))) {
logger.exit(
`Couldn't find a 'package.json' file in '${root}'.\n Are you in a project folder?`
);
}

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

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

await fs.writeJSON(pak, pkg, {
await fs.writeJSON(projectPackagePath, pkg, {
spaces: 2,
});

Expand Down Expand Up @@ -447,7 +447,13 @@ yargs
`)
);
})
.command('build', 'build files for publishing', {}, async (argv) => {
.command('build', 'build files for publishing', args, async (argv) => {
if (!(await fs.pathExists(projectPackagePath))) {
throw new Error(
`Couldn't find a 'package.json' file in '${root}'. Are you in a project folder?`
);
}

const result = await explorer.search();

if (!result?.config) {
Expand Down Expand Up @@ -499,60 +505,81 @@ yargs
success: logger.success,
};

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;

report.info(`Building target ${kleur.blue(targetName)}`);

switch (targetName) {
case 'commonjs':
await buildCommonJS({
root,
source: path.resolve(root, source as string),
output: path.resolve(root, output as string, 'commonjs'),
exclude,
options: targetOptions,
report,
});
break;
case 'module':
await buildModule({
root,
source: path.resolve(root, source as string),
output: path.resolve(root, output as string, 'module'),
exclude,
options: targetOptions,
report,
});
break;
case 'typescript':
await buildTypescript({
root,
source: path.resolve(root, source as string),
output: path.resolve(root, output as string, 'typescript'),
options: targetOptions,
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)}.`);
if (argv.target != null) {
buildTarget(
argv.target,
report,
source as string,
output as string,
exclude
);
} else {
for (const target of options.targets!) {
buildTarget(
target,
report,
source as string,
output as string,
exclude
);
}
}
})
.demandCommand()
.recommendCommands()
.strict().argv;

async function buildTarget(
target: Exclude<Options['targets'], undefined>[number],
report: Report,
source: string,
output: string,
exclude: string
) {
const targetName = Array.isArray(target) ? target[0] : target;
const targetOptions = Array.isArray(target) ? target[1] : undefined;

report.info(`Building target ${kleur.blue(targetName)}`);

switch (targetName) {
case 'commonjs':
await buildCommonJS({
root,
source: path.resolve(root, source),
output: path.resolve(root, output, 'commonjs'),
exclude,
options: targetOptions,
report,
});
break;
case 'module':
await buildModule({
root,
source: path.resolve(root, source),
output: path.resolve(root, output, 'module'),
exclude,
options: targetOptions,
report,
});
break;
case 'typescript':
await buildTypescript({
root,
source: path.resolve(root, source),
output: path.resolve(root, output, 'typescript'),
options: targetOptions,
report,
});
break;
case 'codegen':
await buildCodegen({
root,
source: path.resolve(root, source),
output: path.resolve(root, output, 'typescript'),
report,
});
break;
default:
logger.exit(`Invalid target ${kleur.blue(targetName)}.`);
}
}
19 changes: 5 additions & 14 deletions packages/react-native-builder-bob/src/targets/codegen.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
import path from 'path';
import fs from 'fs-extra';
import spawn from 'cross-spawn';
import type { Input } from '../types';
import { patchCodegen } from '../utils/patchCodegen';
import { spawn } from '../utils/spawn';

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',
await spawn('npx', ['react-native', 'codegen'], {
stdio: 'ignore',
});

patchCodegen(root);

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

throw new Error('Failed generate codegen files.');
throw new Error('Failed generate the 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 @@ -18,6 +18,6 @@ export type Target = 'commonjs' | 'module' | 'typescript' | 'codegen';
export type Options = {
source?: string;
output?: string;
targets?: (Target | [Target, object])[];
targets?: (Target | [target: Target, options: object])[];
exclude?: string;
};
9 changes: 6 additions & 3 deletions packages/react-native-builder-bob/src/utils/patchCodegen.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import fs from 'fs-extra';
import path from 'path';

const CODEGEN_DOCS =
'https://github.com/reactwg/react-native-new-architecture/blob/main/docs/enable-libraries-prerequisites.md#configure-codegen';

/**
* Currently, running react-native codegen generates java files with package name `com.facebook.fbreact.specs`.
* This is a known issue in react-native itself.
Expand All @@ -17,22 +20,22 @@ export async function patchCodegen(projectPath: string) {
packageJson.codegenConfig.outputDir.android;
if (!codegenAndroidPath) {
throw new Error(
'You need to define codegenConfig.outputDir.android in your package.json'
`Your package.json doesn't contain codegenConfig.outputDir.android. Please see ${CODEGEN_DOCS}`
);
}
codegenAndroidPath = path.resolve(projectPath, codegenAndroidPath);

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

const codegenJavaPackageName: string | undefined =
packageJson.codegenConfig.android.javaPackageName;
if (!codegenJavaPackageName) {
throw new Error(
'You need to define codegenConfig.android.javaPackageName in your package.json'
`Your package.json doesn't contain codegenConfig.android.javaPackageName. Please see ${CODEGEN_DOCS}`
);
}

Expand Down
29 changes: 29 additions & 0 deletions packages/react-native-builder-bob/src/utils/spawn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import crossSpawn from 'cross-spawn';

export const spawn = async (...args: Parameters<typeof crossSpawn>) => {
return new Promise<string>((resolve, reject) => {
const child = crossSpawn(...args);

let stdout = '';
let stderr = '';

child.stdout?.setEncoding('utf8');
child.stdout?.on('data', (data) => {
stdout += data;
});

child.stderr?.setEncoding('utf8');
child.stderr?.on('data', (data) => {
stderr += data;
});

child.once('error', reject);
child.once('close', (code) => {
if (code === 0) {
resolve(stdout.trim());
} else {
reject(new Error(stderr.trim()));
}
});
});
};

0 comments on commit 07df18c

Please sign in to comment.