Skip to content

Commit

Permalink
open_package_repository 추가, node_modules 폴더에서 repo를 여는 역할
Browse files Browse the repository at this point in the history
원래는 blob 을 통해서 코드위치를 열려고했는데 .git 정보가 로컬에 없어서 할수 없었음
별 유용하지는 않지만 개선 여지가 있을 수 있어서 구현한 것 커밋
  • Loading branch information
deptno committed Nov 1, 2023
1 parent ab01836 commit b539549
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 16 deletions.
50 changes: 50 additions & 0 deletions lua/lab/gx/handlers_v/open_package_repository.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
local get_git_root = require('lab/gx/lib/get_git_root')
local get_directory_of_current_file = require("lab/gx/lib/get_directory_of_current_file")
local parse_repository = require("lab.gx.lib.parse_repository")
local get_package_repository = require("lab.gx.lib.get_package_repository")
local M = {}

M.handler = function(args)
local remote_info = M.match(args.lines)
if not remote_info then
return vim.notify(string.format('remote_info is nil'), vim.log.levels.ERROR)
end

local domain = remote_info.domain
local username = remote_info.username
local repository = remote_info.repository

local Job = require("plenary.job")
local command = "open"
local url = string.format("https://%s/%s/%s", domain, username, repository)

Job:new({
command,
args = {
url,
},
}):sync()

vim.notify(string.format('Open url: %s', url), vim.log.levels.INFO)
end
M.match = function(lines)
local cwd = get_directory_of_current_file()
local cwd_expanded = vim.fn.expand(cwd)
local git_root = get_git_root(cwd)
local pattern = vim.fn.expand(git_root):gsub('-', '%%-') .. '/node_modules/[%w%-_]+'
local package_in_node_modules = cwd_expanded:match(pattern)

if not package_in_node_modules then
return
end

local package_repo = get_package_repository(package_in_node_modules)
if not package_repo then
return
end

return parse_repository(package_repo)
end
M.name = 'open package repository'

return M
11 changes: 11 additions & 0 deletions lua/lab/gx/lib/file_exists.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
local file_exists = function (file)
local f = io.open(file, "rb")

if f then
f:close()
end

return f ~= nil
end

return file_exists
18 changes: 2 additions & 16 deletions lua/lab/gx/lib/get_git_remote_info.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local remove_suffix = require("lab/gx/lib/remove_suffix")
local parse_repository = require("lab.gx.lib.parse_repository")

local get_git_remote_info = function (cwd)
local command = type(cwd) == 'string'
Expand All @@ -11,21 +11,7 @@ local get_git_remote_info = function (cwd)
return nil, origin
end

local parts = {}

for part in origin:gmatch("[^@:/]+") do
table.insert(parts, part)
end

local domain = parts[2]
local username = parts[3]
local repository = remove_suffix('.git', parts[4])

return {
domain = domain,
username = username,
repository = repository,
}
return parse_repository(origin)
end

return get_git_remote_info
16 changes: 16 additions & 0 deletions lua/lab/gx/lib/get_package_repository.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
local try_read_file = require("lab/gx/lib/try_read_file")

local get_package_repository = function (cwd)
local f = vim.fn.expand(cwd) .. '/package.json'
local json = try_read_file(f)

if json then
local encoded = vim.fn.json_decode(json)

if encoded then
return encoded.repository
end
end
end

return get_package_repository
21 changes: 21 additions & 0 deletions lua/lab/gx/lib/parse_repository.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
local remove_suffix = require("lab/gx/lib/remove_suffix")

local parse_repository = function (origin)
local parts = {}

for part in origin:gmatch("[^@:/]+") do
table.insert(parts, part)
end

local domain = parts[2]
local username = parts[3]
local repository = remove_suffix('.git', parts[4])

return {
domain = domain,
username = username,
repository = repository,
}
end

return parse_repository
16 changes: 16 additions & 0 deletions lua/lab/gx/lib/try_read_file.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
local file_exists = require("lab/gx/lib/file_exists")

local function try_read_file(file)
if not file_exists(file) then
return nil
end

local lines = {}
for line in io.lines(file) do
lines[#lines + 1] = line
end

return table.concat(lines)
end

return try_read_file

0 comments on commit b539549

Please sign in to comment.