-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generators for installation and example view component
- Loading branch information
Nicholas Barone
committed
Aug 8, 2024
1 parent
422afea
commit ff1b07b
Showing
8 changed files
with
284 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require 'rails/generators' | ||
|
||
class Storybook::ExampleGenerator < Rails::Generators::Base | ||
def install_view_components | ||
insert_into_file "Gemfile", "gem 'view_component'" | ||
system "bundle install" | ||
end | ||
|
||
def create_example_component | ||
create_file "app/components/example_component.rb", <<~RUBY | ||
# frozen_string_literal: true | ||
class ExampleComponent < ViewComponent::Base | ||
slim_template <<~SLIM | ||
span | ||
= @title | ||
SLIM | ||
def initialize(title:) | ||
@title = title | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def create_example_component_preview | ||
create_file "test/components/previews/example_component_preview.rb", <<~RUBY | ||
# frozen_string_literal: true | ||
class ExampleComponentPreview < ViewComponent::Preview | ||
layout "storybook" | ||
def default | ||
render(ExampleComponent.new(title: "title")) | ||
end | ||
def with_content_block | ||
render(ExampleComponent.new(title: "This component accepts a block of content")) do | ||
tag.div do | ||
content_tag(:span, "Hello") | ||
end | ||
end | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def create_example_story | ||
create_file "stories/example_component.stories.json", <<~JSON | ||
{ | ||
"title": "ExampleComponent", | ||
"stories": [ | ||
{ | ||
"name": "default", | ||
"parameters": { | ||
"server": { "id": "example_component/default" } | ||
} | ||
} | ||
] | ||
} | ||
JSON | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
require 'rails/generators' | ||
|
||
class Storybook::InstallGenerator < Rails::Generators::Base | ||
def install_storybook | ||
system "npx storybook@latest init --yes --dev --no-dev --builder webpack5" | ||
system "yarn add @storybook/server --dev" | ||
system "yarn add @storybook/server-webpack5 --dev" | ||
system "yarn install" | ||
insert_into_file "Procfile.dev", "storybook: yarn storybook -p 6006" | ||
end | ||
|
||
def install_rack_cors | ||
insert_into_file "Gemfile", " gem 'rack-cors'\n", after: "group :development do\n" | ||
system "bundle install" | ||
create_file "config/initializers/cors.rb", <<~RUBY | ||
Rails.application.config.middleware.insert_before 0, Rack::Cors do | ||
allow do | ||
origins '*' | ||
resource '*', headers: :any, methods: [:get, :post, :patch, :put] | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def create_storybook_layout_slim | ||
create_file "app/views/layouts/storybook.html.slim", <<~SLIM | ||
doctype html | ||
html lang='en' | ||
head | ||
title Trainyard | ||
meta charset="UTF-8" | ||
meta[name="viewport" content="width=device-width,initial-scale=1"] | ||
= csrf_meta_tags | ||
= csp_meta_tag | ||
= stylesheet_link_tag "tailwind", "inter-font", "data-turbo-track": "reload" | ||
= stylesheet_link_tag "application" | ||
= javascript_pack_tag "application" | ||
body | ||
div | ||
main.ml-64.p-8 | ||
== yield | ||
SLIM | ||
end | ||
|
||
def update_storybook | ||
# Main.js | ||
insert_into_file ".storybook/main.js", "\n \"@storybook/server\",", after: "addons: [" | ||
gsub_file '.storybook/main.js', "*.stories.@(js|jsx|mjs|ts|tsx)", "*.stories.@(js|jsx|mjs|ts|tsx|json)" | ||
gsub_file '.storybook/main.js', "@storybook/react-webpack5", "@storybook/server-webpack5" | ||
|
||
# Preview.js | ||
base_url = "http://localhost:3000/rails/view_components" | ||
insert_into_file ".storybook/preview.js", "\n server: { url: \"#{base_url}\", },", after: "parameters: {" | ||
|
||
# insert_into_file ".storybook/main.js", "@storybook/addon-controls", after: "addons: [" | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require "storybook/version" | ||
|
||
module Storybook | ||
class Error < StandardError; end | ||
# Your code goes here... | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
module Storybook | ||
VERSION = "0.1.0" | ||
end | ||
|
This file contains 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