@@ -15,6 +15,7 @@ const limitedWebRequest = async (url, type) => {
1515 } ) , url , type ) ;
1616}
1717
18+ // check hashes of all files in a feed
1819const checkFileIntegrity = async ( feeds ) => {
1920 for ( var feed of feeds ) {
2021 var feedHashesValid = true ;
@@ -44,6 +45,46 @@ const checkFileIntegrity = async (feeds) => {
4445 return feeds ;
4546}
4647
48+ const extractProperty = ( script , property ) => {
49+ var match = Array . from ( script . matchAll ( new RegExp ( `\\s*${ property } \\s*=\\s*(?:tr)?(?:"([^"]*)"|'([^']*)')` , 'g' ) ) ) ;
50+ if ( match . length === 1 ) {
51+ return ( match [ 0 ] [ 1 ] || "" ) + ( match [ 0 ] [ 2 ] || "" ) ;
52+ }
53+ return null ;
54+ }
55+
56+ // extracts data from lua or moon macros and saves it alongside feed data
57+ const extractScriptData = async ( feeds ) => {
58+ for ( var feed of feeds ) {
59+ for ( var [ macroID , macro ] of Object . entries ( feed . macros || { } ) ) {
60+ var defaultChannel = Object . values ( macro . channels ) . filter ( ( c ) => c . default ) [ 0 ] ;
61+ macro [ "_defaultVersion" ] = defaultChannel . version ;
62+
63+ var urls = Object . values ( defaultChannel . files )
64+ . filter ( ( f ) => f . type !== "test" && ! f . delete )
65+ . map ( ( f ) => f . url ) ;
66+ if ( urls . length !== 1 ) {
67+ continue ;
68+ }
69+
70+ var url = urls [ 0 ] ;
71+ if ( url . endsWith ( ".lua" ) || url . endsWith ( ".moon" ) ) {
72+ var script = ( await limitedWebRequest ( url , "buffer" ) ) . toString ( ) ;
73+ var scriptData = {
74+ "name" : extractProperty ( script , "script_name" ) ,
75+ "description" : extractProperty ( script , "script_description" ) ,
76+ "author" : extractProperty ( script , "script_author" ) ,
77+ "version" : extractProperty ( script , "script_version" ) ,
78+ "namespace" : extractProperty ( script , "script_namespace" )
79+ }
80+ macro [ "_scriptData" ] = scriptData ;
81+ } else {
82+ continue ;
83+ }
84+ }
85+ }
86+ return feeds ;
87+ }
4788
4889// fetch single feed
4990const fetchFeed = ( url ) => {
@@ -153,6 +194,7 @@ function fillTemplateVar(data, repDict = {}, parentKey = "", depth = 0) {
153194// store data locally to not compute feeds multiple times
154195const data = fetchAllFeeds ( seedFeed )
155196 . then ( fillTemplateVar )
156- . then ( checkFileIntegrity ) ;
197+ . then ( checkFileIntegrity )
198+ . then ( extractScriptData ) ;
157199
158200module . exports . getData = ( ) => data ;
0 commit comments