-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.cli.js
80 lines (55 loc) · 2.26 KB
/
index.cli.js
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
var chalk = require("chalk");
var args = process.argv.slice(2);
switch(args[0]){
case "inspect":
return doInspect(args[1],args[2]);
case "help":
return showHelp();
default:
console.log("Unknown command: " + args[0]);
return showHelp();
}
function showHelp(){
console.log("Heeelp");
}
function doInspect(rolesFile,srcDir){
var treeSeparator = ":";
var roles = require(process.cwd() + "/" + rolesFile);
var actions = {};
function saveRole(roleName,role,inherited){
if(role.can) Object.entries(role.can).forEach(action => saveAction(actions,action,roleName,inherited));
if(role.inherits) Object.entries(role.inherits).forEach(inheritedRole => saveRole(roleName + "(" + inheritedRole[1] + ")",roles[inheritedRole[1]],true));
}
function saveAction(parent,action,roleName,inherited){
var parts = action[0].split(treeSeparator);
if(!parent[parts[0]]) parent[parts[0]] = {roles:{},children:{}};
if(parts[1]){
saveAction(parent[parts[0]].children,[parts.slice(1).join(treeSeparator),action[1]],roleName,inherited);
}
else{
let actionValue = {can:action[1],inherited:inherited};
if(parent[parts[0]].roles[roleName] && parent[parts[0]].roles[roleName].can === true) return;
if(actionValue.can === true || parent[parts[0]].roles[roleName] === undefined) parent[parts[0]].roles[roleName] = actionValue;
}
}
Object.entries(roles).forEach(role => saveRole(role[0],role[1],false))
function writeAction(action,prepend){
if(Object.keys(action[1].roles).length !== 0){
console.log(prepend + action[0] + ": " + Object.entries(action[1].roles).map(role => {
var roleName = role[1].inherited ? role[0] : chalk.bold(role[0]);
if(role[1].can === true) return chalk.green(roleName)
if(role[1].can === false) return chalk.red(roleName)
return chalk.yellow(roleName);
}).join(", "));
}
if(Object.keys(action[1].children).length !== 0){
console.log(prepend + action[0]);
Object.entries(action[1].children).forEach(action => {
writeAction(action,prepend + " ");
});
}
}
Object.entries(actions).forEach(action => {
writeAction(action,"");
});
}