1
1
// @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' ;
2
7
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 ) ) ;
6
9
7
10
const CAPABILITY_MAP = {
8
11
// empty for now
9
12
} ;
10
13
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
-
16
14
/**
17
15
* Returns the contents of the file at specified path.
18
16
* @param {string } path
@@ -22,6 +20,8 @@ function readFile(path) {
22
20
return fs . readFileSync ( path , { encoding : 'utf-8' } ) ;
23
21
}
24
22
23
+ const { name : thisPackageName , devDependencies } = JSON . parse ( readFile ( './package.json' ) ) ;
24
+
25
25
const packages = { } ;
26
26
27
27
// Look for react-native capabilities
@@ -31,10 +31,10 @@ for (const [name, capability] of Object.entries(CAPABILITY_MAP)) {
31
31
}
32
32
}
33
33
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' ) {
38
38
continue ;
39
39
}
40
40
@@ -48,22 +48,25 @@ for (const manifestPath of workspacePackages.sort()) {
48
48
packages [ name ] = { name, version, devOnly } ;
49
49
}
50
50
51
- const { major, minor } = semver . coerce ( devDependencies [ 'react-native' ] ) ;
52
-
53
- let profiles ;
51
+ const { major, minor } = semverCoerce ( devDependencies [ 'react-native' ] ) ?? { } ;
54
52
55
53
// When updating FURN to a new react-native version, save the profile for
56
54
// the current react-native version in index.js to a new file under src named
57
55
// "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
+ }
64
67
65
68
const source = [
66
- `// This file was generated by '${ path . basename ( __filename ) } '` ,
69
+ `// This file was generated by '${ path . basename ( import . meta . url ) } '` ,
67
70
'/* eslint-disable */' ,
68
71
`module.exports = ${ JSON . stringify ( profiles , undefined , 2 ) } ;` ,
69
72
'' ,
0 commit comments