-
Notifications
You must be signed in to change notification settings - Fork 1
/
rake-nodejs.rb
99 lines (81 loc) · 2.34 KB
/
rake-nodejs.rb
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
require "albacore"
require "runcommandwithfail"
require "rake-filesystem"
require "json"
class NodeJs
include Albacore::Task
include RunCommandWithFail
attr_accessor :base
attr_accessor :script
attr_accessor :parameters
def initialize
@base = "."
if FileSystem.FindExecutable("node") == nil
puts red("Node.js not found! Please install Node.js")
end
super()
end
def execute
@command = "node"
@working_directory = @base
params = [@script]
params << @parameters unless @parameters.nil? || @parameters.length == 0
run_command("Node.js", params)
end
end
class Npm
include Albacore::Task
include RunCommandWithFail
# base location of the web project (also location of package.json)
# (default ".")
attr_accessor :base
# npm command to execute
# (default "update")
attr_accessor :command
# parameters to send to npm command
# (no default [])
attr_accessor :parameters
def initialize
@base = "."
@command = "update"
if FileSystem.FindExecutable("npm") == nil
puts red("NPM not found! Please install Node.js")
end
super()
end
def run command, *parameters
npm_command = @command
begin
@command = "npm"
@working_directory = @base
params = [command]
params << parameters unless parameters.nil? || parameters.length == 0
run_command("npm", params)
ensure
@command = npm_command
end
end
def update *parameters
run "update", parameters
end
def install *parameters
run "install", parameters
end
def require packages
return if packages.nil? || packages.length == 0
packageJson = File.join(@base, "package.json")
json = File.read(packageJson)
obj = JSON.parse(json)
devDependencies = obj["devDependencies"]
packages.each do |package, version|
devDependencies[package] = version
end
devDependencies.keys.sort.each { |key| devDependencies[key] = devDependencies.delete key }
File.write(packageJson, JSON.pretty_generate(obj))
install
end
def execute
install
run @command, @parameters
end
end