forked from gatarelib/pwa-asset-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·200 lines (185 loc) · 5.73 KB
/
cli.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
#!/usr/bin/env node
const meow = require('meow');
const puppets = require('./puppets');
const pwa = require('./helpers/pwa');
const preLogger = require('./helpers/logger');
const cli = meow(
`
$ pwa-asset-generator --help
Usage
$ pwa-asset-generator [source] [output-folder]
The assets will be saved to the folder where the command is executed if no output-folder provided.
Options
-b --background Page background to use when image source is provided: css value [default: transparent]
-o --opaque Making screenshots to be saved with a background color [default: true]
-p --padding Padding to use when image source provided: css value [default: "10%"]
-s --scrape Scraping Apple Human Interface Guidelines to fetch splash screen specs [default: true]
-m --manifest Web app manifest file path to automatically update manifest file with the generated images
-i --index Index html file path to automatically put splash screen meta tags in
-t --type Image type: png|jpeg [default: png]
-q --quality Image quality: 0...100 (Only for JPEG) [default: 100]
-h --splash-only Only generate splash screens [default: false]
-c --icon-only Only generate icons [default: false]
-l --landscape-only Only generate landscape splash screens [default: false]
-r --portrait-only Only generate portrait splash screens [default: false]
Examples
$ pwa-asset-generator logo.html .
$ pwa-asset-generator https://your-cdn-server.com/assets/logo.png . -t jpeg -q 90 --splash-only --portrait-only
$ pwa-asset-generator logo.svg ./assets --scrape false --icon-only
$ pwa-asset-generator https://raw.githubusercontent.com/onderceylan/pwa-asset-generator/HEAD/static/logo.png -p "15%" -b "linear-gradient(to right, #fa709a 0%, #fee140 100%)"
Flag examples
--background="rgba(255, 255, 255, .5)"
--opaque=false
--padding="10px"
--scrape=false
--manifest=./src/manifest.json
--index=./src/index.html
--type=jpeg
--quality=80
--splash-only
--icon-only
--landscape-only
--portrait-only
`,
{
flags: {
background: {
type: 'string',
alias: 'b',
default: 'transparent',
},
manifest: {
type: 'string',
alias: 'm',
},
index: {
type: 'string',
alias: 'i',
},
opaque: {
type: 'boolean',
alias: 'o',
default: true,
},
scrape: {
type: 'boolean',
alias: 's',
default: true,
},
padding: {
type: 'string',
alias: 'p',
default: '10%',
},
type: {
type: 'string',
alias: 't',
default: 'png',
},
quality: {
type: 'number',
alias: 'q',
default: 100,
},
splashOnly: {
type: 'boolean',
alias: 'h',
default: false,
},
iconOnly: {
type: 'boolean',
alias: 'c',
default: false,
},
landscapeOnly: {
type: 'boolean',
alias: 'l',
default: false,
},
portraitOnly: {
type: 'boolean',
alias: 'r',
default: false,
},
},
},
);
const source = cli.input[0];
let output = cli.input[1];
let options = cli.flags;
const logger = preLogger('cli');
const normalizeOnlyFlagPairs = (flag1Key, flag2Key, opts) => {
const stripOnly = key => key.replace('Only', '');
if (opts[flag1Key] && opts[flag2Key]) {
logger.warn(
`Hmm, you want to _only_ generate both ${stripOnly(
flag1Key,
)} and ${stripOnly(
flag2Key,
)} set. Ignoring --x-only settings as this is default behavior`,
);
return {
...opts,
[flag1Key]: false,
[flag2Key]: false,
};
}
return opts;
};
if (!source) {
logger.error('Please specify a URL or file path as a source');
process.exit(1);
}
options = normalizeOnlyFlagPairs('splashOnly', 'iconOnly', options);
options = normalizeOnlyFlagPairs('landscapeOnly', 'portraitOnly', options);
if (!output) {
output = '.';
}
(async () => {
try {
const savedImages = await puppets.generateImages(source, output, options);
const manifestJsonContent = pwa.generateIconsContentForManifest(
savedImages,
options.manifest,
);
const htmlContent = pwa.generateHtmlForIndexPage(
savedImages,
options.index,
);
if (!options.splashOnly) {
if (options.manifest) {
await pwa.addIconsToManifest(manifestJsonContent, options.manifest);
logger.success(
`Icons are saved to Web App Manifest file ${options.manifest}`,
);
} else if (!options.splashOnly) {
logger.warn(
'Web App Manifest file is not specified, printing out the content to console instead',
);
logger.success(
'Below is the icons content for your manifest.json file. You can copy/paste it manually',
);
process.stdout.write(
`\n${JSON.stringify(manifestJsonContent, null, 2)}\n\n`,
);
}
}
if (options.index) {
await pwa.addMetaTagsToIndexPage(htmlContent, options.index);
logger.success(
`iOS meta tags are saved to index html file ${options.index}`,
);
} else {
logger.warn(
'Index html file is not specified, printing out the content to console instead',
);
logger.success(
'Below is the iOS meta tags content for your index.html file. You can copy/paste it manually',
);
process.stdout.write(`\n${htmlContent}\n`);
}
} catch (e) {
logger.error(e);
process.exit(1);
}
})();