File tree Expand file tree Collapse file tree 7 files changed +72
-4
lines changed Expand file tree Collapse file tree 7 files changed +72
-4
lines changed Original file line number Diff line number Diff line change @@ -229,6 +229,7 @@ npx qiita version
229
229
設定できるオプションは以下の通りです。
230
230
231
231
- includePrivate: 限定共有記事を含めるかどうかを選べます。デフォルトは` false ` です。
232
+ - host: ` qiita preview ` コマンドで利用するホストを指定できます。デフォルトは` localhost ` です。
232
233
- port: ` qiita preview ` コマンドで利用するポートを指定できます。デフォルトは` 8888 ` です。
233
234
234
235
## オプション
Original file line number Diff line number Diff line change 1
1
import { config } from "../lib/config" ;
2
2
import { getFileSystemRepo } from "../lib/get-file-system-repo" ;
3
3
import { getQiitaApiInstance } from "../lib/get-qiita-api-instance" ;
4
+ import { getUrlAddress } from "../lib/getUrlAddress" ;
4
5
import { syncArticlesFromQiita } from "../lib/sync-articles-from-qiita" ;
5
6
import { startLocalChangeWatcher , startServer } from "../server/app" ;
6
7
@@ -13,9 +14,11 @@ export const preview = async () => {
13
14
const server = await startServer ( ) ;
14
15
15
16
const address = server . address ( ) ;
16
- if ( address && typeof address !== "string" ) {
17
+ const url = getUrlAddress ( address ) ;
18
+
19
+ if ( url ) {
17
20
const open = ( await import ( "open" ) ) . default ;
18
- await open ( `http://localhost: ${ address . port } ` ) ;
21
+ await open ( url ) ;
19
22
}
20
23
21
24
startLocalChangeWatcher ( {
Original file line number Diff line number Diff line change @@ -224,6 +224,7 @@ describe("config", () => {
224
224
beforeEach ( ( ) => {
225
225
const userConfigData = {
226
226
includePrivate : true ,
227
+ host : "localhost" ,
227
228
port : 9999 ,
228
229
} ;
229
230
resetFiles ( ) ;
@@ -234,6 +235,7 @@ describe("config", () => {
234
235
const userConfig = await config . getUserConfig ( ) ;
235
236
expect ( userConfig ) . toStrictEqual ( {
236
237
includePrivate : true ,
238
+ host : "localhost" ,
237
239
port : 9999 ,
238
240
} ) ;
239
241
} ) ;
@@ -248,6 +250,7 @@ describe("config", () => {
248
250
const userConfig = await config . getUserConfig ( ) ;
249
251
expect ( userConfig ) . toStrictEqual ( {
250
252
includePrivate : false ,
253
+ host : "localhost" ,
251
254
port : 8888 ,
252
255
} ) ;
253
256
} ) ;
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ interface Options {
14
14
15
15
type UserConfig = {
16
16
includePrivate : boolean ;
17
+ host : string ;
17
18
port : number ;
18
19
} ;
19
20
@@ -107,6 +108,7 @@ class Config {
107
108
async getUserConfig ( ) {
108
109
const defaultConfig = {
109
110
includePrivate : false ,
111
+ host : "localhost" ,
110
112
port : 8888 ,
111
113
} as UserConfig ;
112
114
Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ import { EmojiRouter } from "./api/emoji";
10
10
import { ItemsRouter } from "./api/items" ;
11
11
import { ReadmeRouter } from "./api/readme" ;
12
12
import { config } from "../lib/config" ;
13
+ import { getUrlAddress } from "../lib/getUrlAddress" ;
13
14
14
15
export async function startServer ( ) {
15
16
const app = express ( ) ;
@@ -33,13 +34,17 @@ export async function startServer() {
33
34
const server = createServer ( app ) ;
34
35
const userConfig = await config . getUserConfig ( ) ;
35
36
const port = userConfig . port ;
36
- const host = "localhost" ;
37
+ const host = userConfig . host ;
37
38
38
39
return new Promise < Server > ( ( resolve , reject ) => {
39
40
server
40
41
. listen ( port , host )
41
42
. 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
+ }
43
48
44
49
resolve ( server ) ;
45
50
} )
You can’t perform that action at this time.
0 commit comments