This repository has been archived by the owner on Sep 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·65 lines (50 loc) · 1.93 KB
/
index.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
#!/usr/bin/env node
/**
* Fetches a config object that must hold all the app's components.
* Then it generate a firebase rules based on this object and it's components.
* Then it parses this objects and checks the app's components rules coverage.
* At last it creates a valid firebase rules json file
* and prints the coverage reports on the terminal.
*/
const { writeFile } = require('fs');
const program = require('commander');
const getLogOwners = require(__dirname + '/src/utils/getLogOwners');
const buildGetLogPath = require(__dirname + '/src/utils/getLogPath');
const generateRules = require(__dirname + '/src/rules');
const checkRulesCoverage = require(__dirname + '/src/rules/utils/checkRulesCoverage');
program
.arguments('<customConfigPath>')
.action(function(customConfigPath) {
/**
* Fetch the config object specified by the passed param.
*/
const commandSource = process.cwd();
const configPath = customConfigPath || 'firebaseapp.config.js';
const config = require(`${commandSource}/${configPath}`);
/**
* Build the needed objects based on the config object.
*/
const logOwners = getLogOwners(config);
const getLogPath = buildGetLogPath(config, logOwners);
/**
* Generate the rules object.
*/
const rules = generateRules(config, logOwners, getLogPath, commandSource);
/**
* Check the rules coverage.
*/
const coverage = checkRulesCoverage(config, logOwners, getLogPath, rules);
/**
* Write file and print coverage reports.
*/
const rulesFileName = config.rulesFileName || 'database.rules.json';
writeFile(`${commandSource}/${rulesFileName}`, JSON.stringify({ rules }), function (err) {
if (!err) {
console.log(`Paths coverage: ${coverage.covered} of ${coverage.total} (${coverage.percentage})`);
console.log(`File created in ${commandSource}/${rulesFileName}`);
} else {
console.warn('Error creating rules file.');
}
});
})
.parse(process.argv);