1+ import assert from "node:assert/strict" ;
12import fs from "node:fs" ;
23import path from "node:path" ;
4+ import { packageDirectorySync } from "pkg-dir" ;
5+ import { readPackageSync } from "read-pkg" ;
6+
37import {
48 Command ,
9+ Option ,
510 prettyPath ,
611 wrapAction ,
712} from "@react-native-node-api/cli-utils" ;
@@ -12,23 +17,22 @@ import {
1217 type GypToCmakeListsOptions ,
1318} from "./transformer.js" ;
1419
15- export type TransformOptions = Omit <
16- GypToCmakeListsOptions ,
17- "gyp" | "projectName"
18- > & {
20+ export type TransformOptions = Omit < GypToCmakeListsOptions , "gyp" > & {
1921 disallowUnknownProperties : boolean ;
20- projectName ?: string ;
2122} ;
2223
2324export function generateProjectName ( gypPath : string ) {
24- return path . dirname ( gypPath ) . replaceAll ( path . sep , "-" ) ;
25+ const packagePath = packageDirectorySync ( { cwd : path . dirname ( gypPath ) } ) ;
26+ assert ( packagePath , "Expected the binding.gyp file to be inside a package" ) ;
27+ const packageJson = readPackageSync ( { cwd : packagePath } ) ;
28+ return packageJson . name ;
2529}
2630
2731export function transformBindingGypFile (
2832 gypPath : string ,
2933 {
3034 disallowUnknownProperties,
31- projectName = generateProjectName ( gypPath ) ,
35+ projectName,
3236 ...restOfOptions
3337 } : TransformOptions ,
3438) {
@@ -49,19 +53,27 @@ export function transformBindingGypFile(
4953
5054export function transformBindingGypsRecursively (
5155 directoryPath : string ,
52- options : TransformOptions ,
56+ options : Omit < TransformOptions , "projectName" > ,
5357) {
5458 const entries = fs . readdirSync ( directoryPath , { withFileTypes : true } ) ;
5559 for ( const entry of entries ) {
5660 const fullPath = path . join ( directoryPath , entry . name ) ;
5761 if ( entry . isDirectory ( ) ) {
5862 transformBindingGypsRecursively ( fullPath , options ) ;
5963 } else if ( entry . isFile ( ) && entry . name === "binding.gyp" ) {
60- transformBindingGypFile ( fullPath , options ) ;
64+ transformBindingGypFile ( fullPath , {
65+ ...options ,
66+ projectName : generateProjectName ( fullPath ) ,
67+ } ) ;
6168 }
6269 }
6370}
6471
72+ const projectNameOption = new Option (
73+ "--project-name <name>" ,
74+ "Project name to use in CMakeLists.txt" ,
75+ ) . default ( undefined , "Uses name from the surrounding package.json" ) ;
76+
6577export const program = new Command ( "gyp-to-cmake" )
6678 . description ( "Transform binding.gyp to CMakeLists.txt" )
6779 . option (
@@ -70,7 +82,12 @@ export const program = new Command("gyp-to-cmake")
7082 )
7183 . option ( "--weak-node-api" , "Link against the weak-node-api library" , false )
7284 . option ( "--define-napi-version" , "Define NAPI_VERSION for all targets" , false )
85+ . option (
86+ "--no-apple-framework" ,
87+ "Disable emitting target properties to produce Apple frameworks" ,
88+ )
7389 . option ( "--cpp <version>" , "C++ standard version" , "17" )
90+ . addOption ( projectNameOption )
7491 . argument (
7592 "[path]" ,
7693 "Path to the binding.gyp file or directory to traverse recursively" ,
@@ -80,19 +97,30 @@ export const program = new Command("gyp-to-cmake")
8097 wrapAction (
8198 (
8299 targetPath : string ,
83- { pathTransforms, cpp, defineNapiVersion, weakNodeApi } ,
100+ {
101+ pathTransforms,
102+ cpp,
103+ defineNapiVersion,
104+ weakNodeApi,
105+ appleFramework,
106+ projectName,
107+ } ,
84108 ) => {
85- const options : TransformOptions = {
109+ const options : Omit < TransformOptions , "projectName" > = {
86110 unsupportedBehaviour : "throw" ,
87111 disallowUnknownProperties : false ,
88112 transformWinPathsToPosix : pathTransforms ,
89113 compileFeatures : cpp ? [ `cxx_std_${ cpp } ` ] : [ ] ,
90114 defineNapiVersion,
91115 weakNodeApi,
116+ appleFramework,
92117 } ;
93118 const stat = fs . statSync ( targetPath ) ;
94119 if ( stat . isFile ( ) ) {
95- transformBindingGypFile ( targetPath , options ) ;
120+ transformBindingGypFile ( targetPath , {
121+ ...options ,
122+ projectName : projectName ?? generateProjectName ( targetPath ) ,
123+ } ) ;
96124 } else if ( stat . isDirectory ( ) ) {
97125 transformBindingGypsRecursively ( targetPath , options ) ;
98126 } else {
0 commit comments