@@ -10,7 +10,7 @@ c. let my_Function_A = *import MY_PACKAGE: Function_A
1010
1111async function preprocessor ( source : string ) : Promise < string > {
1212 console . log ( 'The source code after preprocessing' ) ;
13- let result = await preprocessDFS ( source , 'root' ) ;
13+ const result = await preprocessDFS ( source , 'root' ) ;
1414 console . log ( result ) ;
1515 console . log ( 'End of the source code after preprocessing' ) ;
1616 return result ;
@@ -27,12 +27,14 @@ Output: The root location of the package. For example, "https://raw.githubuserco
2727*/
2828
2929function getPackageLocation ( packageName : string , theFullListInJson : string ) : string {
30- console . log ( "Package name: " + packageName + " , full list in json: " + theFullListInJson ) ;
31- let jsonObj = JSON . parse ( theFullListInJson ) ; let result : string [ ] = [ ] ;
32- for ( let element of jsonObj ) {
33- if ( element [ "name" ] === packageName || element [ "alias" ] === packageName ) { return element [ "location" ] ; }
30+ console . log ( 'Package name: ' + packageName + ' , full list in json: ' + theFullListInJson ) ;
31+ const jsonObj = JSON . parse ( theFullListInJson ) ;
32+ for ( const element of jsonObj ) {
33+ if ( element [ 'name' ] === packageName || element [ 'alias' ] === packageName ) {
34+ return element [ 'location' ] ;
35+ }
3436 }
35- throw " Cannot find the package with name: " + packageName ;
37+ throw ' Cannot find the package with name: ' + packageName ;
3638}
3739
3840/*
@@ -43,60 +45,79 @@ Input: secondPart: the second part of current line splitted by "*import". For ex
4345 strCurrentCallStack: current call stack
4446Output: A list of string, each string represents the corresponding hhs source file
4547*/
46- async function parseRegisterdPackage ( secondPart : string , strCurrentCallStack :string ) : Promise < Array < string > > {
47- let returnListOfFunctions : string [ ] = [ ] ;
48- let theFullListInJson = await fetch ( "https://raw.githubusercontent.com/Hedgehog-Computing/Hedgehog-Package-Manager/main/hedgehog-packages.json" , { method : 'get' } ) . then ( body => body . text ( ) ) ;
49- let splittedResult = secondPart . split ( ':' ) ;
50- if ( splittedResult . length != 2 ) throw "Invalid importing library: " + secondPart ;
48+ async function parseRegisterdPackage (
49+ secondPart : string ,
50+ strCurrentCallStack : string
51+ ) : Promise < Array < string > > {
52+ const returnListOfFunctions : string [ ] = [ ] ;
53+ const theFullListInJson = await fetch (
54+ 'https://raw.githubusercontent.com/Hedgehog-Computing/Hedgehog-Package-Manager/main/hedgehog-packages.json' ,
55+ { method : 'get' }
56+ ) . then ( ( body ) => body . text ( ) ) ;
57+ const splittedResult = secondPart . split ( ':' ) ;
58+ if ( splittedResult . length != 2 ) throw 'Invalid importing library: ' + secondPart ;
5159
5260 //get the right package name and HHS list string
53- let packageName = splittedResult [ 0 ]
54- let importedHHSListString = splittedResult [ 1 ] ;
61+ const packageName = splittedResult [ 0 ]
62+ const importedHHSListString = splittedResult [ 1 ] ;
5563
5664 //get package location
57- let packageLocation = getPackageLocation ( packageName . replace ( / \s / g, '' ) , theFullListInJson ) ;
65+ const packageLocation = getPackageLocation ( packageName . replace ( / \s / g, '' ) , theFullListInJson ) ;
5866
5967 //get the package json file: package_location + hedgehog-package.json
60- let packageJsonFile = packageLocation + " hedgehog-package.json" ;
68+ const packageJsonFile = packageLocation + ' hedgehog-package.json' ;
6169
62- console . log ( " Package Json file: " + packageJsonFile ) ;
70+ console . log ( ' Package Json file: ' + packageJsonFile ) ;
6371 //get the hedgehog-pacakge.json, then get the list of "includes" libraries
64- let thePackageJsonString = await fetch ( packageJsonFile , { method : 'get' } ) . then ( body => body . text ( ) ) ;
65- console . log ( "Package Json string: " + thePackageJsonString ) ;
66- let thePackageJsonObj = JSON . parse ( thePackageJsonString ) ;
67- if ( "includes" in thePackageJsonObj ) {
68- let hhsCompleteList = thePackageJsonObj [ "includes" ] ;
69- let setHHSCompleteList = new Set ( hhsCompleteList ) ;
70- let importedItemList = importedHHSListString . split ( ',' ) ;
71- for ( let eachItem of importedItemList ) {
72- let eachItemWithoutSpace = eachItem . replace ( / \s / g, '' ) ;
72+ const thePackageJsonString = await fetch ( packageJsonFile , { method : 'get' } ) . then ( ( body ) =>
73+ body . text ( )
74+ ) ;
75+ console . log ( 'Package Json string: ' + thePackageJsonString ) ;
76+ const thePackageJsonObj = JSON . parse ( thePackageJsonString ) ;
77+ if ( 'includes' in thePackageJsonObj ) {
78+ const hhsCompleteList = thePackageJsonObj [ 'includes' ] ;
79+ const setHHSCompleteList = new Set ( hhsCompleteList ) ;
80+ const importedItemList = importedHHSListString . split ( ',' ) ;
81+ for ( const eachItem of importedItemList ) {
82+ const eachItemWithoutSpace = eachItem . replace ( / \s / g, '' ) ;
7383 if ( setHHSCompleteList . has ( eachItemWithoutSpace ) ) {
74- let currentHHSLocation = packageLocation + eachItemWithoutSpace + ".hhs" ;
75- let currentItemSourceCode = await fetch ( currentHHSLocation , { method : 'get' } ) . then ( body => body . text ( ) ) ;
84+ const currentHHSLocation = packageLocation + eachItemWithoutSpace + ".hhs" ;
85+ const currentItemSourceCode = await fetch ( currentHHSLocation , { method : 'get' } ) . then (
86+ ( body ) => body . text ( )
87+ ) ;
7688
7789 // After get the whole hhs source file, preprocess the string via DFS preprocessor
7890 // to handle the dependencies of current source file string
79- let preprocessedCurrentItemSourceCode = await preprocessDFS ( currentItemSourceCode , strCurrentCallStack + " -> " + strCurrentCallStack + ":" + eachItemWithoutSpace ) ;
91+ const preprocessedCurrentItemSourceCode = await preprocessDFS (
92+ currentItemSourceCode ,
93+ strCurrentCallStack + ' -> ' + strCurrentCallStack + ':' + eachItemWithoutSpace
94+ ) ;
8095 returnListOfFunctions . push ( preprocessedCurrentItemSourceCode ) ;
8196 }
8297 }
98+ } else {
99+ throw (
100+ 'Cannot find "includes" key in the hedgehog-package.json configuration file! Please add a key with name "includes" with a complete list of exported libraries. Exception at ' +
101+ secondPart +
102+ '.\nCall Stack at: ' +
103+ strCurrentCallStack
104+ ) ;
83105 }
84- else { throw "Cannot find \"includes\" key in the hedgehog-package.json configuration file! Please add a key with name \"includes\" with a complete list of exported libraries. Exception at " + secondPart + ".\nCall Stack at: " + strCurrentCallStack }
85106 return returnListOfFunctions ;
86107}
87108
88109// A helper function to check if a string contains URL or not. Reference: https://regexr.com/3e6m0
89110function containsURL ( code : string ) : boolean {
90- let expression = / ( h t t p ( s ) ? : \/ \/ .) ? ( w w w \. ) ? [ - a - z A - Z 0 - 9 @ : % . _ \+ ~ # = ] { 2 , 256 } \. [ a - z ] { 2 , 6 } \b ( [ - a - z A - Z 0 - 9 @ : % _ \+ . ~ # ? & / / = ] * ) / g;
91- let regex = new RegExp ( expression ) ;
111+ const expression = / ( h t t p ( s ) ? : \/ \/ .) ? ( w w w \. ) ? [ - a - z A - Z 0 - 9 @ : % . _ \+ ~ # = ] { 2 , 256 } \. [ a - z ] { 2 , 6 } \b ( [ - a - z A - Z 0 - 9 @ : % _ \+ . ~ # ? & / / = ] * ) / g;
112+ const regex = new RegExp ( expression ) ;
92113 if ( code . match ( regex ) ) return true ;
93114 return false ;
94115}
95116
96117// code is the string of code, and strCurrentCallStack is the full call stack
97118async function preprocessDFS ( code : string , strCurrentCallStack : string ) : Promise < string > {
98119 //1. split the codes into lines
99- let vecSplittedString = code . split ( / \r ? \n / ) ;
120+ const vecSplittedString = code . split ( / \r ? \n / ) ;
100121
101122 //2. initialize the the chunk of string to return
102123 let returnCode = '' ;
@@ -106,31 +127,36 @@ async function preprocessDFS(code: string, strCurrentCallStack: string): Promise
106127 for ( let i = 0 ; i < vecSplittedString . length ; i ++ ) {
107128 returnCode += '\n' ;
108129 //3.1 if current line of code doesn't contain "*import ", just append it to returnCode
109- if ( ! vecSplittedString [ i ] . includes ( "*import " ) ) { returnCode += '\n' + vecSplittedString [ i ] ; }
130+ if ( ! vecSplittedString [ i ] . includes ( '*import ' ) ) {
131+ returnCode += '\n' + vecSplittedString [ i ] ;
132+ }
110133 //3.2 otherwise, split the string by "*import ", keep the first part (if it exists), then download
111134 // and fetch the second part recursively (which should be and must be a valid URL or a registered package)
112135 else {
113- let currentString = vecSplittedString [ i ] ;
114- let splittedResult = currentString . split ( "*import " ) ;
136+ const currentString = vecSplittedString [ i ] ;
137+ const splittedResult = currentString . split ( "*import " ) ;
115138 if ( splittedResult . length < 2 ) {
116- throw "Invalid current line of code for preprocessing: \n"
117- + "\nCall stack: \n" + strCurrentCallStack
118- + "\nCurrent line: " + currentString + "\n" ;
139+ throw (
140+ 'Invalid current line of code for preprocessing: \n' +
141+ + "\nCall stack: \n" + strCurrentCallStack +
142+ '\nCurrent line: ' +
143+ currentString +
144+ '\n'
145+ ) ;
119146 }
120147 //3.2.1 add the first part
121148 returnCode += splittedResult [ 0 ] ;
122149 //3.2.2 Is it imported from URL or from a registered package?
123150 if ( containsURL ( splittedResult [ 1 ] ) ) {
124- //3.2.2.1 download the library from URL
125- let libraryFromUrl = await fetch ( splittedResult [ 1 ] , { method : 'get' } )
126- . then ( function ( body ) {
127- let real_library = body . text ( ) ;
128- return real_library ;
129- } ) ;
151+ //3.2.2.1 download the library from URL
152+ const libraryFromUrl = await fetch ( splittedResult [ 1 ] , { method : 'get' } ) . then ( ( body ) => {
153+ const real_library = body . text ( ) ;
154+ return real_library ;
155+ } ) ;
130156
131157 //3.2.3 get the current file information (get "FunctionABC.js" from URL string http://mywebsite/FunctionABC.js)
132- let splittedURLResult = splittedResult [ 1 ] . split ( '/' ) ;
133- let strCallStack = strCurrentCallStack + " -> " + splittedURLResult [ splittedResult . length - 1 ] ;
158+ const splittedURLResult = splittedResult [ 1 ] . split ( '/' ) ;
159+ const strCallStack = strCurrentCallStack + " -> " + splittedURLResult [ splittedResult . length - 1 ] ;
134160
135161 //3.2.4 process the big chunk of code
136162 let currentResult = await preprocessDFS ( libraryFromUrl , strCallStack ) ;
0 commit comments