1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | 'use strict';
|
14 |
|
15 | var fs = require('fs');
|
16 | var pathUtil = require('path');
|
17 | var Ajv = require('ajv');
|
18 |
|
19 | var isFile = function(path) {
|
20 | try {
|
21 | return fs.statSync(path).isFile();
|
22 | } catch (e) {
|
23 | return false;
|
24 | }
|
25 | };
|
26 |
|
27 | var isDir = function(path) {
|
28 | try {
|
29 | return fs.statSync(path).isDirectory();
|
30 | } catch (e) {
|
31 | return false;
|
32 | }
|
33 | };
|
34 |
|
35 | var stripQueryAndAnchor = function(path) {
|
36 | path = path.split('?').shift();
|
37 | path = path.split('#').shift();
|
38 | return path;
|
39 | };
|
40 |
|
41 | var validateJsonStructure = function(extensionDescriptor) {
|
42 | var platform = extensionDescriptor.platform;
|
43 |
|
44 | if (!platform) {
|
45 | return 'the required property "platform" is missing.';
|
46 | }
|
47 |
|
48 | var extensionDescriptorSchema =
|
49 | require('@adobe/reactor-turbine-schemas/schemas/extension-package-' + platform + '.json');
|
50 |
|
51 | var ajv = new Ajv({
|
52 | schemaId: 'auto'
|
53 | });
|
54 | ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json'));
|
55 |
|
56 | if (!ajv.validate(extensionDescriptorSchema, extensionDescriptor)) {
|
57 | return ajv.errorsText();
|
58 | }
|
59 | };
|
60 |
|
61 | var validateViewBasePath = function(extensionDescriptor) {
|
62 | var absViewBasePath = pathUtil.resolve(
|
63 | process.cwd(),
|
64 | extensionDescriptor.viewBasePath
|
65 | );
|
66 |
|
67 | if (!isDir(absViewBasePath)) {
|
68 | return absViewBasePath + ' is not a directory.';
|
69 | }
|
70 | };
|
71 |
|
72 | var validateFiles = function(extensionDescriptor) {
|
73 | var paths = [];
|
74 | var platform = extensionDescriptor.platform;
|
75 |
|
76 | if (!platform) {
|
77 | return 'the required property "platform" is missing.';
|
78 | }
|
79 |
|
80 | if (extensionDescriptor.configuration) {
|
81 | paths.push(pathUtil.resolve(
|
82 | process.cwd(),
|
83 | extensionDescriptor.viewBasePath,
|
84 | stripQueryAndAnchor(extensionDescriptor.configuration.viewPath)
|
85 | ));
|
86 | }
|
87 |
|
88 | if (extensionDescriptor.main) {
|
89 | paths.push(pathUtil.resolve(
|
90 | process.cwd(),
|
91 | extensionDescriptor.main
|
92 | ));
|
93 | }
|
94 |
|
95 | ['events', 'conditions', 'actions', 'dataElements'].forEach(function(featureType) {
|
96 | var features = extensionDescriptor[featureType];
|
97 |
|
98 | if (features) {
|
99 | features.forEach(function(feature) {
|
100 | if (feature.viewPath) {
|
101 | paths.push(pathUtil.resolve(
|
102 | process.cwd(),
|
103 | extensionDescriptor.viewBasePath,
|
104 | stripQueryAndAnchor(feature.viewPath)
|
105 | ));
|
106 | }
|
107 |
|
108 | if (platform === 'web') {
|
109 | paths.push(pathUtil.resolve(
|
110 | process.cwd(),
|
111 | feature.libPath
|
112 | ));
|
113 | }
|
114 | });
|
115 | }
|
116 | });
|
117 |
|
118 | for (var i = 0; i < paths.length; i++) {
|
119 | var path = paths[i];
|
120 | if (!isFile(path)) {
|
121 | return path + ' is not a file.';
|
122 | }
|
123 | }
|
124 | };
|
125 |
|
126 |
|
127 | module.exports = function(extensionDescriptor) {
|
128 | var validators = [
|
129 | validateJsonStructure,
|
130 | validateViewBasePath,
|
131 | validateFiles
|
132 | ];
|
133 |
|
134 | for (var i = 0; i < validators.length; i++) {
|
135 | var error = validators[i](extensionDescriptor);
|
136 |
|
137 | if (error) {
|
138 | return 'An error was found in your extension.json: ' + error;
|
139 | }
|
140 | }
|
141 | };
|