UNPKG

3.71 kBJavaScriptView Raw
1/***************************************************************************************
2 * (c) 2017 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 ****************************************************************************************/
12
13'use strict';
14
15var fs = require('fs');
16var pathUtil = require('path');
17var Ajv = require('ajv');
18
19var isFile = function(path) {
20 try {
21 return fs.statSync(path).isFile();
22 } catch (e) {
23 return false;
24 }
25};
26
27var isDir = function(path) {
28 try {
29 return fs.statSync(path).isDirectory();
30 } catch (e) {
31 return false;
32 }
33};
34
35var stripQueryAndAnchor = function(path) {
36 path = path.split('?').shift();
37 path = path.split('#').shift();
38 return path;
39};
40
41var 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
61var 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
72var 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
127module.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};