watchit 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8de94659ec9c15c23c2575ec1bd5aef8d44bad38
4
+ data.tar.gz: dd797351e7744ae574baf4741b3d45102867d550
5
+ SHA512:
6
+ metadata.gz: db69f4e4d9461610d322480a09371084d07997a4552d68b13d53d9ae5b1e220771dc30506954aba004c7affeddedb173a12bbb7f302b6c4120a3c6c609c6ff53
7
+ data.tar.gz: 8f3709c983976592e5b6894dac59f23c7027b498f59d6b4ebaa2cd4efb16e8a4c9a5bef885a6d0c20561f6852de7c9efae1728a6981f15c010f7a4504036e581
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.4
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in watchit.gemspec
4
+ gemspec
@@ -0,0 +1,15 @@
1
+ # watchit
2
+
3
+ watchit是一个前端开发的辅助工具,其主要功能有:
4
+ - 在指定目录下启动一个静态web服务器,用于向浏览器提供静态文件访问(效果与python3 -m http.server相同)
5
+ - 监视启动web服务器的目录中任意文件的修改操作,当有修改时自动刷新页面
6
+
7
+ # 基本使用
8
+
9
+ ```
10
+ Usage: watchit [options]
11
+ -w, --watch Watch path, 要监视的路径
12
+ -p, --port Server port, 启动的服务器端口
13
+ -v, --version Version, 版本信息
14
+ -h, --help Display this help message, 帮助消息
15
+ ```
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
Binary file
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "watchit"
5
+
6
+ # 进入pry调试环境
7
+ require "pry"
8
+ Pry.start
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "slop"
5
+ require "sinatra/base"
6
+
7
+ module Watchit
8
+ # 入参解析
9
+ opts = Slop.parse({ help: true, strict: true }) do
10
+ banner 'Usage: watchit [options]'
11
+
12
+ on 'w=', 'watch=', 'Watch path'
13
+ on 'p=', 'port=', 'Server port'
14
+ on 'v', 'version', 'Version' do
15
+ puts Slop::VERSION
16
+ exit
17
+ end
18
+ end
19
+
20
+ # 监视路径与端口
21
+ Watch_path = File.absolute_path(opts[:watch] || Dir.pwd) # 绝对路径
22
+ Watch_port = opts[:port] || 8080
23
+
24
+ # 配置
25
+ class WatchitApp < Sinatra::Application
26
+ configure do
27
+ # set :public_folder, Watchit::Watch_path
28
+ set :port, Watchit::Watch_port
29
+ set :views, File.expand_path('../../lib/watchit/views', __FILE__)
30
+ end
31
+ end
32
+
33
+ # 启动Watchit服务器
34
+ # 本句会在require_paths中搜索
35
+ require 'watchit'
36
+ end
@@ -0,0 +1,6 @@
1
+ <!DOCTYPE>
2
+ <html>
3
+ <body>
4
+ <h3>Have a nice day!</h3>
5
+ </body>
6
+ <html>
@@ -0,0 +1,50 @@
1
+ require 'erb'
2
+ require 'json'
3
+ require "watchit/version"
4
+ require 'sinatra/base'
5
+ require 'watchit/injection'
6
+ require 'watchit/render'
7
+ require 'watchit/file_system'
8
+
9
+ module Watchit
10
+ class WatchitApp < Sinatra::Application
11
+ include Watchit::Injection
12
+ include Watchit::Render
13
+ include Watchit::FileSystem
14
+
15
+ get '/mtime' do
16
+ content_type :json
17
+ {:mtime => dir_mtime(Watchit::Watch_path).serilize}.to_json
18
+ end
19
+
20
+ get '/*' do
21
+ # 请求路径
22
+ request_file_path = params[:splat][0]
23
+ # 文件实际位置
24
+ location = File.expand_path request_file_path, Watchit::Watch_path
25
+ # 扩展名
26
+ regex_start = (/.*\.(\w+)/ =~ request_file_path)
27
+
28
+ if File.directory? location
29
+ return file_list_html(location)
30
+ end
31
+
32
+ # 为html文件插入js脚本
33
+ if regex_start != nil && $1 == 'html'
34
+ file = File.open location
35
+ content = file.read
36
+ file.close
37
+
38
+ content_type :html
39
+ # 注入文本
40
+ inject content
41
+ # 不需要插入,直接返回文件内容
42
+ else
43
+ send_file location
44
+ end
45
+ end
46
+
47
+ # 启动服务器
48
+ run!
49
+ end
50
+ end
@@ -0,0 +1,18 @@
1
+ // var matchingPath = '{{WATCHING_PATH}}';
2
+ var modifiedTime = '{{MODIFIED_TIME}}';
3
+ // alert('Hi this is from the auto-refresh script!')
4
+
5
+ var xhr = new XMLHttpRequest();
6
+ xhr.addEventListener("load", function(){
7
+ var mtime = JSON.parse(this.responseText).mtime;
8
+ if( mtime !== modifiedTime ){
9
+ location.reload();
10
+ }
11
+ });
12
+
13
+ document.addEventListener("visibilitychange", function handleVisibilityChange() {
14
+ if(document.hidden) return;
15
+
16
+ xhr.open("GET", "/mtime");
17
+ xhr.send();
18
+ }, false);
@@ -0,0 +1,33 @@
1
+ module Watchit
2
+ module FileSystem
3
+ def dir_mtime(dir_name)
4
+ old_dir = Dir.pwd
5
+ Dir.chdir dir_name
6
+
7
+ # 最新修改时间
8
+ lastest = Time.at(0)
9
+
10
+ file_names = Dir.entries('.').select{|name| !name.start_with? '.'}
11
+ file_names.each do |file_name|
12
+ next if File.symlink? file_name # 忽略软连接
13
+
14
+ # 普通文件
15
+ if !File.directory?(file_name)
16
+ mtime = File.mtime file_name
17
+ # 目录
18
+ else
19
+ mtime = dir_mtime file_name
20
+ end
21
+
22
+ lastest = mtime if lastest < mtime
23
+ end
24
+
25
+ def lastest.serilize
26
+ "#{self.year}/#{self.month}/#{self.day}/#{self.hour}/#{self.min}/#{self.sec}"
27
+ end
28
+
29
+ Dir.chdir old_dir
30
+ return lastest
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,29 @@
1
+ require 'watchit/file_system'
2
+
3
+ module Watchit
4
+ module Injection
5
+ include Watchit::FileSystem
6
+
7
+ # 向给定html文本中注入自动刷新html脚本
8
+ def inject(raw_html)
9
+ insert_index = (/<\/body>/ =~ raw_html)
10
+ raw_html.insert insert_index, "<script>#{auto_refresh_script}</script>"
11
+ end
12
+
13
+ # 获取自动刷新js脚本代码
14
+ def auto_refresh_script
15
+ script_file = File.open File.expand_path('../auto_refresh.js', __FILE__)
16
+
17
+ # 监视路径的修改时间
18
+ mtime = dir_mtime Watchit::Watch_path
19
+ time_str = mtime.serilize
20
+
21
+ script = script_file.read
22
+ script = script.sub(/{{MODIFIED_TIME}}/, time_str)
23
+ .sub(/{{WATCHING_PATH}}/, Watchit::Watch_path)# 设置修改时间
24
+
25
+ script_file.close
26
+ return script
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,12 @@
1
+ module Watchit
2
+ module Render
3
+ def file_list_html(path)
4
+ file_list = Dir.entries(path)
5
+ .select{|name| !name.start_with? '.'}
6
+ erb :file_list, :locals => {
7
+ :path => path,
8
+ :file_list => file_list
9
+ }
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module Watchit
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,12 @@
1
+ <!DOCTYPE>
2
+ <html>
3
+ <body>
4
+ <p>Welcome to Wathit!</p>
5
+ <p>Files(directories) under [<%=path%>]:</p>
6
+ <ul>
7
+ <% file_list.each do |file| %>
8
+ <li><a href=./<%=file%>><%=file%></a></li>
9
+ <% end %>
10
+ </ul>
11
+ </body>
12
+ </html>
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'watchit/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "watchit"
8
+ spec.version = Watchit::VERSION
9
+ spec.authors = ["luminocean"]
10
+ spec.email = ["282896922@qq.com"]
11
+
12
+ spec.summary = 'Frontend modification listening and auto-refreshing tool'
13
+ spec.files = `git ls-files -z`.split("\x0")
14
+ spec.bindir = "bin"
15
+ spec.executables = ["watchit"]
16
+ spec.require_paths = ["lib"]
17
+ spec.license = 'MIT'
18
+ spec.homepage = 'https://github.com/luminocean/watchit'
19
+
20
+ spec.add_dependency "sinatra", "~> 1.4"
21
+ spec.add_dependency "slop", "~> 3.4"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.10"
24
+ spec.add_development_dependency "rake", "~> 11.1.2"
25
+ spec.add_development_dependency "pry", "~> 0.10"
26
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: watchit
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - luminocean
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sinatra
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: slop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.4'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 11.1.2
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 11.1.2
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.10'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.10'
83
+ description:
84
+ email:
85
+ - 282896922@qq.com
86
+ executables:
87
+ - watchit
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - README.md
95
+ - Rakefile
96
+ - avatar.jpg
97
+ - bin/console
98
+ - bin/watchit
99
+ - demo.html
100
+ - lib/watchit.rb
101
+ - lib/watchit/auto_refresh.js
102
+ - lib/watchit/file_system.rb
103
+ - lib/watchit/injection.rb
104
+ - lib/watchit/render.rb
105
+ - lib/watchit/version.rb
106
+ - lib/watchit/views/file_list.erb
107
+ - watchit.gemspec
108
+ homepage: https://github.com/luminocean/watchit
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.4.5.1
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: Frontend modification listening and auto-refreshing tool
132
+ test_files: []