lookbook_storybook_json_generator 0.2.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4706ac0d61b2baa9bc6489c25928ed6a0ac54a6aa7563bf80503ad98816132e3
4
- data.tar.gz: aa9934bf2431c4841044b503359e9cc2da0a8ec0f378fb1de5741da44dfd9f74
3
+ metadata.gz: 521bdf4ded7b02c6d834456c0fd254fc85fcd9f899728e8c9fd7a5e8986ed378
4
+ data.tar.gz: c3ab604a9e1190749df573a90792df5732a1fc6f28d251c040db9521bf114922
5
5
  SHA512:
6
- metadata.gz: 3c5853c5ace1a5150db8a08b2480a2146ed3a58edc1ff22b87060e9c626874476e4671b8fa40d5263c8e73114dc7ee931e299b9f3c84119f25c8c24c9ca8bc58
7
- data.tar.gz: ce6935361b6979972f5e96ee33c626802f1f58443d6c87cfdacbaaa8368b8e632468ecac92173a334c30e3a3f622c3ee727fee8acc935df26404d8200ab0e868
6
+ metadata.gz: ad3fedd7af02b633e271ddcc655a1050cbeb5d4ce1740622066afc923de8531089efae5717bb3e861264efdbd4f46b8e205cf8638fbb3d7950cce3b9cd1f07dc
7
+ data.tar.gz: 07bd22663bb68468b7b95b6da909fb1566feedc6c7373bc25a4720f691f44d5c1483f79a7294c79666f3be4c721890449ce4b92a97f6b1b07f74a156d2dfb850
data/README.md CHANGED
@@ -3,6 +3,8 @@
3
3
  This is a gem that allows you to use lookbook with storybook.
4
4
  It outputs JSON that can be used with storybook.
5
5
 
6
+ https://github.com/forgxisto/lookbook_storybook_json_generator/assets/1164914/08be7939-6882-43cd-9d60-4e4369d007ed
7
+
6
8
  ## Installation
7
9
 
8
10
  Install the gem and add to the application's Gemfile by executing:
@@ -15,13 +17,72 @@ If bundler is not being used to manage dependencies, install the gem by executin
15
17
 
16
18
  ## Usage
17
19
 
18
- TODO: Write usage instructions here
20
+ This gem depends on lookbook.
21
+ No special settings are required.
22
+
23
+ Create `_preview.stories.json` in the same directory as `_preview.rb` written according to the lookbook rules.
24
+
25
+ This JSON is created when the lookbook loads `_preview.rb`.
26
+
27
+ ### example
28
+
29
+ 0. You may want to refer to `dummy_app` in this repository.
30
+
31
+ 1. First, write the following in Gemfile and complete the lookbook settings.
32
+
33
+ ```
34
+ gem 'lookbook'
35
+ gem 'lookbook_storybook_json_generator'
36
+ ```
37
+
38
+ Please refer to the official website for the lookbook.
39
+ https://lookbook.build/
40
+
41
+ 2. Create `_preview.rb` according to the lookbook description rules.
42
+
43
+ By default, it is created under the `test/components/previews/` directory.
44
+
45
+ 3. Start Rails.
46
+
47
+ At this point, the lookbook preview file is read and a JSON file is created in the same directory.
48
+
49
+ 4. Install Storybook.
50
+
51
+ Please refer to the official website for Storybook.
52
+ https://storybook.js.org/
53
+
54
+ 5. To reference the JSON created with this gem in Storybook, use `@storybook/server-webpack5
55
+ Add `.
56
+
57
+ ```
58
+ npm install @storybook/server-webpack5
59
+ ```
60
+
61
+ 6. Set up `.storybook/main.js` and `.storybook/preview.js`.
62
+
63
+ Again, `dummy_app/.storybook/` in this repository may be helpful.
64
+
65
+ See the repository README for details.
66
+ https://www.npmjs.com/package/@storybook/server-webpack5
67
+
68
+ 7. Launch Storybook.
69
+
70
+ That's it! If all goes well, the preview created in lookbook will also be reflected in Storybook.
71
+
72
+ If you have any problems, please raise an issue in this repository.
19
73
 
20
74
  ## Development
21
75
 
22
76
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
23
77
 
24
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
78
+ A small Rails app for development is included in this repository. (located in the `dummy_app` directory)
79
+
80
+ We provide a shortcut command to start this little Rails app.
81
+
82
+ - `bin/dummy_rails` ... start Rails
83
+ - `bin/dummy_storybook` ... start Storybook
84
+
85
+ If you use overmind, we provide a Procfile.
25
86
 
26
87
  ## Contributing
27
88
 
@@ -20,43 +20,37 @@ module LookbookStorybookJsonGenerator
20
20
 
21
21
  attr_reader :code_object
22
22
 
23
+ def preview_entity
24
+ @preview_entity ||= Lookbook::PreviewEntity.new(code_object)
25
+ end
26
+
23
27
  def title
