-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnwjs-downloader.js
109 lines (91 loc) · 2.95 KB
/
nwjs-downloader.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
require('shelljs/global');
var BASE_URL = "https://dl.nwjs.io/v{{NWJS_VERSION}}/nwjs{{DEBUG}}-v{{NWJS_VERSION}}-{{PLATFORM}}-{{ARCHITECTURE}}.{{PACKAGE_FORMAT}}",
FLAVOUR = "nwjs{{DEBUG}}-v{{NWJS_VERSION}}-{{PLATFORM}}-{{ARCHITECTURE}}",
NWJS_VERSION = '0.20.1',
DEBUG = false,
PLATFORMS = ['linux', 'osx', 'win'],
ARCHITECTURES = {
'linux': ['ia32', 'x64'],
'osx': ['x64'],
'win': ['ia32', 'x64']
},
PACKAGE_FORMATS = {
'linux': 'tar.gz',
'osx': 'zip',
'win': 'zip'
},
NWJS_DOWNLOAD_DIR = require('os').homedir() + "/nwjs_download_cache";
function buildUrl() {
return BASE_URL
.replace(/{{NWJS_VERSION}}/g, NWJS_VERSION)
.replace(/{{DEBUG}}/g, DEBUG ? "-sdk" : "")
.replace(/{{PLATFORM}}/g, PLATFORM)
.replace(/{{ARCHITECTURE}}/g, ARCHITECTURE)
.replace(/{{PACKAGE_FORMAT}}/g, PACKAGE_FORMATS[PLATFORM]);
}
function getFlavour() {
return FLAVOUR
.replace(/{{NWJS_VERSION}}/g, NWJS_VERSION)
.replace(/{{DEBUG}}/g, DEBUG ? "-sdk" : "")
.replace(/{{PLATFORM}}/g, PLATFORM)
.replace(/{{ARCHITECTURE}}/g, ARCHITECTURE);
}
function prepareDownloadDir() {
if (!test('-d', NWJS_DOWNLOAD_DIR)) {
mkdir(NWJS_DOWNLOAD_DIR);
}
}
var public = {
setVersion: function(version) {
NWJS_VERSION = version;
return public;
},
setDebug: function(debug) {
DEBUG = debug;
return public;
},
setPlatform: function(platform) {
PLATFORM = platform;
return public;
},
setArchitecture: function(architecture) {
ARCHITECTURE = architecture == 'x32' ? 'ia32' : architecture;
return public;
},
download: function() {
prepareDownloadDir();
var flavour = getFlavour();
var downloaded = test('-f', NWJS_DOWNLOAD_DIR + "/" + flavour + "." + PACKAGE_FORMATS[PLATFORM]);
if (!downloaded) {
pushd(NWJS_DOWNLOAD_DIR);
var url = buildUrl();
echo(flavour + " not yet found. Downloading from " + url);
exec("curl -O " + url);
popd();
}
return public;
},
extract: function() {
var flavour = getFlavour();
pushd(NWJS_DOWNLOAD_DIR);
extracted = test('-d', flavour);
if (!extracted) {
echo(flavour + " is not yet extracted.. extracting.");
switch (PACKAGE_FORMATS[PLATFORM]) {
case 'tar.gz':
exec("tar -zxf ./" + flavour + ".tar.gz");
break;
case 'zip':
exec("unzip ./" + flavour + ".zip");
break;
}
}
popd();
echo("Extracted path for " + flavour + ": " + NWJS_DOWNLOAD_DIR + "/" + flavour);
return NWJS_DOWNLOAD_DIR + "/" + flavour;
},
get: function() {
return public.download().extract();
}
}
module.exports = public;