forked from koush/electron-chrome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
package.js
276 lines (248 loc) · 7.85 KB
/
package.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
const process = require('process');
const path = require('path');
const fs = require('fs');
const AdmZip = require('adm-zip');
const mkdirp = require('mkdirp');
const os = require('os');
const electronInstaller = require('electron-winstaller');
var deleteRecursive = function(inPath) {
// existsSync follows symlinks and crap, so just try to delete straight up first
try {
fs.unlinkSync(inPath);
}
catch (ignore) {
}
if (fs.existsSync(inPath) && fs.lstatSync(inPath).isDirectory()) {
fs.readdirSync(inPath).forEach(function(file,index) {
var curPath = path.join(inPath, file);
deleteRecursive(curPath);
});
fs.rmdirSync(inPath);
}
};
const buildPath = path.join(__dirname, 'build');
deleteRecursive(buildPath);
mkdirp.sync(buildPath);
var appDir;
var appId;
var runtimeId;
var assets;
for (var arg of process.argv) {
if (arg.startsWith('--app-id=')) {
appId = arg.substring('--app-id='.length)
}
else if (arg.startsWith('--app-dir=')) {
appDir = path.resolve(arg.substring('--app-dir='.length))
}
else if (arg.startsWith('--runtime-id=')) {
runtimeId = arg.substring('--runtime-id='.length)
}
else if (arg.startsWith('--assets=')) {
assets = path.resolve(arg.substring('--assets='.length))
}
}
if (!appDir) {
console.error('missing --app-dir argument');
console.error('example: --app-dir=/path/to/chrome/app')
process.exit(-1);
}
var manifest = JSON.parse(fs.readFileSync(path.join(appDir, 'manifest.json')).toString());
var chrome;
try {
chrome = JSON.parse(fs.readFileSync(path.join(appDir, 'electron.json')).toString());
}
catch (e) {
}
runtimeId = runtimeId || (chrome && chrome.runtimeId);
if (!runtimeId) {
console.warn('missing --runtime-id')
console.warn('Chrome runtime will only be updated with full electron upgrades.')
console.warn('');
}
else {
console.log(`chrome runtime id ${runtimeId}`)
console.log();
}
function withAppId() {
// grab largest
var key = Object.keys(manifest.icons).sort((a,b) => parseInt(a) < parseInt(b))[0].toString();
var icon = path.join(appDir, manifest.icons[key]);
var iconScript = null;
if (os.platform() == 'win32')
iconScript = 'icon.bat';
else if (os.platform() == 'darwin')
iconScript = './icon.sh';
if (iconScript) {
console.log(iconScript);
var child = require('child_process').exec(`${iconScript} ${icon}`);
child.stdout.pipe(process.stdout)
child.on('exit', function() {
console.log('icon creation done')
startPackager();
})
child.on('error', function() {
console.error(arguments);
})
}
else {
startPackager();
}
}
const platformIconExtensions = {
win32: '.ico',
darwin: '.icns',
linux: '.png',
}
const platformIcon = path.join(process.cwd(), 'build/MyIcon' + platformIconExtensions[os.platform()]);
function startPackager() {
var packager = require('electron-packager')
var out = path.join(__dirname, 'build');
packager({
dir: __dirname,
out: out,
platform: os.platform(),
arch: 'all',
name: manifest.name,
icon: platformIcon,
appVersion: manifest.version,
buildVersion: manifest.version,
appCopyright: 'Copyright ' + (manifest.author || manifest.name),
overwrite: true,
// windows file details (needed for shortcut and stuff)
win32metadata: {
CompanyName: manifest.author || manifest.name,
FileDescription: manifest.name,
ProductName: manifest.name,
InternalName: manifest.name,
},
// mac signing and url handler
osxSign: true,
protocols: [
{
name: manifest.name,
schemes: [ `ec-${appId}` ]
}
],
// all: true,
afterCopy: [function(buildPath, electronVersion, platform, arch, callback) {
var ncp = require('ncp').ncp;
console.log(appDir, buildPath);
var electronJson = path.join(buildPath, 'package.json');
var electronPackage = JSON.parse(fs.readFileSync(electronJson).toString());
electronPackage.name = manifest.name;
electronPackage.description = manifest.description;
electronPackage.version = manifest.version;
chrome = chrome || {};
chrome.runtimeId = chrome.runtimeId || runtimeId;
chrome.appId = chrome.appId || appId;
electronPackage.chrome = chrome;
fs.writeFileSync(electronJson, JSON.stringify(electronPackage, null, 2));
console.log('copying app into place');
ncp(appDir, path.join(buildPath, 'unpacked-crx'), {
clobber: false,
dereference: true,
},
function (err) {
if (err) {
console.error(err);
process.exit(-1);
}
console.log('app copied into place');
if (!assets) {
callback();
return;
}
console.log('copying platform-assets into place for', os.platform());
var platformAssets = path.join(assets, os.platform());
var platformAssetsDest = path.join(buildPath, 'platform-assets', os.platform());
mkdirp.sync(platformAssetsDest);
ncp(platformAssets, platformAssetsDest, {
clobber: false,
dereference: true,
}, function(err) {
if (err) {
console.error(err);
process.exit(-1);
}
console.log('platform-assets copied into place');
callback();
})
});
}]
}, function (err, appPaths) {
if (err) {
console.error(err);
throw err;
}
function makeMacZip(appPath) {
var child = require('child_process').spawn('zip', ['-ry', `${manifest.name}-mac.zip`, `${manifest.name}.app`], {
cwd: appPath,
});
child.stdout.pipe(process.stdout);
child.on('exit', function() {
console.log('zip complete');
})
}
appPaths
.filter(appPath => appPath.indexOf('darwin') != -1)
.forEach(appPath => {
console.log(appPath);
/*
var infoPlist = path.join(appPath, manifest.name + '.app', 'Contents', 'Info.plist');
console.log(infoPlist);
var child = require('child_process').exec(`defaults write ${infoPlist} CFBundleURLTypes '<array><dict><key>CFBundleURLName</key><string>${manifest.name}</string><key>CFBundleURLSchemes</key><array><string>ec-${appId}</string></array></dict></array>'`)
child.stdout.pipe(process.stdout);
child.on('exit', function() {
makeMacZip();
})
*/
makeMacZip(appPath);
})
appPaths
.filter(appPath => appPath.indexOf('win32') != -1)
.forEach(appPath => {
var key = Object.keys(manifest.icons).sort((a,b) => parseInt(a) < parseInt(b))[0].toString();
var icon = path.join(appDir, manifest.icons[key]);
var iconUrl = 'file://' + icon.replace(/\\/g, '/').replace(':', '');
iconUrl = 'file://' + path.join(process.cwd(), platformIcon).replace(/\\/g, '/').replace(':', '');
console.log(iconUrl);
var resultPromise = electronInstaller.createWindowsInstaller({
appDirectory: appPath,
outputDirectory: appPath + '-installer',
authors: manifest.author || manifest.name,
version: manifest.version,
exe: manifest.name + '.exe',
setupExe: path.basename(appPath) + '.exe',
productName: manifest.name,
title: manifest.name,
name: manifest.name,
iconUrl: iconUrl,
description: manifest.description,
noMsi: true,
});
resultPromise.then(() => console.log("Windows Intaller created."), (e) => { console.log(`Windows Installer failed: ${e.message}`); console.log(e); } );
})
})
}
function needAppId() {
console.error('missing --app-id argument');
console.error('example: --app-id=gidgenkbbabolejbgbpnhbimgjbffefm')
process.exit(-1);
}
if (!appId) {
if (!manifest.key) {
needAppId();
return;
}
require('./chrome/main/chrome-app-id.js').calculateId(manifest.key)
.then(id => {
appId = id;
withAppId();
})
.catch(() => {
needAppId();
})
}
else {
withAppId();
}