11import * as fs from "fs-extra" ;
22import * as path from "path" ;
3- import * as url from "url" ;
43import axios from "axios" ;
5- import { promisify } from "util " ;
4+ import Handlebars from "handlebars " ;
65
76// Regular expressions for finding license files
87const licenseRegex = / ^ ( L I C E N S E | L I C E N C E ) ( $ | \. m d | \. t x t ) / i;
@@ -198,7 +197,7 @@ async function main() {
198197 packages . push ( {
199198 name,
200199 url : url || vcsUrl || null ,
201- license : licenseText ,
200+ license : licenseText . trim ( ) ,
202201 version,
203202 source_url : sourceUrl || vcsUrl || null ,
204203 } ) ;
@@ -213,46 +212,11 @@ async function main() {
213212
214213 console . log ( `Writing results to ${ outputFile } ` ) ;
215214
216- // Generate Markdown from template
217- const output = template . replace (
218- / { { # e a c h p a c k a g e s } } ( [ \s \S ] * ?) { { \/ e a c h } } / g,
219- ( _match , block ) => {
220- return packages
221- . map ( ( pkg ) => {
222- let result = block
223- . replace ( / { { name} } / g, pkg . name )
224- . replace ( / { { version} } / g, pkg . version )
225- . replace ( / { { { l i c e n s e } } } / g, pkg . license ) ;
226-
227- if ( pkg . url ) {
228- result = result
229- . replace ( / { { # i f u r l } } ( .* ?) { { \/ i f } } / g, "$1" )
230- . replace ( / { { url} } / g, pkg . url ) ;
231- } else {
232- result = result . replace ( / { { # i f u r l } } ( .* ?) { { \/ i f } } / g, "" ) ;
233- }
234-
235- if ( pkg . source_url ) {
236- result = result
237- . replace ( / { { # i f s o u r c e _ u r l } } ( [ \s \S ] * ?) { { \/ i f } } / g, "$1" )
238- . replace ( / { { source_ u r l } } / g, pkg . source_url ) ;
239- } else {
240- result = result . replace ( / { { # i f s o u r c e _ u r l } } [ \s \S ] * ?{ { \/ i f } } / g, "" ) ;
241- }
242-
243- return result ;
244- } )
245- . join ( "" ) ;
246- } ,
247- ) ;
248-
249- // Final output replacements
250- const finalOutput = output . replace (
251- / { { # e a c h p a c k a g e s } } [ \s \S ] * ?{ { \/ e a c h } } / g,
252- "" ,
253- ) ;
254-
255- fs . writeFileSync ( outputFile , finalOutput ) ;
215+ const compiledTemplate = Handlebars . compile ( template ) ;
216+
217+ const output = compiledTemplate ( { packages } ) ;
218+
219+ fs . writeFileSync ( outputFile , output ) ;
256220 console . log ( "Finished!" ) ;
257221}
258222
0 commit comments