@@ -15,6 +15,7 @@ local ws = require 'workspace.workspace'
1515local scope = require ' workspace.scope'
1616local inspect = require ' inspect'
1717local jsonc = require ' jsonc'
18+ local json = require ' json'
1819
1920local m = {}
2021
@@ -275,7 +276,8 @@ local function initBuiltIn(uri)
275276end
276277
277278--- @param libraryDir fs.path
278- local function loadSingle3rdConfig (libraryDir )
279+ --- @return table ?
280+ local function loadSingle3rdConfigFromJson (libraryDir )
279281 local path = libraryDir / ' config.json'
280282 local configText = fsu .loadFile (path )
281283 if not configText then
@@ -289,6 +291,65 @@ local function loadSingle3rdConfig(libraryDir)
289291 return nil
290292 end
291293
294+ if type (cfg ) ~= ' table' then
295+ log .error (' config.json must be an object:' , libraryDir :string ())
296+ return nil
297+ end
298+
299+ return cfg
300+ end
301+
302+ --- @param libraryDir fs.path
303+ --- @return table ?
304+ local function loadSingle3rdConfigFromLua (libraryDir )
305+ local path = libraryDir / ' config.lua'
306+ local configText = fsu .loadFile (path )
307+ if not configText then
308+ return nil
309+ end
310+
311+ local env = setmetatable ({}, { __index = _G })
312+ local f , err = load (configText , ' @' .. libraryDir :string (), ' t' , env )
313+ if not f then
314+ log .error (' Load config.lua failed at:' , libraryDir :string (), err )
315+ return nil
316+ end
317+
318+ local suc = xpcall (f , function (err )
319+ log .error (' Load config.lua failed at:' , libraryDir :string (), err )
320+ end )
321+
322+ if not suc then
323+ return nil
324+ end
325+
326+ local cfg = {}
327+ for k , v in pairs (env ) do
328+ cfg [k ] = v
329+ end
330+
331+ return cfg
332+ end
333+
334+ --- @param libraryDir fs.path
335+ local function loadSingle3rdConfig (libraryDir )
336+ local cfg = loadSingle3rdConfigFromJson (libraryDir )
337+ if not cfg then
338+ cfg = loadSingle3rdConfigFromLua (libraryDir )
339+ if not cfg then
340+ return
341+ end
342+ local jsonbuf = json .beautify (cfg )
343+ client .requestMessage (' Info' , lang .script .WINDOW_CONFIG_LUA_DEPRECATED , {
344+ lang .script .WINDOW_CONVERT_CONFIG_LUA ,
345+ }, function (action , index )
346+ if index == 1 and jsonbuf then
347+ fsu .saveFile (libraryDir / ' config.json' , jsonbuf )
348+ fsu .fileRemove (libraryDir / ' config.lua' )
349+ end
350+ end )
351+ end
352+
292353 cfg .path = libraryDir :filename ():string ()
293354 cfg .name = cfg .name or cfg .path
294355
0 commit comments