@@ -124,4 +124,47 @@ export const fetchPaths = (module, interpreter, config_fetch) =>
124124 . then ( ( buffer ) => module . writeFile ( interpreter , path , buffer ) ) ,
125125 ) ,
126126 ) ;
127+
128+ const fillName = ( source , dest ) => dest . endsWith ( '/' ) ?
129+ `${ dest } ${ source . split ( '/' ) . pop ( ) } ` : dest ;
130+
131+ const parseTemplate = ( src , map ) => src . replace (
132+ / \{ .+ ?\} / g,
133+ k => {
134+ if ( ! map . has ( k ) )
135+ throw new SyntaxError ( `Invalid template: ${ k } ` ) ;
136+ return map . get ( k ) ;
137+ }
138+ ) ;
139+
140+ const calculateFilesPaths = files => {
141+ const map = new Map ;
142+ const targets = new Set ;
143+ const sourceDest = [ ] ;
144+ for ( const [ source , dest ] of Object . entries ( files ) ) {
145+ if ( / ^ \{ .+ \} $ / . test ( source ) ) {
146+ if ( map . has ( source ) )
147+ throw new SyntaxError ( `Duplicated template: ${ source } ` ) ;
148+ map . set ( source , parseTemplate ( dest , map ) ) ;
149+ }
150+ else {
151+ const url = parseTemplate ( source , map ) ;
152+ const path = fillName ( url , parseTemplate ( dest , map ) ) ;
153+ if ( targets . has ( path ) )
154+ throw new SyntaxError ( `Duplicated destination: ${ path } ` ) ;
155+ targets . add ( path ) ;
156+ sourceDest . push ( { url, path } ) ;
157+ }
158+ }
159+ return sourceDest ;
160+ } ;
161+
162+ export const fetchFiles = ( module , interpreter , config_files ) =>
163+ all (
164+ calculateFilesPaths ( config_files ) . map ( ( { url, path } ) =>
165+ fetchResolved ( config_files , url )
166+ . then ( getBuffer )
167+ . then ( ( buffer ) => module . writeFile ( interpreter , path , buffer ) ) ,
168+ ) ,
169+ ) ;
127170/* c8 ignore stop */
0 commit comments