Igniter `Proceed with changes` message when there is no changes

Hi, as you see this is my simple code for igniter, i am using update_file function, because before it i have a function that ensure_package_json_exists. I do not know why it shows me even there is not changes Proceed with changes? It is is my bug? I tried to use create_or_update_file function, but if file exist and there is not anything to change, it still shows Proceed with changes?
def update_package_json_deps(igniter, deps, options \\ []) do
package_json_path = "assets/package.json"
parsed_deps = parse_deps(deps)
deps_key = if options[:dev], do: "devDependencies", else: "dependencies"

new_igniter =
igniter
|> Igniter.update_file(package_json_path, fn source ->
original_content = Rewrite.Source.get(source, :content)

case Jason.decode(original_content) do
{:ok, json} ->
existing_deps = Map.get(json, deps_key, %{})

formatted =
Enum.reduce(parsed_deps, existing_deps, fn {name, version}, acc ->
Map.put(acc, name, version)
end)
|> then(&Map.put(json, deps_key, &1))
|> Jason.encode!(pretty: true)

Rewrite.Source.update(source, :content, formatted)

{:error, _} ->
igniter
|> Igniter.add_issue("Failed to parse package.json. Ensure it contains valid JSON.")
end
end)

new_igniter
end
def update_package_json_deps(igniter, deps, options \\ []) do
package_json_path = "assets/package.json"
parsed_deps = parse_deps(deps)
deps_key = if options[:dev], do: "devDependencies", else: "dependencies"

new_igniter =
igniter
|> Igniter.update_file(package_json_path, fn source ->
original_content = Rewrite.Source.get(source, :content)

case Jason.decode(original_content) do
{:ok, json} ->
existing_deps = Map.get(json, deps_key, %{})

formatted =
Enum.reduce(parsed_deps, existing_deps, fn {name, version}, acc ->
Map.put(acc, name, version)
end)
|> then(&Map.put(json, deps_key, &1))
|> Jason.encode!(pretty: true)

Rewrite.Source.update(source, :content, formatted)

{:error, _} ->
igniter
|> Igniter.add_issue("Failed to parse package.json. Ensure it contains valid JSON.")
end
end)

new_igniter
end
Another question is when i use create_or_update_file i should give the address of file, but sometimes the file dose not exist yet in my disk, for example i created it before this function and this file exists in my igniter var and still is not written. what should i do for this? thank you in advance
No description
Solution:
I think the latest version of igniter may have a fix for this?
Jump to solution
9 Replies
Solution
ZachDaniel
ZachDaniel•2w ago
I think the latest version of igniter may have a fix for this?
Shahryar
ShahryarOP•2w ago
I am using last version 0.6.1, i think 🙂
ZachDaniel
ZachDaniel•2w ago
Well, the latest is 0.6.2
Shahryar
ShahryarOP•2w ago
My bad yes it is 0.6.2, and has this problem too unfortunately
ZachDaniel
ZachDaniel•2w ago
What happens if you commit everything first to not trigger that git warning?
Shahryar
ShahryarOP•2w ago
No description
ZachDaniel
ZachDaniel•2w ago
Try checking if the new content is the same as the old content manually:
content = Rewrite.Source.get(source, :content)
IO.inspect(content == formatted, label: "contents equal?")
Rewrite.Source.update(source, :content, formatted)
content = Rewrite.Source.get(source, :content)
IO.inspect(content == formatted, label: "contents equal?")
Rewrite.Source.update(source, :content, formatted)
Shahryar
ShahryarOP•2w ago
No it is not contents equal?: false 🥲 , i think i need to rest 😂 Original:
"{\n \"dependencies\": {\n \"loadash\": \"latest\"\n },\n \"description\": \"Assets for mishka_chelekom Phoenix application\",\n \"devDependencies\": {},\n \"name\": \"mishka_chelekom\",\n \"version\": \"1.0.0\"\n}\n"
"{\n \"dependencies\": {\n \"loadash\": \"latest\"\n },\n \"description\": \"Assets for mishka_chelekom Phoenix application\",\n \"devDependencies\": {},\n \"name\": \"mishka_chelekom\",\n \"version\": \"1.0.0\"\n}\n"
Formatted
"{\n \"dependencies\": {\n \"loadash\": \"latest\"\n },\n \"description\": \"Assets for mishka_chelekom Phoenix application\",\n \"devDependencies\": {},\n \"name\": \"mishka_chelekom\",\n \"version\": \"1.0.0\"\n}"
"{\n \"dependencies\": {\n \"loadash\": \"latest\"\n },\n \"description\": \"Assets for mishka_chelekom Phoenix application\",\n \"devDependencies\": {},\n \"name\": \"mishka_chelekom\",\n \"version\": \"1.0.0\"\n}"
What a mistake I made. This was unbelievable. 🚑 original_content has an extra \n (newline) at the very end of the string formatted does not have that trailing newline so much sorry to waste your time i fixed it with. :))
|> then(&Map.put(json, deps_key, &1))
|> Jason.encode!(pretty: true)
|> Kernel.<>("\n") # for testing
|> then(&Map.put(json, deps_key, &1))
|> Jason.encode!(pretty: true)
|> Kernel.<>("\n") # for testing
--- Could you please help me in this? Another question is when i use create_or_update_file i should give the address of file, but sometimes the file dose not exist yet in my disk, for example i created it before this function and this file exists in my igniter var and still is not written. what should i do for this? Aha i can use something like this
Rewrite.has_source?(igniter.rewrite, package_json_path)
source = Rewrite.source!(igniter.rewrite, package_json_path)
Rewrite.Source.get(source, :content) |> IO.inspect()
Rewrite.has_source?(igniter.rewrite, package_json_path)
source = Rewrite.source!(igniter.rewrite, package_json_path)
Rewrite.Source.get(source, :content) |> IO.inspect()

Did you find this page helpful?