Skip to content

Commit

Permalink
feat: store used ports
Browse files Browse the repository at this point in the history
  • Loading branch information
Nauxscript committed Sep 21, 2023
1 parent 76d28ea commit 232b1fe
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 10 deletions.
16 changes: 13 additions & 3 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Selection } from './types'
const app = Application.currentApplication()
app.includeStandardAdditions = true

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

export const getenv = (name: string) => {
Expand Down Expand Up @@ -32,13 +32,17 @@ export const parsePort = (port: string): Selection => {
},
mods: {
cmd: {
subtitle: `http://127.0.0.1:${port}`,
subtitle: `open http://127.0.0.1:${port}`,
arg: `127.0.0.1:${port}`,
},
ctrl: {
subtitle: `http://${getIntranetIP()}:${port}`,
subtitle: `open http://${getIntranetIP()}:${port}`,
arg: `${getIntranetIP()}:${port}`,
},
alt: {
subtitle: `remove ${port}`,
arg: port,
},
},
}
}
Expand All @@ -57,6 +61,11 @@ export const isFileExist = (path: string) => {
return fileExists === 'true'
}

export const getCachePortsFilePath = () => {
const alfredWorkflowDataPath = getenv('alfred_workflow_data')
return `${alfredWorkflowDataPath}/${cacheFileName}`
}

export const getContentFromFile = (path: string) => {
if (!isFileExist(path))
return ''
Expand All @@ -65,6 +74,7 @@ export const getContentFromFile = (path: string) => {
}

export const getCachePorts = () => {
const cachePortFilePath = getCachePortsFilePath()
const portsStr = getContentFromFile(cachePortFilePath)
return portsStr.split(',')
}
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { cachePortFilePath, getContentFromFile, getenv, innerDefaultPort, matchPort, parsePortsStr, portsStr2Arr } from './core'
import { getCachePortsFilePath, getContentFromFile, getenv, innerDefaultPort, matchPort, parsePortsStr, portsStr2Arr } from './core'
import type { Selection } from './types'

export default function run(argv: [string, boolean]) {
const currPort = argv[0]

const envPortsStr = getenv('myPorts') as string
const cachePortFilePath = getCachePortsFilePath()
const cachePortsStr = getContentFromFile(cachePortFilePath)
const allPortsStr = `${currPort},${envPortsStr},${cachePortsStr},${innerDefaultPort}`
const allPorts = portsStr2Arr(allPortsStr)
Expand Down
91 changes: 85 additions & 6 deletions src/info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@
<key>vitoclose</key>
<false/>
</dict>
<dict>
<key>destinationuid</key>
<string>ECACC8FF-09ED-4D32-8C85-947E65E0ACCD</string>
<key>modifiers</key>
<integer>524288</integer>
<key>modifiersubtext</key>
<string></string>
<key>vitoclose</key>
<false/>
</dict>
</array>
<key>3CAD799C-0423-4214-B0BC-2BFE6B9B7E98</key>
<array>
Expand Down Expand Up @@ -124,25 +134,35 @@
<key>escaping</key>
<integer>102</integer>
<key>script</key>
<string>FILE="ports"
<string>ALFRED_WORKFLOW_DATA="${alfred_workflow_data}"
FILE="${ALFRED_WORKFLOW_DATA}/ports"
INPUT="${currPort}" # take the input from command line argument
DEAFAULT_VALUE="${myPorts}"
if [ ! -d "${ALFRED_WORKFLOW_DATA}" ]; then
mkdir -p "${ALFRED_WORKFLOW_DATA}"
fi
# If the file does not exist, create it with initial value
if [ ! -f "$FILE" ]; then
echo "$DEAFAULT_VALUE" &gt; $FILE
_DEAFAULT_VALUE=$(echo "$DEAFAULT_VALUE" | tr -dc '0-9,')
echo "$_DEAFAULT_VALUE" &gt; "$FILE"
fi
# If the file does not exist, create it with initial value
# if [ ! -f "$FILE" ]; then
# echo "$DEAFAULT_VALUE" &gt; "$FILE"
# fi
if [ -z "$INPUT" ]; then
echo "Input is empty, no action performed."
exit 0
fi
# Check if the input already exists in the file
if ! grep -q $INPUT $FILE; then
if ! grep -q "$INPUT" "$FILE"; then
# If not, append the input to the file
echo -n ",$INPUT" &gt;&gt; $FILE
echo -n ",$INPUT" &gt;&gt; "$FILE"
fi</string>
<key>scriptargtype</key>
<integer>1</integer>
Expand Down Expand Up @@ -325,6 +345,54 @@ fi</string>
<key>version</key>
<integer>1</integer>
</dict>
<dict>
<key>config</key>
<dict>
<key>concurrently</key>
<false/>
<key>escaping</key>
<integer>102</integer>
<key>script</key>
<string>ALFRED_WORKFLOW_DATA="${alfred_workflow_data}"
FILE="${ALFRED_WORKFLOW_DATA}/ports"
INPUT=$1
if [ ! -d "${ALFRED_WORKFLOW_DATA}" ]; then
echo "no this workflow folder"
exit 0
fi
# If the file does not exist, create it with initial value
if [ ! -f "$FILE" ]; then
echo "no user ports data file"
exit 0
fi
if [ -z "$INPUT" ]; then
echo "Input is empty, no action performed."
exit 0
fi
# remove the port
sed -i '' "s/[[:&lt;:]]$INPUT[[:&gt;:]]//g" "$FILE"
# remove consecutive or leading/trailing commas.
sed -i '' -e $'s/,,*/,/g;s/^,//;s/,$//' "$FILE"</string>
<key>scriptargtype</key>
<integer>1</integer>
<key>scriptfile</key>
<string></string>
<key>type</key>
<integer>0</integer>
</dict>
<key>type</key>
<string>alfred.workflow.action.script</string>
<key>uid</key>
<string>ECACC8FF-09ED-4D32-8C85-947E65E0ACCD</string>
<key>version</key>
<integer>2</integer>
</dict>
</array>
<key>readme</key>
<string>
Expand Down Expand Up @@ -355,6 +423,8 @@ fi</string>
</dict>
<key>205BBC80-E258-474A-AA68-9DDC4B2C8EA8</key>
<dict>
<key>note</key>
<string>store port</string>
<key>xpos</key>
<real>695</real>
<key>ypos</key>
Expand All @@ -370,9 +440,9 @@ fi</string>
<key>4D59CF3D-7FD5-417D-B25C-C5FEC1306A00</key>
<dict>
<key>xpos</key>
<real>700</real>
<real>695</real>
<key>ypos</key>
<real>410</real>
<real>390</real>
</dict>
<key>7B64AF79-0000-4978-B325-3163AE16459A</key>
<dict>
Expand All @@ -381,6 +451,15 @@ fi</string>
<key>ypos</key>
<real>505</real>
</dict>
<key>ECACC8FF-09ED-4D32-8C85-947E65E0ACCD</key>
<dict>
<key>note</key>
<string>remove port from cache</string>
<key>xpos</key>
<real>695</real>
<key>ypos</key>
<real>540</real>
</dict>
</dict>
<key>userconfigurationconfig</key>
<array/>
Expand Down

0 comments on commit 232b1fe

Please sign in to comment.