Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an add-on to document & generate the style guide #124

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions lib/nimble_template/addons/variants/phoenix/web/kss.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
defmodule NimbleTemplate.Addons.Phoenix.Web.Kss do
@moduledoc false

use NimbleTemplate.Addon

@impl true
def do_apply(%Project{} = project, _opts) do
project
|> edit_files()
|> copy_files()
end

defp edit_files(%Project{} = project) do
project
|> edit_package_json()
|> edit_endpoint()
end

defp copy_files(%Project{} = project) do
Generator.copy_file([{:text, ".kss-config.json", "assets/.kss-config.json"}])

project
end

defp edit_package_json(%Project{} = project) do
Generator.replace_content(
"assets/package.json",
"""
"devDependencies": {
""",
"""
"devDependencies": {
"kss": "^3.0.1",
"""
)

Generator.replace_content(
"assets/package.json",
"""
"scripts": {
""",
"""
"scripts": {
"styleguide.generate": "kss --config .kss-config.json",
"""
)

project
end

def edit_endpoint(%Project{otp_app: otp_app} = project) do
Generator.replace_content(
"lib/#{otp_app}_web/endpoint.ex",
"""
only: ~w(css fonts images js favicon.ico robots.txt)
""",
"""
only: ~w(css fonts images styleguide js favicon.ico robots.txt)
"""
)

project
end
end
4 changes: 4 additions & 0 deletions lib/nimble_template/variants/phoenix/web/template.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
defmodule NimbleTemplate.Phoenix.Web.Template do
@moduledoc false

import NimbleTemplate.AddonHelper

alias NimbleTemplate.Addons.Phoenix.Web
alias NimbleTemplate.Project

def apply(%Project{} = project) do
if install_addon_prompt?("Style guide (KSS)"), do: Web.Kss.apply(project)

project
|> Web.Assets.apply()
|> Web.CoreJS.apply()
Expand Down
25 changes: 25 additions & 0 deletions priv/templates/nimble_template/.kss-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"title": "Living Style Guide",

"//": "The home page content, in markdown format",
"homepage": "styleguide/homepage.md",

"//": "Source tells KSS where the CSS, Sass, or SCSS is that it should parse for documentation comments.",
"//": "The source and destination paths are relative to this file.",
"source": "css",

"//": "Destination tells KSS where to compile the style guide to.",
"destination": "../priv/static/styleguide/",

"//": "CSS gives KSS the path to the CSS files, so it can pull the styles into the style guide.",
"//": "The path needs to be relative to the style guide destination.",
"css": [
"../css/app.css"
],

"//": "To include any javascript files, add this block, with the path to the javascript file.",
"//": "Also relative to the style guide destination.",
"js" : [
"../js/app.js"
]
}
39 changes: 39 additions & 0 deletions test/nimble_template/addons/variants/phoenix/web/kss_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
defmodule NimbleTemplate.Addons.Phoenix.Web.KssTest do
use NimbleTemplate.AddonCase, async: false

describe "#apply/2" do
test "adds kss and a custom script into package.json", %{
project: project,
test_project_path: test_project_path
} do
in_test_project(test_project_path, fn ->
AddonsWeb.Kss.apply(project)

assert_file("assets/package.json", fn file ->
assert file =~ """
"kss": "^3.0.1",
"""

assert file =~ """
"styleguide.generate": "kss --config .kss-config.json",
"""
end)
end)
end

test "updates lib/nimble_template_web/endpoint.ex", %{
project: project,
test_project_path: test_project_path
} do
in_test_project(test_project_path, fn ->
AddonsWeb.Wallaby.apply(project)

assert_file("lib/nimble_template_web/endpoint.ex", fn file ->
assert file =~ """
only: ~w(css fonts images styleguide js favicon.ico robots.txt)
"""
end)
end)
end
end
end