UNPKG

2.62 kBJavaScriptView Raw
1var fs = require('fs-extra');
2var path = require('path');
3var semver = require('semver');
4var log = require('../util/log');
5var isValidJSName = require('../util/isValidJSName');
6
7module.exports = {
8 /**
9 * Get the local manifest.json file, ensuring it's valid.
10 */
11 get: function(manifestPath = 'manifest.json', verify = true){
12 var manifest;
13 try {
14 manifest = fs.readFileSync(path.resolve(manifestPath));
15 }
16 catch (e){
17 if (verify){
18 return log.fail('No manifest.json file in this directory', 'Please publish from a directory containing a Custom App design manifest.json file');
19 }
20 else{
21 return;
22 }
23 }
24
25 try {
26 manifest = JSON.parse(manifest);
27 }
28 catch (e){
29 return log.fail('Your manifest.json file is not valid JSON', e.message);
30 }
31 if (verify){
32 validateManifest(manifest);
33 }
34 return manifest;
35 },
36
37 /**
38 * update the manifest (in memory and on disk)
39 */
40 persistDesignId: function(design, manifest){
41 manifest.id = design.id;
42 this.updateManifest(manifest);
43 return design;
44 },
45
46 updateManifest: function(manifest){
47 fs.writeJsonSync('manifest.json', manifest, {spaces: 2});
48 }
49}
50
51
52
53/**
54* Validate the manifest file.
55*/
56function validateManifest(manifest){
57
58 // validate manifest file for required fields
59 requireProperty(manifest, 'name');
60 requireProperty(manifest, 'version');
61
62 // validate required mapping fields
63 manifest.mapping.forEach(function(dataPoint){
64 requireProperty(dataPoint, 'dataSetId');
65 requireProperty(dataPoint, 'alias');
66 requireProperty(dataPoint, 'fields');
67 ensureValidAlias(dataPoint.alias);
68
69 // validate dataPoint mapping fields
70 dataPoint.fields.forEach(function(field){
71 requireProperty(field, 'alias');
72 requireProperty(field, 'columnName');
73 ensureValidAlias(field.alias);
74 });
75 });
76
77 // validate semver
78 if (!semver.valid(manifest.version)){
79 log.fail('Invalid version', 'Please make sure the `version` in your manifest.json file is a valid semantic version. See http://semver.org/');
80 }
81
82 function requireProperty(obj, prop){
83 if (!obj[prop]){
84 log.fail('Missing required property: ' + prop, 'Please add the missing `'+ prop +'` property to your manifest.json file.');
85 }
86 }
87
88 return manifest;
89}
90
91
92/**
93 * Alias must be a valid JS property
94 */
95function ensureValidAlias(name){
96 if (!isValidJSName(name)){
97 log.fail('Alias names must be valid JavaScript properties', 'Please rename the ' + JSON.stringify(name) + ' alias and update any /data/v1/<alias> calls with the new alias name');
98 }
99}