Skip to content

Commit 8817726

Browse files
authored
refactor(local-server): Ctrl+Cでローカルサーバを終了できるメッセージを追加 (#2)
refactor(local-server): Ctrl+Cでローカルサーバを終了できるメッセージを追加
2 parents 400c36a + 51ea0f5 commit 8817726

File tree

8 files changed

+1220
-1750
lines changed

8 files changed

+1220
-1750
lines changed

.travis.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
sudo: false
22
language: node_js
3-
node_js: "stable"
3+
node_js:
4+
- "10"
5+
- "node"
6+
# https://travis-ci.community/t/timeout-after-build-finished-and-succeeded/1336
7+
env:
8+
- YARN_GPG=no
9+
os:
10+
- linux
11+
- windows

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,20 @@ Local Server for [js-primer](https://github.com/asciidwango/js-primer "js-primer
2525
指定したディレクトリをベースにサーバを起動する
2626

2727
npx @js-primer/local-server ./docs
28-
28+
29+
指定したポート番号でサーバを起動する
30+
31+
npx @js-primer/local-server --port 8000
32+
33+
起動したローカルサーバは、コマンドラインで<kbd>Ctrl+C</kbd>のショートカットを押下することで終了できます。
34+
2935
### Usage for old Node.js
3036

31-
NOde.js 8.2未満の場合は[npm](https://www.npmjs.com/)でインストールし利用できます
37+
Node.js 8.2未満の場合は[npm](https://www.npmjs.com/)でインストールすることで利用できます
3238

3339
npm install local-server --global
3440
$ js-primer-local-server
3541

36-
3742
## Changelog
3843

3944
See [Releases page](https://github.com/js-primer/local-server/releases).

package.json

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,31 +46,31 @@
4646
"tabWidth": 4
4747
},
4848
"dependencies": {
49-
"chalk": "^2.4.1",
50-
"connect": "^3.6.6",
51-
"detect-port": "^1.2.3",
52-
"log-symbols": "^2.2.0",
49+
"chalk": "^2.4.2",
50+
"connect": "^3.7.0",
51+
"detect-port": "^1.3.0",
52+
"log-symbols": "^3.0.0",
5353
"meow": "^5.0.0",
54-
"serve-static": "^1.13.2"
54+
"serve-static": "^1.14.1"
5555
},
5656
"devDependencies": {
5757
"@types/chalk": "^2.2.0",
5858
"@types/connect": "^3.4.32",
5959
"@types/detect-port": "^1.1.0",
60-
"@types/meow": "^4.0.1",
61-
"@types/mocha": "^5.2.0",
62-
"@types/node": "^10.1.1",
60+
"@types/meow": "^5.0.0",
61+
"@types/mocha": "^5.2.7",
62+
"@types/node": "^12.6.2",
6363
"@types/serve-static": "^1.13.2",
64-
"@types/supertest": "^2.0.4",
65-
"cross-env": "^5.1.5",
66-
"husky": "^0.14.3",
67-
"lint-staged": "^7.1.0",
68-
"mocha": "^5.1.1",
69-
"prettier": "^1.12.1",
70-
"supertest": "^3.1.0",
71-
"ts-node": "^6.0.3",
72-
"ts-node-test-register": "^3.0.0",
73-
"typescript": "^2.8.3"
64+
"@types/supertest": "^2.0.8",
65+
"cross-env": "^5.2.0",
66+
"husky": "^3.0.0",
67+
"lint-staged": "^9.2.0",
68+
"mocha": "^6.1.4",
69+
"prettier": "^1.18.2",
70+
"supertest": "^4.0.2",
71+
"ts-node": "^8.3.0",
72+
"ts-node-test-register": "^8.0.1",
73+
"typescript": "^3.5.3"
7474
},
7575
"publishConfig": {
7676
"access": "public"

src/cli.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,21 @@ export const run = () => {
1919
port: {
2020
type: "string"
2121
}
22-
}
22+
},
23+
autoVersion: true,
24+
autoHelp: true
2325
}
2426
);
2527

2628
const server = new LocalServer({
2729
rootDir: cli.input.length > 0 ? cli.input[0] : process.cwd(),
2830
port: cli.flags.port !== undefined ? Number(cli.flags.port) : 3000
2931
});
32+
// Ctrl + C を押したときにメッセージを出す
33+
process.on("SIGINT", function() {
34+
server.stop();
35+
process.exit();
36+
});
3037
return server
3138
.start()
3239
.then(() => {})

src/local-server.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import { HandleFunction } from "connect";
66
import detectPort = require("detect-port");
77
import { responseLog } from "./middlewares/response-log";
88
import chalk from "chalk";
9-
10-
const logSymbols = require("log-symbols");
9+
import logSymbols from "log-symbols";
1110

1211
export interface LocalServerOptions {
1312
rootDir?: string;
@@ -24,36 +23,42 @@ export class LocalServer {
2423
this.port = options.port || 3000;
2524
}
2625

26+
get appName() {
27+
return this.rootDir ? path.basename(this.rootDir) : "app";
28+
}
29+
2730
start() {
2831
return detectPort(this.port).then(newPort => {
2932
if (this.port !== newPort) {
3033
console.log(
31-
`${logSymbols.warning} ポート番号:${
32-
this.port
33-
}はすでに使われています。利用できる別のポート番号を探索中です。`
34+
`${logSymbols.warning} ポート番号:${this.port}はすでに使われています。利用できる別のポート番号を探索中です。`
3435
);
3536
}
3637
const serve = serveStatic(this.rootDir, { index: ["index.html", "index.htm"] }) as HandleFunction;
3738
this.server = connect()
3839
.use(responseLog())
3940
.use(serve)
4041
.listen(newPort, () => {
41-
const appName = path.basename(this.rootDir) || "app";
4242
console.log(`
43-
${chalk.underline(appName)}のローカルサーバを起動しました。
43+
${chalk.underline(this.appName)}のローカルサーバを起動しました。
4444
次のURLをブラウザで開いてください。
4545
4646
URL: ${chalk.underline(`http://localhost:${newPort}`)}
4747
48+
Ctrl+Cのショートカットを押下することでローカルサーバを終了できます。
4849
`);
4950
});
5051
return this.server;
5152
});
5253
}
5354

5455
stop() {
55-
if (this.server) {
56-
this.server.close();
56+
if (!this.server) {
57+
return;
5758
}
59+
this.server.close();
60+
console.log(`
61+
${chalk.underline(this.appName)}のローカルサーバを終了しました。
62+
`);
5863
}
5964
}

test/test.js renamed to test/test.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
1-
const assert = require("assert");
2-
const path = require("path");
3-
const request = require("supertest");
4-
const { LocalServer } = require("../src/local-server");
1+
import path from "path";
2+
import request from "supertest";
3+
import { LocalServer } from "../src/local-server";
4+
import * as http from "http";
5+
56
const fixtures = path.join(__dirname, "/fixtures");
67

7-
async function createServer(dir) {
8+
async function createServer(dir?: string) {
89
const server = new LocalServer({
910
rootDir: dir || fixtures
1011
});
1112
await server.start();
12-
return server.server;
13+
return server;
1314
}
1415

1516
describe("LocalServer", function() {
1617
describe("basic operations", function() {
17-
let server;
18+
let localServer: LocalServer;
19+
let server: http.Server;
1820
before(async function() {
19-
server = await createServer();
21+
localServer = await createServer();
22+
server = localServer.server;
2023
});
2124
after(() => {
22-
return server.close();
25+
return localServer.stop();
2326
});
2427

2528
it("should serve static files", function(done) {
@@ -96,7 +99,7 @@ describe("LocalServer", function() {
9699
}
97100
request(server)
98101
.get("/todo.txt")
99-
.set("If-None-Match", res.headers.etag)
102+
.set("If-None-Match", (res as any).headers.etag)
100103
.expect(304, done);
101104
});
102105
});

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"target": "es5",
99
"sourceMap": true,
1010
"declaration": true,
11+
"esModuleInterop": true,
1112
"jsx": "preserve",
1213
"lib": [
1314
"es2017",
@@ -32,4 +33,4 @@
3233
".git",
3334
"node_modules"
3435
]
35-
}
36+
}

0 commit comments

Comments
 (0)