-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
125 lines (118 loc) · 3.95 KB
/
utils.js
File metadata and controls
125 lines (118 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const fs = require('fs');
let colors = require('colors/safe');
const yaml = require('js-yaml');
colors.setTheme({
silly: 'rainbow',
input: 'grey',
verbose: 'cyan',
prompt: 'grey',
info: 'green',
data: 'grey',
help: 'cyan',
warn: 'yellow',
debug: 'blue',
error: 'red'
});
String.prototype.capitalize = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
}
const buildCached = (tsfile,manifest,table) => {
tsfile.cache[table] = {
scopeRead : false,
scopeList : false,
scopeCreate : false,
scopeDelete : false,
scopeUpdate : false
}
let isCached = false;
Object.keys(manifest).forEach(acl => {
let expired = 1;
//console.log((typeof(manifest[acl]) === 'object') && ('cache' in manifest[acl]))
if((typeof(manifest[acl]) === 'object') && ('cache' in manifest[acl])){
if(typeof(manifest[acl].cache) === 'object' && 'expired' in manifest[acl].cache){
tsfile.cache[table]['scope'+acl.capitalize()] = manifest[acl].cache.expired;
isCached = true;
}
else {
if(manifest[acl].cache){
tsfile.cache[table]['scope'+acl.capitalize()] = expired;
isCached = true;
}
else{
tsfile.cache[table]['scope'+acl.capitalize()] = manifest[acl].cache;
}
}
}
});
if(!isCached){
tsfile.cache[table] = {};
}
}
const buildParams = (tsfile,manifest,table,acls) => {
if(acls === 'read' || acls === 'list'){
if(manifest[acls] && manifest[acls].params && (manifest[acls].params.length > 0)){
tsfile[acls+"Params"][table] = manifest[acls].params
}
}
}
module.exports = {
writeFile: (path, data) => {
try {
fs.writeFile(path, data, function (err) {
if (err) {
return console.log(err);
}
});
}
catch (e) {
return false;
}
return true;
},
log: colors,
manifest: (p,errorMsg="ERROR") => {
if (!fs.existsSync(p.file) && !fs.existsSync("tarsius.yml")) {
console.log(colors.error(errorMsg));
p.outputHelp();
process.exit(0);
}
let tsfile;
try {
tsfile = {
tables : {},
cache : {},
acls : {},
readParams : {},
listParams : {}
}
let manifest = yaml.safeLoad(fs.readFileSync(p.file, 'utf8'));
// parse tables
Object.keys(manifest.objects).forEach((table)=>{
if(typeof(manifest.objects[table].fields) == 'string'){
let include = manifest.objects[table].fields;
if(fs.lstatSync(include).isFile()){
let manifestDep = yaml.safeLoad(fs.readFileSync(include, 'utf8'));
tsfile.tables[table] = manifestDep.objects[table].fields;
let acls = manifest.objects[table].acls;
tsfile.acls[table] = Object.keys(acls);
buildCached(tsfile,acls,table)
buildParams(tsfile, acls,table,"read");
buildParams(tsfile, acls,table,"list");
}
}
else {
tsfile.tables[table] = manifest.objects[table].fields;
let acls = manifest.objects[table].acls;
tsfile.acls[table] = Object.keys(acls);
buildCached(tsfile,acls,table)
buildParams(tsfile, acls,table,"read");
buildParams(tsfile, acls,table,"list");
}
});
} catch (e) {
console.log(e);
process.exit(0);
}
return tsfile;
}
}