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 ,
59 prettyPath ,
@@ -13,23 +17,22 @@ import {
1317 type GypToCmakeListsOptions ,
1418} from "./transformer.js" ;
1519
16- export type TransformOptions = Omit <
17- GypToCmakeListsOptions ,
18- "gyp" | "projectName"
19- > & {
20+ export type TransformOptions = Omit < GypToCmakeListsOptions , "gyp" > & {
2021 disallowUnknownProperties : boolean ;
21- projectName ?: string ;
2222} ;
2323
2424export function generateProjectName ( gypPath : string ) {
25- 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 ;
2629}
2730
2831export function transformBindingGypFile (
2932 gypPath : string ,
3033 {
3134 disallowUnknownProperties,
32- projectName = generateProjectName ( gypPath ) ,
35+ projectName,
3336 ...restOfOptions
3437 } : TransformOptions ,
3538) {
@@ -50,19 +53,27 @@ export function transformBindingGypFile(
5053
5154export function transformBindingGypsRecursively (
5255 directoryPath : string ,
53- options : TransformOptions ,
56+ options : Omit < TransformOptions , "projectName" > ,
5457) {
5558 const entries = fs . readdirSync ( directoryPath , { withFileTypes : true } ) ;
5659 for ( const entry of entries ) {
5760 const fullPath = path . join ( directoryPath , entry . name ) ;
5861 if ( entry . isDirectory ( ) ) {
5962 transformBindingGypsRecursively ( fullPath , options ) ;
6063 } else if ( entry . isFile ( ) && entry . name === "binding.gyp" ) {
61- transformBindingGypFile ( fullPath , options ) ;
64+ transformBindingGypFile ( fullPath , {
65+ ...options ,
66+ projectName : generateProjectName ( fullPath ) ,
67+ } ) ;
6268 }
6369 }
6470}
6571
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+
6677export const program = new Command ( "gyp-to-cmake" )
6778 . description ( "Transform binding.gyp to CMakeLists.txt" )
6879 . option (
@@ -71,7 +82,13 @@ export const program = new Command("gyp-to-cmake")
7182 )
7283 . option ( "--weak-node-api" , "Link against the weak-node-api library" , false )
7384 . option ( "--define-napi-version" , "Define NAPI_VERSION for all targets" , false )
85+ . option (
86+ "--apple-framework" ,
87+ "Set target properties to produce Apple frameworks" ,
88+ true ,
89+ )
7490 . option ( "--cpp <version>" , "C++ standard version" , "17" )
91+ . addOption ( projectNameOption )
7592 . argument (
7693 "[path]" ,
7794 "Path to the binding.gyp file or directory to traverse recursively" ,
@@ -81,19 +98,30 @@ export const program = new Command("gyp-to-cmake")
8198 wrapAction (
8299 (
83100 targetPath : string ,
84- { pathTransforms, cpp, defineNapiVersion, weakNodeApi } ,
101+ {
102+ pathTransforms,
103+ cpp,
104+ defineNapiVersion,
105+ weakNodeApi,
106+ appleFramework,
107+ projectName,
108+ } ,
85109 ) => {
86- const options : TransformOptions = {
110+ const options : Omit < TransformOptions , "projectName" > = {
87111 unsupportedBehaviour : "throw" ,
88112 disallowUnknownProperties : false ,
89113 transformWinPathsToPosix : pathTransforms ,
90114 compileFeatures : cpp ? [ `cxx_std_${ cpp } ` ] : [ ] ,
91115 defineNapiVersion,
92116 weakNodeApi,
117+ appleFramework,
93118 } ;
94119 const stat = fs . statSync ( targetPath ) ;
95120 if ( stat . isFile ( ) ) {
96- transformBindingGypFile ( targetPath , options ) ;
121+ transformBindingGypFile ( targetPath , {
122+ ...options ,
123+ projectName : projectName ?? generateProjectName ( targetPath ) ,
124+ } ) ;
97125 } else if ( stat . isDirectory ( ) ) {
98126 transformBindingGypsRecursively ( targetPath , options ) ;
99127 } else {
0 commit comments