Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,6 @@ examples/**/package-lock.json
examples/.npm-local/*

.DS_Store

# combinator
.combinator
9 changes: 9 additions & 0 deletions examples/vite-project/combinator.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"apps": [
{
"name": "vite",
"cmd": "vite --port=${port} --strictPort",
"path": "/vite"
}
]
}
3 changes: 2 additions & 1 deletion examples/vite-project/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview",
"combinator": "combinator-proxy"
"combinator-proxy": "combinator-proxy",
"combinator": "combinator"
},
"devDependencies": {
"typescript": "~5.7.2",
Expand Down
5 changes: 5 additions & 0 deletions examples/vite-project/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { UserConfig } from 'vite'

export default {
base: "/vite",
} satisfies UserConfig
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"dist"
],
"bin": {
"combinator-proxy": "dist/proxy.js"
"combinator-proxy": "dist/proxy.js",
"combinator": "dist/runner.js"
},
"scripts": {
"format:check": "prettier . --check",
Expand Down Expand Up @@ -47,6 +48,7 @@
"zx": "^8.5.3"
},
"dependencies": {
"concurrently": "^9.1.2",
"express": "^5.1.0",
"http-proxy-middleware": "^3.0.5",
"testcontainers": "^10.25.0",
Expand Down
61 changes: 61 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 71 additions & 0 deletions src/bin/runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import fs from "node:fs";
import path from "node:path";

import { parseArgs } from "@lib/runner/cli";
import { parseRunnerConfig } from "@lib/runner/config/parse";
import { panic } from "@lib/utils/panic";
import type { RunnerConfig } from "@lib/runner/config/schema";
import { getRandomPort } from "@lib/runner/get-random-port";
import { ProxyConfig } from "@lib/proxy/config/schema";
import concurrently from "concurrently";
import { getDirname } from "@lib/utils/get-dirname";

const proxyRoutingPath = ".combinator/routing.json";

const { port, file } = parseArgs();

console.log(`port=${port} file=${file}`);

const content = fs.readFileSync(file, { encoding: "utf8" });
const result = parseRunnerConfig(content);
if (result instanceof Error) {
panic(result.message);
}
const config: RunnerConfig = result;

console.log(config);

const appsWithPort = await Promise.all(
config.apps.map(async (app) => {
const port = await getRandomPort();
return {
name: app.name,
path: app.path,
port,
command: app.cmd.replace("${port}", port.toFixed(0)),
};
}),
);

console.log(appsWithPort);
const proxyConfig: ProxyConfig = {
routes: appsWithPort.map((app) => ({
path: app.path,
target: `http://localhost:${app.port}${app.path}`,
})),
};

try {
const dirname = path.dirname(proxyRoutingPath);
fs.rmSync(dirname, { recursive: true, force: true });
fs.mkdirSync(dirname);
fs.writeFileSync(proxyRoutingPath, JSON.stringify(proxyConfig, null, 2));
} catch (error) {
panic(error as string);
}

const __dirname = getDirname(import.meta.url);
console.log(__dirname);

concurrently(
[
{
command: `${__dirname}/proxy.js --port=${port} --file=${proxyRoutingPath}`,
name: "@proxy",
},
...appsWithPort,
],
{
killOthers: "failure",
},
);
2 changes: 1 addition & 1 deletion src/lib/proxy/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe("parseArgs", () => {
test("should default to `combinator-proxy.json` file when no fie argument is specified", () => {
process.argv = ["path/to/node", "path/to/js-executable"];
expect(parseArgs()).toMatchObject({
file: "combinator-proxy.json",
file: "./combinator-proxy.json",
});
});
});
2 changes: 1 addition & 1 deletion src/lib/proxy/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function parseArgs() {
alias: "f",
type: "string",
description: "path to config file",
default: "combinator-proxy.json",
default: "./combinator-proxy.json",
})
.option("port", {
alias: "p",
Expand Down
56 changes: 56 additions & 0 deletions src/lib/runner/cli.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { test, expect, describe, beforeAll, afterAll } from "vitest";
import { parseArgs } from "./cli";

describe("parseArgs", () => {
let originalArgv: string[];

beforeAll(() => {
originalArgv = process.argv;
});

afterAll(() => {
process.argv = originalArgv;
});

test("should accept numeric port argument", () => {
process.argv = ["path/to/node", "path/to/js-executable", "--port=1234"];
expect(parseArgs()).toMatchObject({
port: 1234,
});
});

test("should accept numeric port argument as shorthand `p`", () => {
process.argv = ["path/to/node", "path/to/js-executable", "-p", "1234"];
expect(parseArgs()).toMatchObject({
port: 1234,
});
});

test("should default to port 8080 when no port is specified", () => {
process.argv = ["path/to/node", "path/to/js-executable"];
expect(parseArgs()).toMatchObject({
port: 8080,
});
});

test("should accept path to config file argument", () => {
process.argv = ["path/to/node", "path/to/js-executable", "--file=/path/to/file"];
expect(parseArgs()).toMatchObject({
file: "/path/to/file",
});
});

test("should accept path to config file as shorthand `f`", () => {
process.argv = ["path/to/node", "path/to/js-executable", "-f", "/path/to/file"];
expect(parseArgs()).toMatchObject({
file: "/path/to/file",
});
});

test("should default to `combinator.json` file when no fie argument is specified", () => {
process.argv = ["path/to/node", "path/to/js-executable"];
expect(parseArgs()).toMatchObject({
file: "./combinator.json",
});
});
});
23 changes: 23 additions & 0 deletions src/lib/runner/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import yargs from "yargs";
import { hideBin } from "yargs/helpers";

export function parseArgs() {
const args = yargs(hideBin(process.argv))
.scriptName("combinator")
.usage("$0 [args]")
.option("file", {
alias: "f",
type: "string",
description: "path to config file",
default: "./combinator.json",
})
.option("port", {
alias: "p",
type: "number",
description: "port to use",
default: 8080,
})
.parseSync();

return args;
}
24 changes: 24 additions & 0 deletions src/lib/runner/config/parse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { ZodError } from "zod";
import { type RunnerConfig, schema } from "./schema";

/**
* Parses and validates raw string content to config object.
*
* Errors are formatted for stdout/stderr.
*/
export function parseRunnerConfig(strData: string): RunnerConfig | Error {
try {
const result = schema.parse(JSON.parse(strData));
delete result.$schema;
return result;
} catch (error) {
if (error instanceof ZodError) {
const issues = error.issues.map((issue) => `- ${issue.message} "${issue.path.join(".")}"`);
return new Error(`invalid config:\n${issues.join("\n")}`);
} else if (error instanceof SyntaxError) {
return new Error(`invalid config: not a valid json:\n${error.message}\nconfig: ${strData}`);
} else {
return new Error(`unexpected error: ${error}`);
}
}
}
Loading