1 | var fs = require('fs-extra');
|
2 | var path = require('path');
|
3 | var semver = require('semver');
|
4 | var log = require('../util/log');
|
5 | var isValidJSName = require('../util/isValidJSName');
|
6 |
|
7 | module.exports = {
|
8 | |
9 |
|
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 |
|
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 |
|
55 |
|
56 | function validateManifest(manifest){
|
57 |
|
58 |
|
59 | requireProperty(manifest, 'name');
|
60 | requireProperty(manifest, 'version');
|
61 |
|
62 |
|
63 | manifest.mapping.forEach(function(dataPoint){
|
64 | requireProperty(dataPoint, 'dataSetId');
|
65 | requireProperty(dataPoint, 'alias');
|
66 | requireProperty(dataPoint, 'fields');
|
67 | ensureValidAlias(dataPoint.alias);
|
68 |
|
69 |
|
70 | dataPoint.fields.forEach(function(field){
|
71 | requireProperty(field, 'alias');
|
72 | requireProperty(field, 'columnName');
|
73 | ensureValidAlias(field.alias);
|
74 | });
|
75 | });
|
76 |
|
77 |
|
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 |
|
94 |
|
95 | function 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 | }
|