mirror of
https://gitlab.silvrtree.co.uk/martind2000/ft-webplatform.git
synced 2025-02-11 11:49:17 +00:00
234 lines
6.8 KiB
JavaScript
234 lines
6.8 KiB
JavaScript
import gulp from 'gulp';
|
|
import gutil from 'gulp-util';
|
|
import browserSync from 'browser-sync';
|
|
import historyApiFallback from 'connect-history-api-fallback/lib';
|
|
import project from '../aurelia.json';
|
|
import {buildAll, buildJs, buildDocs} from './build';
|
|
import {CLIOptions} from 'aurelia-cli';
|
|
import request from 'request';
|
|
|
|
function onChange(path) {
|
|
gutil.log(`File Changed: ${path}`);
|
|
}
|
|
|
|
function reload(done) {
|
|
browserSync.reload();
|
|
done();
|
|
}
|
|
|
|
let serve = gulp.series(
|
|
buildAll,
|
|
done => {
|
|
browserSync({
|
|
online: false,
|
|
open: false,
|
|
port: 9000,
|
|
logLevel: 'info',
|
|
server: {
|
|
baseDir: ['.'],
|
|
middleware: [
|
|
relaxAllowOrigin,
|
|
proxyLocalPages,
|
|
proxyFixtures,
|
|
historyApiFallback()
|
|
]
|
|
}
|
|
}, function(err, bs) {
|
|
let urls = bs.options.get('urls').toJS();
|
|
gutil.log(`Application Available At: ${urls.local}`);
|
|
gutil.log(`BrowserSync Available At: ${urls.ui}`);
|
|
done();
|
|
});
|
|
}
|
|
);
|
|
|
|
let relaxAllowOrigin = function(req, res, next) {
|
|
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
next();
|
|
};
|
|
|
|
let proxyFixtures = function(req, res, next) {
|
|
// rest api rewrites
|
|
let isLocalMode = !CLIOptions.hasFlag('use-test-server');
|
|
if (isLocalMode) {
|
|
proxyLocalFixtures(req, res, next);
|
|
} else {
|
|
proxyRemoteFixtures(req, res, next);
|
|
}
|
|
};
|
|
|
|
// TODO: not sure if this is needed. segment should be defined by tag on page eg:
|
|
// <div id="site-config" aurelia-app="configuration/gw/main" segment="investor" site="en-gb" locale="en-gb" long-date="dd MMMM yyyy" short-date="dd.MM.yyyy"></div>
|
|
/**
|
|
* allow us to test injected actor
|
|
* eg investor in /etf-gb/investor/products/price-and-distribution?FundID=21559
|
|
* @param req
|
|
* @param res
|
|
* @param next
|
|
*/
|
|
let proxyLocalPages = function(req, res, next) {
|
|
let matches = req.url.match(/^\/([^/]+)\/([^/]+)\/products\/([^?]*)/);
|
|
if (matches !== null) {
|
|
let fixturePage = `/test/pages/${matches[1]}/${matches[3]}.html`;
|
|
gutil.log(`${req.url} -> ${fixturePage}`);
|
|
req.url = fixturePage;
|
|
}
|
|
next();
|
|
};
|
|
|
|
/**
|
|
* proxies data feed into server
|
|
* eg
|
|
* http://localhost:9000/etf-gb/investor/data/21412/etf.fund-details
|
|
* proxied to
|
|
* http://rcovlnx0192:8322/etf-gb/investor/data/21412/etf.fund-details
|
|
*
|
|
* @param req
|
|
* @param res
|
|
* @param next
|
|
*/
|
|
let proxyRemoteFixtures = function(req, res, next) {
|
|
let testFundId = CLIOptions.getFlagValue('test-server-fund-id') || '21412';
|
|
let testServer = CLIOptions.getFlagValue('test-server') || 'http://rcovlnx0192:8322';
|
|
let testSite = CLIOptions.getFlagValue('test-site') || false;
|
|
|
|
let proxyUrl;
|
|
let matches = req.url.match(/^\/([^/]+)\/([^/]+)\/data\/([^/]+)\/(\d+)\/(.*)/);
|
|
if (matches !== null) {
|
|
// gutil.log(`site=${matches[1]}, segment=${matches[2]}, dslConf=${matches[3]}, fundId=${matches[4]}, beanId=${matches[5]}`);
|
|
let site = testSite ? testSite : matches[1];
|
|
proxyUrl = `${testServer}/${site}/${matches[2]}/data/${matches[3]}/${testFundId}/${matches[5]}`;
|
|
} else {
|
|
matches = req.url.match(/^\/([^/]+)\/([^/]+)\/data\/([^/]+)\/(.*)/);
|
|
if (matches !== null) {
|
|
// gutil.log(`site=${matches[1]}, segment=${matches[2]}, dslConf=${matches[3]}, beanId=${matches[4]}`);
|
|
let site = testSite ? testSite : matches[1];
|
|
proxyUrl = `${testServer}/${site}/${matches[2]}/data/${matches[3]}/${matches[4]}`;
|
|
}
|
|
}
|
|
if (proxyUrl) {
|
|
// any small bean changes add here
|
|
// eg
|
|
// proxyUrl = proxyUrl.replace('etf.fund-details', 'etf.fund-details');
|
|
// proxyUrl = proxyUrl.replace('etf.distribution-history', 'etf.distribution-history');
|
|
|
|
gutil.log(`${req.url} -> ${proxyUrl}`);
|
|
request.get(
|
|
proxyUrl, {},
|
|
function(error, response, contents) {
|
|
// gutil.log(error, response, body);
|
|
if (!error && response.statusCode === 200) {
|
|
try {
|
|
if (!contents.match(/^( |[\r\n])*$/)) {
|
|
let json = JSON.parse(contents);
|
|
json.__debug = {
|
|
injectedByNode: true,
|
|
sourceUrl: proxyUrl
|
|
};
|
|
contents = JSON.stringify(json, null, 2);
|
|
}
|
|
} catch (e) {
|
|
gutil.log(e);
|
|
}
|
|
res.end(contents);
|
|
} else {
|
|
gutil.log(error);
|
|
}
|
|
next();
|
|
}
|
|
);
|
|
} else {
|
|
gutil.log(req.url);
|
|
next();
|
|
}
|
|
};
|
|
|
|
/**
|
|
* @param req
|
|
* @param res
|
|
* @param next
|
|
*/
|
|
let proxyLocalFixtures = function(req, res, next) {
|
|
// /en-gb/investor/data/gw/4264/gw.price-history?ShareClass=I&toDate=2017-09-26&fromDate=2001-01-01
|
|
let matches = req.url.match(/^\/([^/]+)\/([^/]+)\/data\/[^/]+\/(\d+)\/([^?]*)\??(.*)/); // regex updated to ignore query params ie: '?' and anything after
|
|
if (matches !== null) {
|
|
// TODO: quick and messy way to get ShareClass param into data url as sub-folder
|
|
// clean this up later
|
|
let fixtureFile;
|
|
let ShareClass;
|
|
let params = {};
|
|
let hash;
|
|
gutil.log(`Query params -> ${matches[5]}`);
|
|
// quick hack reusing code from lib
|
|
if (matches[5]) {
|
|
let hashes = matches[5].split('&');
|
|
for (let i = 0; i < hashes.length; i++) {
|
|
if (!hashes[i]) {
|
|
continue;
|
|
}
|
|
hash = hashes[i].split('=');
|
|
params[decodeURIComponent(hash[0]).replace(/\+/g, ' ')] = decodeURIComponent(hash[1]).replace(/\+/g, ' ');
|
|
}
|
|
ShareClass = params.ShareClass;
|
|
}
|
|
if (ShareClass) {
|
|
fixtureFile = `/test/data/${matches[1]}/${matches[3]}/${ShareClass}/${matches[4]}.json`;
|
|
} else {
|
|
// gutil.log(`site=${matches[1]}, segment=${matches[2]}, fundId=${matches[3]}, beanId=${matches[4]}`);
|
|
fixtureFile = `/test/data/${matches[1]}/${matches[3]}/${matches[4]}.json`;
|
|
}
|
|
gutil.log(`${req.url} -> ${fixtureFile}`);
|
|
req.url = fixtureFile;
|
|
} else {
|
|
matches = req.url.match(/^\/([^/]+)\/([^/]+)\/data\/[^/]+\/(.*)/);
|
|
if (matches !== null) {
|
|
// gutil.log(`site=${matches[1]}, segment=${matches[2]}, beanId=${matches[3]}`);
|
|
let fixtureFile = `/test/data/${matches[1]}/${matches[3]}.json`;
|
|
gutil.log(`${req.url} -> ${fixtureFile}`);
|
|
req.url = fixtureFile;
|
|
} else {
|
|
gutil.log(req.url);
|
|
}
|
|
}
|
|
next();
|
|
};
|
|
|
|
let refresh = gulp.series(
|
|
buildAll,
|
|
reload
|
|
);
|
|
|
|
let refreshJs = gulp.series(
|
|
buildJs,
|
|
reload
|
|
);
|
|
|
|
let refreshDocs = gulp.series(
|
|
buildDocs,
|
|
reload
|
|
);
|
|
|
|
let watch = function() {
|
|
// aurelia bundles
|
|
gulp.watch(project.transpiler.source, refreshJs).on('change', onChange);
|
|
gulp.watch(project.markupProcessor.source, refreshJs).on('change', onChange);
|
|
|
|
// markdown pages
|
|
gulp.watch(project.docsProcessor.source, refreshDocs).on('change', onChange);
|
|
|
|
gulp.watch(project.cssProcessor.source, refresh).on('change', onChange);
|
|
};
|
|
|
|
let run;
|
|
|
|
if (CLIOptions.hasFlag('watch')) {
|
|
run = gulp.series(
|
|
serve,
|
|
watch
|
|
);
|
|
} else {
|
|
run = serve;
|
|
}
|
|
|
|
export default run;
|