@@ -6,6 +6,11 @@ const fs = require("fs").promises;
66const path = require ( "path" ) ;
77const process = require ( "process" ) ;
88
9+ const extractPackageNameWhenScoped = ( packageName ) =>
10+ packageName . startsWith ( "@" )
11+ ? packageName . substring ( packageName . indexOf ( "/" ) + 1 )
12+ : packageName ;
13+
914const readPackageConfig = async ( packagePath ) => {
1015 const packageContent = await fs . readFile ( packagePath ) . catch ( ( err ) => {
1116 console . error ( `unable to read package.json at '${ packagePath } ': ${ err } ` ) ;
@@ -24,26 +29,80 @@ const readPackageConfig = async (packagePath) => {
2429 return null ;
2530} ;
2631
32+ const fileExists = async ( filePath ) => {
33+ return await fs
34+ . stat ( filePath )
35+ . then ( ( stat ) => stat . isFile ( ) )
36+ . catch ( ( err ) => {
37+ if ( err . code === "ENOENT" ) {
38+ return false ;
39+ }
40+ } ) ;
41+ } ;
42+
43+ const findPackagePath = async ( workspacePath , packageName ) => {
44+ const trivialPath = path . join (
45+ workspacePath ,
46+ extractPackageNameWhenScoped ( packageName )
47+ ) ;
48+
49+ if ( await fileExists ( path . join ( trivialPath , "package.json" ) ) ) {
50+ return path . join ( trivialPath ) ;
51+ }
52+
53+ const workspaceMembers = await fs . readdir ( workspacePath ) ;
54+
55+ for ( const member of workspaceMembers ) {
56+ const memberPath = path . join ( workspacePath , member ) ;
57+
58+ const packageData = await fs
59+ . readFile ( path . join ( memberPath , "package.json" ) )
60+ . then ( ( packageContent ) => {
61+ return JSON . parse ( packageContent ) ;
62+ } )
63+ . catch ( ( ) => {
64+ return null ;
65+ } ) ;
66+
67+ if ( packageData ?. name === packageName ) {
68+ return memberPath ;
69+ }
70+ }
71+
72+ return null ;
73+ } ;
74+
2775const main = async (
2876 packageJsonPath ,
2977 wallyOutputPath ,
78+ wallyRojoConfigPath ,
3079 rojoConfigPath ,
3180 { workspacePath }
3281) => {
3382 const packageData = await readPackageConfig ( packageJsonPath ) ;
3483
35- const { name : scopedName , version, license, dependencies = [ ] } = packageData ;
84+ const {
85+ name : scopedName ,
86+ version,
87+ license,
88+ dependencies = [ ] ,
89+ private,
90+ } = packageData ;
91+
92+ const tomlLines = [ "[package]" , `name = "${ scopedName . substring ( 1 ) } "` ] ;
93+
94+ if ( private ) {
95+ tomlLines . push ( "private = true" ) ;
96+ }
3697
37- const tomlLines = [
38- "[package]" ,
39- `name = "${ scopedName . substring ( 1 ) } "` ,
98+ tomlLines . push (
4099 `version = "${ version } "` ,
41100 'registry = "https://github.com/UpliftGames/wally-index"' ,
42101 'realm = "shared"' ,
43102 `license = "${ license } "` ,
44103 "" ,
45- "[dependencies]" ,
46- ] ;
104+ "[dependencies]"
105+ ) ;
47106
48107 const rojoConfig = {
49108 name : "WallyPackage" ,
@@ -58,9 +117,7 @@ const main = async (
58117 for ( const [ dependencyName , specifiedVersion ] of Object . entries (
59118 dependencies
60119 ) ) {
61- const name = dependencyName . startsWith ( "@" )
62- ? dependencyName . substring ( dependencyName . indexOf ( "/" ) + 1 )
63- : dependencyName ;
120+ const name = extractPackageNameWhenScoped ( dependencyName ) ;
64121
65122 rojoConfig . tree [ name ] = {
66123 $path : dependencyName + ".luau" ,
@@ -69,11 +126,12 @@ const main = async (
69126 const wallyPackageName = name . indexOf ( "-" ) !== - 1 ? `"${ name } "` : name ;
70127
71128 if ( specifiedVersion == "workspace:^" ) {
129+ const packagePath =
130+ workspacePath && ( await findPackagePath ( workspacePath , dependencyName ) ) ;
131+
72132 const dependentPackage =
73- workspacePath &&
74- ( await readPackageConfig (
75- path . join ( workspacePath , name , "package.json" )
76- ) ) ;
133+ packagePath &&
134+ ( await readPackageConfig ( path . join ( packagePath , "package.json" ) ) ) ;
77135
78136 if ( dependentPackage ) {
79137 tomlLines . push (
@@ -91,6 +149,13 @@ const main = async (
91149
92150 tomlLines . push ( "" ) ;
93151
152+ const wallyRojoConfig = {
153+ name : scopedName . substring ( scopedName . indexOf ( "/" ) + 1 ) ,
154+ tree : {
155+ $path : "src" ,
156+ } ,
157+ } ;
158+
94159 await Promise . all ( [
95160 fs . writeFile ( wallyOutputPath , tomlLines . join ( "\n" ) ) . catch ( ( err ) => {
96161 console . error (
@@ -104,6 +169,13 @@ const main = async (
104169 `unable to write rojo config at '${ rojoConfigPath } ': ${ err } `
105170 ) ;
106171 } ) ,
172+ fs
173+ . writeFile ( wallyRojoConfigPath , JSON . stringify ( wallyRojoConfig , null , 2 ) )
174+ . catch ( ( err ) => {
175+ console . error (
176+ `unable to write rojo config at '${ wallyRojoConfigPath } ': ${ err } `
177+ ) ;
178+ } ) ,
107179 ] ) ;
108180} ;
109181
@@ -115,17 +187,25 @@ const createCLI = () => {
115187 . description ( "a utility to convert npm packages to wally packages" )
116188 . argument ( "<package-json>" )
117189 . argument ( "<wally-toml>" )
190+ . argument ( "<wally-rojo-config>" )
118191 . argument ( "<package-rojo-config>" )
119192 . option (
120193 "--workspace-path <workspace>" ,
121194 "the path containing all workspace members"
122195 )
123196 . action (
124- async ( packageJson , wallyToml , rojoConfig , { workspacePath = null } ) => {
197+ async (
198+ packageJson ,
199+ wallyToml ,
200+ wallyRojoConfig ,
201+ rojoConfig ,
202+ { workspacePath = null }
203+ ) => {
125204 const cwd = process . cwd ( ) ;
126- main (
205+ await main (
127206 path . join ( cwd , packageJson ) ,
128207 path . join ( cwd , wallyToml ) ,
208+ path . join ( cwd , wallyRojoConfig ) ,
129209 path . join ( cwd , rojoConfig ) ,
130210 {
131211 workspacePath : workspacePath && path . join ( cwd , workspacePath ) ,
0 commit comments