Skip to content

add host config #57

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

Merged
merged 9 commits into from
Sep 5, 2023
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ npx qiita version
設定できるオプションは以下の通りです。

- includePrivate: 限定共有記事を含めるかどうかを選べます。デフォルトは`false`です。
- host: `qiita preview`コマンドで利用するホストを指定できます。デフォルトは`localhost`です。
- port: `qiita preview`コマンドで利用するポートを指定できます。デフォルトは`8888`です。

## オプション
Expand Down
7 changes: 5 additions & 2 deletions src/commands/preview.ts
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions src/lib/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ describe("config", () => {
beforeEach(() => {
const userConfigData = {
includePrivate: true,
host: "localhost",
port: 9999,
};
resetFiles();
Expand All @@ -234,6 +235,7 @@ describe("config", () => {
const userConfig = await config.getUserConfig();
expect(userConfig).toStrictEqual({
includePrivate: true,
host: "localhost",
port: 9999,
});
});
Expand All @@ -248,6 +250,7 @@ describe("config", () => {
const userConfig = await config.getUserConfig();
expect(userConfig).toStrictEqual({
includePrivate: false,
host: "localhost",
port: 8888,
});
});
Expand Down
2 changes: 2 additions & 0 deletions src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface Options {

type UserConfig = {
includePrivate: boolean;
host: string;
port: number;
};

Expand Down Expand Up @@ -107,6 +108,7 @@ class Config {
async getUserConfig() {
const defaultConfig = {
includePrivate: false,
host: "localhost",
port: 8888,
} as UserConfig;

Expand Down
42 changes: 42 additions & 0 deletions src/lib/getUrlAddress.test.ts
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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}`;
};
9 changes: 7 additions & 2 deletions src/server/app.ts
Original file line number Diff line number Diff line change
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 All @@ -33,13 +34,17 @@ export async function startServer() {
const server = createServer(app);
const userConfig = await config.getUserConfig();
const port = userConfig.port;
const host = "localhost";
const host = userConfig.host;

return new Promise<Server>((resolve, reject) => {
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