@@ -6,8 +6,8 @@ const findNodeModules = require('find-node-modules');
66const ts = require ( 'typescript' ) ;
77
88const formatHost = {
9- getCurrentDirectory : ts . sys . getCurrentDirectory ,
10- getNewLine : ( ) => ts . sys . newLine
9+ getCurrentDirectory : ts . sys . getCurrentDirectory ,
10+ getNewLine : ( ) => ts . sys . newLine
1111} ;
1212
1313function reportDiagnostic ( diagnostic ) {
@@ -16,16 +16,26 @@ function reportDiagnostic(diagnostic) {
1616
1717const [ nodeModules ] = findNodeModules ( { cwd : process . argv [ 1 ] , relative : false } ) ;
1818
19+ const hasArg = ( argName ) => {
20+ const argIndex = process . argv . indexOf ( argName ) ;
21+ return argIndex !== - 1 ;
22+ } ;
1923const getArg = ( argName ) => {
2024 const argIndex = process . argv . indexOf ( argName ) ;
2125 return argIndex !== - 1 ? process . argv [ argIndex + 1 ] : null ;
2226} ;
2327
28+ const nodeModulesOutputDir = path . resolve ( nodeModules , '@types/__federated_types/' ) ;
29+ const saveToNodeMoulesArg = hasArg ( '--saveToNodeModules' ) ;
2430const outDirArg = getArg ( '--outputDir' ) ;
2531
2632const outputDir = outDirArg
2733 ? path . resolve ( './' , outDirArg )
28- : path . resolve ( nodeModules , '@types/__federated_types/' ) ;
34+ : nodeModulesOutputDir ;
35+
36+ const outputDirs = outputDir !== nodeModulesOutputDir && saveToNodeMoulesArg
37+ ? [ nodeModulesOutputDir , outputDir ]
38+ : [ outputDir ] ;
2939
3040const configPathArg = getArg ( '--config' ) ;
3141const configPath = configPathArg
@@ -69,14 +79,14 @@ console.log(`Using config file: ${federationConfigPath}`);
6979const federationConfig = require ( federationConfigPath ) ;
7080const compileFiles = Object . values ( federationConfig . exposes ) ;
7181const compileKeys = Object . keys ( federationConfig . exposes ) ;
72- const outFile = path . resolve ( outputDir , `${ federationConfig . name } .d.ts` ) ;
7382
7483function getModuleDeclareName ( exposeName ) {
7584 // windows paths 🤦
7685 return path . join ( federationConfig . name , exposeName ) . replace ( / [ \\ / ] / g, '/' ) ;
7786}
7887
7988try {
89+ const outFile = path . resolve ( outputDir , `${ federationConfig . name } .d.ts` ) ;
8090 if ( fs . existsSync ( outFile ) ) {
8191 fs . unlinkSync ( outFile ) ;
8292 }
@@ -128,39 +138,40 @@ try {
128138 ] . join ( '\n' ) ;
129139 } ) ;
130140
131- console . log ( 'writing typing file:' , outFile ) ;
141+ outputDirs . forEach ( _outputDir => {
142+ const _outFile = path . resolve ( _outputDir , `${ federationConfig . name } .d.ts` ) ;
143+ console . log ( 'writing typing file:' , _outFile ) ;
132144
133- fs . writeFileSync ( outFile , typing ) ;
145+ fs . writeFileSync ( _outFile , typing ) ;
134146
135- // if we are writing to the node_modules/@types directory, add a package.json file
136- if ( outputDir . includes ( path . join ( 'node_modules' , '@types' ) ) ) {
137- const packageJsonPath = path . resolve ( outputDir , 'package.json' ) ;
147+ console . debug ( `using output dir: ${ _outputDir } ` ) ;
148+ // if we are writing to the node_modules/@types directory, add a package.json file
149+ if ( _outputDir . includes ( path . join ( 'node_modules' , '@types' ) ) ) {
150+ const packageJsonPath = path . resolve ( _outputDir , 'package.json' ) ;
138151
139- if ( ! fs . existsSync ( packageJsonPath ) ) {
140- console . debug ( 'writing package.json:' , packageJsonPath ) ;
141- fs . copyFileSync ( path . resolve ( __dirname , 'typings.package.tmpl.json' ) , packageJsonPath ) ;
142- } else {
143- console . debug ( packageJsonPath , 'already exists' ) ;
152+ if ( ! fs . existsSync ( packageJsonPath ) ) {
153+ console . debug ( 'writing package.json:' , packageJsonPath ) ;
154+ fs . copyFileSync ( path . resolve ( __dirname , 'typings.package.tmpl.json' ) , packageJsonPath ) ;
155+ } else {
156+ console . debug ( packageJsonPath , 'already exists' ) ;
157+ }
144158 }
145- } else {
146- console . debug ( 'not writing to node modules, dont need a package.json' ) ;
147- }
148159
149- // write/update the index.d.ts file
150- const indexPath = path . resolve ( outputDir , 'index.d.ts' ) ;
151- const importStatement = `export * from './${ federationConfig . name } ';` ;
152-
153- if ( ! fs . existsSync ( indexPath ) ) {
154- console . log ( 'creating index.d.ts file' ) ;
155- fs . writeFileSync ( indexPath , `${ importStatement } \n` ) ;
156- } else {
157- console . log ( 'updating index.d.ts file' ) ;
158- const contents = fs . readFileSync ( indexPath ) ;
159- if ( ! contents . includes ( importStatement ) ) {
160- fs . writeFileSync ( indexPath , `${ contents } ${ importStatement } \n` ) ;
161- }
162- }
160+ // write/update the index.d.ts file
161+ const indexPath = path . resolve ( _outputDir , 'index.d.ts' ) ;
162+ const importStatement = `export * from './${ federationConfig . name } ';` ;
163163
164+ if ( ! fs . existsSync ( indexPath ) ) {
165+ console . log ( 'creating index.d.ts file' ) ;
166+ fs . writeFileSync ( indexPath , `${ importStatement } \n` ) ;
167+ } else {
168+ console . log ( 'updating index.d.ts file' ) ;
169+ const contents = fs . readFileSync ( indexPath ) ;
170+ if ( ! contents . includes ( importStatement ) ) {
171+ fs . writeFileSync ( indexPath , `${ contents } ${ importStatement } \n` ) ;
172+ }
173+ }
174+ } ) ;
164175 console . debug ( 'Success!' ) ;
165176} catch ( e ) {
166177 console . error ( `ERROR:` , e ) ;
0 commit comments