/
node.cr
56 lines (49 loc) · 1.4 KB
/
node.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
module MStrap
module Runtimes
# Node runtime management implmentation. It contains methods for interacting
# with Node via ASDF and bootstrapping a Node project based on conventions.
class Node < Runtime
def asdf_plugin_name
"nodejs"
end
def language_name : String
"node"
end
def bootstrap
if File.exists?("yarn.lock")
cmd "brew install yarn", quiet: true
skip_reshim { cmd "yarn install", quiet: true }
elsif File.exists?("package.json")
skip_reshim { cmd "npm install", quiet: true }
end
end
def install_packages(packages : Array(Defs::PkgDef), runtime_version : String? = nil) : Bool
cmd_args = ["install", "-g"]
packages.each do |pkg|
if pkg.version
cmd_args << "#{pkg.name}@#{pkg.version}"
else
cmd_args << pkg.name
end
end
skip_reshim { asdf_exec "npm", cmd_args, runtime_version: runtime_version }
end
def matches? : Bool
[
"yarn.lock",
"package.json",
".node-version",
].any? do |file|
File.exists?(file)
end
end
private def skip_reshim(&)
ENV["ASDF_SKIP_RESHIM"] = "1"
yield
ensure
ENV.delete("ASDF_SKIP_RESHIM")
asdf_exec "asdf", ["reshim", "nodejs"]
end
end
end
end