-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
75 lines (61 loc) · 2.59 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
66
67
68
69
70
71
72
73
74
75
/*eslint-env node*/
'use strict';
var path = require('path');
var fs = require('fs');
var denodeify = require('rsvp').denodeify;
var readFile = denodeify(fs.readFile);
var admin = require("firebase-admin");
const DeployPluginBase = require('ember-cli-deploy-plugin');
module.exports = {
name: 'ember-cli-deploy-firebase-database',
createDeployPlugin: function(options) {
let DeployPlugin = DeployPluginBase.extend({
name: options.name,
defaultConfig: {
filePattern: 'index.html',
distDir: function(context) {
return context.distDir || './dist';
},
parentPath: '',
indexKey: 'index_html',
firebaseDatabaseUID: 'index_writer'
},
requiredConfig: ['serviceAccountKeyPath', 'firebaseAppName'],
upload(/*context*/) {
var distDir = this.readConfig('distDir');
var filePattern = this.readConfig('filePattern');
var filePath = path.join(distDir, filePattern);
var serviceAccountKeyPath = this.readConfig('serviceAccountKeyPath');
var firebaseAppName = this.readConfig('firebaseAppName');
var databaseURL = "https://" + firebaseAppName + ".firebaseio.com";
var parentPath = this.readConfig('parentPath');
var indexKey = this.readConfig('indexKey');
var fullPath = parentPath + indexKey;
var firebaseDatabaseUID = this.readConfig('firebaseDatabaseUID');
this.log('Uploading `' + filePath + '` to `' + databaseURL + '` at `' + fullPath + '` as user `' + firebaseDatabaseUID + '`', { verbose: true });
return this._readFileContents(serviceAccountKeyPath).then((serviceAccount)=>{
this.log("read serviceAccount", { verbose: true })
serviceAccount = JSON.parse(serviceAccount)
this.log("setting up firebase", { verbose: true })
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: databaseURL,
databaseAuthVariableOverride: { uid: firebaseDatabaseUID }
});
return this._readFileContents(filePath).then((data) => {
this.log("setting index", { verbose: true })
return admin.database().ref(fullPath).set(data);
});
});
},
_readFileContents: function(path) {
this.log("reading file " + path, { verbose: true })
return readFile(path)
.then(function(buffer) {
return buffer.toString();
});
},
});
return new DeployPlugin();
}
};