@@ -91,130 +91,201 @@ export function generateIconsRawCssCode(params: {
9191}
9292
9393async function main ( ) {
94- const packageName = JSON . parse (
94+ const codegouvfrReactDsfr : string = JSON . parse (
9595 fs . readFileSync ( pathJoin ( getProjectRoot ( ) , "package.json" ) ) . toString ( "utf8" )
9696 ) [ "name" ] ;
9797
9898 const cwd = process . cwd ( ) ;
9999
100- const dsfrDistDirPath =
101- getProjectRoot ( ) === cwd
102- ? pathJoin ( cwd , "dist" , "dsfr" )
103- : await ( async function callee ( n : number ) : Promise < string > {
104- if ( n >= cwd . split ( pathSep ) . length ) {
105- throw new Error ( "Need to install node modules?" ) ;
106- }
107-
108- const dirPath = pathJoin (
109- ...[
110- cwd ,
111- ...new Array ( n ) . fill ( ".." ) ,
112- "node_modules" ,
113- ...packageName . split ( "/" ) ,
114- "dsfr"
115- ]
116- ) ;
117-
118- try {
119- await access ( dirPath ) ;
120- } catch {
121- return callee ( n + 1 ) ;
122- }
123-
124- return dirPath ;
125- } ) ( 0 ) ;
100+ const isCwdReactDsfr = pathJoin ( getProjectRoot ( ) , ".." ) === cwd ;
101+
102+ const dsfrDistDirPath = isCwdReactDsfr
103+ ? pathJoin ( cwd , "dist" , "dsfr" )
104+ : await ( async function callee ( n : number ) : Promise < string > {
105+ if ( n >= cwd . split ( pathSep ) . length ) {
106+ throw new Error ( "Need to install node modules?" ) ;
107+ }
108+
109+ const dirPath = pathJoin (
110+ ...[
111+ cwd ,
112+ ...new Array ( n ) . fill ( ".." ) ,
113+ "node_modules" ,
114+ ...codegouvfrReactDsfr . split ( "/" ) ,
115+ "dsfr"
116+ ]
117+ ) ;
118+
119+ try {
120+ await access ( dirPath ) ;
121+ } catch {
122+ return callee ( n + 1 ) ;
123+ }
124+
125+ return dirPath ;
126+ } ) ( 0 ) ;
126127
127128 const icons : Icon [ ] = JSON . parse (
128129 ( await readFile ( pathJoin ( dsfrDistDirPath , pathOfIconsJson ) ) ) . toString ( "utf8" )
129130 ) ;
130131
131132 const { usedIconClassNames } = await ( async function getUsedIconClassNames ( ) {
132133 const candidateFilePaths = (
133- await crawl ( {
134- "dirPath" : cwd ,
135- "getDoCrawlInDir" : async ( { relativeDirPath } ) => {
136- if ( relativeDirPath === "node_modules" ) {
137- return true ;
138- }
139-
140- if (
141- relativeDirPath . startsWith ( `node_modules${ pathSep } @` ) &&
142- relativeDirPath . split ( pathSep ) . length === 2
143- ) {
144- return true ;
145- }
146-
147- if (
148- relativeDirPath . startsWith ( "node_modules" ) &&
149- ( relativeDirPath . split ( pathSep ) . length === 2 ||
150- ( relativeDirPath . startsWith ( `node_modules${ pathSep } @` ) &&
151- relativeDirPath . split ( pathSep ) . length === 3 ) )
152- ) {
153- const parsedPackageJson = await readFile (
154- pathJoin ( relativeDirPath , "package.json" )
155- ) . then (
156- buff => JSON . parse ( buff . toString ( "utf8" ) ) ,
157- ( ) => undefined
158- ) ;
159-
160- if ( parsedPackageJson === undefined ) {
161- return false ;
162- }
163-
164- if ( parsedPackageJson [ "name" ] === "tss-react" ) {
165- return false ;
166- }
167-
168- for ( const packageName of [
169- "@gouvfr/dsfr" ,
170- "@codegouvfr/react-dsfr" ,
171- "@dataesr/react-dsfr"
172- ] ) {
173- if (
174- Object . keys ( {
175- ...parsedPackageJson [ "dependencies" ] ,
176- ...parsedPackageJson [ "devDependencies" ] ,
177- ...parsedPackageJson [ "peerDependencies" ]
178- } ) . includes ( packageName )
179- ) {
180- return true ;
181- }
182- }
183-
184- return false ;
185- }
186-
187- if ( relativeDirPath === `public${ pathSep } dsfr` ) {
188- return false ;
189- }
190-
191- if ( pathBasename ( relativeDirPath ) === "generatedFromCss" ) {
192- return false ;
193- }
194-
195- if (
196- pathDirname ( relativeDirPath ) . endsWith ( pathJoin ( ...packageName . split ( "/" ) ) )
197- ) {
198- return pathBasename ( relativeDirPath ) === "src" ;
199- }
200-
201- if ( pathBasename ( relativeDirPath ) === "node_modules" ) {
202- return false ;
203- }
204-
205- if ( pathBasename ( relativeDirPath ) . startsWith ( "." ) ) {
206- return false ;
207- }
208-
209- return true ;
210- }
211- } )
212- ) . filter (
213- filePath =>
214- [ "tsx" , "jsx" , "js" , "ts" , "html" , "htm" ] . find ( ext =>
215- filePath . endsWith ( `.${ ext } ` )
216- ) !== undefined
217- ) ;
134+ await Promise . all (
135+ isCwdReactDsfr
136+ ? [
137+ crawl ( {
138+ "dirPath" : pathJoin ( cwd , "stories" ) ,
139+ "returnedPathsType" : "absolute"
140+ } ) ,
141+ crawl ( {
142+ "dirPath" : pathJoin ( cwd , "src" ) ,
143+ "returnedPathsType" : "absolute" ,
144+ "getDoCrawlInDir" : async ( { relativeDirPath } ) => {
145+ if ( pathBasename ( relativeDirPath ) === "generatedFromCss" ) {
146+ return false ;
147+ }
148+
149+ return true ;
150+ }
151+ } )
152+ ]
153+ : [
154+ crawl ( {
155+ "dirPath" : cwd ,
156+ "returnedPathsType" : "absolute" ,
157+ "getDoCrawlInDir" : async ( { relativeDirPath } ) => {
158+ if ( pathBasename ( relativeDirPath ) === "node_modules" ) {
159+ return false ;
160+ }
161+
162+ if ( relativeDirPath === `public${ pathSep } dsfr` ) {
163+ return false ;
164+ }
165+
166+ if ( pathBasename ( relativeDirPath ) . startsWith ( "." ) ) {
167+ return false ;
168+ }
169+
170+ return true ;
171+ }
172+ } ) ,
173+ ( async ( ) => {
174+ const nodeModuleDirPath = await ( async function callee (
175+ n : number
176+ ) : Promise < string > {
177+ if ( n >= cwd . split ( pathSep ) . length ) {
178+ throw new Error ( "Need to install node modules?" ) ;
179+ }
180+
181+ const nodeModuleDirPath = pathJoin (
182+ ...[ cwd , ...new Array ( n ) . fill ( ".." ) , "node_modules" ]
183+ ) ;
184+
185+ try {
186+ await access (
187+ pathJoin (
188+ ...[
189+ nodeModuleDirPath ,
190+ ...codegouvfrReactDsfr . split ( "/" )
191+ ]
192+ )
193+ ) ;
194+ } catch {
195+ return callee ( n + 1 ) ;
196+ }
197+
198+ return nodeModuleDirPath ;
199+ } ) ( 0 ) ;
200+
201+ return await crawl ( {
202+ "dirPath" : nodeModuleDirPath ,
203+ "returnedPathsType" : "absolute" ,
204+ "getDoCrawlInDir" : async ( { relativeDirPath } ) => {
205+ if (
206+ relativeDirPath . startsWith ( "@" ) &&
207+ relativeDirPath . split ( pathSep ) . length === 1
208+ ) {
209+ return true ;
210+ }
211+
212+ if (
213+ relativeDirPath . split ( pathSep ) . length === 1 ||
214+ ( relativeDirPath . startsWith ( "@" ) &&
215+ relativeDirPath . split ( pathSep ) . length === 2 )
216+ ) {
217+ const parsedPackageJson = await readFile (
218+ pathJoin (
219+ nodeModuleDirPath ,
220+ relativeDirPath ,
221+ "package.json"
222+ )
223+ ) . then (
224+ buff => JSON . parse ( buff . toString ( "utf8" ) ) ,
225+ ( ) => undefined
226+ ) ;
227+
228+ if ( parsedPackageJson === undefined ) {
229+ return false ;
230+ }
231+
232+ if ( parsedPackageJson [ "name" ] === "tss-react" ) {
233+ return false ;
234+ }
235+
236+ for ( const packageName of [
237+ codegouvfrReactDsfr ,
238+ "@gouvfr/dsfr" ,
239+ "@dataesr/react-dsfr"
240+ ] ) {
241+ if (
242+ Object . keys ( {
243+ ...parsedPackageJson [ "dependencies" ] ,
244+ ...parsedPackageJson [ "devDependencies" ] ,
245+ ...parsedPackageJson [ "peerDependencies" ]
246+ } ) . includes ( packageName )
247+ ) {
248+ return true ;
249+ }
250+ }
251+
252+ return false ;
253+ }
254+
255+ if (
256+ pathDirname ( relativeDirPath ) . endsWith (
257+ pathJoin ( ...codegouvfrReactDsfr . split ( "/" ) )
258+ )
259+ ) {
260+ return pathBasename ( relativeDirPath ) === "src" ;
261+ }
262+
263+ if ( pathBasename ( relativeDirPath ) === "generatedFromCss" ) {
264+ return false ;
265+ }
266+
267+ if ( pathBasename ( relativeDirPath ) === "node_modules" ) {
268+ return false ;
269+ }
270+
271+ if ( pathBasename ( relativeDirPath ) . startsWith ( "." ) ) {
272+ return false ;
273+ }
274+
275+ return true ;
276+ }
277+ } ) ;
278+ } ) ( )
279+ ]
280+ )
281+ )
282+ . flat ( )
283+ . filter (
284+ filePath =>
285+ [ "tsx" , "jsx" , "js" , "ts" , "html" , "htm" ] . find ( ext =>
286+ filePath . endsWith ( `.${ ext } ` )
287+ ) !== undefined
288+ ) ;
218289
219290 const prefixes = { "prefixDsfr" : "fr-icon-" , "prefixRemixIcon" : "ri-" } as const ;
220291
0 commit comments