@@ -2,37 +2,58 @@ import * as fs from "fs";
22import { join as pathJoin } from "path" ;
33import { getProjectRoot } from "../bin/tools/getProjectRoot" ;
44import { exclude } from "tsafe/exclude" ;
5+ import { execSync } from "child_process" ;
6+ import { same } from "evt/tools/inDepth/same" ;
57
68const packageJsonFilePath = pathJoin ( getProjectRoot ( ) , "package.json" ) ;
79
810const packageJsonParsed = JSON . parse ( fs . readFileSync ( packageJsonFilePath ) . toString ( "utf8" ) ) ;
911
12+ const srcDirPath = pathJoin ( getProjectRoot ( ) , "src" ) ;
13+
14+ const newExports = {
15+ "." : `./${ packageJsonParsed [ "module" ] } ` ,
16+ ...Object . fromEntries (
17+ fs
18+ . readdirSync ( srcDirPath )
19+ . filter ( basename => {
20+ try {
21+ execSync ( `git ls-files --error-unmatch ${ pathJoin ( srcDirPath , basename ) } ` , {
22+ "stdio" : [ ]
23+ } ) ;
24+ } catch {
25+ return false ;
26+ }
27+
28+ return true ;
29+ } )
30+ . map ( basename => {
31+ const match = basename . match ( / ^ ( [ ^ . ] + ) \. t s x ? $ / ) ;
32+
33+ if ( match === null ) {
34+ return undefined ;
35+ }
36+
37+ return match [ 1 ] ;
38+ } )
39+ . filter ( exclude ( undefined ) )
40+ . sort ( )
41+ . reverse ( )
42+ . map ( name => [ `./${ name } ` , `./dist/${ name } .js` ] )
43+ )
44+ } ;
45+
46+ if ( same ( packageJsonParsed [ "exports" ] , newExports ) ) {
47+ process . exit ( 0 ) ;
48+ }
49+
1050fs . writeFileSync (
1151 packageJsonFilePath ,
1252 Buffer . from (
1353 JSON . stringify (
1454 {
1555 ...packageJsonParsed ,
16- "exports" : {
17- "." : `./${ packageJsonParsed [ "module" ] } ` ,
18- ...Object . fromEntries (
19- fs
20- . readdirSync ( pathJoin ( getProjectRoot ( ) , "src" ) )
21- . map ( basename => {
22- const match = basename . match ( / ^ ( [ ^ . ] + ) \. t s x ? $ / ) ;
23-
24- if ( match === null ) {
25- return undefined ;
26- }
27-
28- return match [ 1 ] ;
29- } )
30- . filter ( exclude ( undefined ) )
31- . sort ( )
32- . reverse ( )
33- . map ( name => [ `./${ name } ` , `./dist/${ name } .js` ] )
34- )
35- }
56+ "exports" : newExports
3657 } ,
3758 null ,
3859 2
0 commit comments