@lionad/port-key 0.1.4 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,115 @@
1
+ # PortKey
2
+
3
+ <p align="center">
4
+ <img width="200" src="/public/logo.png" />
5
+ </p>
6
+
7
+ <p align="center">
8
+ <strong>PortKey:A Simple, Practical Port Naming Strategy</strong>
9
+ </p>
10
+
11
+ ## Brief
12
+
13
+ Generate ports with a letter-to-number keyboard mapping
14
+
15
+ When you’re running a bunch of projects locally, picking port numbers becomes annoying.
16
+
17
+ - Over the last couple of years, there have been *so many* new projects. To really try them out, you often need to boot them locally—and then ports start colliding.
18
+ - If you want to keep browser tabs (or bookmarks) stable, a project’s port shouldn’t keep changing.
19
+
20
+ For example, I have more than ten Nuxt apps on my machine. If they all default to `3000`, that’s obviously not going to work. So I came up with a simple, consistent port naming rule to “assign” ports per project.
21
+
22
+ [Source Blog Post](https://lionad.art/articles/simple-naming-method)
23
+
24
+ ### Core idea
25
+
26
+ Instead of picking random numbers, map the **project name to numbers based on the keyboard**, so the port is *readable* and *memorable*.
27
+
28
+ As long as the result is within the valid port range (**0–65535**) and doesn’t hit reserved/system ports, you can just use it.
29
+
30
+ More specifically: using a standard QWERTY keyboard, map each letter to a single digit based on its **row/column position**.
31
+
32
+ Example:
33
+
34
+ `"cfetch"` → `c(3) f(4) e(3) t(5) c(3) h(6)` → `34353`(port number)
35
+
36
+ Then you can take the first 4 digits (e.g. `3453`), or keep more digits (e.g. `34353`). Either is fine.
37
+
38
+ If a project needs multiple ports (frontend, backend, database, etc.), pick **one** of these two approaches:
39
+
40
+ 1. Use the project prefix, then append a “role suffix”
41
+ - For `"cfetch"`, take `3435` as the base
42
+ - Frontend (`fe`, i.e. `43`) → `34354`
43
+ - Backend (`server`) → `34352`
44
+ - Database (`mongo`) → `34357`
45
+ - …and so on
46
+
47
+ 2. Use the project prefix, then assign sequential roles
48
+ - For `"cfetch"`, take `3435` as the base
49
+ - Web → `34351`
50
+ - Backend → `34352`
51
+ - Database → `34353`
52
+ - …and so on
53
+
54
+ ### Valid port range
55
+
56
+ - Ports must be within **0–65535**.
57
+ - For custom services, it’s usually best to use **1024–49151** (non-reserved) or **49152–65535** (private/dynamic).
58
+ - As long as the mapped number stays under the limit, it’s valid.
59
+
60
+ ---
61
+
62
+ ## How to use
63
+
64
+ ```
65
+ npx @lionad/port-key <your-project-name>
66
+ ```
67
+
68
+ ### CLI options
69
+
70
+ - `-m, --map <object>`: custom mapping (JSON or JS-like object literal)
71
+ - `--lang <code>`: output language (currently only `en` and `cn`, default: `cn`)
72
+ - `-d, --digits <count>`: preferred digit count for port (4 or 5, default: 4)
73
+ - `-h, --help`: show help
74
+
75
+ Examples:
76
+
77
+ ```bash
78
+ npx @lionad/port-key cfetch # -> 3435
79
+ npx @lionad/port-key cfetch --digits 4 # -> 3435 (4-digit port)
80
+ npx @lionad/port-key cfetch --digits 5 # -> 34353 (5-digit port)
81
+ ```
82
+
83
+ Notes:
84
+ - Default log language is `cn`. Use `--lang en` to show English messages.
85
+ - Use `-h` or `--help` to show help.
86
+
87
+ ### Config
88
+
89
+ PortKey reads optional user config from:
90
+
91
+ - `~/.port-key/config.json`
92
+
93
+ A full Example:
94
+
95
+ ```json
96
+ {
97
+ // Preferred digit count for port (4 or 5)
98
+ "preferDigitCount": 5,
99
+ // Custom letter-to-digit mapping
100
+ "blockedPorts": [3000, 3001, 3002, 6666],
101
+ // Port range limits (inclusive)
102
+ "minPort": 1024,
103
+ "maxPort": 49151
104
+ }
105
+ ```
106
+
107
+ ---
108
+
109
+ ## For Developer
110
+
111
+ ### Project Structure
112
+
113
+ - 本仓库采用 pnpm monorepo;核心包位于 `packages/core`。
114
+ - 安装:在根目录执行 `pnpm install`。
115
+ - 运行测试:`pnpm -C packages/core test` 或 `pnpm -C packages/core test:watch`。
package/bin/port-key.js CHANGED
File without changes
package/package.json CHANGED
@@ -1,10 +1,14 @@
1
1
  {
2
2
  "name": "@lionad/port-key",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "A simple, practical port naming strategy",
5
5
  "type": "module",
6
+ "types": "./src/port-key.d.ts",
6
7
  "exports": {
7
- ".": "./src/port-key.js"
8
+ ".": {
9
+ "types": "./src/port-key.d.ts",
10
+ "default": "./src/port-key.js"
11
+ }
8
12
  },
9
13
  "keywords": [
10
14
  "portkey",
@@ -0,0 +1,43 @@
1
+ export type DigitKey = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
2
+
3
+ export type DigitLetterMap = Record<DigitKey | `${DigitKey}`, string>;
4
+
5
+ export type RejectedCandidate = {
6
+ candidate: string;
7
+ reason: string;
8
+ };
9
+
10
+ export type PickPortOptions = {
11
+ preferDigitCount?: number;
12
+ minPort?: number;
13
+ maxPort?: number;
14
+ blockedPorts?: ReadonlySet<number> | number[];
15
+ };
16
+
17
+ export const DEFAULT_MAP: Readonly<DigitLetterMap>;
18
+ export const DEFAULT_BLOCKED_PORTS: ReadonlySet<number>;
19
+
20
+ export function buildReverseMap(map: DigitLetterMap): Map<string, string>;
21
+ export function normalizeInput(text: unknown): string;
22
+ export function mapToDigits(text: unknown, map?: DigitLetterMap): string;
23
+ export function isValidPort(port: unknown): port is number;
24
+
25
+ export function pickPortFromDigits(
26
+ digits: unknown,
27
+ options?: PickPortOptions
28
+ ):
29
+ | { port: number; rejectedCandidates?: RejectedCandidate[] }
30
+ | { port: null; reason: string; rejectedCandidates?: RejectedCandidate[] };
31
+
32
+ export function mapToPort(
33
+ text: unknown,
34
+ map?: DigitLetterMap,
35
+ options?: PickPortOptions
36
+ ): {
37
+ digits: string;
38
+ port: number | null;
39
+ rejectedCandidates?: RejectedCandidate[];
40
+ reason?: string;
41
+ };
42
+
43
+ export function parseUserMap(mapString: unknown): DigitLetterMap;