@@ -11,41 +11,51 @@ function plugin(opts) {
11
11
// set global information about the available locales
12
12
const defaultLocale = meta . defaultLocale = opts . defaultLocale ;
13
13
const locales = meta . locales = opts . locales ;
14
+
15
+ // creates a pattern for the given locales
14
16
const pattern = ( locales ) => {
15
17
if ( ! Array . isArray ( locales ) ) {
16
18
locales = [ locales ]
17
19
}
18
-
19
20
return new RegExp ( `.*\\/(?:${ locales . join ( '|' ) } )\\/(.+)(\\..+)` )
20
21
} ;
21
22
23
+ // creates a list of required files based on the default locale
22
24
const requiredFiles = Object . keys ( files )
23
25
. filter ( file => file . match ( pattern ( defaultLocale ) ) )
24
26
. map ( file => {
25
27
return file . replace ( `${ path . sep } ${ defaultLocale } ${ path . sep } ` , `${ path . sep } {LOCALE}${ path . sep } ` ) ;
26
28
} ) ;
29
+
27
30
const otherLocales = locales . filter ( l => l !== defaultLocale ) ;
28
31
32
+ // builds out the file structure for all the other locales
29
33
otherLocales . forEach ( ( locale ) => {
34
+
30
35
requiredFiles . forEach ( ( file ) => {
36
+ const original_file = file . replace ( '{LOCALE}' , defaultLocale ) ;
37
+ console . log ( original_file ) ;
38
+
31
39
const new_file = file . replace ( '{LOCALE}' , locale ) ;
32
- const dir = path . dirname ( file ) ;
40
+ const new_file_path = path . resolve ( sourcePath , new_file ) ;
41
+ const dir = path . dirname ( new_file_path ) ;
42
+
33
43
mkdirp ( dir , ( err ) => {
34
44
if ( err ) throw err ;
35
- const exists = fs . existsSync ( new_file ) ;
36
- if ( ! exists ) {
37
- fs . writeFileSync ( new_file , files [ file . replace ( '{LOCALE}' , defaultLocale ) ] . contents , ( ) => {
38
- console . log ( 'Written new file at ' , new_file )
39
- } )
45
+ if ( ! fs . existsSync ( new_file_path ) ) {
46
+ const data = files [ original_file ] ;
47
+ const contents = data ? data . contents : '' ;
48
+ fs . writeFileSync ( new_file_path , contents ) ;
49
+
50
+ files [ new_file ] = Object . assign ( { } , data , {
51
+ contents : new Buffer ( contents )
52
+ } ) ;
40
53
}
41
54
} )
42
55
} )
43
56
} ) ;
44
- console . log ( otherLocales , requiredFiles )
45
- // console.log(path.resolve(sourcePath, defaultLocale));
46
-
47
57
48
- // done();
58
+ done ( ) ;
49
59
}
50
60
}
51
61
0 commit comments