-
-
Save damaon/f3e79d8e2d42fe4d4118c237850cab3a to your computer and use it in GitHub Desktop.
Generate types from widget template code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "json" | |
search_for = ARGV.shift || "widget" | |
file = ARGF.read | |
search_regex = /#{search_for}\.[^ "'}]*/i | |
lines = file.scan(search_regex) | |
types = {}; | |
def set(o, path) | |
if path.size == 0 | |
o | |
elsif path.size == 1 | |
prop = path[0] | |
type = "any" | |
type = "Function" if prop.end_with?("Handler") | |
type = "boolean" if prop.start_with?("is") | |
o[prop] = type; | |
else | |
prop = path[0] | |
o[prop] = o[prop].is_a?(Hash) ? o[prop] : {} | |
set(o[prop], path.drop(1)) | |
end | |
end | |
name = lines[0].split(".").first.capitalize | |
lines.uniq.sort.each do |path| | |
props = path.split(".").drop(1) | |
set(types, props) | |
end | |
puts "type #{name} = #{JSON.pretty_generate(types).tr('"', '')}"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment