Packages

Duskmoon fork of npm_ex — resolve, fetch, and manage npm dependencies with Mix tasks.

Current section

Files

Jump to
duskmoon_npm lib npm compiler.ex
Raw

lib/npm/compiler.ex

defmodule NPM.Compiler do
@moduledoc """
Mix compiler that ensures npm packages are installed.
Add to your project's compilers list to automatically install
npm dependencies during `mix compile`:
def project do
[
compilers: [:npm | Mix.compilers()],
...
]
end
Checks if `package-lock.json` exists and `node_modules/` is populated.
Only runs the full install if needed.
"""
use Mix.Task.Compiler
@impl true
def run(_argv) do
if needs_install?() do
case NPM.install() do
:ok -> {:ok, []}
{:error, _reason} -> {:error, []}
end
else
{:noop, []}
end
end
defp needs_install? do
File.exists?("package.json") and
(not File.exists?(NPM.Lockfile.default_path()) or not File.exists?("node_modules"))
end
end