lit-cli 0.0.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/bin/lit +15 -2
  3. data/lib/config.rb +115 -0
  4. data/lib/lit_cli.rb +87 -0
  5. data/lib/lit_pry.rb +68 -0
  6. metadata +20 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 01f4a1f5271c4c2d83ba1cd6ac53afcfbd2deadc16f5c7d392b8e9fb867f63f4
4
- data.tar.gz: 0ee8ab728e6b47438f32e61172e849c7920bc6e528f8b827ab225061791161a9
3
+ metadata.gz: 0ed5d7713a6943b0043bf26568c069dc4c0a647a8581dc48d7a88c05abb3b5de
4
+ data.tar.gz: 53c911d1424038fa5bb836dfc68f739514d53a62b91ed813dc506078d4c5742d
5
5
  SHA512:
6
- metadata.gz: f013c03c7cf113875c0813695a3b14ad7cf46e3678a0568bd959f193689173a76801e7e1337548e7187f12a4ede4283fd4eea5d2a25a21a0e43820220a1605a3
7
- data.tar.gz: fb071c6372d2c90950729f688991bc0f53492f63aed17593f1dc27c1383ac27f0fc368ed6b68975413b38c1ba08a1cad4ef51bf581309f8ba774a20991847541
6
+ metadata.gz: e75aab70ca6f9aac9258b68f289ca7efdaf24d8328030a7195c91e24ecd7828eaf32d42d7cee87acc6b1d188d2ef692969cc402055b1a98ed71bc5a0edf4632e
7
+ data.tar.gz: 0f88b27bcb0308d93b8e7ba223a91a11125faf4c29fdb600e3ff77a18a1526c1be51f5dc4c60e276f5b2a0aca2e91577a92e8f4ef85b6e6c233d80cab8c0a2c3
data/bin/lit CHANGED
@@ -8,9 +8,22 @@
8
8
  # lit rails server
9
9
  ################################################################################
10
10
 
11
- # Enable Lit API.
12
11
  ENV['LIT_ENABLED'] = 'true'
13
12
 
13
+ # Get flags from arguments.
14
+ args = []
15
+ flags = []
16
+ ARGV.each do |arg|
17
+ if arg.start_with? '@'
18
+ flags << arg.delete_prefix('@')
19
+ else
20
+ args << arg
21
+ end
22
+ end
23
+ ENV['LIT_FLAGS'] = flags.join " "
24
+
25
+ # Get user input.
26
+ command = args.join " "
27
+
14
28
  # Action original command.
15
- command = ARGV.join " "
16
29
  system command
