stipa 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 387748a4e29d9918defffcbf6e020d826933bc96619f9579f27bbcc4bde3a8c8
4
- data.tar.gz: e7a6122363efaeff5405b48bb5fe8b334af837b9269b8ad854fdc2aa020b8186
3
+ metadata.gz: 4808a7cc4235af581c6e5bf34b072562ef8c3992d05b51d82659a9462e1c3395
4
+ data.tar.gz: ba5bf60d232d22a9e49a4cbb496e34f5272a85aaa4148b319269c6ae4df3258a
5
5
  SHA512:
6
- metadata.gz: b2787738c43370505fec1cecdf96ce6d5ff951abd4eb0c80dfd4328dad29e24c9c322c8f0d85faee29d918d40f904da6bc7cf59bfdca7383420c4644b2a897ba
7
- data.tar.gz: d3bfdfcd68a3920214ab00547e9272d7d7ad2f721c4062793aa38bddaf0f256d9eeeafcc3d2ed0e49c9d5fd3df15c7cec50d917b6b0073f6667b6d642c3c9f81
6
+ metadata.gz: f9eea7112fdfda768ec2c5eb8272155b5eb5bf5b0ce8e4ac1be6b365574a2f727415372852cdefa5ac81d926d67c3004a3c24992d2ea5128e551bfbb067cf773
7
+ data.tar.gz: 12af42c56b74b227918e3c786c145bb71a17754a6f0a41192a2a451d352c949f88458d5212665e63ccb7d176d81fa4a6ddd0c69b0c2b2211757a568b1bd3470b
@@ -23,6 +23,7 @@ module Stipa
23
23
 
24
24
  def files
25
25
  {
26
+ '.gitignore' => t_gitignore,
26
27
  'Gemfile' => t_gemfile,
27
28
  'server.rb' => t_server,
28
29
  'config/routes.rb' => t_routes(
@@ -34,6 +35,10 @@ module Stipa
34
35
  }
35
36
  end
36
37
 
38
+ def t_gitignore
39
+ t_gitignore_common
40
+ end
41
+
37
42
  def t_server
38
43
  <<~RUBY
39
44
  require 'stipa'
@@ -23,6 +23,7 @@ module Stipa
23
23
  make_dirs
24
24
  write_files
25
25
  post_generate
26
+ init_git
26
27
  say done_message
27
28
  end
28
29
 
@@ -41,14 +42,46 @@ module Stipa
41
42
 
42
43
  def post_generate = nil
43
44
 
45
+ def init_git
46
+ return unless git_available?
47
+
48
+ Dir.chdir(target) do
49
+ system('git init -q')
50
+ system('git add .')
51
+ system("git commit -q -m 'Initial commit'")
52
+ end
53
+ say ' git initialized repository with initial commit'
54
+ rescue => e
55
+ say " warn git init failed: #{e.message}"
56
+ end
57
+
44
58
  def say(msg) = puts(msg)
45
59
 
46
60
  def app_title
47
61
  name.split(/[-_]/).map(&:capitalize).join(' ')
48
62
  end
49
63
 
64
+ def git_available?
65
+ system('git --version > /dev/null 2>&1')
66
+ end
67
+
50
68
  # ── Shared templates ───────────────────────────────────────────────────────
51
69
 
70
+ def t_gitignore_common
71
+ <<~GITIGNORE
72
+ # Ruby
73
+ .bundle/
74
+ vendor/bundle/
75
+ Gemfile.lock
76
+
77
+ # OS
78
+ .DS_Store
79
+ Thumbs.db
80
+ *.swp
81
+ *.swo
82
+ GITIGNORE
83
+ end
84
+
52
85
  def t_gemfile
53
86
  <<~RUBY
54
87
  source 'https://rubygems.org'
@@ -53,6 +53,7 @@ module Stipa
53
53
 
54
54
  def files
55
55
  {
56
+ '.gitignore' => t_gitignore,
56
57
  'Gemfile' => t_gemfile,
57
58
  'package.json' => t_package_json,
58
59
  'rollup.config.js' => t_rollup_config,
@@ -75,6 +76,20 @@ module Stipa
75
76
  }
76
77
  end
77
78
 
79
+ def t_gitignore
80
+ t_gitignore_common + <<~GITIGNORE
81
+
82
+ # Node
83
+ node_modules/
84
+ package-lock.json
85
+
86
+ # Build output
87
+ public/app.js
88
+ public/app.js.map
89
+ public/vendor/
90
+ GITIGNORE
91
+ end
92
+
78
93
  def t_package_json
79
94
  JSON.pretty_generate(
80
95
  name: name,
@@ -84,7 +99,7 @@ module Stipa
84
99
  'copy:vue' => 'cp node_modules/vue/dist/vue.esm-browser.prod.js public/vendor/vue.esm-browser.prod.js',
85
100
  build: 'npm run copy:vue && rollup -c',
86
101
  watch: 'rollup -c --watch',
87
- dev: 'npm run copy:vue && concurrently "bundle exec ruby server.rb" "rollup -c --watch"',
102
+ dev: 'npm run copy:vue && concurrently "STIPA_RELOAD=1 bundle exec ruby server.rb" "rollup -c --watch"',
88
103
  typecheck: 'vue-tsc --noEmit',
89
104
  },
90
105
  devDependencies: {
@@ -400,6 +415,10 @@ module Stipa
400
415
  const component: DefineComponent
401
416
  export default component
402
417
  }
418
+
419
+ interface Window {
420
+ _t0?: number
421
+ }
403
422
  TS
404
423
  end
405
424
 
@@ -0,0 +1,85 @@
1
+ module Stipa
2
+ # Development file watcher that restarts the process when Ruby source files change.
3
+ #
4
+ # Enabled when:
5
+ # - STIPA_RELOAD=1 environment variable is set, OR
6
+ # - reload: true is passed to App#start / Server#start
7
+ #
8
+ # Strategy:
9
+ # A background thread polls the mtime of all .rb files visible to Ruby
10
+ # ($LOADED_FEATURES) plus any extra watch paths supplied by the user.
11
+ # When a change is detected it calls exec($0, *ARGV) which replaces the
12
+ # current process image with a fresh one — same PID namespace, same
13
+ # command line arguments, all changes picked up from scratch.
14
+ #
15
+ # Why exec instead of in-process reload:
16
+ # In-process reload requires clearing constants, unloading files, and
17
+ # rebuilding the route table. exec is simpler, safer, and handles any
18
+ # kind of change (routes, middleware, config, gems) without edge cases.
19
+ class Reloader
20
+ DEFAULT_INTERVAL = 0.5 # seconds between polls
21
+
22
+ def initialize(logger:, interval: DEFAULT_INTERVAL, watch: [])
23
+ @logger = logger
24
+ @interval = interval
25
+ @extra = Array(watch).map { |p| File.expand_path(p) }
26
+ @mtimes = {}
27
+ @thread = nil
28
+ end
29
+
30
+ def start
31
+ snapshot!
32
+ @thread = Thread.new { watch_loop }
33
+ @thread.name = 'stipa-reloader'
34
+ @thread.abort_on_exception = false
35
+ @logger.warn('reloader active — watching for file changes')
36
+ end
37
+
38
+ def stop
39
+ @thread&.kill
40
+ end
41
+
42
+ private
43
+
44
+ def watch_loop
45
+ loop do
46
+ sleep @interval
47
+ if changed?
48
+ @logger.warn('file change detected — restarting')
49
+ $stdout.flush
50
+ $stderr.flush
51
+ exec($0, *ARGV)
52
+ end
53
+ end
54
+ rescue => e
55
+ @logger.error("reloader crashed: #{e.class}: #{e.message}")
56
+ end
57
+
58
+ # Collect the current set of watched files: everything Ruby has loaded
59
+ # plus any extra paths the user specified.
60
+ def watched_files
61
+ ($LOADED_FEATURES + @extra).uniq
62
+ end
63
+
64
+ def snapshot!
65
+ watched_files.each do |path|
66
+ @mtimes[path] = mtime(path)
67
+ end
68
+ end
69
+
70
+ def changed?
71
+ watched_files.any? do |path|
72
+ current = mtime(path)
73
+ previous = @mtimes[path]
74
+ @mtimes[path] = current
75
+ current != previous
76
+ end
77
+ end
78
+
79
+ def mtime(path)
80
+ File.mtime(path)
81
+ rescue Errno::ENOENT
82
+ nil
83
+ end
84
+ end
85
+ end
data/lib/stipa/server.rb CHANGED
@@ -53,6 +53,7 @@ module Stipa
53
53
  max_body_size: 1 * 1024 * 1024,
54
54
  backpressure: :drop, # :drop (503) or :block (wait briefly)
55
55
  log_level: :info,
56
+ reload: ENV['STIPA_RELOAD'] == '1',
56
57
  }.freeze
