Skip to content

Commit

Permalink
Merge pull request #120 from NYDrewReynolds/feature/add-installation-…
Browse files Browse the repository at this point in the history
…rake-task

Create an install generator with helpful output
  • Loading branch information
kirillplatonov authored Sep 8, 2021
2 parents 604b6ea + b33615c commit 85bbd5e
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ In `Gemfile`, add:
gem 'polaris_view_components'
```

Run install generator:
```bash
rails generate polaris_view_components:install
```

Setup Polaris styles in your layouts `<head>` tag:

```erb
Expand Down
5 changes: 5 additions & 0 deletions lib/generators/polaris_view_components/USAGE
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Description:
Creates or adds import of NPM package to your application + additional installation steps.

Example:
rails generate polaris_view_components:install
35 changes: 35 additions & 0 deletions lib/generators/polaris_view_components/install_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

require 'rails/generators/active_record'

module PolarisViewComponents
class InstallGenerator < Rails::Generators::Base
source_root File.expand_path('templates', __dir__)

def add_npm_package
say "Adding NPM package", :green
run "yarn add polaris-view-components"
end

def add_to_stimulus_controller
say "Adding import to to Stimulus controller", :green
dir_path = "app/javascript/controllers"
empty_directory('app/javascript')
empty_directory(dir_path)

file_path = "#{dir_path}/index.js"

unless File.exist?(file_path)
copy_file 'stimulus_index.js', file_path
end

append_to_file file_path do
"import { registerPolarisControllers } from 'polaris-view-components'\nregisterPolarisControllers(application)"
end
end

def show_readme
readme 'README'
end
end
end
14 changes: 14 additions & 0 deletions lib/generators/polaris_view_components/templates/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
===============================================================================

Some manual setup is still required:

1. Setup Polaris styles in your layouts <head> tag:

<link rel="stylesheet" href="https://unpkg.com/@shopify/[email protected]/dist/styles.css" />
<%= stylesheet_link_tag 'polaris_view_components' %>

2. Define Polaris style on your <body> tag:

<body style="<%= polaris_body_styles %>">

===============================================================================
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Application } from 'stimulus'
import { definitionsFromContext } from 'stimulus/webpack-helpers'

const application = Application.start(document.documentElement)
const context = require.context('.', true, /_controller\.js$/)
application.load(definitionsFromContext(context))

0 comments on commit 85bbd5e

Please sign in to comment.