UNPKG

3.07 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const path_1 = tslib_1.__importDefault(require("path"));
5const stream_1 = tslib_1.__importDefault(require("stream"));
6const plugin_error_1 = tslib_1.__importDefault(require("plugin-error"));
7const pngjs_1 = require("@fitbit/pngjs");
8const diagnostics_1 = require("./diagnostics");
9const ProjectConfiguration_1 = require("./ProjectConfiguration");
10const EXPECTED_ICON_HEIGHT = 80;
11const EXPECTED_ICON_WIDTH = 80;
12const PLUGIN_NAME = 'validateIcon';
13function 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}
26function 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}
72exports.default = validateIcon;