-
Notifications
You must be signed in to change notification settings - Fork 39
/
generate-xml.js
212 lines (186 loc) · 5.19 KB
/
generate-xml.js
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
var fs = require('fs')
var map = require('async-each')
var join = require('path').join
var el = require('./lib/hyperxml')
module.exports = generateXml
function generateXml(options, cb) {
getComponents('.', options, function(err, components, ids) {
if (err) return cb(err)
var optionsWithIds = Object.create(options)
optionsWithIds.componentIds = ids
cb(null, installerFor(components, optionsWithIds).toXml({pretty: true}))
})
}
function installerFor (components, options) {
return el('Wix', {
xmlns: 'http://schemas.microsoft.com/wix/2006/wi'
}, [
el('Product', {
Id: '*',
Name: options.name,
UpgradeCode: options.upgradeCode,
Language: '1033',
Codepage: '1252',
Version: options.version,
Manufacturer: options.manufacturer
}, [
el('Property', {
Id: 'PREVIOUSVERSIONSINSTALLED',
Secure: 'yes'
}),
el('Upgrade', {
Id: options.upgradeCode
}, [
el('UpgradeVersion', {
Minimum: '0.0.0',
Property: "PREVIOUSVERSIONSINSTALLED",
IncludeMinimum: "yes",
IncludeMaximum: "no"
})
]),
options.runAfter ? el('Property', {
Id: "cmd",
Value: "cmd.exe"
}) : "",
options.runAfter ? el('CustomAction', {
Id: "LaunchApplication",
ExeCommand: "/c start \"\" \"%programfiles%\\"+options.name+"\\"+options.executable+"\"",
Execute: "",
Property: "cmd",
Impersonate: "yes"
}) : "",
el('InstallExecuteSequence', [
el('RemoveExistingProducts', {
Before: "InstallInitialize"
}),
options.runAfter ? el('Custom', {
Action: 'LaunchApplication',
After: 'InstallFinalize'
}, ["NOT Installed"]) : ""
]),
el('CustomAction', {
Id: "LaunchInstalledExe",
FileKey: "mainExecutableFile", // what goes here?
ExeCommand: "", // and here?
Execute: "immediate",
Impersonate: "yes",
Return: "asyncNoWait"
}),
el('Package', {
InstallerVersion: "200",
Compressed: "yes",
Comments: "Windows Installer Package",
InstallScope: options.localInstall ? "perUser" : "perMachine"
}),
el('Media', {
Id: '1',
Cabinet: 'app.cab',
EmbedCab: 'yes'
}),
el('Icon', {
Id: "icon.ico",
SourceFile: options.iconPath
}),
el('Property', {
Id: 'ARPPRODUCTICON',
Value: 'icon.ico'
}),
el('Directory', {
Id: 'TARGETDIR',
Name: 'SourceDir'
}, [
el('Directory', {
Id: getProgramsFolder(options),
}, [
el('Directory', {
Id: 'INSTALLDIR',
Name: options.name,
}, components)
])
]),
el('Feature', {
Id: 'App',
Level: '1'
}, options.componentIds.map(function(id) {
return el('ComponentRef', { Id: id })
}))
])
])
}
function getComponents (path, options, cb) {
var fullPath = join(options.source, path)
var ids = []
fs.readdir(fullPath, function (err, entries) {
if (err) return cb(err)
entries = entries.filter(function(entry) {
return entry !== '.DS_Store'
})
map(entries, function (entry, next) {
var subPath = join(path, entry)
fs.stat(join(fullPath, entry), function(err, stats) {
if (err) return next(err)
if (stats.isDirectory()) {
getComponents(subPath, options, function(err, components, subIds) {
if (err) return next(err)
ids.push.apply(ids, subIds)
next(null, el('Directory', {
Id: escapeId(subPath),
Name: entry
}, components))
})
} else {
var id = escapeId(subPath)
ids.push(id)
entry = entry.replace(new RegExp('\\$', 'g'), '$$$$')
var items = [
el('File', {
Id: id,
Source: join(fullPath, entry),
Name: entry
})
]
if (subPath === options.executable) {
items.push(el('Shortcut', {
Id: 'StartMenuShortcut',
Advertise: 'yes',
Icon: 'icon.ico',
Name: options.name,
Directory: 'ProgramMenuFolder',
WorkingDirectory: 'INSTALLDIR',
Description: options.description || ''
}), el('Shortcut', {
Id: 'DesktopShortcut',
Advertise: 'yes',
Icon: 'icon.ico',
Name: options.name,
Directory: 'DesktopFolder',
WorkingDirectory: 'INSTALLDIR',
Description: options.description || ''
}))
}
next(null, el('Component', {
Id: id,
Guid: '*'
}, items))
}
})
}, function (err, components) {
if (err) return cb(err)
cb(null, components, ids)
})
})
}
function getProgramsFolder (options) {
if (options.localInstall) {
return 'LocalAppDataFolder'
} else {
if (options.arch === 'x64') {
return 'ProgramFiles64Folder'
} else {
return 'ProgramFilesFolder'
}
}
}
function escapeId (id) {
return encodeURIComponent(id)
}