-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: separate process spawning logic
- Loading branch information
Showing
2 changed files
with
70 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
local uv = vim.loop | ||
local logger = require 'cord.util.logger' | ||
local utils = require 'cord.util' | ||
|
||
local M = {} | ||
|
||
function M.spawn_server(config, path, callback) | ||
local executable | ||
if config.advanced.server.executable_path then | ||
executable = config.advanced.server.executable_path | ||
else | ||
executable = utils.os_name == 'Windows' and 'target/release/cord.exe' | ||
or 'target/release/cord' | ||
end | ||
|
||
if not utils.file_exists(executable) then | ||
logger.error('Server executable not found at \'' .. executable .. '\'') | ||
return | ||
end | ||
|
||
local stdout = uv.new_pipe() | ||
local stderr = uv.new_pipe() | ||
uv.spawn(executable, { | ||
args = { | ||
'-p', | ||
path, | ||
'-c', | ||
config.editor.client, | ||
'-t', | ||
tostring(config.advanced.server.timeout), | ||
}, | ||
stdio = { nil, stdout, stderr }, | ||
detached = true, | ||
hide = true, | ||
}) | ||
|
||
stderr:read_start(vim.schedule_wrap(function(err, chunk) | ||
if err then | ||
logger.error('Failed to read stderr: ' .. err) | ||
return | ||
end | ||
if chunk then | ||
if chunk:match 'kind: AlreadyExists' then | ||
callback() | ||
stderr:close() | ||
stdout:close() | ||
return | ||
end | ||
logger.error('Server error: ' .. chunk) | ||
end | ||
end)) | ||
|
||
stdout:read_start(vim.schedule_wrap(function(err, chunk) | ||
if err then | ||
logger.error('Failed to read pipe: ' .. err) | ||
return | ||
end | ||
|
||
if chunk and chunk:match 'Ready' then | ||
callback() | ||
return | ||
end | ||
end)) | ||
end | ||
|
||
return M |