This repository was archived by the owner on May 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathfirebase-bolt
More file actions
executable file
·243 lines (210 loc) · 6.2 KB
/
firebase-bolt
File metadata and controls
executable file
·243 lines (210 loc) · 6.2 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/env node
/*
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
var fs = require('fs');
var parseArgs = require('minimist');
var util = require('../lib/util');
var bolt = require('../lib/bolt');
var pkg = require('../package.json');
var VERSION_STRING = "Firebase Bolt v" + pkg.version;
var commandName = 'bolt';
var opts = {
boolean: ['version', 'help'],
string: ['output'],
alias: {
'f': 'functions',
'h': 'help',
'o': 'output',
'v': 'version',
},
unknown: function(flag) {
if (flag[0] == '-') {
log("Unknown flag: '" + flag + "'");
usage(1);
}
}
};
var commands = {
migrate: function(args) {
if (args._.length != 2) {
log("Missing JSON file name to migrate to bolt file.");
process.exit(1);
}
var functionsFileName = getInputFileName(args.functions, 'bolt');
var functions;
if (functionsFileName !== undefined) {
readInputFile(functionsFileName, 'Bolt', function(boltText) {
try {
var symbols = bolt.parse(boltText);
functions = symbols.functions;
} catch (e) {
log(e.message, e.line, e.column);
process.exit(1);
}
});
}
var inputFileName = getInputFileName(args._[1], 'json');
var outputFileName = getOutputFileName(args.output, inputFileName, 'bolt');
readInputFile(inputFileName, 'JSON', function(jsonData) {
writeOutputFile(outputFileName, bolt.decodeRules(jsonData, functions));
});
}
};
function main() {
var args = parseArgs(process.argv.slice(2), opts);
// Option present - but missing value.
if (args.output == '') {
log("Missing output file name.");
process.exit(1);
}
if (args.version) {
console.log(VERSION_STRING);
return;
}
if (args.help) {
usage(0);
}
// command file
if (args._.length >= 1 && commands[args._[0]] !== undefined) {
commandName += '-' + args._[0];
commands[args._[0]](args);
return;
}
if (args._.length > 1) {
log("Can only compile a single file.");
usage(1);
}
var inputFileName = getInputFileName(args._[0], 'bolt');
var outputFileName = getOutputFileName(args.output, inputFileName, 'json');
readInputFile(inputFileName, 'Bolt', function(boltData) {
writeOutputFile(outputFileName, translateRules(boltData));
});
}
function getInputFileName(name, extension) {
// Read file from stdin
if (name === undefined) {
return undefined;
}
return util.ensureExtension(name, extension);
}
function getOutputFileName(name, inputFileName, extension) {
if (name === undefined) {
if (inputFileName === undefined) {
return undefined;
}
name = util.replaceExtension(inputFileName, extension);
} else {
name = util.ensureExtension(name, extension);
}
if (name === inputFileName) {
log("Cannot overwrite input file: " + inputFileName);
log("(Did you mean '" +
util.replaceExtension(name, extension == 'json' ? 'bolt' : 'json') +
"'?)");
process.exit(1);
}
return name;
}
function readInputFile(name, kind, callback) {
if (name === undefined) {
if (process.stdin.isTTY) {
log("Type in a " + kind + " file terminated by CTRL-D.");
}
readStream(process.stdin, callback);
return;
}
fs.readFile(name, 'utf8', function(err, data) {
if (err) {
log("Could not read file: " + name);
process.exit(1);
}
callback(data);
});
}
function writeOutputFile(name, data) {
if (name === undefined) {
console.log(data + '\n');
} else {
log("Generating " + name + "...");
fs.writeFile(name, data + '\n', 'utf8', function(err) {
if (err) {
log("Could not write file: " + outputFile);
process.exit(1);
}
});
}
}
function usage(code) {
var cmdName = process.argv[1].split('/').slice(-1);
console.error("Translate Firebase Bolt file into JSON rules format");
console.error(" (or use migrate sub-command to generate a Bolt file equivalent\n" +
" of a Firebase JSON rule set).\n");
console.error(" Usage: " + cmdName + " [options] [file[.bolt]]");
console.error(" " + cmdName + " [options] migrate [file[.json]]\n");
console.error(" Examples: " + cmdName + " rules.bolt --output rules.json");
console.error(" " + cmdName + " < rules.bolt > rules.json");
console.error(" " + cmdName + " rules");
console.error(" (rules.bolt => rules.json)");
console.error(" " + cmdName + " migrate [--functions file] rules.json");
console.error(" (rules.json => rules.bolt)\n");
console.error(" Options:\n");
console.error(util.formatColumns(4, [
["-f --functions file", "Include file of top level Bolt functions (for migrate)."],
["-h --help", "Display this helpful message."],
["-o --output file", "Output to file."],
["-v --version", "Display Firebase Bolt version."],
[]
]).join('\n'));
process.exit(code);
}
main();
function readStream(f, callback) {
var input = "";
f.setEncoding('utf8');
f.on('data', function(chunk) {
input += chunk;
});
f.on('end', function() {
callback(input);
});
}
function translateRules(input) {
var symbols;
var rules;
try {
symbols = bolt.parse(input);
} catch (e) {
log(e.message, e.line, e.column);
process.exit(1);
}
try {
var gen = new bolt.Generator(symbols);
rules = gen.generateRules();
} catch (e) {
log(e.message);
process.exit(2);
}
return JSON.stringify(rules, null, 2);
}
function log(message, line, column) {
var parts = [commandName];
if (line) {
util.extendArray(parts, [line, column]);
}
parts.push(' ' + message);
console.error(parts.join(':'));
}