@b12k/porto 0.1.0 → 0.2.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 CHANGED
@@ -66,7 +66,7 @@ import { defineConfig } from '@b12k/porto';
66
66
 
67
67
  export default defineConfig({
68
68
  envFile: '.env.local',
69
- vars: ['API_PORT', { WEB_URL: (_block, port) => `http://localhost:${port}` }],
69
+ vars: ['PORT', (_block, port) => ({ PORT_2: port, HOST_NAME: `https://localhost:${port}` }), 'DB_PORT'],
70
70
  onReserved(block, ports) {
71
71
  console.error(`Block ${block.index} has ports ${ports.join(', ')}`);
72
72
  },
@@ -75,7 +75,7 @@ export default defineConfig({
75
75
 
76
76
  Relative paths in config start at the config directory. Relative paths in flags start at the current directory.
77
77
 
78
- Each variable selects a port by position. A function receives the block index, port, and zero-based variable index.
78
+ Each `vars` entry gets one port. A name exports the port number. A function receives the block index, port, and zero-based entry index. It can return several variables for the same port.
79
79
 
80
80
  A hook receives the block and its assigned ports. `onReserved` runs after file output. `onReleased` runs once per freed block. Porto runs release hooks in parallel.
81
81
 
package/dist/config.d.ts CHANGED
@@ -11,7 +11,7 @@ declare const CONFIG_SCHEMA: z.ZodObject<{
11
11
  env: z.ZodOptional<z.ZodBoolean>;
12
12
  envFile: z.ZodOptional<z.ZodString>;
13
13
  jsonFile: z.ZodOptional<z.ZodUnion<readonly [z.ZodLiteral<false>, z.ZodString]>>;
14
- vars: z.ZodOptional<z.ZodPipe<z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodCustom<Record<string, import("./vars.js").VarFactory>, Record<string, import("./vars.js").VarFactory>>]>>, z.ZodTransform<import("./vars.js").VarEntry[], (string | Record<string, import("./vars.js").VarFactory>)[]>>>;
14
+ vars: z.ZodOptional<z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodCustom<import("./vars.js").VarFactory, import("./vars.js").VarFactory>]>>>;
15
15
  }, z.core.$strict>;
16
16
  type PortoConfig = z.input<typeof CONFIG_SCHEMA>;
17
17
  declare const libraryName = "porto";
@@ -27,7 +27,7 @@ declare function defineConfig(config: PortoConfig): {
27
27
  env?: boolean | undefined;
28
28
  envFile?: string | undefined;
29
29
  jsonFile?: string | false | undefined;
30
- vars?: (string | Record<string, import("./vars.js").VarFactory>)[] | undefined;
30
+ vars?: (string | import("./vars.js").VarFactory)[] | undefined;
31
31
  };
32
32
  declare function parseConfig(config: unknown): {
33
33
  onReleased?: ((block: import("./index.js").PortBlock, ports: Array<number>) => Promise<void> | void) | undefined;
@@ -40,7 +40,7 @@ declare function parseConfig(config: unknown): {
40
40
  env?: boolean | undefined;
41
41
  envFile?: string | undefined;
42
42
  jsonFile?: string | false | undefined;
43
- vars?: import("./vars.js").VarEntry[] | undefined;
43
+ vars?: (string | import("./vars.js").VarFactory)[] | undefined;
44
44
  };
45
45
  declare function applyReserveDefaults(settings: ReturnType<typeof parseConfig>): {
46
46
  onReleased?: ((block: import("./index.js").PortBlock, ports: Array<number>) => Promise<void> | void) | undefined;
@@ -50,7 +50,7 @@ declare function applyReserveDefaults(settings: ReturnType<typeof parseConfig>):
50
50
  keep?: boolean | undefined;
51
51
  size: number;
52
52
  start: number;
53
- vars?: import("./vars.js").VarEntry[] | undefined;
53
+ vars?: (string | import("./vars.js").VarFactory)[] | undefined;
54
54
  env: boolean;
55
55
  envFile: string;
56
56
  jsonFile: string | false;
@@ -66,7 +66,7 @@ declare function fetchConfig(worktree: string): Promise<{
66
66
  env?: boolean | undefined;
67
67
  envFile?: string | undefined;
68
68
  jsonFile?: string | false | undefined;
69
- vars?: import("./vars.js").VarEntry[] | undefined;
69
+ vars?: (string | import("./vars.js").VarFactory)[] | undefined;
70
70
  }>;
71
71
  export { applyReserveDefaults, DEFAULT_ENV_FILE, defineConfig, fetchConfig, libraryName, name, parseConfig, version };
72
72
  export type { PortoConfig };
package/dist/vars.d.ts CHANGED
@@ -1,14 +1,14 @@
1
1
  import { z } from 'zod';
2
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>)[]>>;
3
+ type VarEntry = string | VarFactory;
4
+ type VarFactory = (block: number, port: number, index: number) => Record<string, number | string>;
5
+ type Vars = Array<VarEntry>;
6
+ declare const VARS_SCHEMA: z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodCustom<VarFactory, VarFactory>]>>;
7
7
  declare function assertVarsFit(varsCount: number, size: number): void;
8
- declare function mapBlockToVars(block: PortBlock, entries: Array<VarEntry>): {
8
+ declare function mapBlockToVars(block: PortBlock, entries: Vars): {
9
9
  [k: string]: string | number;
10
10
  };
11
- declare function parseVarEntries(vars: unknown): VarEntry[];
11
+ declare function parseVarEntries(vars: unknown): (string | VarFactory)[];
12
12
  declare function parseVars(source: string): VarEntry[];
13
13
  export { assertVarsFit, mapBlockToVars, parseVarEntries, parseVars, VARS_SCHEMA };
14
14
  export type { VarEntry, VarFactory, Vars };
package/dist/vars.js CHANGED
@@ -1,62 +1,70 @@
1
+ import { isPlainObject } from "es-toolkit";
1
2
  import { types } from "node:util";
2
3
  import { z } from "zod";
3
4
  import { PortoError } from "./error.js";
4
5
  const VAR_NAME_SCHEMA = z.string().regex(/^[A-Za-z_][A-Za-z0-9_]*$/u);
5
6
  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([
7
+ const VAR_FACTORY_SCHEMA = z.custom((entry)=>'function' == typeof entry);
8
+ const VAR_RESULT_ENTRY_SCHEMA = z.tuple([
11
9
  VAR_NAME_SCHEMA,
12
- VAR_RECORD_SCHEMA
10
+ z.union([
11
+ VAR_TEXT_SCHEMA,
12
+ z.number()
13
+ ])
13
14
  ]);
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, {
15
+ const VAR_RESULT_SCHEMA = z.custom(isPlainObject).transform((variables)=>Object.entries(variables)).pipe(z.array(VAR_RESULT_ENTRY_SCHEMA));
16
+ const VARS_SCHEMA = z.array(z.union([
17
+ VAR_NAME_SCHEMA,
18
+ VAR_FACTORY_SCHEMA
19
+ ])).refine((entries)=>{
20
+ const names = entries.filter((entry)=>'string' == typeof entry);
21
+ return new Set(names).size === names.length;
22
+ }, {
20
23
  message: 'Variable names must be unique.'
21
24
  });
22
25
  function assertVarsFit(varsCount, size) {
23
26
  if (varsCount > size) throw new PortoError('mappingInvalid', 'Vars contain more entries than the block contains.');
24
27
  }
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,
28
+ function mapVarEntry(entry, block, index) {
29
+ const port = block.getPort(index);
30
+ if ('string' == typeof entry) return [
31
+ [
32
+ entry,
30
33
  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);
34
+ ]
35
+ ];
36
+ try {
37
+ const variables = entry(block.index, port, index);
38
+ if (types.isPromise(variables)) variables.catch(()=>{});
39
+ return VAR_RESULT_SCHEMA.parse(variables);
40
+ } catch (error) {
41
+ throw new PortoError('mappingInvalid', `Cannot create variables for entry ${index + 1}; expected an object with valid names and single-line strings or finite numbers.`, {
42
+ cause: error
43
+ });
44
+ }
45
+ }
46
+ function mapBlockToVars(block, entries) {
47
+ const environment = new Map();
48
+ for (const [index, entry] of entries.entries())for (const [name, value] of mapVarEntry(entry, block, index)){
49
+ if (environment.has(name)) throw new PortoError('mappingInvalid', `Variable names must be unique. Duplicate: ${name}.`);
50
+ environment.set(name, value);
51
+ }
52
+ return Object.fromEntries(environment);
46
53
  }
47
54
  function parseVar(entry) {
48
55
  const separator = entry.indexOf(':');
49
56
  const name = (-1 === separator ? entry : entry.slice(0, separator)).trim();
50
- if (-1 === separator) return name;
57
+ if (-1 === separator) return {
58
+ name
59
+ };
51
60
  const template = entry.slice(separator + 1);
52
61
  const result = VAR_TEXT_SCHEMA.safeParse(template);
53
62
  if (!result.success) throw new PortoError('mappingInvalid', 'Vars templates must fit on one line.', {
54
63
  cause: result.error
55
64
  });
56
65
  return {
57
- [name] (block, port) {
58
- return template.replaceAll('{{block}}', String(block)).replaceAll('{{port}}', String(port));
59
- }
66
+ name,
67
+ template
60
68
  };
61
69
  }
62
70
  function parseVarEntries(vars) {
@@ -67,6 +75,13 @@ function parseVarEntries(vars) {
67
75
  return result.data;
68
76
  }
69
77
  function parseVars(source) {
70
- return parseVarEntries(source.split(';').map((entry)=>parseVar(entry)));
78
+ const entries = source.split(';').map((entry)=>parseVar(entry));
79
+ parseVarEntries(entries.map(({ name })=>name));
80
+ return entries.map(({ name, template })=>{
81
+ if (void 0 === template) return name;
82
+ return (block, port)=>({
83
+ [name]: template.replaceAll('{{block}}', String(block)).replaceAll('{{port}}', String(port))
84
+ });
85
+ });
71
86
  }
72
87
  export { VARS_SCHEMA, assertVarsFit, mapBlockToVars, parseVarEntries, parseVars };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@b12k/porto",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Port range allocator for git worktrees",
5
5
  "keywords": [
6
6
  "cli",