1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 | const path_1 = tslib_1.__importDefault(require("path"));
|
5 | const stream_1 = tslib_1.__importDefault(require("stream"));
|
6 | const plugin_error_1 = tslib_1.__importDefault(require("plugin-error"));
|
7 | const pngjs_1 = require("@fitbit/pngjs");
|
8 | const diagnostics_1 = require("./diagnostics");
|
9 | const ProjectConfiguration_1 = require("./ProjectConfiguration");
|
10 | const EXPECTED_ICON_HEIGHT = 80;
|
11 | const EXPECTED_ICON_WIDTH = 80;
|
12 | const PLUGIN_NAME = 'validateIcon';
|
13 | function getPNGDimensions(buffer) {
|
14 | return new Promise((resolve, reject) => {
|
15 | const png = new pngjs_1.PNG();
|
16 | png.on('metadata', (metadata) => {
|
17 | png.on('parsed', (parsed) => resolve({
|
18 | width: png.width,
|
19 | height: png.height,
|
20 | }));
|
21 | });
|
22 | png.on('error', reject);
|
23 | png.parse(buffer);
|
24 | });
|
25 | }
|
26 | function validateIcon({ projectConfig, onDiagnostic, }) {
|
27 | if (projectConfig.appType === ProjectConfiguration_1.AppType.CLOCKFACE) {
|
28 | return new stream_1.default.PassThrough({ objectMode: true });
|
29 | }
|
30 | let iconExists = false;
|
31 | return new stream_1.default.Transform({
|
32 | objectMode: true,
|
33 | transform(file, _, cb) {
|
34 | if (file.isNull() ||
|
35 | file.relative !== path_1.default.normalize(projectConfig.iconFile)) {
|
36 | return cb(undefined, file);
|
37 | }
|
38 | iconExists = true;
|
39 | if (file.isBuffer()) {
|
40 | getPNGDimensions(file.contents)
|
41 | .then((metadata) => {
|
42 | const iconHeight = metadata.height;
|
43 | const iconWidth = metadata.width;
|
44 | if (iconWidth !== EXPECTED_ICON_WIDTH ||
|
45 | iconHeight !== EXPECTED_ICON_HEIGHT) {
|
46 | const errorMessage = `Icon was of invalid size, expected ${EXPECTED_ICON_WIDTH}x${EXPECTED_ICON_HEIGHT}, got ${iconWidth}x${iconHeight}`;
|
47 | return cb(new plugin_error_1.default(PLUGIN_NAME, errorMessage, {
|
48 | fileName: file.relative,
|
49 | }));
|
50 | }
|
51 | cb(undefined, file);
|
52 | })
|
53 | .catch((err) => cb(new plugin_error_1.default(PLUGIN_NAME, err, { fileName: file.relative })));
|
54 | }
|
55 | else {
|
56 | cb(new plugin_error_1.default(PLUGIN_NAME, 'Icon file is not a buffer', {
|
57 | fileName: file.relative,
|
58 | }));
|
59 | }
|
60 | },
|
61 | flush(cb) {
|
62 | if (!iconExists) {
|
63 | onDiagnostic({
|
64 | category: diagnostics_1.DiagnosticCategory.Warning,
|
65 | messageText: `There is no app icon present in this project. To set an app icon, add a ${EXPECTED_ICON_WIDTH}x${EXPECTED_ICON_HEIGHT} PNG file named ${projectConfig.iconFile} to your project.`,
|
66 | });
|
67 | }
|
68 | cb();
|
69 | },
|
70 | });
|
71 | }
|
72 | exports.default = validateIcon;
|