data/lib/config.rb ADDED
@@ -0,0 +1,115 @@
1
+ require 'set'
2
+
3
+ module LitCLI
4
+
5
+ ##############################################################################
6
+ # Default config that can be overridden from the command line or application.
7
+ #
8
+ # @usage:
9
+ # LitCLI.configure do |config|
10
+ # config.<property> = <value>
11
+ # end
12
+ #
13
+ # @precedence:
14
+ # 3. Defaults - initialize()
15
+ # 2. Application - LitCLI.configure()
16
+ # 1. Command line flags - cli_configure()
17
+ ##############################################################################
18
+
19
+ class Config
20
+
21
+ # Track errors and only show them once.
22
+ @@errors = Set.new
23
+
24
+ attr_accessor :enabled
25
+ attr_accessor :types
26
+ attr_accessor :type
27
+ attr_accessor :step
28
+ attr_accessor :delay
29
+
30
+ def initialize()
31
+
32
+ @types = {
33
+ :info => { icon: "ℹ", color: :blue },
34
+ :pass => { icon: "✔", color: :green },
35
+ :warn => { icon: "⚠", color: :yellow },
36
+ :fail => { icon: "⨯", color: :red },
37
+ :error => { icon: "!", color: :red },
38
+ :debug => { icon: "?", color: :purple },
39
+ }
40
+
41
+ # Lit is disabled by default, then enabled via the `lit` command.
42
+ # Or it can be permanently enabled, without the use of the `lit` command.
43
+ @enabled = false
44
+
45
+ ##
46
+ # FLAGS.
47
+ #
48
+ # Flag defaults when not supplied via command line.
49
+ ##
50
+
51
+ # Array of types to filter by, for example... [:warn, :error, :fail]
52
+ @type = nil
53
+
54
+ # Boolean on whether or not to step through each lit() breakpoint.
55
+ @step = false
56
+
57
+ # Integer or float representing amount of seconds to delay each lit() by.
58
+ @delay = 0
59
+
60
+ cli_configure()
61
+ end
62
+
63
+ # Override config from command line.
64
+ def cli_configure()
65
+ @enabled = true if ENV['LIT_ENABLED'] == 'true'
66
+
67
+ # Convert flag string to hash.
68
+ flags = {}
69
+ if ENV['LIT_FLAGS'] && !ENV['LIT_FLAGS'].empty?
70
+ ENV['LIT_FLAGS'].split().each do |flag|
71
+ values = flag.split('=')
72
+
73
+ key = values.shift.to_sym
74
+
75
+ # No arguments.
76
+ if values.empty?
77
+ flags[key] = nil
78
+ else
79
+ args = values.pop.split(',')
80
+ # Single argument.
81
+ if args.count == 1
82
+ flags[key] = args.first
83
+ # Multiple arguments.
84
+ else
85
+ flags[key] = args
86
+ end
87
+ end
88
+ end
89
+ end
90
+
91
+ @step = true if flags.has_key? :step
92
+ @type = Array(flags[:type]).map(&:to_sym) if valid? flags, :type
93
+ @delay = flags[:delay].to_f if valid? flags, :delay
94
+ end
95
+
96
+ def valid? flags, flag
97
+ # Has flag even been entered on the command line?
98
+ unless flags.has_key? flag
99
+ return false
100
+ end
101
+
102
+ if flags[flag].nil?
103
+ error = "🔥 ERROR: Invalid argument for @#{flag}."
104
+ unless @@errors.include? error
105
+ @@errors.add error
106
+ puts error
107
+ end
108
+ return false
109
+ end
110
+
111
+ true
112
+ end
113
+
114
+ end
115
+ end
data/lib/lit_cli.rb ADDED
@@ -0,0 +1,87 @@
1
+ require 'pry'
2
+ require 'pastel'
3
+ require 'config'
4
+ require 'lit_pry'
5
+
6
+ module LitCLI
7
+ @@pastel = Pastel.new
8
+ @@config = Config.new
9
+ @@is_prying = false
10
+
11
+ def lit(message, type = :info)
12
+ if @@config.enabled
13
+ return if LitCLI.filter? type
14
+ LitCLI.render(type)
15
+ LitCLI.step()
16
+ LitCLI.delay()
17
+ end
18
+ end
19
+ alias 🔥 lit
20
+
21
+ def self.render(type)
22
+ type_config = @@config.types[type]
23
+
24
+ time_text = LitCLI.colorize(Time.now().strftime("%k:%M"), :cyan)
25
+ type_text = type_config[:icon] + " " + type.to_s
26
+ type_text_color = LitCLI.colorize(type_text, type_config[:color])
27
+
28
+ message = "🔥 #{time_text} #{type_text_color} #{message}"
29
+
30
+ puts message
31
+ end
32
+
33
+ def self.step()
34
+ if @@config.step
35
+ puts "🔥 Press ENTER to step or P to Pry:"
36
+ input = gets.chomp
37
+ binding while input == nil
38
+ @@is_prying = true if input.downcase == "p"
39
+ end
40
+ end
41
+
42
+ def self.filter? type
43
+ unless @@config.type.nil? || @@config.type.include?(type)
44
+ return true
45
+ end
46
+
47
+ false
48
+ end
49
+
50
+ def self.delay()
51
+ if @@config.delay > 0
52
+ sleep(@@config.delay)
53
+ end
54
+ end
55
+
56
+ def self.is_prying?
57
+ @@config.step && @@is_prying
58
+ end
59
+
60
+ def self.colorize(text, color)
61
+ case color
62
+ when :blue
63
+ return @@pastel.bright_blue(text)
64
+ when :green
65
+ return @@pastel.green(text)
66
+ when :yellow
67
+ return @@pastel.yellow(text)
68
+ when :red
69
+ return @@pastel.red(text)
70
+ when :purple, :magenta
71
+ return @@pastel.magenta(text)
72
+ when :cyan
73
+ return @@pastel.cyan(text)
74
+ else
75
+ return text
76
+ end
77
+ end
78
+
79
+ # Override config from application.
80
+ def self.configure()
81
+ yield(@@config)
82
+
83
+ # Override config from command line.
84
+ @@config.cli_configure()
85
+ end
86
+
87
+ end
data/lib/lit_pry.rb ADDED
@@ -0,0 +1,68 @@
1
+ # Only override kernel methods when Lit's @step flag is true.
2
+ if ENV['LIT_FLAGS'] && ENV['LIT_FLAGS'].include?('step')
3
+ module Kernel
4
+
5
+ def require_relative relative_path
6
+ filename = relative_path
7
+
8
+ # Get directory of the file that called this file.
9
+ absolute_path = caller_locations.first.absolute_path.split('/')
10
+ absolute_path.pop
11
+
12
+ # When relative path is current directory.
13
+ if relative_path.start_with?('./')
14
+ filename = relative_path.delete_prefix! './'
15
+ end
16
+
17
+ # When relative path is parent directory.
18
+ while relative_path.start_with?('../')
19
+ relative_path.delete_prefix! '../'
20
+ absolute_path.pop
21
+ end
22
+
23
+ # Append default extension.
24
+ unless filename.end_with? '.rb'
25
+ filename << '.rb'
26
+ end
27
+
28
+ filepath = File.join(absolute_path.join('/'), relative_path)
29
+
30
+ # Add pry binding beneath each lit message.
31
+ new_lines = ''
32
+ File.foreach(filepath) do |line|
33
+ new_lines << line
34
+ if line.strip.start_with? 'lit "'
35
+ new_lines << "binding.pry if LitCLI.is_prying?\n"
36
+ new_lines << "@@is_prying = false\n"
37
+ end
38
+ end
39
+
40
+ eval(new_lines)
41
+ end
42
+
43
+ # TODO: Investigate RubyGems `require` before overriding.
44
+ # def require path
45
+ # absolute_path = File.join(Dir.pwd, path)
46
+ # #p "requiring #{absolute_path}"
47
+ # #p __dir__
48
+ # #p File.dirname(File.realpath(__FILE__))
49
+ # #p File.realpath(__FILE__)
50
+ #
51
+ # # Split client-side and server-side code.
52
+ # new_lines = []
53
+ # binding_line = "binding.pry if LitCLI.is_prying?"
54
+ #
55
+ # # Add pry binding beneath each lit message.
56
+ # p path
57
+ # File.foreach(path) do |line|
58
+ # new_lines << line
59
+ # if line.strip.start_with? 'lit "'
60
+ # new_lines << binding_line
61
+ # end
62
+ # end
63
+ #
64
+ # eval(new_lines.join)
65
+ # end
66
+
67
+ end
68
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lit-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maedi Prichard
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-01 00:00:00.000000000 Z
11
+ date: 2021-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pastel
@@ -24,7 +24,21 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
- description: Show more detailed logs by starting commands with 'lit'.
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Console logs that are only visible after prefixing commands with 'lit'.
28
42
  email: maediprichard@gmail.com
29
43
  executables:
30
44
  - lit
@@ -32,6 +46,9 @@ extensions: []
32
46
  extra_rdoc_files: []
33
47
  files:
34
48
  - bin/lit
49
+ - lib/config.rb
50
+ - lib/lit_cli.rb
51
+ - lib/lit_pry.rb
35
52
  homepage: https://reflekt.dev/lit
36
53
  licenses:
37
54
  - MPL-2.0