This repository has been archived by the owner on May 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 142
/
pwabuilder.js
201 lines (178 loc) · 7.31 KB
/
pwabuilder.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
#!/usr/bin/env node
'use strict';
var Q = require('q');
var lib = require('pwabuilder-lib');
var log = lib.log,
packageTools = lib.packageTools,
platformTools = lib.platformTools,
manifestTools = lib.manifestTools,
validations = lib.validations;
var commands = require('./commands');
// Get the available formats for the --forceManifestFormat parameter
var availableManifestFormats = manifestTools.listAvailableManifestFormats();
function checkParameters(program) {
var unknownArgs = 0;
if (program.args.length > 0) {
var command = program.args[0].toLowerCase();
switch (command) {
case 'run':
unknownArgs = 3;
program.run = true;
break;
case 'package':
unknownArgs = 2;
program.package = true;
break;
case 'platform':
unknownArgs = 4;
program.platform = true;
break;
case 'open':
unknownArgs = 3;
program.open = true;
break;
case 'visualstudio':
unknownArgs = 1;
program.visualstudio = true;
break;
default:
unknownArgs = 1;
break;
}
} else {
if (!program.manifest) {
return 'You must specify either the web site URL or the location of a W3C manifest (-m | --manifest).';
}
}
if (program.args.length > unknownArgs) {
return 'Unexpected parameters - [' + program.args.slice(unknownArgs).join() + '].';
}
// check platforms
// TODO: loading registered platforms twice, by calling platformsValid and to generate the usage text. Consolidate!
if (program.platforms) {
var platforms = program.platforms.split(/[\s,]+/);
if (!validations.platformsValid(platforms)) {
return 'Invalid platform(s) specified.';
}
}
// check log level
if (program.loglevel) {
if (!validations.logLevelValid(program.loglevel)) {
return 'Invalid loglevel specified. Valid values are: debug, info, warn, error.';
}
}
if (program.forceManifestFormat) {
if (!validations.manifestFormatValid(program.forceManifestFormat)) {
return 'Invalid manifest format specified. Valid values are: ' + availableManifestFormats.join(', ') + '.';
}
}
}
// get the list of registered platforms
var availablePlatforms = platformTools.listPlatforms();
// dynamically generates the help text with the list of registered
// platforms and splits it into multiple lines so that it doesn't
// break the layout of the usage text
function getPlatformHelpText() {
// offset of the column where the parameter help text starts
var columnOffset = 38;
// maximum width of the help text
var maxWidth = 80;
return availablePlatforms.reduce(function (list, platform) {
var segmentLength = list.length - list.lastIndexOf('\n') - 1;
if (segmentLength > maxWidth - columnOffset) {
list += '\n';
}
return list + '[' + (list ? ',' : '') + platform + ']';
}, '').replace(/\n/g, '\n' + new Array(columnOffset - 1).join(' '));
}
var program = require('commander')
.usage('<website-url> [options]' +
'\n pwabuilder -m <manifest-location> [options]' +
'\n options:' +
'\n -d | --directory, -s | --short-name, -l | --loglevel, -p | --platforms' +
'\n -i | --image, -m | --manifest, -f | --forceManifestFormat, -c | --crosswalk' +
'\n -or-' +
'\n pwabuilder package [project-directory] [options]' +
'\n options:' +
'\n -l | --loglevel, -p | --platforms, -S | --Sign, -W | --DotWeb, -a | --AutoPublish' +
'\n' +
'\n -or-' +
'\n pwabuilder platform add <platform-id> <source> [options]' +
'\n pwabuilder platform remove <platform-id> [options]' +
'\n pwabuilder platform list [options]' +
'\n options:' +
'\n -l | --loglevel' +
'\n' +
'\n -or-' +
'\n pwabuilder run <platform> [project-directory] [options]' +
'\n options:' +
'\n -l | --loglevel' +
'\n' +
'\n -or-' +
'\n pwabuilder open <platform> [project-directory] [options]' +
'\n options:' +
'\n -l | --loglevel')
.option('-d, --directory <app-dir>', 'path to the generated project files')
.option('-s, --shortname <short-name>', 'application short name')
.option('-l, --loglevel <log-level>', 'debug|info|warn|error', 'warn')
.option('-p, --platforms <platforms>', getPlatformHelpText())
.option('-m, --manifest <manifest-location>', 'location of the W3C Web App manifest\n ' +
'file (URL or local path)')
.option('-i, --image <image-location>', 'local path to the image file used to\n ' +
'generate missing icons in the manifest')
.option('-c, --crosswalk', 'enable Crosswalk for Android', false)
.option('-S, --Sign', 'return a signed package for Windows 10', false)
.option('-w, --webAppToolkit', 'adds the Web App Toolkit cordova plugin', false)
.option('-f, --forceManifestFormat <format>', availableManifestFormats.join('|'))
.option('-W, --DotWeb', 'generate a .web package for Windows 10', false)
.option('-a, --AutoPublish', 'auto-publish a package for Windows 10', false)
.parse(process.argv);
if (!process.argv.slice(2).length) {
program.help();
}
var validationResult = checkParameters(program);
if (validationResult) {
log.error(validationResult);
process.exit(1);
}
global.logLevel = program.loglevel;
log.setLevel(global.logLevel);
if (process.env.NODE_ENV === 'development') {
Q.longStackSupport = true;
}
packageTools.checkForUpdate(function (err, updateAvailable) {
if (!err && updateAvailable) {
log.write();
log.write('*******************************************************************************');
log.write('A new version of pwabuilder is available (v' + updateAvailable + ').');
log.write('We recommend that you upgrade.');
log.write('*******************************************************************************');
log.write();
}
var command;
if (program.run) {
command = commands.run(program);
}
else if (program.open) {
command = commands.open(program);
}
else if (program.visualstudio) {
command = commands.visualstudio(program);
}
else if (program.package) {
command = commands.package(program);
}
else if (program.platform) {
command = commands.platform(program);
}
else {
command = commands.generate(program);
}
command.catch(function (err) {
var errmsg = err.getMessage();
if (log.getLevel() !== log.levels.DEBUG) {
errmsg += '\nFor more information, run pwabuilder with the diagnostics level set to debug (e.g. pwabuilder [...] -l debug)';
}
log.error(errmsg);
});
});