Skip to content

Commit

Permalink
feat: store / search the prev port
Browse files Browse the repository at this point in the history
  • Loading branch information
Nauxscript committed Sep 19, 2023
1 parent 5d3d940 commit d67f072
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 23 deletions.
42 changes: 30 additions & 12 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import type { Selection } from './types'
const app = Application.currentApplication()
app.includeStandardAdditions = true

export const cachePortFilePath = `${app.doShellScript('pwd')}/ports`
export const innerDefaultPort = '8080'

export const getenv = (name: string) => {
if (typeof $ === 'undefined')
return process.env[name]
Expand All @@ -24,6 +27,9 @@ export const parsePort = (port: string): Selection => {
title: `Port: ${port}`,
subtitle: `http://localhost:${port}`,
arg: `localhost:${port}`,
variables: {
currPort: port,
},
mods: {
cmd: {
subtitle: `http://127.0.0.1:${port}`,
Expand All @@ -37,18 +43,30 @@ export const parsePort = (port: string): Selection => {
}
}

export const parseEnvPort = (ports: string) => {
const portArr = ports.split(',')
return portArr.filter(item => item).map(item => parsePort(item))
export const portsStr2Arr = (portsStr: string) => portsStr.split(',')

export const parsePortsStr = (ports: string[]) => {
const uniquePorts = [...new Set(ports.filter(item => item.replace(/\D/g, '')))]
return uniquePorts.map(item => parsePort(item))
}

export const deduplicate = (target: Selection[]) => {
const uniqueKeys = {} as Record<string, boolean>
target.reduce((curr, next) => {
if (uniqueKeys[next.title]) {
uniqueKeys[next.title] = true
curr.push(next)
}
return curr
}, [] as Selection[])
export const isFileExist = (path: string) => {
// Check if the file exists
const fileExists = app.doShellScript(`test -e "${path}" && echo "true" || echo "false"`)

return fileExists === 'true'
}

export const getContentFromFile = (path: string) => {
if (!isFileExist(path))
return ''
const fileContent = app.doShellScript(`cat "${path}"`) as string
return fileContent
}

export const getCachePorts = () => {
const portsStr = getContentFromFile(cachePortFilePath)
return portsStr.split(',')
}

export const matchPort = (ports: string[], query: string) => ports.filter(port => port.includes(query))
21 changes: 11 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { deduplicate, getenv, parseEnvPort, parsePort } from './core'
import { cachePortFilePath, getContentFromFile, getenv, innerDefaultPort, matchPort, parsePortsStr, portsStr2Arr } from './core'
import type { Selection } from './types'

export default function run(argv: [string, boolean]) {
const query = argv[0]
const items = []
const currPort = parsePort(query)
query && items.push(currPort)
const envPortsStr = getenv('myPort') as string
envPortsStr && items.push(...parseEnvPort(envPortsStr))
items.push(parsePort('8080'))
deduplicate(items)
const currPort = argv[0]

const envPortsStr = getenv('myPorts') as string
const cachePortsStr = getContentFromFile(cachePortFilePath)
const allPortsStr = `${currPort},${envPortsStr},${cachePortsStr},${innerDefaultPort}`
const allPorts = portsStr2Arr(allPortsStr)
const matchPorts = matchPort(allPorts, currPort)
const ports = parsePortsStr(matchPorts)

const result: {
items: Selection[]
} = {
items,
items: ports,
}

// for testing
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface Selection {
}
match?: string
quicklookurl?: string

variables: Record<string, string>
}

export type Mod = Partial<Record<ModComb, ModItem>>
Expand Down

0 comments on commit d67f072

Please sign in to comment.