Skip to content

Commit 62ae8c3

Browse files
committed
init
0 parents  commit 62ae8c3

File tree

24 files changed

+3315
-0
lines changed

24 files changed

+3315
-0
lines changed

.gitignore

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
### https://raw.github.com/github/gitignore/608690d6b9a78c2a003affc792e49a84905b3118/Node.gitignore
2+
3+
# Logs
4+
logs
5+
*.log
6+
7+
# Runtime data
8+
pids
9+
*.pid
10+
*.seed
11+
12+
# Directory for instrumented libs generated by jscoverage/JSCover
13+
lib-cov
14+
15+
# Coverage directory used by tools like istanbul
16+
coverage
17+
18+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
19+
.grunt
20+
21+
# node-waf configuration
22+
.lock-wscript
23+
24+
# Compiled binary addons (http://nodejs.org/api/addons.html)
25+
build/Release
26+
27+
# Dependency directory
28+
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-
29+
node_modules
30+
31+
# Debug log from npm
32+
npm-debug.log
33+
34+
35+
### https://raw.github.com/github/gitignore/608690d6b9a78c2a003affc792e49a84905b3118/Global/JetBrains.gitignore
36+
37+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm
38+
39+
*.iml
40+
41+
## Directory-based project format:
42+
.idea/
43+
# if you remove the above rule, at least ignore the following:
44+
45+
# User-specific stuff:
46+
# .idea/workspace.xml
47+
# .idea/tasks.xml
48+
# .idea/dictionaries
49+
50+
# Sensitive or high-churn files:
51+
# .idea/dataSources.ids
52+
# .idea/dataSources.xml
53+
# .idea/sqlDataSources.xml
54+
# .idea/dynamic.xml
55+
# .idea/uiDesigner.xml
56+
57+
# Gradle:
58+
# .idea/gradle.xml
59+
# .idea/libraries
60+
61+
# Mongo Explorer plugin:
62+
# .idea/mongoSettings.xml
63+
64+
## File-based project format:
65+
*.ipr
66+
*.iws
67+
68+
## Plugin-specific files:
69+
70+
# IntelliJ
71+
out/
72+
73+
# mpeltonen/sbt-idea plugin
74+
.idea_modules/
75+
76+
# JIRA plugin
77+
atlassian-ide-plugin.xml
78+
79+
# Crashlytics plugin (for Android Studio and IntelliJ)
80+
com_crashlytics_export_strings.xml
81+
crashlytics.properties
82+
crashlytics-build.properties
83+
84+
85+
/lib

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
sudo: false
2+
language: node_js
3+
node_js: "stable"

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2018 azu
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.

