Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = function(defaults) {
'ember-cli-babel': {
includePolyfill: true
},
storeConfigInMeta : false,
storeConfigInMeta : true,
autoprefixer : {
browsers : targets.browsers,
cascade : false
Expand Down
12 changes: 5 additions & 7 deletions scripts/fastboot-server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require('dotenv').config()
require('dotenv').config();

const FastBootAppServer = require('fastboot-app-server');
const { injectEnvironment } = require('./replace-config');
Expand All @@ -19,10 +19,8 @@ let fastbootServer = new FastBootAppServer({
workerCount : parseInt(fastbootWorkers)
});

(async () => {
if (process.env.INJECT_ENV === 'true') {
await injectEnvironment();
}
if (process.env.INJECT_ENV === 'true') {
injectEnvironment();
}

fastbootServer.start();
})();
fastbootServer.start();
141 changes: 30 additions & 111 deletions scripts/replace-config.js
Original file line number Diff line number Diff line change
@@ -1,132 +1,51 @@
require('dotenv').config();

const fs = require('fs');
const promisify = require('util').promisify;
const { merge } = require('lodash');
const safeEval = require('safe-eval');

const env = require('../config/environment');
const appName = 'open-event-frontend';

const environment = env('production');

async function replaceFastbootConfig() {
/**
* Replace the fastboot-context config located within dist/package.json
*/
function replaceFastbootConfig() {
const packagePath = './dist/package.json';
const packageInfo = require('.' + packagePath);

const packageInfo = require(`.${packagePath}`);
const old = packageInfo.fastboot.config[appName];
packageInfo.fastboot.config[appName] = merge(old, environment);
await promisify(fs.writeFile)(packagePath, JSON.stringify(packageInfo));

console.log('Transformed package.json with new environment')
}

function findObject(js, objectStart) {
let braceCount = 1;

const start = js.indexOf('{', objectStart);

if (start < 0) {
return null;
}

let index = start + 1;
let parseBlocker = null;
let parseBlockerBuffer = null;

while (index < js.length) {
const c = js[index];

if (!parseBlocker && !parseBlockerBuffer) {
if (c === '{') {
braceCount += 1;
} else if (c == "}") {
braceCount -= 1;
} else if (c === '"' || c === "'" || c === "`") {
parseBlocker = c;
} else if (js[index - 1] === '/') {
if (c === '/') { parseBlocker = "\n"; }
else if (c === "*") { parseBlockerBuffer = "*/"; }
}

if (braceCount === 0) {
break;
}
} else {
if (c === parseBlocker) {
parseBlocker = null;
} else if (c === '/' && js[index - 1] == '*') {
parseBlockerBuffer = null;
}
}

index++;
}

if (index >= js.length) return null;

return {
start,
end: index
};
}

function replace(str, start, end, replacement) {
return str.substring(0, start) + replacement + str.substring(end + 1, str.length);
fs.writeFileSync(packagePath, JSON.stringify(packageInfo));
}

async function replaceWebConfig() {
const pattern = new RegExp(`^${appName}.*\.js$`);
const basePath = './dist/assets/';
const appJs = (await promisify(fs.readdir)(basePath)).filter(name => name.match(pattern));

const envDefinition = 'define("open-event-frontend/config/environment"';

for (const js of appJs) {
const jsPath = basePath + js;
const code = (await promisify(fs.readFile)(jsPath)).toString()
let defineIndex = code.indexOf(envDefinition);

if (defineIndex < 0) {
defineIndex = code.indexOf(envDefinition.replace(/"/g, "'"));
if (defineIndex < 0)
continue;
}

const defaultIndex = code.indexOf('default', defineIndex + 1);

if (defaultIndex < 0) {
continue;
}
// File with environment definition found

console.log('Transforming ' + js)

const object = findObject(code, defaultIndex);

console.log('Environment Object Found', object)

const webEnvJson = code.substring(object.start, object.end + 1)
const old = safeEval('(' + webEnvJson + ')')

const newEnv = JSON.stringify(merge(old, environment))
const newCode = replace(code, object.start, object.end, newEnv)

console.log('Injected new environment')

await promisify(fs.writeFile)(jsPath, newCode)

console.log('Transformation Complete')

break;
}
/**
* Replace the config for the SPA by editing the config located inside the meta-tag.
*/
function replaceWebConfig() {
const indexFilePath = './dist/index.html';
const configPath = 'open-event-frontend/config/environment';
const indexFileContent = fs.readFileSync(indexFilePath).toString();
const regex = new RegExp(`name="${configPath}" content="([^"]+)"`, 'i');
const existingEnvironment = JSON.parse(decodeURIComponent(regex.exec(indexFileContent)[1]));
const newEnvironment = encodeURIComponent(JSON.stringify(merge(existingEnvironment, environment)));
fs.writeFileSync(
indexFilePath,
indexFileContent.replace(
regex,
`name="${configPath}" content="${newEnvironment}"`
)
);
}

async function injectEnvironment() {
await replaceFastbootConfig()
await replaceWebConfig()
/**
* Inject a modified environment config into the fastboot application.
*/
function injectEnvironment() {
replaceFastbootConfig();
replaceWebConfig();
}

module.exports = {
injectEnvironment
}
};