Skip to content

Commit e07c04e

Browse files
authored
Merge pull request #57 from ikepu-tp/main
add host config
2 parents fcf9402 + 13bb561 commit e07c04e

File tree

7 files changed

+72
-4
lines changed

7 files changed

+72
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ npx qiita version
229229
設定できるオプションは以下の通りです。
230230

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

234235
## オプション

src/commands/preview.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { config } from "../lib/config";
22
import { getFileSystemRepo } from "../lib/get-file-system-repo";
33
import { getQiitaApiInstance } from "../lib/get-qiita-api-instance";
4+
import { getUrlAddress } from "../lib/getUrlAddress";
45
import { syncArticlesFromQiita } from "../lib/sync-articles-from-qiita";
56
import { startLocalChangeWatcher, startServer } from "../server/app";
67

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

1516
const address = server.address();
16-
if (address && typeof address !== "string") {
17+
const url = getUrlAddress(address);
18+
19+
if (url) {
1720
const open = (await import("open")).default;
18-
await open(`http://localhost:${address.port}`);
21+
await open(url);
1922
}
2023

2124
startLocalChangeWatcher({

src/lib/config.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ describe("config", () => {
224224
beforeEach(() => {
225225
const userConfigData = {
226226
includePrivate: true,
227+
host: "localhost",
227228
port: 9999,
228229
};
229230
resetFiles();
@@ -234,6 +235,7 @@ describe("config", () => {
234235
const userConfig = await config.getUserConfig();
235236
expect(userConfig).toStrictEqual({
236237
includePrivate: true,
238+
host: "localhost",
237239
port: 9999,
238240
});
239241
});
@@ -248,6 +250,7 @@ describe("config", () => {
248250
const userConfig = await config.getUserConfig();
249251
expect(userConfig).toStrictEqual({
250252
includePrivate: false,
253+
host: "localhost",
251254
port: 8888,
252255
});
253256
});

src/lib/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ interface Options {
1414

1515
type UserConfig = {
1616
includePrivate: boolean;
17+
host: string;
1718
port: number;
1819
};
1920

@@ -107,6 +108,7 @@ class Config {
107108
async getUserConfig() {
108109
const defaultConfig = {
109110
includePrivate: false,
111+
host: "localhost",
110112
port: 8888,
111113
} as UserConfig;
112114

src/lib/getUrlAddress.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { AddressInfo } from "net";
2+
import { getUrlAddress } from "./getUrlAddress";
3+
4+
describe("getUrlAddress", () => {
5+
describe("when null is passed", () => {
6+
it("returns null", () => {
7+
const url = getUrlAddress(null);
8+
expect(url).toBeNull();
9+
});
10+
});
11+
12+
describe("when string is passed", () => {
13+
it("returns null", () => {
14+
const url = getUrlAddress("foobar");
15+
expect(url).toBeNull();
16+
});
17+
});
18+
19+
describe("when IPv4 is passed", () => {
20+
it("returns correct url", () => {
21+
const address: AddressInfo = {
22+
address: "0.0.0.0",
23+
family: "IPv4",
24+
port: 8888,
25+
};
26+
const url = getUrlAddress(address);
27+
expect(url).toEqual(`http://${address.address}:${address.port}`);
28+
});
29+
});
30+
31+
describe("when IPv6 is passed", () => {
32+
it("returns correct url", () => {
33+
const address: AddressInfo = {
34+
address: "::",
35+
family: "IPv6",
36+
port: 8888,
37+
};
38+
const url = getUrlAddress(address);
39+
expect(url).toEqual(`http://[${address.address}]:${address.port}`);
40+
});
41+
});
42+
});

src/lib/getUrlAddress.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { AddressInfo } from "net";
2+
3+
export const getUrlAddress = (address: string | AddressInfo | null) => {
4+
if (!address || typeof address === "string") return null;
5+
6+
if (["IPv4", "IPv6"].indexOf(address.family) === -1)
7+
throw new Error("Unknown address family");
8+
9+
return `http://${
10+
address.family === "IPv4" ? address.address : `[${address.address}]`
11+
}:${address.port}`;
12+
};

src/server/app.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { EmojiRouter } from "./api/emoji";
1010
import { ItemsRouter } from "./api/items";
1111
import { ReadmeRouter } from "./api/readme";
1212
import { config } from "../lib/config";
13+
import { getUrlAddress } from "../lib/getUrlAddress";
1314

1415
export async function startServer() {
1516
const app = express();
@@ -33,13 +34,17 @@ export async function startServer() {
3334
const server = createServer(app);
3435
const userConfig = await config.getUserConfig();
3536
const port = userConfig.port;
36-
const host = "localhost";
37+
const host = userConfig.host;
3738

3839
return new Promise<Server>((resolve, reject) => {
3940
server
4041
.listen(port, host)
4142
.once("listening", () => {
42-
console.log(`Preview: http://${host}:${port}`);
43+
const address = server.address();
44+
const url = getUrlAddress(address);
45+
if (url) {
46+
console.log(`Preview: ${url}`);
47+
}
4348

4449
resolve(server);
4550
})

0 commit comments

Comments
 (0)