Last active
October 18, 2016 07:41
-
-
Save liesislukas/ad4798400776d19aab5191a4e6bf3cbf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {config} from './../config/config'; | |
const firebase = require('firebase'); | |
const moment = require('moment'); | |
const recursive = require('recursive-readdir'); | |
const fs = require('fs'); | |
const numeral = require('numeral'); | |
firebase.initializeApp({ | |
databaseURL: config.firebase.db_name, | |
serviceAccount: './config/firebase.json' | |
}); | |
const db = firebase.database(); | |
const run_replace = (values) => { | |
return new Promise((resolve, reject) => { | |
recursive('./dist/', ['*.css', '.nojekyll'], function (err, files) { | |
files.forEach(file => { | |
let data = fs.readFileSync(file); | |
data = data.toString(); | |
if (data.indexOf('[@') !== -1) { | |
if (data.indexOf('[@') !== -1) { | |
let matches = data.match(/\[\S+@]/g); | |
if (matches && matches.length > 0) { | |
matches.forEach(m => { | |
// formating flags goes after constant name and __ ex.: VAR__NUMBER to format as number: 1 000 000 | |
if (m.indexOf('__NUMBER') !== -1) { | |
let variable = m.replace('[@', '').replace('__NUMBER@]', ''); | |
let value = numeral(values[variable]).format('0,0'); | |
console.log(`var: ${variable} m: ${m} value: ${value}`); | |
data = data.split(m).join(value); | |
} | |
}); | |
} | |
} | |
if (data.indexOf('[@') !== -1) { | |
let matches = data.match(/\[\S+@]/g); | |
if (matches && matches.length > 0) { | |
matches.forEach(m => { | |
console.log(`**** MISSING VALUE **** ${file} -----> ${m}`); | |
}); | |
} | |
} | |
fs.writeFileSync(file, data); | |
} | |
}); | |
resolve(); | |
}); | |
}); | |
}; | |
const deploy = () => { | |
return new Promise((resolve, reject) => { | |
db.ref('constants').once('value', snapshot => { | |
let constants = snapshot.val(); | |
const now = moment(new Date()); | |
let values = {}; | |
Object.keys(constants).forEach(c => { | |
let constant = constants[c]; | |
values[c] = constant.value; | |
var end = moment(constant.at); | |
var duration = moment.duration(end.diff(now)); | |
var days = duration.asDays(); | |
if (days < -14) { | |
console.error(`#sdjfdsl value of ${c} is ${days} days old.`); | |
} | |
run_replace(values) | |
.then(() => resolve()) | |
.catch(e => reject()) | |
; | |
}); | |
}); | |
}); | |
}; | |
deploy() | |
.then(() => { | |
process.exit(0); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment