@b12k/porto 0.8.0 → 0.10.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/README.md +158 -91
- package/dist/busy-ports-error.d.ts +2 -2
- package/dist/cli.js +44 -43
- package/dist/config.d.ts +29 -43
- package/dist/config.js +41 -66
- package/dist/errors.d.ts +20 -8
- package/dist/errors.js +20 -8
- package/dist/git.d.ts +1 -1
- package/dist/git.js +12 -12
- package/dist/index.d.ts +102 -27
- package/dist/index.js +47 -46
- package/dist/output.d.ts +3 -3
- package/dist/output.js +25 -19
- package/dist/ports.d.ts +7 -10
- package/dist/ports.js +10 -15
- package/dist/registry.d.ts +3 -12
- package/dist/registry.js +17 -12
- package/dist/reserve.d.ts +73 -24
- package/dist/reserve.js +93 -62
- package/dist/schemas.d.ts +60 -13
- package/dist/schemas.js +47 -41
- package/dist/vars.d.ts +11 -4
- package/dist/vars.js +21 -9
- package/package.json +20 -4
- package/dist/load-config.d.ts +0 -1
- package/dist/load-config.js +0 -17
package/dist/config.js
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { cosmiconfig } from "cosmiconfig";
|
|
2
|
+
import { realpath } from "node:fs/promises";
|
|
2
3
|
import node_path from "node:path";
|
|
3
4
|
import { z } from "zod";
|
|
4
5
|
import package_0 from "../package.json" with {
|
|
5
6
|
type: "json"
|
|
6
7
|
};
|
|
7
8
|
import { PortoError } from "./error.js";
|
|
8
|
-
import {
|
|
9
|
-
import { loadConfigFile } from "./load-config.js";
|
|
10
|
-
import { HOOKS_SCHEMA, OPTIONS_SCHEMA, parseOptions } from "./schemas.js";
|
|
9
|
+
import { HOOKS_SCHEMA, OPTIONS_SCHEMA, OUTPUT_OPTIONS_SCHEMA } from "./schemas.js";
|
|
11
10
|
import { VARS_SCHEMA } from "./vars.js";
|
|
12
|
-
const DEFAULT_ENV_FILE = '.env';
|
|
13
11
|
const CONFIG_FILES = [
|
|
14
12
|
'porto.config.ts',
|
|
15
13
|
'porto.config.js'
|
|
@@ -17,39 +15,29 @@ const CONFIG_FILES = [
|
|
|
17
15
|
const CONFIG_SCHEMA = z.strictObject({
|
|
18
16
|
...OPTIONS_SCHEMA.shape,
|
|
19
17
|
...HOOKS_SCHEMA.shape,
|
|
20
|
-
|
|
21
|
-
envFile: z.string().min(1),
|
|
22
|
-
jsonFile: z.union([
|
|
23
|
-
z.literal(false),
|
|
24
|
-
z.string().min(1)
|
|
25
|
-
]),
|
|
26
|
-
silent: z.boolean(),
|
|
18
|
+
...OUTPUT_OPTIONS_SCHEMA.shape,
|
|
27
19
|
vars: VARS_SCHEMA
|
|
28
20
|
}).partial();
|
|
29
21
|
const libraryName = 'porto';
|
|
30
22
|
const { name: config_name, version } = package_0;
|
|
23
|
+
function applyReserveDefaults(settings, range) {
|
|
24
|
+
const size = settings.size ?? range.portsPerSlot;
|
|
25
|
+
const start = settings.start ?? range.startPort;
|
|
26
|
+
if (start + size - 1 > 65535) throw new PortoError('optionsInvalid', 'size: size must fit in the TCP port range.');
|
|
27
|
+
return {
|
|
28
|
+
...settings,
|
|
29
|
+
env: settings.env ?? false,
|
|
30
|
+
envFile: settings.envFile ?? '.env',
|
|
31
|
+
idleTtlMinutes: settings.idleTtlMinutes ?? 60,
|
|
32
|
+
jsonFile: settings.jsonFile ?? false,
|
|
33
|
+
silent: settings.silent ?? false,
|
|
34
|
+
size,
|
|
35
|
+
start
|
|
36
|
+
};
|
|
37
|
+
}
|
|
31
38
|
function defineConfig(config) {
|
|
32
39
|
return config;
|
|
33
40
|
}
|
|
34
|
-
async function fetchConfigPath(candidates) {
|
|
35
|
-
let configPath;
|
|
36
|
-
for (const filePath of candidates)try {
|
|
37
|
-
await access(filePath);
|
|
38
|
-
configPath = filePath;
|
|
39
|
-
break;
|
|
40
|
-
} catch (error) {
|
|
41
|
-
if (!checkIsFileMissing(error)) throw error;
|
|
42
|
-
}
|
|
43
|
-
return configPath;
|
|
44
|
-
}
|
|
45
|
-
function getConfigPaths(directoryPath, worktreePath) {
|
|
46
|
-
const filePaths = [];
|
|
47
|
-
for(let currentPath = directoryPath;; currentPath = node_path.dirname(currentPath)){
|
|
48
|
-
filePaths.push(...CONFIG_FILES.map((fileName)=>node_path.join(currentPath, fileName)));
|
|
49
|
-
if (currentPath === worktreePath) return filePaths;
|
|
50
|
-
if (node_path.dirname(currentPath) === currentPath) throw new PortoError('configInvalid', 'Cannot find the checkout root.');
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
41
|
function parseConfig(config) {
|
|
54
42
|
const result = CONFIG_SCHEMA.safeParse(config);
|
|
55
43
|
if (!result.success) {
|
|
@@ -59,45 +47,32 @@ function parseConfig(config) {
|
|
|
59
47
|
});
|
|
60
48
|
}
|
|
61
49
|
const settings = result.data;
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
...void 0 !== settings.envFile && void 0 === settings.env ? {
|
|
65
|
-
env: true
|
|
66
|
-
} : {}
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
function applyReserveDefaults(settings) {
|
|
70
|
-
return {
|
|
71
|
-
...settings,
|
|
72
|
-
...parseOptions(settings),
|
|
73
|
-
env: settings.env ?? false,
|
|
74
|
-
envFile: settings.envFile ?? DEFAULT_ENV_FILE,
|
|
75
|
-
jsonFile: settings.jsonFile ?? false,
|
|
76
|
-
silent: settings.silent ?? false
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
function resolveConfigPaths(config, filePath) {
|
|
80
|
-
const directoryPath = node_path.dirname(filePath);
|
|
81
|
-
for (const key of [
|
|
82
|
-
'envFile',
|
|
83
|
-
'jsonFile'
|
|
84
|
-
]){
|
|
85
|
-
const configuredPath = config[key];
|
|
86
|
-
if ('string' == typeof configuredPath) config[key] = node_path.resolve(directoryPath, configuredPath);
|
|
87
|
-
}
|
|
88
|
-
return config;
|
|
50
|
+
if (void 0 !== settings.envFile) settings.env ??= true;
|
|
51
|
+
return settings;
|
|
89
52
|
}
|
|
90
|
-
async function fetchConfig(
|
|
91
|
-
|
|
92
|
-
const filePath = await fetchConfigPath(candidates);
|
|
93
|
-
if (!filePath) return parseConfig({});
|
|
53
|
+
async function fetchConfig(worktreePath) {
|
|
54
|
+
let result;
|
|
94
55
|
try {
|
|
95
|
-
|
|
96
|
-
|
|
56
|
+
result = await cosmiconfig(libraryName, {
|
|
57
|
+
searchPlaces: CONFIG_FILES,
|
|
58
|
+
stopDir: worktreePath
|
|
59
|
+
}).search(await realpath(process.cwd()));
|
|
97
60
|
} catch (error) {
|
|
98
|
-
throw new PortoError('configInvalid',
|
|
61
|
+
throw new PortoError('configInvalid', error instanceof Error ? error.message : String(error), {
|
|
99
62
|
cause: error
|
|
100
63
|
});
|
|
101
64
|
}
|
|
65
|
+
if (!result) return {};
|
|
66
|
+
const config = result.config;
|
|
67
|
+
const settings = parseConfig(config && 'object' == typeof config && 1 === Object.keys(config).length && 'default' in config ? config.default : config);
|
|
68
|
+
const directoryPath = node_path.dirname(result.filepath);
|
|
69
|
+
for (const key of [
|
|
70
|
+
'envFile',
|
|
71
|
+
'jsonFile'
|
|
72
|
+
]){
|
|
73
|
+
const configuredPath = settings[key];
|
|
74
|
+
if ('string' == typeof configuredPath) settings[key] = node_path.resolve(directoryPath, configuredPath);
|
|
75
|
+
}
|
|
76
|
+
return settings;
|
|
102
77
|
}
|
|
103
|
-
export {
|
|
78
|
+
export { applyReserveDefaults, config_name as name, defineConfig, fetchConfig, libraryName, parseConfig, version };
|
package/dist/errors.d.ts
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
1
|
export declare const ERRORS_BY_KEY: {
|
|
2
|
-
readonly blockUnavailable: {
|
|
3
|
-
readonly instruction: 'Release unused blocks or choose another range.';
|
|
4
|
-
readonly message: 'No free port block remains in the TCP port range.';
|
|
5
|
-
};
|
|
6
2
|
readonly configInvalid: {
|
|
7
3
|
readonly instruction: 'Check porto.config.ts or porto.config.js.';
|
|
8
4
|
readonly message: 'Invalid Porto configuration.';
|
|
@@ -16,11 +12,11 @@ export declare const ERRORS_BY_KEY: {
|
|
|
16
12
|
readonly message: 'Lifecycle hook failed.';
|
|
17
13
|
};
|
|
18
14
|
readonly mappingInvalid: {
|
|
19
|
-
readonly instruction: 'Use unique variable names, valid callbacks, and a
|
|
15
|
+
readonly instruction: 'Use unique variable names, valid callbacks, and a slot large enough for all entries.';
|
|
20
16
|
readonly message: 'Invalid variables.';
|
|
21
17
|
};
|
|
22
18
|
readonly offsetInvalid: {
|
|
23
|
-
readonly instruction: 'Choose an integer offset inside the
|
|
19
|
+
readonly instruction: 'Choose an integer offset inside the slot.';
|
|
24
20
|
readonly message: 'Invalid port offset.';
|
|
25
21
|
};
|
|
26
22
|
readonly optionsInvalid: {
|
|
@@ -31,13 +27,29 @@ export declare const ERRORS_BY_KEY: {
|
|
|
31
27
|
readonly instruction: 'Stop the services using these ports and try again.';
|
|
32
28
|
readonly message: 'Cannot release reservations while ports are in use.';
|
|
33
29
|
};
|
|
30
|
+
readonly rangeMismatch: {
|
|
31
|
+
readonly instruction: 'Use the repository start and size, or release all slots before changing them.';
|
|
32
|
+
readonly message: 'Port range settings differ from the repository registry.';
|
|
33
|
+
};
|
|
34
34
|
readonly registryInvalid: {
|
|
35
35
|
readonly instruction: 'Restore or repair the reservation file before continuing.';
|
|
36
36
|
readonly message: 'Cannot read reservation.';
|
|
37
37
|
};
|
|
38
38
|
readonly reservationNotFound: {
|
|
39
|
-
readonly instruction: 'Run porto reserve
|
|
40
|
-
readonly message: 'No
|
|
39
|
+
readonly instruction: 'Run porto show --all to list slots or porto reserve to reserve one.';
|
|
40
|
+
readonly message: 'No matching port slot.';
|
|
41
|
+
};
|
|
42
|
+
readonly slotIndexInvalid: {
|
|
43
|
+
readonly instruction: 'Choose a non-negative integer slot index.';
|
|
44
|
+
readonly message: 'Invalid slot index.';
|
|
45
|
+
};
|
|
46
|
+
readonly slotSelectionInvalid: {
|
|
47
|
+
readonly instruction: 'Use a slot number or --all.';
|
|
48
|
+
readonly message: 'Cannot combine a slot number with --all.';
|
|
49
|
+
};
|
|
50
|
+
readonly slotUnavailable: {
|
|
51
|
+
readonly instruction: 'Release unused slots or choose another range.';
|
|
52
|
+
readonly message: 'No free port slot remains in the TCP port range.';
|
|
41
53
|
};
|
|
42
54
|
readonly worktreeNotFound: {
|
|
43
55
|
readonly instruction: 'Run Porto inside a regular Git checkout or linked worktree.';
|
package/dist/errors.js
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
1
|
const ERRORS_BY_KEY = {
|
|
2
|
-
blockUnavailable: {
|
|
3
|
-
instruction: 'Release unused blocks or choose another range.',
|
|
4
|
-
message: 'No free port block remains in the TCP port range.'
|
|
5
|
-
},
|
|
6
2
|
configInvalid: {
|
|
7
3
|
instruction: 'Check porto.config.ts or porto.config.js.',
|
|
8
4
|
message: 'Invalid Porto configuration.'
|
|
@@ -16,11 +12,11 @@ const ERRORS_BY_KEY = {
|
|
|
16
12
|
message: 'Lifecycle hook failed.'
|
|
17
13
|
},
|
|
18
14
|
mappingInvalid: {
|
|
19
|
-
instruction: 'Use unique variable names, valid callbacks, and a
|
|
15
|
+
instruction: 'Use unique variable names, valid callbacks, and a slot large enough for all entries.',
|
|
20
16
|
message: 'Invalid variables.'
|
|
21
17
|
},
|
|
22
18
|
offsetInvalid: {
|
|
23
|
-
instruction: 'Choose an integer offset inside the
|
|
19
|
+
instruction: 'Choose an integer offset inside the slot.',
|
|
24
20
|
message: 'Invalid port offset.'
|
|
25
21
|
},
|
|
26
22
|
optionsInvalid: {
|
|
@@ -31,13 +27,29 @@ const ERRORS_BY_KEY = {
|
|
|
31
27
|
instruction: 'Stop the services using these ports and try again.',
|
|
32
28
|
message: 'Cannot release reservations while ports are in use.'
|
|
33
29
|
},
|
|
30
|
+
rangeMismatch: {
|
|
31
|
+
instruction: 'Use the repository start and size, or release all slots before changing them.',
|
|
32
|
+
message: 'Port range settings differ from the repository registry.'
|
|
33
|
+
},
|
|
34
34
|
registryInvalid: {
|
|
35
35
|
instruction: 'Restore or repair the reservation file before continuing.',
|
|
36
36
|
message: 'Cannot read reservation.'
|
|
37
37
|
},
|
|
38
38
|
reservationNotFound: {
|
|
39
|
-
instruction: 'Run porto reserve
|
|
40
|
-
message: 'No
|
|
39
|
+
instruction: 'Run porto show --all to list slots or porto reserve to reserve one.',
|
|
40
|
+
message: 'No matching port slot.'
|
|
41
|
+
},
|
|
42
|
+
slotIndexInvalid: {
|
|
43
|
+
instruction: 'Choose a non-negative integer slot index.',
|
|
44
|
+
message: 'Invalid slot index.'
|
|
45
|
+
},
|
|
46
|
+
slotSelectionInvalid: {
|
|
47
|
+
instruction: 'Use a slot number or --all.',
|
|
48
|
+
message: 'Cannot combine a slot number with --all.'
|
|
49
|
+
},
|
|
50
|
+
slotUnavailable: {
|
|
51
|
+
instruction: 'Release unused slots or choose another range.',
|
|
52
|
+
message: 'No free port slot remains in the TCP port range.'
|
|
41
53
|
},
|
|
42
54
|
worktreeNotFound: {
|
|
43
55
|
instruction: 'Run Porto inside a regular Git checkout or linked worktree.',
|
package/dist/git.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
declare function fetchGitContext(): Promise<{
|
|
2
2
|
commonDirectory: string;
|
|
3
|
-
worktree: string;
|
|
4
3
|
worktreeId: string;
|
|
4
|
+
worktreePath: string;
|
|
5
5
|
}>;
|
|
6
6
|
declare function listWorktreePaths(commonDirectory: string): Promise<Map<string, string>>;
|
|
7
7
|
export { fetchGitContext, listWorktreePaths };
|
package/dist/git.js
CHANGED
|
@@ -17,17 +17,17 @@ async function fetchGitContext() {
|
|
|
17
17
|
'--git-common-dir',
|
|
18
18
|
'--absolute-git-dir'
|
|
19
19
|
]);
|
|
20
|
-
const [
|
|
21
|
-
if (!
|
|
22
|
-
const [
|
|
23
|
-
realpath(
|
|
20
|
+
const [rootPath, commonPath, gitPath] = stdout.split(/\r?\n/u);
|
|
21
|
+
if (!rootPath || !commonPath || !gitPath) throw new Error('Missing Git paths.');
|
|
22
|
+
const [worktreePath, commonDirectory, gitDirectory] = await Promise.all([
|
|
23
|
+
realpath(rootPath),
|
|
24
24
|
realpath(commonPath),
|
|
25
25
|
realpath(gitPath)
|
|
26
26
|
]);
|
|
27
27
|
return {
|
|
28
28
|
commonDirectory,
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
worktreeId: getWorktreeId(commonDirectory, gitDirectory),
|
|
30
|
+
worktreePath
|
|
31
31
|
};
|
|
32
32
|
} catch (error) {
|
|
33
33
|
throw new PortoError('worktreeNotFound', void 0, {
|
|
@@ -35,10 +35,10 @@ async function fetchGitContext() {
|
|
|
35
35
|
});
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
|
-
async function fetchLinkedWorktreeId(commonDirectory,
|
|
39
|
-
const gitLink = await readFile(node_path.join(
|
|
38
|
+
async function fetchLinkedWorktreeId(commonDirectory, worktreePath) {
|
|
39
|
+
const gitLink = await readFile(node_path.join(worktreePath, '.git'), 'utf8');
|
|
40
40
|
const gitPath = /^gitdir: (?<gitPath>.+)\r?\n?$/u.exec(gitLink)?.groups?.['gitPath'];
|
|
41
|
-
return gitPath && getWorktreeId(commonDirectory, await realpath(node_path.resolve(
|
|
41
|
+
return gitPath && getWorktreeId(commonDirectory, await realpath(node_path.resolve(worktreePath, gitPath)));
|
|
42
42
|
}
|
|
43
43
|
async function listWorktreePaths(commonDirectory) {
|
|
44
44
|
const { stdout } = await execFileAsync('git', [
|
|
@@ -52,9 +52,9 @@ async function listWorktreePaths(commonDirectory) {
|
|
|
52
52
|
const [location, ...flags] = entry.split('\0');
|
|
53
53
|
if (!location?.startsWith('worktree ') || flags.some((flag)=>'bare' === flag || flag.startsWith('prunable '))) return;
|
|
54
54
|
try {
|
|
55
|
-
const
|
|
56
|
-
const worktreeId = 0 === index ? '.' : await fetchLinkedWorktreeId(commonDirectory,
|
|
57
|
-
if (worktreeId) worktreePathsById.set(worktreeId,
|
|
55
|
+
const worktreePath = await realpath(location.slice(9));
|
|
56
|
+
const worktreeId = 0 === index ? '.' : await fetchLinkedWorktreeId(commonDirectory, worktreePath);
|
|
57
|
+
if (worktreeId) worktreePathsById.set(worktreeId, worktreePath);
|
|
58
58
|
} catch (error) {
|
|
59
59
|
if (!checkIsFileMissing(error)) throw error;
|
|
60
60
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,45 +1,120 @@
|
|
|
1
1
|
import type { ReservationRecord } from './registry.js';
|
|
2
|
-
import {
|
|
3
|
-
type
|
|
2
|
+
import { buildSlot } from './ports.js';
|
|
3
|
+
type PortSlot = ReturnType<typeof buildSlot<ReservationRecord>>;
|
|
4
4
|
type ReservationStats = Omit<ReservationRecord, 'worktreeId'> & {
|
|
5
|
-
|
|
5
|
+
index: number;
|
|
6
6
|
isExpired: boolean;
|
|
7
7
|
};
|
|
8
|
-
declare function
|
|
9
|
-
|
|
8
|
+
declare function cleanupSlots(): Promise<({
|
|
9
|
+
env: boolean;
|
|
10
|
+
envFile: string;
|
|
11
|
+
jsonFile: string | false;
|
|
12
|
+
silent: boolean;
|
|
13
|
+
expiresAt: string | null;
|
|
14
|
+
firstPort: number;
|
|
15
|
+
host?: string | undefined;
|
|
16
|
+
idleTtlMinutes: number;
|
|
17
|
+
keep: boolean;
|
|
18
|
+
lastPort: number;
|
|
19
|
+
lastUsedAt: string;
|
|
20
|
+
ports: number[];
|
|
21
|
+
vars: {
|
|
22
|
+
PORTO_SLOT: number;
|
|
23
|
+
};
|
|
24
|
+
worktreeId: string;
|
|
25
|
+
worktreePath: string;
|
|
26
|
+
} & {
|
|
10
27
|
getPort(offset?: number): number;
|
|
11
28
|
index: number;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
declare function current(): Promise<{
|
|
15
|
-
end: number;
|
|
16
|
-
getPort(offset?: number): number;
|
|
29
|
+
})[]>;
|
|
30
|
+
declare function getAllSlots(): Promise<{
|
|
17
31
|
index: number;
|
|
18
|
-
|
|
32
|
+
isExpired: boolean;
|
|
33
|
+
worktreePath: string;
|
|
34
|
+
env: boolean;
|
|
35
|
+
envFile: string;
|
|
36
|
+
jsonFile: string | false;
|
|
37
|
+
silent: boolean;
|
|
38
|
+
expiresAt: string | null;
|
|
39
|
+
firstPort: number;
|
|
40
|
+
host?: string | undefined;
|
|
41
|
+
idleTtlMinutes: number;
|
|
42
|
+
keep: boolean;
|
|
43
|
+
lastPort: number;
|
|
44
|
+
lastUsedAt: string;
|
|
45
|
+
ports: number[];
|
|
46
|
+
vars: {
|
|
47
|
+
PORTO_SLOT: number;
|
|
48
|
+
};
|
|
19
49
|
}[]>;
|
|
20
|
-
declare function
|
|
21
|
-
|
|
50
|
+
declare function getSlot(slotIndex?: number): Promise<({
|
|
51
|
+
env: boolean;
|
|
52
|
+
envFile: string;
|
|
53
|
+
jsonFile: string | false;
|
|
54
|
+
silent: boolean;
|
|
55
|
+
expiresAt: string | null;
|
|
56
|
+
firstPort: number;
|
|
57
|
+
host?: string | undefined;
|
|
58
|
+
idleTtlMinutes: number;
|
|
59
|
+
keep: boolean;
|
|
60
|
+
lastPort: number;
|
|
61
|
+
lastUsedAt: string;
|
|
62
|
+
ports: number[];
|
|
63
|
+
vars: {
|
|
64
|
+
PORTO_SLOT: number;
|
|
65
|
+
};
|
|
66
|
+
worktreeId: string;
|
|
67
|
+
worktreePath: string;
|
|
68
|
+
} & {
|
|
22
69
|
getPort(offset?: number): number;
|
|
23
70
|
index: number;
|
|
24
|
-
|
|
25
|
-
|
|
71
|
+
}) | undefined>;
|
|
72
|
+
declare function releaseAllSlots(): Promise<({
|
|
73
|
+
env: boolean;
|
|
74
|
+
envFile: string;
|
|
75
|
+
jsonFile: string | false;
|
|
76
|
+
silent: boolean;
|
|
77
|
+
expiresAt: string | null;
|
|
78
|
+
firstPort: number;
|
|
79
|
+
host?: string | undefined;
|
|
80
|
+
idleTtlMinutes: number;
|
|
81
|
+
keep: boolean;
|
|
82
|
+
lastPort: number;
|
|
83
|
+
lastUsedAt: string;
|
|
26
84
|
ports: number[];
|
|
27
|
-
|
|
28
|
-
|
|
85
|
+
vars: {
|
|
86
|
+
PORTO_SLOT: number;
|
|
87
|
+
};
|
|
88
|
+
worktreeId: string;
|
|
89
|
+
worktreePath: string;
|
|
90
|
+
} & {
|
|
91
|
+
getPort(offset?: number): number;
|
|
29
92
|
index: number;
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
93
|
+
})[]>;
|
|
94
|
+
declare function releaseSlot(slotIndex?: number): Promise<({
|
|
95
|
+
env: boolean;
|
|
96
|
+
envFile: string;
|
|
97
|
+
jsonFile: string | false;
|
|
98
|
+
silent: boolean;
|
|
99
|
+
expiresAt: string | null;
|
|
100
|
+
firstPort: number;
|
|
36
101
|
host?: string | undefined;
|
|
37
102
|
idleTtlMinutes: number;
|
|
38
103
|
keep: boolean;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
104
|
+
lastPort: number;
|
|
105
|
+
lastUsedAt: string;
|
|
106
|
+
ports: number[];
|
|
107
|
+
vars: {
|
|
108
|
+
PORTO_SLOT: number;
|
|
109
|
+
};
|
|
110
|
+
worktreeId: string;
|
|
111
|
+
worktreePath: string;
|
|
112
|
+
} & {
|
|
113
|
+
getPort(offset?: number): number;
|
|
114
|
+
index: number;
|
|
115
|
+
}) | undefined>;
|
|
116
|
+
export { cleanupSlots, getAllSlots, getSlot, releaseAllSlots, releaseSlot };
|
|
117
|
+
export type { PortSlot, ReservationStats };
|
|
43
118
|
export { BusyPortsError } from './busy-ports-error.js';
|
|
44
119
|
export { defineConfig } from './config.js';
|
|
45
120
|
export type { PortoConfig } from './config.js';
|
package/dist/index.js
CHANGED
|
@@ -1,57 +1,58 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { BusyPortsError } from "./busy-ports-error.js";
|
|
1
|
+
import { PortoError } from "./error.js";
|
|
3
2
|
import { fetchGitContext, listWorktreePaths } from "./git.js";
|
|
4
|
-
import {
|
|
3
|
+
import { buildSlot } from "./ports.js";
|
|
5
4
|
import { updateRegistry } from "./registry.js";
|
|
6
|
-
import { checkIsExpired, removeExpiredRecords, reserve } from "./reserve.js";
|
|
7
|
-
async function
|
|
5
|
+
import { checkIsExpired, releaseRecord, removeExpiredRecords, reserve } from "./reserve.js";
|
|
6
|
+
async function cleanupSlots() {
|
|
8
7
|
const { commonDirectory } = await fetchGitContext();
|
|
9
|
-
return updateRegistry(commonDirectory, (
|
|
8
|
+
return updateRegistry(commonDirectory, (registry)=>removeExpiredRecords(registry, Date.now()));
|
|
10
9
|
}
|
|
11
|
-
async function
|
|
12
|
-
const { commonDirectory
|
|
13
|
-
|
|
10
|
+
async function getAllSlots() {
|
|
11
|
+
const { commonDirectory } = await fetchGitContext();
|
|
12
|
+
const worktreePathsById = await listWorktreePaths(commonDirectory);
|
|
13
|
+
return updateRegistry(commonDirectory, ({ slots })=>{
|
|
14
|
+
const now = Date.now();
|
|
15
|
+
return slots.flatMap((record, index)=>{
|
|
16
|
+
if (null === record) return [];
|
|
17
|
+
const { worktreeId, ...settings } = record;
|
|
18
|
+
const worktreePath = worktreePathsById.get(worktreeId) ?? record.worktreePath;
|
|
19
|
+
return [
|
|
20
|
+
{
|
|
21
|
+
...settings,
|
|
22
|
+
index,
|
|
23
|
+
isExpired: checkIsExpired(record, now),
|
|
24
|
+
worktreePath
|
|
25
|
+
}
|
|
26
|
+
];
|
|
27
|
+
});
|
|
28
|
+
});
|
|
14
29
|
}
|
|
15
|
-
async function
|
|
30
|
+
async function getSlot(slotIndex) {
|
|
31
|
+
if (void 0 !== slotIndex && (!Number.isSafeInteger(slotIndex) || slotIndex < 0)) throw new PortoError('slotIndexInvalid');
|
|
16
32
|
const { commonDirectory, worktreeId } = await fetchGitContext();
|
|
17
|
-
return updateRegistry(commonDirectory,
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
const ports = await listBusyPorts(block.start, block.end, {
|
|
22
|
-
host: record.host
|
|
23
|
-
});
|
|
24
|
-
return ports.map((port)=>({
|
|
25
|
-
block: block.index,
|
|
26
|
-
port
|
|
27
|
-
}));
|
|
28
|
-
}));
|
|
29
|
-
const busyPorts = portsByRecord.flat();
|
|
30
|
-
if (busyPorts.length > 0) throw new BusyPortsError(busyPorts);
|
|
31
|
-
remove(records, (record)=>record.worktreeId === worktreeId);
|
|
32
|
-
return currentRecords.map((record)=>Object.assign(buildBlock(record), {
|
|
33
|
-
ports: record.ports
|
|
34
|
-
}));
|
|
33
|
+
return updateRegistry(commonDirectory, ({ slots })=>{
|
|
34
|
+
const index = slotIndex ?? slots.findIndex((record)=>record?.worktreeId === worktreeId);
|
|
35
|
+
const record = slots[index];
|
|
36
|
+
return record ? buildSlot(record, index) : void 0;
|
|
35
37
|
});
|
|
36
38
|
}
|
|
37
|
-
async function
|
|
39
|
+
async function releaseAllSlots() {
|
|
38
40
|
const { commonDirectory } = await fetchGitContext();
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
]));
|
|
41
|
+
return updateRegistry(commonDirectory, async (registry)=>{
|
|
42
|
+
const releasedSlots = [];
|
|
43
|
+
for (const [index, record] of registry.slots.entries())if (null !== record) releasedSlots.push(await releaseRecord(registry, index, record));
|
|
44
|
+
return releasedSlots;
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
async function releaseSlot(slotIndex) {
|
|
48
|
+
if (void 0 !== slotIndex && (!Number.isSafeInteger(slotIndex) || slotIndex < 0)) throw new PortoError('slotIndexInvalid');
|
|
49
|
+
const { commonDirectory, worktreeId } = await fetchGitContext();
|
|
50
|
+
return updateRegistry(commonDirectory, (registry)=>{
|
|
51
|
+
const index = slotIndex ?? registry.slots.findIndex((record)=>record?.worktreeId === worktreeId);
|
|
52
|
+
const record = registry.slots[index];
|
|
53
|
+
return record ? releaseRecord(registry, index, record) : void 0;
|
|
54
|
+
});
|
|
54
55
|
}
|
|
56
|
+
export { BusyPortsError } from "./busy-ports-error.js";
|
|
55
57
|
export { defineConfig } from "./config.js";
|
|
56
|
-
export { PortoError
|
|
57
|
-
export { BusyPortsError, cleanup, current, release, reserve, stats };
|
|
58
|
+
export { PortoError, cleanupSlots, getAllSlots, getSlot, releaseAllSlots, releaseSlot, reserve };
|
package/dist/output.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { PortSlot, ReservationStats } from './index.js';
|
|
2
2
|
type Environment = Record<string, number | string>;
|
|
3
3
|
declare function fetchExportPaths(settings: {
|
|
4
4
|
env: boolean;
|
|
@@ -9,10 +9,10 @@ declare function fetchExportPaths(settings: {
|
|
|
9
9
|
jsonPath: string | undefined;
|
|
10
10
|
}>;
|
|
11
11
|
declare function formatEnvironment(environment: Environment): string;
|
|
12
|
-
declare function formatReserveOutput(
|
|
12
|
+
declare function formatReserveOutput(slot: PortSlot, environment: Environment | undefined, isJson: boolean): string;
|
|
13
13
|
declare function formatTable(headers: Array<string>, rows: Array<Array<string>>): string;
|
|
14
14
|
declare function formatStatsTable(reservations: Array<ReservationStats>): string;
|
|
15
15
|
declare function updateEnvironmentFile(filePath: string, environment: Environment, commonDirectory?: string): Promise<void>;
|
|
16
|
-
declare function writeReserveJsonFile(filePath: string,
|
|
16
|
+
declare function writeReserveJsonFile(filePath: string, slot: PortSlot, environment: Environment | undefined): Promise<void>;
|
|
17
17
|
export { fetchExportPaths, formatEnvironment, formatReserveOutput, formatStatsTable, formatTable, updateEnvironmentFile, writeReserveJsonFile, };
|
|
18
18
|
export type { Environment };
|