22
33const path = require ( 'path' ) ;
44const fs = require ( 'fs' ) ;
5- const os = require ( "os" ) ;
5+ const findNodeModules = require ( 'find-node-modules' ) ;
66const ts = require ( 'typescript' ) ;
77
8+ const formatHost = {
9+ getCurrentDirectory : ts . sys . getCurrentDirectory ,
10+ getNewLine : ( ) => ts . sys . newLine
11+ } ;
12+
13+ function reportDiagnostic ( diagnostic ) {
14+ console . log ( "TS Error" , diagnostic . code , ":" , ts . flattenDiagnosticMessageText ( diagnostic . messageText , formatHost . getNewLine ( ) ) ) ;
15+ }
16+
17+ const [ nodeModules ] = findNodeModules ( { cwd : process . argv [ 1 ] , relative : false } ) ;
18+
819const getArg = ( argName ) => {
920 const argIndex = process . argv . indexOf ( argName ) ;
1021 return argIndex !== - 1 ? process . argv [ argIndex + 1 ] : null ;
1122} ;
1223
1324const outDirArg = getArg ( '--outputDir' ) ;
25+
1426const outputDir = outDirArg
1527 ? path . resolve ( './' , outDirArg )
16- : path . resolve ( __dirname , '../../ @types/__federated_types/' ) ;
28+ : path . resolve ( nodeModules , '@types/__federated_types/' ) ;
1729
1830const findFederationConfig = ( base ) => {
1931 let files = fs . readdirSync ( base ) ;
2032 let queue = [ ] ;
2133
22- for ( let i = 0 ; i < files . length ; i ++ ) {
34+ for ( let i = 0 ; i < files . length ; i ++ ) {
2335 const file = files [ i ] ;
2436 const newBase = path . join ( base , file ) ;
2537 if ( file === 'federation.config.json' ) {
2638 return path . resolve ( './' , newBase ) ;
27- } else if ( fs . statSync ( newBase ) . isDirectory ( ) && ! newBase . includes ( 'node_modules' ) ) {
39+ } else if ( fs . statSync ( newBase ) . isDirectory ( ) && ! newBase . includes ( 'node_modules' ) ) {
2840 queue . push ( newBase ) ;
2941 }
3042 }
@@ -45,8 +57,14 @@ console.log(`Using config file: ${federationConfigPath}`);
4557
4658const federationConfig = require ( federationConfigPath ) ;
4759const compileFiles = Object . values ( federationConfig . exposes ) ;
60+ const compileKeys = Object . keys ( federationConfig . exposes ) ;
4861const outFile = path . resolve ( outputDir , `${ federationConfig . name } .d.ts` ) ;
4962
63+ function getModuleDeclareName ( exposeName ) {
64+ // windows paths 🤦
65+ return path . join ( federationConfig . name , exposeName ) . replace ( / [ \\ / ] / g, '/' ) ;
66+ }
67+
5068try {
5169 if ( fs . existsSync ( outFile ) ) {
5270 fs . unlinkSync ( outFile ) ;
6280 esModuleInterop : true ,
6381 } ) ;
6482
65- program . emit ( ) ;
83+ const { emitSkipped, diagnostics } = program . emit ( ) ;
84+
85+ diagnostics . forEach ( reportDiagnostic )
86+
87+ if ( emitSkipped ) {
88+ process . exit ( 0 )
89+ }
6690
6791 let typing = fs . readFileSync ( outFile , { encoding : 'utf8' , flag : 'r' } ) ;
6892
@@ -74,28 +98,41 @@ try {
7498 }
7599
76100 moduleNames . forEach ( ( name ) => {
77- const regex = RegExp ( `"${ name } ` , 'g' ) ;
78- typing = typing . replace ( regex , `"${ federationConfig . name } /${ name } ` ) ;
101+ // exposeName - relative name of exposed component (if not found - just take moduleName)
102+ const [ exposeName = name , ...aliases ] = compileKeys . filter ( key => federationConfig . exposes [ key ] . endsWith ( name ) ) ;
103+ const regex = RegExp ( `"${ name } "` , 'g' ) ;
104+
105+ const moduleDeclareName = getModuleDeclareName ( exposeName ) ;
106+
107+ // language=TypeScript
108+ const createAliasModule = name => `
109+ declare module "${ getModuleDeclareName ( name ) } " {
110+ export * from "${ moduleDeclareName } "
111+ }
112+ ` ;
113+
114+ typing = [
115+ typing . replace ( regex , `"${ moduleDeclareName } "` ) ,
116+ ...aliases . map ( createAliasModule ) ,
117+ ] . join ( '\n' ) ;
79118 } ) ;
80119
81120 console . log ( 'writing typing file:' , outFile ) ;
82121
83122 fs . writeFileSync ( outFile , typing ) ;
84123
85124 // if we are writing to the node_modules/@types directory, add a package.json file
86- if ( outputDir . includes ( os . platform ( ) === "win32"
87- ? "node_modules\\@types"
88- : "node_modules/@types" ) ) {
125+ if ( outputDir . includes ( path . join ( 'node_modules' , '@types' ) ) ) {
89126 const packageJsonPath = path . resolve ( outputDir , 'package.json' ) ;
90127
91128 if ( ! fs . existsSync ( packageJsonPath ) ) {
92- console . log ( 'writing package.json:' , packageJsonPath ) ;
129+ console . debug ( 'writing package.json:' , packageJsonPath ) ;
93130 fs . copyFileSync ( path . resolve ( __dirname , 'typings.package.tmpl.json' ) , packageJsonPath ) ;
94131 } else {
95- console . log ( packageJsonPath , 'already exists' ) ;
132+ console . debug ( packageJsonPath , 'already exists' ) ;
96133 }
97134 } else {
98- console . log ( 'not writing to node modules, dont need a package.json' ) ;
135+ console . debug ( 'not writing to node modules, dont need a package.json' ) ;
99136 }
100137
101138 // write/update the index.d.ts file
@@ -113,7 +150,7 @@ try {
113150 }
114151 }
115152
116- console . log ( 'Success!' ) ;
153+ console . debug ( 'Success!' ) ;
117154} catch ( e ) {
118155 console . error ( `ERROR:` , e ) ;
119156 process . exit ( 1 ) ;
0 commit comments