Permalink
Browse files

worked vhost option

  • Loading branch information...
rhiokim committed Sep 16, 2012
1 parent e9658a0 commit f7471391e0e2ea390f841fee520885ffac71ec99
Showing with 83 additions and 2 deletions.
  1. +3 −0 README.md
  2. +5 −2 lib/locally.js
  3. +74 −0 lib/vhost.js
  4. +1 −0 test/locally-vhost-test.sh
View
@@ -91,6 +91,9 @@ Usage: _locally [options]
$ locally -f ./conf/.locally force adjust locally configuration file
```
+## How to user on Windows
+
+
##options
`locally` 는 기본적으로 실행 위치를 기준으로 `.locally` 파일로 미리 정의된 환경 옵션 셋을 지원한다.
View
@@ -1,7 +1,8 @@
var connect = require('connect'),
program = require('commander'),
fs = require('fs'),
- path = require('path');
+ path = require('path'),
+ vhost = require('./vhost');
/**
* process.argv pre process
@@ -14,14 +15,15 @@ program
.option('-d, --debug', 'debug mode')
.option('-p, --port <port>', 'public port', 8080)
.option('-f, --file <configuration>', 'set configuration file')
- .option('-c, --cname <virtual host>', 'set vhost information', 'localhost');
+ .option('-c, --vhost <virtual host>', 'set vhost information', 'localhost');
program.on('--help', function() {
console.log(' Examples:');
console.log('');
console.log(' $ locally -w ./public -p 8080 start basic locally server');
console.log(' $ locally -d debug mode, default <false>');
console.log(' $ locally -f ./conf/.locally force adjust locally configuration file');
+ console.log(' $ locally -vhost vhost.com virtual host');
console.log('');
});
@@ -90,6 +92,7 @@ function createServer() {
}
if (program.vhost) {
+ vhost[process.platform](program.vhost);
}
app.use(function(req, res, next) {
View
@@ -0,0 +1,74 @@
+var fs = require('fs'),
+ colors = require('colors');
+
+var header = '# locally vhost';
+
+function existHost(lines, vhost) {
+ var res = lines.every(function(line) {
+ return line.match(vhost) != null;
+ });
+}
+
+function loadFile(file) {
+ var res, lines, data
+
+ try {
+ data = fs.readFileSync(file, 'utf8');
+ } catch(e) {
+ return;
+ }
+
+ lines = data.split('\n');
+
+ return lines;
+}
+
+function saveFile(file, lines) {
+ fs.writeFileSync(file, lines.join('\n'), 'utf8');
+}
+
+function applyVirtualHost(file, vhost) {
+ var match, headline;
+
+ console.log('- locally notice! -----------------------------------------------------------------');
+ console.log('- 로컬리(locally)는 호스트 파일에 지정한 도메인 정보를 직접 작성하기 때문에 관리자 권한으로 실행되야 합니다.'.red);
+ console.log('- 또한 사용자가 지정하지 않는 어떠한 정보도 추가, 변경, 삭제하지 않습니다.'.red);
+ console.log('- 소스확인 : https://github.com/rhiokim/locally/blob/master/lib/vhosts.js'.yellow);
+ console.log('-----------------------------------------------------------------------------------');
+
+ var lines = loadFile(file);
+ var res = lines.every(function(line, idx) {
+ match = line.match(header);
+
+ if(match != null) {
+ headline = idx;
+ }
+
+ return line.match(vhost) == null;
+ });
+
+ if(!headline) {
+ lines.push(header);
+ headline = lines.length - 1;
+ }
+
+ if(res) {
+ lines.splice(headline+1, 0, "127.0.0.1\t"+ vhost);
+ }
+
+ saveFile(file, lines);
+}
+
+module.exports = {
+ darwin: function(vhost) {
+ applyVirtualHost('/etc/hosts', vhost);
+ },
+
+ freebsd: function(vhost) {
+ },
+ linux: function(vhost) {},
+ win32: function(vhost) {
+ applyVirtualHost('c:\\windows\\system32\\dirvers\\etc\\hosts', vhost);
+ },
+ sunos: function(vhost) {}
+}
@@ -0,0 +1 @@
+node ../lib/locally.js -w public -p 8081 --vhost locally.com --debug

0 comments on commit f747139

Please sign in to comment.