11// @ts -check
2+ import * as path from 'node:path' ;
3+ import * as fs from 'node:fs' ;
4+ import { URL , fileURLToPath } from 'node:url' ;
5+ import semverCoerce from 'semver/functions/coerce.js' ;
6+ import { getAllPackageJsonFiles } from 'workspace-tools' ;
27
3- const path = require ( 'path' ) ;
4-
5- const OUTPUT_FILE = `${ __dirname } /src/index.js` ;
8+ const OUTPUT_FILE = fileURLToPath ( new URL ( "src/index.js" , import . meta. url ) ) ;
69
710const CAPABILITY_MAP = {
811 // empty for now
912} ;
1013
11- const fs = require ( 'fs' ) ;
12- const semver = require ( 'semver' ) ;
13- const { getAllPackageJsonFiles } = require ( 'workspace-tools' ) ;
14- const { name : thisPackageName , devDependencies } = require ( './package.json' ) ;
15-
1614/**
1715 * Returns the contents of the file at specified path.
1816 * @param {string } path
@@ -22,6 +20,8 @@ function readFile(path) {
2220 return fs . readFileSync ( path , { encoding : 'utf-8' } ) ;
2321}
2422
23+ const { name : thisPackageName , devDependencies } = JSON . parse ( readFile ( './package.json' ) ) ;
24+
2525const packages = { } ;
2626
2727// Look for react-native capabilities
@@ -31,10 +31,10 @@ for (const [name, capability] of Object.entries(CAPABILITY_MAP)) {
3131 }
3232}
3333
34- const workspacePackages = getAllPackageJsonFiles ( __dirname ) ;
35- for ( const manifestPath of workspacePackages . sort ( ) ) {
36- const { name, version, private, devOnly } = JSON . parse ( readFile ( manifestPath ) ) ;
37- if ( private || name === thisPackageName || name === '@fluentui-react-native/codemods' ) {
34+ const workspacePackages = getAllPackageJsonFiles ( OUTPUT_FILE ) ?. sort ( ) ?? [ ] ;
35+ for ( const manifestPath of workspacePackages ) {
36+ const { name, version, private : isPrivate , devOnly } = JSON . parse ( readFile ( manifestPath ) ) ;
37+ if ( isPrivate || name === thisPackageName || name === '@fluentui-react-native/codemods' ) {
3838 continue ;
3939 }
4040
@@ -48,22 +48,25 @@ for (const manifestPath of workspacePackages.sort()) {
4848 packages [ name ] = { name, version, devOnly } ;
4949}
5050
51- const { major, minor } = semver . coerce ( devDependencies [ 'react-native' ] ) ;
52-
53- let profiles ;
51+ const { major, minor } = semverCoerce ( devDependencies [ 'react-native' ] ) ?? { } ;
5452
5553// When updating FURN to a new react-native version, save the profile for
5654// the current react-native version in index.js to a new file under src named
5755// "furn-profile-X.Y.js" and add that profile here. For example:
58- //
59- profiles = {
60- [ `${ major } .${ minor } ` ] : packages ,
61- ...require ( './src/furn-profile-0.72.js' ) ,
62- ...require ( './src/furn-profile-0.71.js' ) ,
63- } ;
56+ const profiles = { [ `${ major } .${ minor } ` ] : packages } ;
57+ for ( const filename of fs . readdirSync ( "./src" ) . sort ( ) . reverse ( ) ) {
58+ if ( ! filename . startsWith ( "furn-profile-" ) ) {
59+ continue ;
60+ }
61+
62+ const { default : profile } = await import ( `./src/${ filename } ` ) ;
63+ for ( const [ key , value ] of Object . entries ( profile ) ) {
64+ profiles [ key ] = value ;
65+ }
66+ }
6467
6568const source = [
66- `// This file was generated by '${ path . basename ( __filename ) } '` ,
69+ `// This file was generated by '${ path . basename ( import . meta . url ) } '` ,
6770 '/* eslint-disable */' ,
6871 `module.exports = ${ JSON . stringify ( profiles , undefined , 2 ) } ;` ,
6972 '' ,
0 commit comments