Skip to content

Commit adf3f72

Browse files
committed
Only install production npm packages.
1 parent 06183d9 commit adf3f72

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

bake/utopia/node.rb

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# Released under the MIT License.
44
# Copyright, 2016-2025, by Samuel Williams.
55

6+
NPM = ENV["NPM"] || "npm"
7+
68
def update
79
require "fileutils"
810
require "utopia/path"
@@ -17,7 +19,11 @@ def update
1719

1820
install_root = root + "public/_components"
1921

20-
package_paths = expand_package_paths(package_root)
22+
# Fetch only production dependencies using `npm ls --production`
23+
production_packages = fetch_production_packages(package_root)
24+
package_paths = expand_package_paths(package_root).select do |path|
25+
production_packages.include?(path.basename.to_s)
26+
end
2127

2228
package_paths.each do |package_path|
2329
package_directory = package_path.relative_path_from(package_root)
@@ -41,6 +47,28 @@ def update
4147

4248
private
4349

50+
def fetch_production_packages(package_root)
51+
require "json"
52+
require "open3"
53+
54+
stdout, _status = Open3.capture2(NPM, "ls", "--production", "--json", chdir: package_root.to_s)
55+
56+
json = JSON.parse(stdout)
57+
58+
flatten_package_dependencies(json).sort.uniq
59+
end
60+
61+
def flatten_package_dependencies(json, into = [])
62+
if json["dependencies"]
63+
json["dependencies"].each do |name, details|
64+
into << name
65+
flatten_package_dependencies(details, into)
66+
end
67+
end
68+
69+
return into
70+
end
71+
4472
def expand_package_paths(root, into = [])
4573
paths = root.children.select(&:directory?)
4674

0 commit comments

Comments
 (0)