24
- if code_object.namespace.root?
25
- code_object.name.to_s.underscore.gsub(/_preview$/, '')
26
- else
27
- code_object.namespace.title.gsub(code_object.namespace.sep, '/')
28
- end
28
+ preview_entity.lookup_path.titleize
29
29
  end
30
30
 
31
31
  def stories
32
- code_object.meths.map { story(_1) }
32
+ preview_entity.scenarios.map { story(_1) }
33
33
  end
34
34
 
35
- def story(meth)
35
+ def story(scenario_entity)
36
36
  {
37
- name: meth.name,
38
- parameters: parameters(meth),
39
- args: args(meth),
40
- argTypes: arg_types(meth)
37
+ name: scenario_entity.name,
38
+ parameters: parameters(scenario_entity),
39
+ args: args(scenario_entity),
40
+ argTypes: arg_types(scenario_entity)
41
41
  }
42
42
  end
43
43
 
44
- def parameters(meth)
45
- { server: { id: "#{title.underscore}/#{meth.name}" } }
44
+ def parameters(scenario_entity)
45
+ { server: { id: scenario_entity.lookup_path } }
46
46
  end
47
47
 
48
- def args(meth)
49
- meth.parameters.each_with_object({}) do |(k, v), a|
50
- key = k.delete(':').to_sym
51
- # 値から引用符を削除し、シンボルがあれば文字列に変換
52
- val = v.gsub(/'|"/, '').then { _1.start_with?(':') ? _1[1..] : _1 }
53
-
54
- a[key] = val
55
- end
48
+ def args(scenario_entity)
49
+ scenario_entity.tags.to_h { [_1.name, _1.value_default] }
56
50
  end
57
51
 
58
- def arg_types(meth)
59
- meth.tags.each_with_object({}) do |e, a|
52
+ def arg_types(scenario_entity)
53
+ scenario_entity.tags.each_with_object({}) do |e, a|
60
54
  a[e.name] = {
61
55
  control: { type: control_type(e.tag_args[:input]) },
62
56
  name: e.name.camelcase,
@@ -69,8 +63,8 @@ module LookbookStorybookJsonGenerator
69
63
  case input
70
64
  when 'toggle'
71
65
  'boolean'
72
- when 'number'
73
- 'number'
66
+ when 'number', 'select', 'color', 'range'
67
+ input
74
68
  else
75
69
  'text'
76
70
  end
@@ -3,14 +3,35 @@
3
3
  module LookbookStorybookJsonGenerator
4
4
  # lookbook のコードオブジェクトを storybook の json として出力する
5
5
  class StorybookJsonGenerator
6
- def self.execute(code_objects, _args)
7
- code_objects.each do |code_object|
8
- # code_object と同じ場所にファイルを作成する
9
- # ただし、ファイル名は .stories.json とする
10
- file_name = code_object.file.to_s.gsub('.rb', '.stories.json')
11
- json = LookbookStorybookJsonGenerator::LookbookCodeObjectParser.new(code_object).to_json
12
- File.open(file_name, 'w') { _1.puts json }
13
- end
6
+ def self.execute(code_objects, changes = nil)
7
+ new(code_objects, changes).execute
8
+ rescue => e
9
+ Rails.loggoer.warn "LookbookStorybookJsonGenerator #{e.message}"
10
+ end
11
+
12
+ def initialize(code_objects, changes)
13
+ @code_objects = code_objects
14
+ @changes = changes
15
+ end
16
+
17
+ def execute
18
+ generate_all
19
+ end
20
+
21
+ private
22
+
23
+ attr_reader :code_objects, :changes
24
+
25
+ def generate_all
26
+ code_objects.each { generate_json(_1) }
27
+ end
28
+
29
+ # code_object と同じ場所にファイルを作成する
30
+ # ただし、ファイル名は .stories.json とする
31
+ def generate_json(code_object)
32
+ file_name = code_object.file.to_s.gsub('.rb', '.stories.json')
33
+ json = LookbookStorybookJsonGenerator::LookbookCodeObjectParser.new(code_object).to_json
34
+ File.open(file_name, 'w') { _1.puts json }
14
35
  end
15
36
  end
16
37
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LookbookStorybookJsonGenerator
4
- VERSION = '0.2.0'
4
+ VERSION = '0.4.0'
5
5
  end
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.files = Dir.chdir(__dir__) do
22
22
  `git ls-files -z`.split("\x0").reject do |f|
23
23
  (File.expand_path(f) == __FILE__) ||
24
- f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor Gemfile dummy_app/])
24
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor Gemfile dummy_app/ Procfile])
25
25
  end
26
26
  end
27
27
  spec.bindir = 'exe'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lookbook_storybook_json_generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ikad
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-01-16 00:00:00.000000000 Z
11
+ date: 2024-01-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lookbook