Skip to content

Commit

Permalink
feat: add Igniter.mkdir (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
petermm authored Dec 3, 2024
1 parent 4585581 commit 022fdbf
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
42 changes: 42 additions & 0 deletions lib/igniter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule Igniter do
warnings: [],
notices: [],
assigns: %{},
mkdirs: [],
moves: %{},
args: %Igniter.Mix.Task.Args{}
]
Expand All @@ -23,6 +24,7 @@ defmodule Igniter do
warnings: [String.t()],
notices: [String.t()],
assigns: map(),
mkdirs: [String.t()],
moves: %{optional(String.t()) => String.t()},
args: Igniter.Mix.Task.Args.t()
}
Expand Down Expand Up @@ -665,6 +667,22 @@ defmodule Igniter do
create_new_file(igniter, target, contents, opts)
end

@doc """
Creates a folder in the project.
"""

@spec mkdir(t(), Path.t()) :: Igniter.t()
def mkdir(igniter, path) do
current_dir = Path.expand(".")
target_path = Path.expand(path)

if String.starts_with?(target_path, current_dir) do
%{igniter | mkdirs: List.wrap(path) ++ igniter.mkdirs}
else
add_issue(igniter, "Igniter.mkdir invalid path: #{path} is outside the current directory.")
end
end

@doc """
Creates a new file in the project with the provided string contents. Adds an error if it already exists.
Expand Down Expand Up @@ -955,6 +973,8 @@ defmodule Igniter do

display_warnings(igniter, title)

display_mkdirs(igniter)

display_moves(igniter)

display_tasks(igniter, result_of_dry_run, opts)
Expand All @@ -981,6 +1001,13 @@ defmodule Igniter do
Mix.shell().cmd("mix deps.get")
end

igniter.mkdirs
|> Enum.map(&Path.expand(&1, ""))
|> Enum.uniq()
|> Enum.each(fn path ->
File.mkdir_p!(path)
end)

igniter.moves
|> Enum.each(fn {from, to} ->
File.mkdir_p!(Path.dirname(to))
Expand Down Expand Up @@ -1529,6 +1556,21 @@ defmodule Igniter do
|> display_list()
end

@doc false
def display_mkdirs(igniter) do
igniter.mkdirs
|> Enum.map(&Path.expand(&1, ""))
|> Enum.uniq()
|> Enum.sort()
|> Enum.map(fn path ->
if not File.exists?(path) do
[:green, path]
end
end)
|> Enum.reject(&is_nil/1)
|> display_list("These folders will be created:")
end

@doc false
def display_moves(igniter) do
igniter.moves
Expand Down
16 changes: 16 additions & 0 deletions test/igniter/code/file_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,20 @@ defmodule Igniter.Code.FileTest do
|> Rewrite.source!("README.md")
|> Rewrite.Source.get(:content) == "Hello Test"
end

test "can create folder" do
assert %{mkdirs: mkdirs} =
Igniter.new()
|> Igniter.mkdir("empty_folder")

assert mkdirs == ["empty_folder"]
end

test "can only create folder inside project directory" do
%{mkdirs: mkdirs} =
Igniter.new()
|> Igniter.mkdir("../empty_folder")

assert mkdirs == []
end
end

0 comments on commit 022fdbf

Please sign in to comment.