Skip to content

Commit 232b1fe

Browse files
committed
feat: store used ports
1 parent 76d28ea commit 232b1fe

File tree

3 files changed

+100
-10
lines changed

3 files changed

+100
-10
lines changed

src/core.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { Selection } from './types'
44
const app = Application.currentApplication()
55
app.includeStandardAdditions = true
66

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

1010
export const getenv = (name: string) => {
@@ -32,13 +32,17 @@ export const parsePort = (port: string): Selection => {
3232
},
3333
mods: {
3434
cmd: {
35-
subtitle: `http://127.0.0.1:${port}`,
35+
subtitle: `open http://127.0.0.1:${port}`,
3636
arg: `127.0.0.1:${port}`,
3737
},
3838
ctrl: {
39-
subtitle: `http://${getIntranetIP()}:${port}`,
39+
subtitle: `open http://${getIntranetIP()}:${port}`,
4040
arg: `${getIntranetIP()}:${port}`,
4141
},
42+
alt: {
43+
subtitle: `remove ${port}`,
44+
arg: port,
45+
},
4246
},
4347
}
4448
}
@@ -57,6 +61,11 @@ export const isFileExist = (path: string) => {
5761
return fileExists === 'true'
5862
}
5963

64+
export const getCachePortsFilePath = () => {
65+
const alfredWorkflowDataPath = getenv('alfred_workflow_data')
66+
return `${alfredWorkflowDataPath}/${cacheFileName}`
67+
}
68+
6069
export const getContentFromFile = (path: string) => {
6170
if (!isFileExist(path))
6271
return ''
@@ -65,6 +74,7 @@ export const getContentFromFile = (path: string) => {
6574
}
6675

6776
export const getCachePorts = () => {
77+
const cachePortFilePath = getCachePortsFilePath()
6878
const portsStr = getContentFromFile(cachePortFilePath)
6979
return portsStr.split(',')
7080
}

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { cachePortFilePath, getContentFromFile, getenv, innerDefaultPort, matchPort, parsePortsStr, portsStr2Arr } from './core'
1+
import { getCachePortsFilePath, getContentFromFile, getenv, innerDefaultPort, matchPort, parsePortsStr, portsStr2Arr } from './core'
22
import type { Selection } from './types'
33

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

77
const envPortsStr = getenv('myPorts') as string
8+
const cachePortFilePath = getCachePortsFilePath()
89
const cachePortsStr = getContentFromFile(cachePortFilePath)
910
const allPortsStr = `${currPort},${envPortsStr},${cachePortsStr},${innerDefaultPort}`
1011
const allPorts = portsStr2Arr(allPortsStr)

src/info.plist

Lines changed: 85 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@
5353
<key>vitoclose</key>
5454
<false/>
5555
</dict>
56+
<dict>
57+
<key>destinationuid</key>
58+
<string>ECACC8FF-09ED-4D32-8C85-947E65E0ACCD</string>
59+
<key>modifiers</key>
60+
<integer>524288</integer>
61+
<key>modifiersubtext</key>
62+
<string></string>
63+
<key>vitoclose</key>
64+
<false/>
65+
</dict>
5666
</array>
5767
<key>3CAD799C-0423-4214-B0BC-2BFE6B9B7E98</key>
5868
<array>
@@ -124,25 +134,35 @@
124134
<key>escaping</key>
125135
<integer>102</integer>
126136
<key>script</key>
127-
<string>FILE="ports"
137+
<string>ALFRED_WORKFLOW_DATA="${alfred_workflow_data}"
138+
FILE="${ALFRED_WORKFLOW_DATA}/ports"
128139
INPUT="${currPort}" # take the input from command line argument
129140
DEAFAULT_VALUE="${myPorts}"
130141
142+
if [ ! -d "${ALFRED_WORKFLOW_DATA}" ]; then
143+
mkdir -p "${ALFRED_WORKFLOW_DATA}"
144+
fi
131145
132146
# If the file does not exist, create it with initial value
133147
if [ ! -f "$FILE" ]; then
134-
echo "$DEAFAULT_VALUE" &gt; $FILE
148+
_DEAFAULT_VALUE=$(echo "$DEAFAULT_VALUE" | tr -dc '0-9,')
149+
echo "$_DEAFAULT_VALUE" &gt; "$FILE"
135150
fi
136151
152+
# If the file does not exist, create it with initial value
153+
# if [ ! -f "$FILE" ]; then
154+
# echo "$DEAFAULT_VALUE" &gt; "$FILE"
155+
# fi
156+
137157
if [ -z "$INPUT" ]; then
138158
echo "Input is empty, no action performed."
139159
exit 0
140160
fi
141161
142162
# Check if the input already exists in the file
143-
if ! grep -q $INPUT $FILE; then
163+
if ! grep -q "$INPUT" "$FILE"; then
144164
# If not, append the input to the file
145-
echo -n ",$INPUT" &gt;&gt; $FILE
165+
echo -n ",$INPUT" &gt;&gt; "$FILE"
146166
fi</string>
147167
<key>scriptargtype</key>
148168
<integer>1</integer>
@@ -325,6 +345,54 @@ fi</string>
325345
<key>version</key>
326346
<integer>1</integer>
327347
</dict>
348+
<dict>
349+
<key>config</key>
350+
<dict>
351+
<key>concurrently</key>
352+
<false/>
353+
<key>escaping</key>
354+
<integer>102</integer>
355+
<key>script</key>
356+
<string>ALFRED_WORKFLOW_DATA="${alfred_workflow_data}"
357+
FILE="${ALFRED_WORKFLOW_DATA}/ports"
358+
INPUT=$1
359+
360+
if [ ! -d "${ALFRED_WORKFLOW_DATA}" ]; then
361+
echo "no this workflow folder"
362+
exit 0
363+
fi
364+
365+
# If the file does not exist, create it with initial value
366+
if [ ! -f "$FILE" ]; then
367+
echo "no user ports data file"
368+
exit 0
369+
fi
370+
371+
if [ -z "$INPUT" ]; then
372+
echo "Input is empty, no action performed."
373+
exit 0
374+
fi
375+
376+
377+
# remove the port
378+
sed -i '' "s/[[:&lt;:]]$INPUT[[:&gt;:]]//g" "$FILE"
379+
380+
# remove consecutive or leading/trailing commas.
381+
sed -i '' -e $'s/,,*/,/g;s/^,//;s/,$//' "$FILE"</string>
382+
<key>scriptargtype</key>
383+
<integer>1</integer>
384+
<key>scriptfile</key>
385+
<string></string>
386+
<key>type</key>
387+
<integer>0</integer>
388+
</dict>
389+
<key>type</key>
390+
<string>alfred.workflow.action.script</string>
391+
<key>uid</key>
392+
<string>ECACC8FF-09ED-4D32-8C85-947E65E0ACCD</string>
393+
<key>version</key>
394+
<integer>2</integer>
395+
</dict>
328396
</array>
329397
<key>readme</key>
330398
<string>
@@ -355,6 +423,8 @@ fi</string>
355423
</dict>
356424
<key>205BBC80-E258-474A-AA68-9DDC4B2C8EA8</key>
357425
<dict>
426+
<key>note</key>
427+
<string>store port</string>
358428
<key>xpos</key>
359429
<real>695</real>
360430
<key>ypos</key>
@@ -370,9 +440,9 @@ fi</string>
370440
<key>4D59CF3D-7FD5-417D-B25C-C5FEC1306A00</key>
371441
<dict>
372442
<key>xpos</key>
373-
<real>700</real>
443+
<real>695</real>
374444
<key>ypos</key>
375-
<real>410</real>
445+
<real>390</real>
376446
</dict>
377447
<key>7B64AF79-0000-4978-B325-3163AE16459A</key>
378448
<dict>
@@ -381,6 +451,15 @@ fi</string>
381451
<key>ypos</key>
382452
<real>505</real>
383453
</dict>
454+
<key>ECACC8FF-09ED-4D32-8C85-947E65E0ACCD</key>
455+
<dict>
456+
<key>note</key>
457+
<string>remove port from cache</string>
458+
<key>xpos</key>
459+
<real>695</real>
460+
<key>ypos</key>
461+
<real>540</real>
462+
</dict>
384463
</dict>
385464
<key>userconfigurationconfig</key>
386465
<array/>

0 commit comments

Comments
 (0)