README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# @js-primer/local-server
2+
3+
Local Server for [js-primer](https://github.com/asciidwango/js-primer "js-primer").
4+
5+
### Requirements
6+
7+
- Node.js 8.2 >=
8+
- It contains `npm` 5.3.0>=
9+
- It contains `npx` command
10+
11+
## Usage
12+
13+
`npx`コマンドを使い直接ダウンロードと実行ができます。
14+
15+
Usage
16+
$ npx @js-primer/local-server [<directory>]
17+
18+
Options:
19+
--port TCP port at which the files will be served
20+
21+
サーバを起動する(ベースディレクトリは現在ディレクトリ)
22+
23+
npx @js-primer/local-server
24+
25+
指定したディレクトリをベースにサーバを起動する
26+
27+
npx @js-primer/local-server ./docs
28+
29+
### Usage for old Node.js
30+
31+
NOde.js 8.2未満の場合は[npm](https://www.npmjs.com/)でインストールし利用できます。
32+
33+
npm install local-server --global
34+
$ js-primer-local-server
35+
36+
37+
## Changelog
38+
39+
See [Releases page](https://github.com/js-primer/local-server/releases).
40+
41+
## Running tests
42+
43+
Install devDependencies and Run `npm test`:
44+
45+
npm i -d && npm test
46+
47+
## Contributing
48+
49+
Pull requests and stars are always welcome.
50+
51+
For bugs and feature requests, [please create an issue](https://github.com/js-primer/local-server/issues).
52+
53+
1. Fork it!
54+
2. Create your feature branch: `git checkout -b my-new-feature`
55+
3. Commit your changes: `git commit -am 'Add some feature'`
56+
4. Push to the branch: `git push origin my-new-feature`
57+
5. Submit a pull request :D
58+
59+
## Author
60+
61+
- [github/azu](https://github.com/azu)
62+
- [twitter/azu_re](https://twitter.com/azu_re)
63+
64+
## License
65+
66+
MIT © azu

bin/cmd.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env node
2+
const cli = require("../lib/cli");
3+
cli.run();

package.json

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
{
2+
"name": "@js-primer/local-server",
3+
"version": "0.0.0",
4+
"description": "Local Server for js-primer.",
5+
"keywords": [
6+
"js-primer",
7+
"local-server",
8+
"server"
9+
],
10+
"homepage": "https://github.com/js-primer/local-server",
11+
"bugs": {
12+
"url": "https://github.com/js-primer/local-server/issues"
13+
},
14+
"license": "MIT",
15+
"author": "azu",
16+
"files": [
17+
"bin/",
18+
"lib/",
19+
"src/"
20+
],
21+
"main": "lib/local-server.js",
22+
"types": "lib/local-server.d.ts",
23+
"bin": {
24+
"js-primer-local-server": "./bin/cmd.js"
25+
},
26+
"directories": {
27+
"lib": "lib",
28+
"test": "test"
29+
},
30+
"repository": {
31+
"type": "git",
32+
"url": "https://github.com/js-primer/local-server.git"
33+
},
34+
"scripts": {
35+
"build": "cross-env NODE_ENV=production tsc -p .",
36+
"precommit": "lint-staged",
37+
"postcommit": "git reset",
38+
"prepublish": "npm run --if-present build",
39+
"test": "mocha \"test/**/*.ts\"",
40+
"prettier": "prettier --write \"**/*.{js,jsx,ts,tsx,css}\"",
41+
"watch": "tsc -p . --watch"
42+
},
43+
"prettier": {
44+
"printWidth": 120,
45+
"singleQuote": false,
46+
"tabWidth": 4
47+
},
48+
"dependencies": {
49+
"chalk": "^2.4.1",
50+
"connect": "^3.6.6",
51+
"detect-port": "^1.2.3",
52+
"log-symbols": "^2.2.0",
53+
"meow": "^5.0.0",
54+
"serve-static": "^1.13.2"
55+
},
56+
"devDependencies": {
57+
"@types/chalk": "^2.2.0",
58+
"@types/connect": "^3.4.32",
59+
"@types/detect-port": "^1.1.0",
60+
"@types/meow": "^4.0.1",
61+
"@types/mocha": "^5.2.0",
62+
"@types/node": "^10.1.1",
63+
"@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"
74+
},
75+
"publishConfig": {
76+
"access": "public"
77+
},
78+
"lint-staged": {
79+
"*.{js,jsx,ts,tsx,css}": [
80+
"prettier --write",
81+
"git add"
82+
]
83+
}
84+
}

src/cli.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import meow = require("meow");
2+
import { LocalServer } from "./local-server";
3+
4+
export const run = () => {
5+
const cli = meow(
6+
`
7+
Usage
8+
$ npx @js-primer/local-server [<directory>]
9+
10+
Options:
11+
--port TCP port at which the files will be served
12+
13+
Examples
14+
$ npx @js-primer/local-server
15+
$ npx @js-primer/local-server ./docs
16+
`,
17+
{
18+
flags: {
19+
port: {
20+
type: "string"
21+
}
22+
}
23+
}
24+
);
25+
26+
const server = new LocalServer({
27+
rootDir: cli.flags.input.length > 0 ? cli.flags.input[0] : process.cwd(),
28+
port: cli.flags.port !== undefined ? Number(cli.flags.port) : 3000
29+
});
30+
return server
31+
.start()
32+
.then(() => {})
33+
.catch(error => {
34+
console.error(error.message);
35+
});
36+
};

src/local-server.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import * as http from "http";
2+
import * as path from "path";
3+
import connect = require("connect");
4+
import serveStatic = require("serve-static");
5+
import { HandleFunction } from "connect";
6+
import detectPort = require("detect-port");
7+
import { responseLog } from "./middlewares/response-log";
8+
9+
const logSymbols = require("log-symbols");
10+
11+
export interface LocalServerOptions {
12+
rootDir?: string;
13+
port?: number;
14+
}
15+
16+
export class LocalServer {
17+
public server!: http.Server;
18+
private rootDir: string;
19+
private port: number;
20+
21+
constructor(options: LocalServerOptions) {
22+
this.rootDir = path.normalize(path.resolve(options.rootDir || "."));
23+
this.port = options.port || 3000;
24+
}
25+
26+
start() {
27+
return detectPort(this.port).then(newPort => {
28+
if (this.port !== newPort) {
29+
console.log(`${logSymbols.warning} Port:${this.port} is already used. Lookup next available port.`);
30+
}
31+
const serve = serveStatic(this.rootDir, { index: ["index.html", "index.htm"] }) as HandleFunction;
32+
this.server = connect()
33+
.use(responseLog())
34+
.use(serve)
35+
.listen(newPort, function() {
36+
console.log(`${logSymbols.info} Open http://localhost:${newPort}`);
37+
});
38+
return this.server;
39+
});
40+
}
41+
42+
stop() {
43+
if (this.server) {
44+
this.server.close();
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)