@@ -2,7 +2,12 @@ import _ from "lodash";
22import path from "path" ;
33import { glob } from "glob" ;
44import { CLIError } from "./errors" ;
5- import { I18nConfig , resolveOverriddenLocale , BucketItem , LocaleDelimiter } from "@lingo.dev/_spec" ;
5+ import {
6+ I18nConfig ,
7+ resolveOverriddenLocale ,
8+ BucketItem ,
9+ LocaleDelimiter ,
10+ } from "@lingo.dev/_spec" ;
611import { bucketTypeSchema } from "@lingo.dev/_spec" ;
712import Z from "zod" ;
813
@@ -12,54 +17,81 @@ type BucketConfig = {
1217 injectLocale ?: string [ ] ;
1318 lockedKeys ?: string [ ] ;
1419 lockedPatterns ?: string [ ] ;
20+ ignoredKeys ?: string [ ] ;
1521} ;
1622
1723export function getBuckets ( i18nConfig : I18nConfig ) {
18- const result = Object . entries ( i18nConfig . buckets ) . map ( ( [ bucketType , bucketEntry ] ) => {
19- const includeItems = bucketEntry . include . map ( ( item ) => resolveBucketItem ( item ) ) ;
20- const excludeItems = bucketEntry . exclude ?. map ( ( item ) => resolveBucketItem ( item ) ) ;
21- const config : BucketConfig = {
22- type : bucketType as Z . infer < typeof bucketTypeSchema > ,
23- paths : extractPathPatterns ( i18nConfig . locale . source , includeItems , excludeItems ) ,
24- } ;
25- if ( bucketEntry . injectLocale ) {
26- config . injectLocale = bucketEntry . injectLocale ;
27- }
28- if ( bucketEntry . lockedKeys ) {
29- config . lockedKeys = bucketEntry . lockedKeys ;
30- }
31- if ( bucketEntry . lockedPatterns ) {
32- config . lockedPatterns = bucketEntry . lockedPatterns ;
33- }
34- return config ;
35- } ) ;
24+ const result = Object . entries ( i18nConfig . buckets ) . map (
25+ ( [ bucketType , bucketEntry ] ) => {
26+ const includeItems = bucketEntry . include . map ( ( item ) =>
27+ resolveBucketItem ( item ) ,
28+ ) ;
29+ const excludeItems = bucketEntry . exclude ?. map ( ( item ) =>
30+ resolveBucketItem ( item ) ,
31+ ) ;
32+ const config : BucketConfig = {
33+ type : bucketType as Z . infer < typeof bucketTypeSchema > ,
34+ paths : extractPathPatterns (
35+ i18nConfig . locale . source ,
36+ includeItems ,
37+ excludeItems ,
38+ ) ,
39+ } ;
40+ if ( bucketEntry . injectLocale ) {
41+ config . injectLocale = bucketEntry . injectLocale ;
42+ }
43+ if ( bucketEntry . lockedKeys ) {
44+ config . lockedKeys = bucketEntry . lockedKeys ;
45+ }
46+ if ( bucketEntry . lockedPatterns ) {
47+ config . lockedPatterns = bucketEntry . lockedPatterns ;
48+ }
49+ if ( bucketEntry . ignoredKeys ) {
50+ config . ignoredKeys = bucketEntry . ignoredKeys ;
51+ }
52+ return config ;
53+ } ,
54+ ) ;
3655
3756 return result ;
3857}
3958
40- function extractPathPatterns ( sourceLocale : string , include : BucketItem [ ] , exclude ?: BucketItem [ ] ) {
59+ function extractPathPatterns (
60+ sourceLocale : string ,
61+ include : BucketItem [ ] ,
62+ exclude ?: BucketItem [ ] ,
63+ ) {
4164 const includedPatterns = include . flatMap ( ( pattern ) =>
42- expandPlaceholderedGlob ( pattern . path , resolveOverriddenLocale ( sourceLocale , pattern . delimiter ) ) . map (
43- ( pathPattern ) => ( {
44- pathPattern,
45- delimiter : pattern . delimiter ,
46- } ) ,
47- ) ,
65+ expandPlaceholderedGlob (
66+ pattern . path ,
67+ resolveOverriddenLocale ( sourceLocale , pattern . delimiter ) ,
68+ ) . map ( ( pathPattern ) => ( {
69+ pathPattern,
70+ delimiter : pattern . delimiter ,
71+ } ) ) ,
4872 ) ;
4973 const excludedPatterns = exclude ?. flatMap ( ( pattern ) =>
50- expandPlaceholderedGlob ( pattern . path , resolveOverriddenLocale ( sourceLocale , pattern . delimiter ) ) . map (
51- ( pathPattern ) => ( {
52- pathPattern,
53- delimiter : pattern . delimiter ,
54- } ) ,
55- ) ,
74+ expandPlaceholderedGlob (
75+ pattern . path ,
76+ resolveOverriddenLocale ( sourceLocale , pattern . delimiter ) ,
77+ ) . map ( ( pathPattern ) => ( {
78+ pathPattern,
79+ delimiter : pattern . delimiter ,
80+ } ) ) ,
81+ ) ;
82+ const result = _ . differenceBy (
83+ includedPatterns ,
84+ excludedPatterns ?? [ ] ,
85+ ( item ) => item . pathPattern ,
5686 ) ;
57- const result = _ . differenceBy ( includedPatterns , excludedPatterns ?? [ ] , ( item ) => item . pathPattern ) ;
5887 return result ;
5988}
6089
6190// Path expansion
62- function expandPlaceholderedGlob ( _pathPattern : string , sourceLocale : string ) : string [ ] {
91+ function expandPlaceholderedGlob (
92+ _pathPattern : string ,
93+ sourceLocale : string ,
94+ ) : string [ ] {
6395 // Throw if pathPattern is an absolute path
6496 const absolutePathPattern = path . resolve ( _pathPattern ) ;
6597 const pathPattern = path . relative ( process . cwd ( ) , absolutePathPattern ) ;
@@ -81,12 +113,15 @@ function expandPlaceholderedGlob(_pathPattern: string, sourceLocale: string): st
81113 // Break down path pattern into parts
82114 const pathPatternChunks = pathPattern . split ( path . sep ) ;
83115 // Find the index of the segment containing "[locale]"
84- const localeSegmentIndexes = pathPatternChunks . reduce ( ( indexes , segment , index ) => {
85- if ( segment . includes ( "[locale]" ) ) {
86- indexes . push ( index ) ;
87- }
88- return indexes ;
89- } , [ ] as number [ ] ) ;
116+ const localeSegmentIndexes = pathPatternChunks . reduce (
117+ ( indexes , segment , index ) => {
118+ if ( segment . includes ( "[locale]" ) ) {
119+ indexes . push ( index ) ;
120+ }
121+ return indexes ;
122+ } ,
123+ [ ] as number [ ] ,
124+ ) ;
90125 // substitute [locale] in pathPattern with sourceLocale
91126 const sourcePathPattern = pathPattern . replaceAll ( / \[ l o c a l e \] / g, sourceLocale ) ;
92127 // get all files that match the sourcePathPattern
@@ -104,7 +139,10 @@ function expandPlaceholderedGlob(_pathPattern: string, sourceLocale: string): st
104139 const sourcePathChunk = sourcePathChunks [ localeSegmentIndex ] ;
105140 const regexp = new RegExp (
106141 "(" +
107- pathPatternChunk . replaceAll ( "." , "\\." ) . replaceAll ( "*" , ".*" ) . replace ( "[locale]" , `)${ sourceLocale } (` ) +
142+ pathPatternChunk
143+ . replaceAll ( "." , "\\." )
144+ . replaceAll ( "*" , ".*" )
145+ . replace ( "[locale]" , `)${ sourceLocale } (` ) +
108146 ")" ,
109147 ) ;
110148 const match = sourcePathChunk . match ( regexp ) ;
0 commit comments