@b12k/porto 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +92 -2
- package/dist/busy-ports-error.d.ts +11 -0
- package/dist/busy-ports-error.js +10 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +116 -0
- package/dist/config.d.ts +72 -0
- package/dist/config.js +101 -0
- package/dist/error.d.ts +5 -0
- package/dist/error.js +13 -0
- package/dist/errors.d.ts +46 -0
- package/dist/errors.js +47 -0
- package/dist/files.d.ts +4 -0
- package/dist/files.js +43 -0
- package/dist/git.d.ts +7 -0
- package/dist/git.js +64 -0
- package/dist/index.d.ts +49 -0
- package/dist/index.js +57 -0
- package/dist/load-config.d.ts +1 -0
- package/dist/load-config.js +17 -0
- package/dist/log.d.ts +4 -0
- package/dist/log.js +19 -0
- package/dist/output.d.ts +18 -0
- package/dist/output.js +159 -0
- package/dist/ports.d.ts +16 -0
- package/dist/ports.js +70 -0
- package/dist/registry.d.ts +14 -0
- package/dist/registry.js +45 -0
- package/dist/reserve.d.ts +36 -0
- package/dist/reserve.js +84 -0
- package/dist/schemas.d.ts +33 -0
- package/dist/schemas.js +72 -0
- package/dist/vars.d.ts +14 -0
- package/dist/vars.js +72 -0
- package/package.json +94 -4
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import type { PortBlock } from './index.js';
|
|
3
|
+
declare const HOOKS_SCHEMA: z.ZodObject<{
|
|
4
|
+
onReleased: z.ZodCustom<(block: PortBlock, ports: Array<number>) => Promise<void> | void, (block: PortBlock, ports: Array<number>) => Promise<void> | void>;
|
|
5
|
+
onReserved: z.ZodCustom<(block: PortBlock, ports: Array<number>) => Promise<void> | void, (block: PortBlock, ports: Array<number>) => Promise<void> | void>;
|
|
6
|
+
}, z.core.$strip>;
|
|
7
|
+
declare const OPTIONS_SCHEMA: z.ZodObject<{
|
|
8
|
+
host: z.ZodOptional<z.ZodString>;
|
|
9
|
+
idleTtlMinutes: z.ZodNumber;
|
|
10
|
+
keep: z.ZodOptional<z.ZodBoolean>;
|
|
11
|
+
size: z.ZodInt;
|
|
12
|
+
start: z.ZodInt;
|
|
13
|
+
}, z.core.$strip>;
|
|
14
|
+
declare const REGISTRY_SCHEMA: z.ZodArray<z.ZodObject<{
|
|
15
|
+
host: z.ZodOptional<z.ZodString>;
|
|
16
|
+
idleTtlMinutes: z.ZodNumber;
|
|
17
|
+
size: z.ZodInt;
|
|
18
|
+
start: z.ZodInt;
|
|
19
|
+
index: z.ZodInt;
|
|
20
|
+
keep: z.ZodBoolean;
|
|
21
|
+
lastUsedAt: z.ZodISODateTime;
|
|
22
|
+
ports: z.ZodArray<z.ZodInt>;
|
|
23
|
+
worktree: z.ZodString;
|
|
24
|
+
worktreeId: z.ZodString;
|
|
25
|
+
}, z.core.$strip>>;
|
|
26
|
+
declare function parseOptions(options: unknown): {
|
|
27
|
+
host?: string | undefined;
|
|
28
|
+
idleTtlMinutes: number;
|
|
29
|
+
keep?: boolean | undefined;
|
|
30
|
+
size: number;
|
|
31
|
+
start: number;
|
|
32
|
+
};
|
|
33
|
+
export { HOOKS_SCHEMA, OPTIONS_SCHEMA, parseOptions, REGISTRY_SCHEMA };
|
package/dist/schemas.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import node_path from "node:path";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { PortoError } from "./error.js";
|
|
4
|
+
const BLOCK_HOOK_SCHEMA = z.custom((hook)=>'function' == typeof hook);
|
|
5
|
+
const HOOKS_SCHEMA = z.object({
|
|
6
|
+
onReleased: BLOCK_HOOK_SCHEMA,
|
|
7
|
+
onReserved: BLOCK_HOOK_SCHEMA
|
|
8
|
+
});
|
|
9
|
+
const OPTIONS_SCHEMA = z.object({
|
|
10
|
+
host: z.string().trim().min(1).optional(),
|
|
11
|
+
idleTtlMinutes: z.number().positive(),
|
|
12
|
+
keep: z.boolean().optional(),
|
|
13
|
+
size: z.int().min(1).max(65535),
|
|
14
|
+
start: z.int().min(1).max(65535)
|
|
15
|
+
}).refine((options)=>options.start + options.size - 1 <= 65535, {
|
|
16
|
+
message: 'size must fit in the TCP port range.',
|
|
17
|
+
path: [
|
|
18
|
+
'size'
|
|
19
|
+
]
|
|
20
|
+
});
|
|
21
|
+
const INPUT_OPTIONS_SCHEMA = z.object({
|
|
22
|
+
...OPTIONS_SCHEMA.shape,
|
|
23
|
+
idleTtlMinutes: OPTIONS_SCHEMA.shape.idleTtlMinutes.default(60),
|
|
24
|
+
size: OPTIONS_SCHEMA.shape.size.default(10),
|
|
25
|
+
start: OPTIONS_SCHEMA.shape.start.default(10000)
|
|
26
|
+
}).pipe(OPTIONS_SCHEMA);
|
|
27
|
+
const RECORD_SCHEMA = OPTIONS_SCHEMA.safeExtend({
|
|
28
|
+
index: z.int().nonnegative(),
|
|
29
|
+
keep: z.boolean(),
|
|
30
|
+
lastUsedAt: z.iso.datetime(),
|
|
31
|
+
ports: z.array(z.int().min(1).max(65535)).refine((ports)=>new Set(ports).size === ports.length, {
|
|
32
|
+
message: 'Assigned ports must be unique.'
|
|
33
|
+
}),
|
|
34
|
+
worktree: z.string().refine((worktree)=>node_path.isAbsolute(worktree)),
|
|
35
|
+
worktreeId: z.string().min(1)
|
|
36
|
+
}).refine((record)=>record.start + (record.index + 1) * record.size - 1 <= 65535, {
|
|
37
|
+
message: 'Block must fit in the TCP port range.',
|
|
38
|
+
path: [
|
|
39
|
+
'index'
|
|
40
|
+
]
|
|
41
|
+
}).refine((record)=>record.ports.every((port)=>port >= record.start + record.index * record.size && port < record.start + (record.index + 1) * record.size), {
|
|
42
|
+
message: 'Assigned ports must belong to the block.',
|
|
43
|
+
path: [
|
|
44
|
+
'ports'
|
|
45
|
+
]
|
|
46
|
+
});
|
|
47
|
+
const REGISTRY_SCHEMA = z.array(RECORD_SCHEMA).refine((records)=>new Set(records.map((record)=>JSON.stringify([
|
|
48
|
+
record.worktreeId,
|
|
49
|
+
record.start,
|
|
50
|
+
record.size
|
|
51
|
+
]))).size === records.length, {
|
|
52
|
+
message: 'Each checkout can have only one reservation per range.'
|
|
53
|
+
}).refine((records)=>{
|
|
54
|
+
const sortedRecords = records.toSorted((first, second)=>first.start + first.index * first.size - second.start - second.index * second.size);
|
|
55
|
+
return sortedRecords.every((record, index)=>{
|
|
56
|
+
const previousRecord = sortedRecords[index - 1];
|
|
57
|
+
return !previousRecord || previousRecord.start + (previousRecord.index + 1) * previousRecord.size <= record.start + record.index * record.size;
|
|
58
|
+
});
|
|
59
|
+
}, {
|
|
60
|
+
message: 'Reserved port blocks must not overlap.'
|
|
61
|
+
});
|
|
62
|
+
function parseOptions(options) {
|
|
63
|
+
const result = INPUT_OPTIONS_SCHEMA.safeParse(options);
|
|
64
|
+
if (!result.success) {
|
|
65
|
+
const details = result.error.issues.map((issue)=>`${issue.path.join('.')}: ${issue.message}`).join('; ');
|
|
66
|
+
throw new PortoError('optionsInvalid', details, {
|
|
67
|
+
cause: result.error
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
return result.data;
|
|
71
|
+
}
|
|
72
|
+
export { HOOKS_SCHEMA, OPTIONS_SCHEMA, REGISTRY_SCHEMA, parseOptions };
|
package/dist/vars.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import type { PortBlock } from './index.js';
|
|
3
|
+
type VarEntry = [string, undefined | VarFactory];
|
|
4
|
+
type VarFactory = (block: number, port: number, idx: number) => string;
|
|
5
|
+
type Vars = Array<Record<string, VarFactory> | string>;
|
|
6
|
+
declare const VARS_SCHEMA: z.ZodPipe<z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodCustom<Record<string, VarFactory>, Record<string, VarFactory>>]>>, z.ZodTransform<VarEntry[], (string | Record<string, VarFactory>)[]>>;
|
|
7
|
+
declare function assertVarsFit(varsCount: number, size: number): void;
|
|
8
|
+
declare function mapBlockToVars(block: PortBlock, entries: Array<VarEntry>): {
|
|
9
|
+
[k: string]: string | number;
|
|
10
|
+
};
|
|
11
|
+
declare function parseVarEntries(vars: unknown): VarEntry[];
|
|
12
|
+
declare function parseVars(source: string): VarEntry[];
|
|
13
|
+
export { assertVarsFit, mapBlockToVars, parseVarEntries, parseVars, VARS_SCHEMA };
|
|
14
|
+
export type { VarEntry, VarFactory, Vars };
|
package/dist/vars.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { types } from "node:util";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { PortoError } from "./error.js";
|
|
4
|
+
const VAR_NAME_SCHEMA = z.string().regex(/^[A-Za-z_][A-Za-z0-9_]*$/u);
|
|
5
|
+
const VAR_TEXT_SCHEMA = z.string().regex(/^[^\r\n\0]*$/u);
|
|
6
|
+
const VAR_RECORD_SCHEMA = z.custom((record)=>{
|
|
7
|
+
if ('object' != typeof record || null === record || Array.isArray(record)) return false;
|
|
8
|
+
return Object.entries(record).every(([name, factory])=>VAR_NAME_SCHEMA.safeParse(name).success && 'function' == typeof factory);
|
|
9
|
+
});
|
|
10
|
+
const VAR_ENTRY_SCHEMA = z.union([
|
|
11
|
+
VAR_NAME_SCHEMA,
|
|
12
|
+
VAR_RECORD_SCHEMA
|
|
13
|
+
]);
|
|
14
|
+
const VARS_SCHEMA = z.array(VAR_ENTRY_SCHEMA).transform((vars)=>vars.flatMap((entry)=>'string' == typeof entry ? [
|
|
15
|
+
[
|
|
16
|
+
entry,
|
|
17
|
+
void 0
|
|
18
|
+
]
|
|
19
|
+
] : Object.entries(entry))).refine((entries)=>new Set(entries.map(([name])=>name)).size === entries.length, {
|
|
20
|
+
message: 'Variable names must be unique.'
|
|
21
|
+
});
|
|
22
|
+
function assertVarsFit(varsCount, size) {
|
|
23
|
+
if (varsCount > size) throw new PortoError('mappingInvalid', 'Vars contain more entries than the block contains.');
|
|
24
|
+
}
|
|
25
|
+
function mapBlockToVars(block, entries) {
|
|
26
|
+
const mappedEntries = entries.map(([name, factory], idx)=>{
|
|
27
|
+
const port = block.getPort(idx);
|
|
28
|
+
if (void 0 === factory) return [
|
|
29
|
+
name,
|
|
30
|
+
port
|
|
31
|
+
];
|
|
32
|
+
try {
|
|
33
|
+
const text = factory(block.index, port, idx);
|
|
34
|
+
if (types.isPromise(text)) text.catch(()=>{});
|
|
35
|
+
return [
|
|
36
|
+
name,
|
|
37
|
+
VAR_TEXT_SCHEMA.parse(text)
|
|
38
|
+
];
|
|
39
|
+
} catch (error) {
|
|
40
|
+
throw new PortoError('mappingInvalid', `Cannot create ${name}; expected a single-line string.`, {
|
|
41
|
+
cause: error
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
return Object.fromEntries(mappedEntries);
|
|
46
|
+
}
|
|
47
|
+
function parseVar(entry) {
|
|
48
|
+
const separator = entry.indexOf(':');
|
|
49
|
+
const name = (-1 === separator ? entry : entry.slice(0, separator)).trim();
|
|
50
|
+
if (-1 === separator) return name;
|
|
51
|
+
const template = entry.slice(separator + 1);
|
|
52
|
+
const result = VAR_TEXT_SCHEMA.safeParse(template);
|
|
53
|
+
if (!result.success) throw new PortoError('mappingInvalid', 'Vars templates must fit on one line.', {
|
|
54
|
+
cause: result.error
|
|
55
|
+
});
|
|
56
|
+
return {
|
|
57
|
+
[name] (block, port) {
|
|
58
|
+
return template.replaceAll('{{block}}', String(block)).replaceAll('{{port}}', String(port));
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function parseVarEntries(vars) {
|
|
63
|
+
const result = VARS_SCHEMA.safeParse(vars);
|
|
64
|
+
if (!result.success) throw new PortoError('mappingInvalid', result.error.issues.map((issue)=>issue.message).join('; '), {
|
|
65
|
+
cause: result.error
|
|
66
|
+
});
|
|
67
|
+
return result.data;
|
|
68
|
+
}
|
|
69
|
+
function parseVars(source) {
|
|
70
|
+
return parseVarEntries(source.split(';').map((entry)=>parseVar(entry)));
|
|
71
|
+
}
|
|
72
|
+
export { VARS_SCHEMA, assertVarsFit, mapBlockToVars, parseVarEntries, parseVars };
|
package/package.json
CHANGED
|
@@ -1,6 +1,96 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@b12k/porto",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Port range allocator for git worktrees",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"cli",
|
|
7
|
+
"git",
|
|
8
|
+
"ports",
|
|
9
|
+
"worktree"
|
|
10
|
+
],
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"author": "Bogdan Kolesnyk",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/b12k/porto.git"
|
|
16
|
+
},
|
|
17
|
+
"bin": {
|
|
18
|
+
"porto": "dist/cli.js"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"type": "module",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"import": "./dist/index.js",
|
|
28
|
+
"require": "./dist/index.js"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "rslib",
|
|
36
|
+
"check": "run-p -l check:*",
|
|
37
|
+
"check:format": "oxfmt . --check",
|
|
38
|
+
"check:scripts": "oxlint",
|
|
39
|
+
"check:types": "tsc --noEmit",
|
|
40
|
+
"check:tests": "rstest run",
|
|
41
|
+
"deps:check": "ncu -e 2 -c 1",
|
|
42
|
+
"deps:update": "ncu -du --doctorInstall \"pnpm deps:update:install\" --doctorTest \"pnpm deps:update:doctor\" -c 1",
|
|
43
|
+
"deps:update:doctor": "run-p -l check:types check:scripts check:tests build",
|
|
44
|
+
"deps:update:install": "pnpm i --no-frozen-lockfile",
|
|
45
|
+
"dev": "rslib --watch",
|
|
46
|
+
"fix": "run-s -l fix:*",
|
|
47
|
+
"fix:format": "oxfmt . --write",
|
|
48
|
+
"fix:scripts": "oxlint --fix",
|
|
49
|
+
"ga:notify": "tsx .github/notify.ts",
|
|
50
|
+
"husky:commit-msg": "commitlint",
|
|
51
|
+
"husky:pre-commit": "lint-staged",
|
|
52
|
+
"husky:pre-push": "branch-name-lint",
|
|
53
|
+
"prepack": "run-p check build",
|
|
54
|
+
"prepare": "husky",
|
|
55
|
+
"release": "release-it",
|
|
56
|
+
"test": "rstest run"
|
|
57
|
+
},
|
|
58
|
+
"dependencies": {
|
|
59
|
+
"boxen": "9.0.0",
|
|
60
|
+
"commander": "15.1.0",
|
|
61
|
+
"cosmiconfig": "10.0.1",
|
|
62
|
+
"date-fns": "4.4.0",
|
|
63
|
+
"es-toolkit": "1.53.2",
|
|
64
|
+
"jiti": "2.7.0",
|
|
65
|
+
"proper-lockfile": "4.1.2",
|
|
66
|
+
"update-notifier": "7.3.1",
|
|
67
|
+
"zod": "4.6.7"
|
|
68
|
+
},
|
|
69
|
+
"devDependencies": {
|
|
70
|
+
"@b12k/branch-name-lint": "1.3.0",
|
|
71
|
+
"@commitlint/config-conventional": "21.2.4",
|
|
72
|
+
"@release-it/conventional-changelog": "12.0.2",
|
|
73
|
+
"@rslib/core": "1.0.7",
|
|
74
|
+
"@rstest/core": "0.12.6",
|
|
75
|
+
"@types/node": "26.6.6",
|
|
76
|
+
"@types/proper-lockfile": "4.1.4",
|
|
77
|
+
"@types/update-notifier": "6.0.9",
|
|
78
|
+
"commitlint": "21.2.4",
|
|
79
|
+
"eslint-plugin-perfectionist": "5.13.0",
|
|
80
|
+
"husky": "9.1.7",
|
|
81
|
+
"lint-staged": "17.6.0",
|
|
82
|
+
"npm-check-updates": "23.1.1",
|
|
83
|
+
"npm-run-all2": "9.0.4",
|
|
84
|
+
"oxfmt": "0.78.0",
|
|
85
|
+
"oxlint": "1.93.0",
|
|
86
|
+
"oxlint-tsgolint": "7.0.2007",
|
|
87
|
+
"release-it": "21.1.2",
|
|
88
|
+
"tsx": "4.23.15",
|
|
89
|
+
"type-fest": "5.10.0",
|
|
90
|
+
"typescript": "7.0.2"
|
|
91
|
+
},
|
|
92
|
+
"engines": {
|
|
93
|
+
"node": ">=24"
|
|
94
|
+
},
|
|
95
|
+
"packageManager": "pnpm@12.18.0"
|
|
96
|
+
}
|