1- const { createFs } = require ( "indexeddb-fs" ) ;
21const { newServer } = require ( "mock-xmlhttprequest" ) ;
32const JSZip = require ( "jszip" ) ;
43
54const savedXMLHttpRequest = globalThis . XMLHttpRequest ;
6- const fs = createFs ( { databaseName : "guida-fs" } ) ;
75
8- const runGuida = function ( extraEnv , args ) {
6+ const runGuida = function ( config , args ) {
97 return new Promise ( ( resolve ) => {
108 let mVarsNextCounter = 0 ;
119 const mVars = { } ;
1210 const lockedFiles = { } ;
1311
14- const env = Object . assign ( {
15- GUIDA_HOME : "root/.guida" ,
16- } , extraEnv ) ;
17-
1812 const download = function ( method , url ) {
1913 const that = this ;
2014
@@ -85,16 +79,15 @@ const runGuida = function (extraEnv, args) {
8579 server . post ( "writeString" , async ( request ) => {
8680 const path = request . requestHeaders . getHeader ( "path" ) ;
8781
88- await fs . writeFile ( path , request . body ) ;
82+ await config . writeFile ( path , request . body ) ;
8983 request . respond ( 200 ) ;
9084 } ) ;
9185
9286 server . post ( "read" , async ( request ) => {
93- const content = await fs . readFile ( request . body ) ;
87+ const content = await config . readFile ( request . body ) ;
9488 request . respond ( 200 , null , content ) ;
9589 } ) ;
9690
97-
9891 server . post ( "getArchive" , ( request ) => {
9992 download . apply ( {
10093 send : ( { sha, archive } ) => {
@@ -105,8 +98,7 @@ const runGuida = function (extraEnv, args) {
10598
10699 server . post ( "dirDoesFileExist" , async ( request ) => {
107100 try {
108- const stats = await fs . details ( request . body ) ;
109- console . log ( "dirDoesFileExist" , request . body , stats ) ;
101+ const stats = await config . details ( request . body ) ;
110102 request . respond ( 200 , null , stats . type === "file" ) ;
111103 } catch ( _err ) {
112104 request . respond ( 200 , null , false ) ;
@@ -126,9 +118,9 @@ const runGuida = function (extraEnv, args) {
126118 await previousPromise ;
127119
128120 try {
129- await fs . details ( directory ) ;
121+ await config . details ( directory ) ;
130122 } catch ( _err ) {
131- await fs . createDirectory ( directory ) ;
123+ await config . createDirectory ( directory ) ;
132124 }
133125 } , Promise . resolve ( ) ) ;
134126
@@ -165,13 +157,13 @@ const runGuida = function (extraEnv, args) {
165157 } ) ;
166158
167159 server . post ( "dirGetModificationTime" , async ( request ) => {
168- const stats = await fs . details ( request . body ) ;
160+ const stats = await config . details ( request . body ) ;
169161 request . respond ( 200 , null , stats . createdAt ) ;
170162 } ) ;
171163
172164 server . post ( "dirDoesDirectoryExist" , async ( request ) => {
173165 try {
174- const stats = await fs . details ( request . body ) ;
166+ const stats = await config . details ( request . body ) ;
175167 request . respond ( 200 , null , stats . type === "directory" ) ;
176168 } catch ( _err ) {
177169 request . respond ( 200 , null , false ) ;
@@ -183,19 +175,19 @@ const runGuida = function (extraEnv, args) {
183175 } ) ;
184176
185177 server . post ( "dirListDirectory" , async ( request ) => {
186- const { files } = await fs . readDirectory ( request . body ) ;
178+ const { files } = await config . readDirectory ( request . body ) ;
187179 request . respond ( 200 , null , JSON . stringify ( files ) ) ;
188180 } ) ;
189181
190182 server . post ( "binaryDecodeFileOrFail" , async ( request ) => {
191- const data = await fs . readFile ( request . body ) ;
183+ const data = await config . readFile ( request . body ) ;
192184 request . respond ( 200 , null , data . buffer ) ;
193185 } ) ;
194186
195187 server . post ( "write" , async ( request ) => {
196188 const path = request . requestHeaders . getHeader ( "path" ) ;
197189
198- await fs . writeFile ( path , request . body ) ;
190+ await config . writeFile ( path , request . body ) ;
199191 request . respond ( 200 ) ;
200192 } ) ;
201193
@@ -204,7 +196,7 @@ const runGuida = function (extraEnv, args) {
204196 } ) ;
205197
206198 server . post ( "envLookupEnv" , ( request ) => {
207- const envVar = env [ request . body ] ?? null ;
199+ const envVar = config . env [ request . body ] ?? null ;
208200 request . respond ( 200 , null , JSON . stringify ( envVar ) ) ;
209201 } ) ;
210202
@@ -297,8 +289,6 @@ const runGuida = function (extraEnv, args) {
297289 } ) ;
298290
299291 server . setDefaultHandler ( ( request ) => {
300- console . log ( "defaultHandler" , request . url ) ;
301-
302292 const headers = request . requestHeaders . getHash ( ) ;
303293
304294 var xhr = new savedXMLHttpRequest ( ) ;
@@ -317,63 +307,29 @@ const runGuida = function (extraEnv, args) {
317307
318308 server . install ( ) ;
319309
320- const { Elm } = require ( "./guida.browser. min.js" ) ;
310+ const { Elm } = require ( "./guida.min.js" ) ;
321311
322- Elm . Browser . Main . init ( ) ;
312+ Elm . API . Main . init ( ) ;
323313 } ) ;
324- }
325-
326- const elmJsonContent = `{
327- "type": "application",
328- "source-directories": [
329- "src"
330- ],
331- "elm-version": "0.19.1",
332- "dependencies": {
333- "direct": {
334- "elm/browser": "1.0.2",
335- "elm/core": "1.0.5",
336- "elm/html": "1.0.0"
337- },
338- "indirect": {
339- "elm/json": "1.1.3",
340- "elm/time": "1.0.0",
341- "elm/url": "1.0.0",
342- "elm/virtual-dom": "1.0.3"
343- }
344- },
345- "test-dependencies": {
346- "direct": {},
347- "indirect": {}
348- }
349- }` ;
314+ } ;
350315
351316module . exports = {
352- init : async ( extraEnv ) => {
353- await fs . writeFile ( "root/elm.json" , elmJsonContent ) ;
354- await fs . createDirectory ( "root/src" ) ;
355-
356- return {
357- make : async ( content , options ) => {
358- await fs . writeFile ( "root/src/Main.guida" , content ) ;
359-
360- return await runGuida ( extraEnv , {
361- command : "make" ,
362- path : "src/Main.guida" ,
363- debug : ! ! options . debug ,
364- optimize : ! ! options . optimize ,
365- sourcemaps : ! ! options . sourcemaps
366- } ) ;
367- } ,
368- format : async ( content ) => {
369- return await runGuida ( extraEnv , { command : "format" , content } ) ;
370- } ,
371- install : async ( pkg ) => {
372- return await runGuida ( extraEnv , { command : "install" , pkg } ) ;
373- } ,
374- uninstall : async ( pkg ) => {
375- return await runGuida ( extraEnv , { command : "uninstall" , pkg } ) ;
376- }
377- } ;
317+ make : async ( config , path , options ) => {
318+ return await runGuida ( config , {
319+ command : "make" ,
320+ path,
321+ debug : ! ! options . debug ,
322+ optimize : ! ! options . optimize ,
323+ sourcemaps : ! ! options . sourcemaps
324+ } ) ;
325+ } ,
326+ format : async ( config , content ) => {
327+ return await runGuida ( config , { command : "format" , content } ) ;
328+ } ,
329+ install : async ( config , pkg ) => {
330+ return await runGuida ( config , { command : "install" , pkg } ) ;
331+ } ,
332+ uninstall : async ( config , pkg ) => {
333+ return await runGuida ( config , { command : "uninstall" , pkg } ) ;
378334 }
379335} ;
0 commit comments