Skip to content

Commit 570d741

Browse files
committed
feat(build): add manifest.json validation
1 parent fd1f579 commit 570d741

3 files changed

Lines changed: 65 additions & 1 deletion

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import * as d from '../../declarations';
2+
import { validateManifestJson } from '../html/validate-manifest-json';
23
import { validatePackageJson } from '../types/validate-package-json';
34

45

56
export function validateFiles(config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx) {
7+
if (buildCtx.hasError) {
8+
return null;
9+
}
10+
611
return Promise.all([
12+
validateManifestJson(config, compilerCtx, buildCtx),
713
validatePackageJson(config, compilerCtx, buildCtx)
814
]);
915
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
}

src/compiler/types/validate-package-json.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { getComponentsDtsTypesFilePath, isOutputTargetDistCollection } from '../
44

55

66
export function validatePackageJson(config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx) {
7-
if (buildCtx.packageJson == null || buildCtx.hasError) {
7+
if (buildCtx.packageJson == null) {
88
return null;
99
}
1010

0 commit comments

Comments
 (0)