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
229229設定できるオプションは以下の通りです。
230230
231231- includePrivate: 限定共有記事を含めるかどうかを選べます。デフォルトは` false ` です。
232+ - host: ` qiita preview ` コマンドで利用するホストを指定できます。デフォルトは` localhost ` です。
232233- port: ` qiita preview ` コマンドで利用するポートを指定できます。デフォルトは` 8888 ` です。
233234
234235## オプション
Original file line number Diff line number Diff line change 11import { config } from "../lib/config" ;
22import { getFileSystemRepo } from "../lib/get-file-system-repo" ;
33import { getQiitaApiInstance } from "../lib/get-qiita-api-instance" ;
4+ import { getUrlAddress } from "../lib/getUrlAddress" ;
45import { syncArticlesFromQiita } from "../lib/sync-articles-from-qiita" ;
56import { 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 ( {
Original file line number Diff line number Diff 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 } ) ;
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ interface Options {
1414
1515type 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
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";
1010import { ItemsRouter } from "./api/items" ;
1111import { ReadmeRouter } from "./api/readme" ;
1212import { config } from "../lib/config" ;
13+ import { getUrlAddress } from "../lib/getUrlAddress" ;
1314
1415export 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 } )
You can’t perform that action at this time.
0 commit comments