|
| 1 | +import * as d from '../../declarations'; |
| 2 | +import { buildError, buildJsonFileWarn } from '@utils'; |
| 3 | +import { isOutputTargetWww } from '../output-targets/output-utils'; |
| 4 | + |
| 5 | + |
| 6 | +export function validateManifestJson(config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx) { |
| 7 | + const outputTargets = config.outputTargets.filter(isOutputTargetWww); |
| 8 | + |
| 9 | + return Promise.all(outputTargets.map(async outputsTarget => { |
| 10 | + const manifestFilePath = config.sys.path.join(outputsTarget.dir, 'manifest.json'); |
| 11 | + |
| 12 | + try { |
| 13 | + const manifestContent = await compilerCtx.fs.readFile(manifestFilePath); |
| 14 | + |
| 15 | + try { |
| 16 | + const manifestData = JSON.parse(manifestContent); |
| 17 | + await validateManifestJsonData(config, compilerCtx, buildCtx, manifestFilePath, manifestData); |
| 18 | + |
| 19 | + } catch (e) { |
| 20 | + const err = buildError(buildCtx.diagnostics); |
| 21 | + err.header = `Invalid manifest.json`; |
| 22 | + err.absFilePath = manifestFilePath; |
| 23 | + } |
| 24 | + |
| 25 | + } catch (e) {} |
| 26 | + })); |
| 27 | +} |
| 28 | + |
| 29 | + |
| 30 | +async function validateManifestJsonData(config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx, manifestFilePath: string, manifestData: any) { |
| 31 | + if (Array.isArray(manifestData.icons)) { |
| 32 | + await Promise.all(manifestData.icons.map((manifestIcon: any) => { |
| 33 | + return validateManifestJsonIcon(config, compilerCtx, buildCtx, manifestFilePath, manifestIcon); |
| 34 | + })); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | + |
| 39 | +async function validateManifestJsonIcon(config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx, manifestFilePath: string, manifestIcon: any) { |
| 40 | + let iconSrc = manifestIcon.src; |
| 41 | + if (typeof iconSrc !== 'string') { |
| 42 | + const msg = `Manifest icon missing "src"`; |
| 43 | + buildJsonFileWarn(compilerCtx, buildCtx.diagnostics, manifestFilePath, msg, `"icons"`); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + if (iconSrc.startsWith('/')) { |
| 48 | + iconSrc = iconSrc.substr(1); |
| 49 | + } |
| 50 | + |
| 51 | + const manifestDir = config.sys.path.dirname(manifestFilePath); |
| 52 | + const iconPath = config.sys.path.join(manifestDir, iconSrc); |
| 53 | + const hasAccess = await compilerCtx.fs.access(iconPath); |
| 54 | + if (!hasAccess) { |
| 55 | + const msg = `Unable to find manifest icon "${manifestIcon.src}"`; |
| 56 | + buildJsonFileWarn(compilerCtx, buildCtx.diagnostics, manifestFilePath, msg, `"${manifestIcon.src}"`); |
| 57 | + } |
| 58 | +} |
0 commit comments