Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add host config #57

Merged
merged 9 commits into from Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/commands/preview.ts
@@ -1,6 +1,7 @@
import { config } from "../lib/config";
import { getFileSystemRepo } from "../lib/get-file-system-repo";
import { getQiitaApiInstance } from "../lib/get-qiita-api-instance";
import { getUrlAddress } from "../lib/getUrlAddress";
import { syncArticlesFromQiita } from "../lib/sync-articles-from-qiita";
import { startLocalChangeWatcher, startServer } from "../server/app";

Expand All @@ -13,9 +14,11 @@ export const preview = async () => {
const server = await startServer();

const address = server.address();
if (address && typeof address !== "string") {
const url = getUrlAddress(address);

if (url) {
const open = (await import("open")).default;
await open(`http://localhost:${address.port}`);
await open(url);
}

startLocalChangeWatcher({
Expand Down
6 changes: 6 additions & 0 deletions src/lib/config.test.ts
Expand Up @@ -224,6 +224,8 @@ describe("config", () => {
beforeEach(() => {
const userConfigData = {
includePrivate: true,
address: "localhost",
family: "IPv4",
port: 9999,
};
resetFiles();
Expand All @@ -234,6 +236,8 @@ describe("config", () => {
const userConfig = await config.getUserConfig();
expect(userConfig).toStrictEqual({
includePrivate: true,
address: "localhost",
family: "IPv4",
port: 9999,
});
});
Expand All @@ -248,6 +252,8 @@ describe("config", () => {
const userConfig = await config.getUserConfig();
expect(userConfig).toStrictEqual({
includePrivate: false,
address: "localhost",
family: "IPv4",
port: 8888,
});
});
Expand Down
4 changes: 4 additions & 0 deletions src/lib/config.ts
Expand Up @@ -14,6 +14,8 @@ interface Options {

type UserConfig = {
includePrivate: boolean;
address: string;
ikepu-tp marked this conversation as resolved.
Show resolved Hide resolved
family: string;
ikepu-tp marked this conversation as resolved.
Show resolved Hide resolved
port: number;
};

Expand Down Expand Up @@ -107,6 +109,8 @@ class Config {
async getUserConfig() {
const defaultConfig = {
includePrivate: false,
address: "localhost",
family: "IPv4",
port: 8888,
} as UserConfig;

Expand Down
42 changes: 42 additions & 0 deletions src/lib/getUrlAddress.test.ts
@@ -0,0 +1,42 @@
import { AddressInfo } from "net";
import { getUrlAddress } from "./getUrlAddress";

describe("getUrlAddress", () => {
describe("when null is passed", () => {
it("returns null", () => {
const url = getUrlAddress(null);
expect(url).toBeNull();
});
});

describe("when string is passed", () => {
it("returns null", () => {
const url = getUrlAddress("foobar");
expect(url).toBeNull();
});
});

describe("when IPv4 is passed", () => {
it("returns correct url", () => {
const address: AddressInfo = {
address: "0.0.0.0",
family: "IPv4",
port: 8888,
};
const url = getUrlAddress(address);
expect(url).toEqual(`http://${address.address}:${address.port}`);
});
});

describe("when IPv6 is passed", () => {
it("returns correct url", () => {
const address: AddressInfo = {
address: "::",
family: "IPv6",
port: 8888,
};
const url = getUrlAddress(address);
expect(url).toEqual(`http://[${address.address}]:${address.port}`);
});
});
});
12 changes: 12 additions & 0 deletions src/lib/getUrlAddress.ts
@@ -0,0 +1,12 @@
import { AddressInfo } from "net";

export const getUrlAddress = (address: string | AddressInfo | null) => {
if (!address || typeof address === "string") return null;

if (["IPv4", "IPv6"].indexOf(address.family) === -1)
throw new Error("Unknown address family");

return `http://${
address.family === "IPv4" ? address.address : `[${address.address}]`
}:${address.port}`;
};
7 changes: 6 additions & 1 deletion src/server/app.ts
ikepu-tp marked this conversation as resolved.
Show resolved Hide resolved
Expand Up @@ -10,6 +10,7 @@ import { EmojiRouter } from "./api/emoji";
import { ItemsRouter } from "./api/items";
import { ReadmeRouter } from "./api/readme";
import { config } from "../lib/config";
import { getUrlAddress } from "../lib/getUrlAddress";

export async function startServer() {
const app = express();
Expand Down Expand Up @@ -39,7 +40,11 @@ export async function startServer() {
server
.listen(port, host)
.once("listening", () => {
console.log(`Preview: http://${host}:${port}`);
const address = server.address();
const url = getUrlAddress(address);
if (url) {
console.log(`Preview: ${url}`);
}

resolve(server);
})
Expand Down