57
58
 
58
59
  def initialize(app:, **overrides)
@@ -64,7 +65,8 @@ module Stipa
64
65
  queue_depth: @config[:queue_depth],
65
66
  on_error: method(:pool_error),
66
67
  )
67
- @running = false
68
+ @running = false
69
+ @reloader = @config[:reload] ? Reloader.new(logger: @logger) : nil
68
70
  end
69
71
 
70
72
  # Start the server. Blocks until SIGTERM/SIGINT.
@@ -73,6 +75,7 @@ module Stipa
73
75
  @running = true
74
76
 
75
77
  register_signals
78
+ @reloader&.start
76
79
 
77
80
  @logger.info(
78
81
  req: nil, res: nil,
@@ -85,6 +88,7 @@ module Stipa
85
88
 
86
89
  accept_loop
87
90
  ensure
91
+ @reloader&.stop
88
92
  @server&.close rescue nil
89
93
  @logger.info(req: nil, res: nil, msg: 'Stīpa stopped')
90
94
  end
data/lib/stipa/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Stipa
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.3'
3
3
  end
data/lib/stipa.rb CHANGED
@@ -20,6 +20,7 @@
20
20
  require_relative 'stipa/version'
21
21
  require_relative 'stipa/logger'
22
22
  require_relative 'stipa/thread_pool'
23
+ require_relative 'stipa/reloader'
23
24
  require_relative 'stipa/middleware'
24
25
  require_relative 'stipa/static'
25
26
  require_relative 'stipa/template'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stipa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pedro Harbs
@@ -100,6 +100,7 @@ files:
100
100
  - lib/stipa/generators/vue.rb
101
101
  - lib/stipa/logger.rb
102
102
  - lib/stipa/middleware.rb
103
+ - lib/stipa/reloader.rb
103
104
  - lib/stipa/request.rb
104
105
  - lib/stipa/response.rb
105
106
  - lib/stipa/server.rb