@ionic/cli-plugin-ionic1
Version:
Ionic CLI build plugin for Ionic 1 projects
110 lines (109 loc) • 4.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const fs = require("fs");
const path = require("path");
const url = require("url");
const config_1 = require("./config");
const live_reload_1 = require("./live-reload");
/**
* Create HTTP server
*/
function createHttpServer(env, options) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const express = yield Promise.resolve().then(function () { return require('express'); });
const app = express();
app.set('serveOptions', options);
app.listen(options.port, options.address);
app.get('/', serveIndex);
app.use('/', express.static(options.wwwDir));
// Lab routes
app.use(config_1.IONIC_LAB_URL + '/static', express.static(path.join(__dirname, '..', '..', 'lab', 'static')));
app.get(config_1.IONIC_LAB_URL, (req, res) => res.sendFile('index.html', { root: path.join(__dirname, '..', '..', 'lab') }));
app.get(config_1.IONIC_LAB_URL + '/api/v1/cordova', (req, res) => tslib_1.__awaiter(this, void 0, void 0, function* () {
const [info] = yield env.hooks.fire('cordova:project:info', { env });
if (info) {
res.json(info);
}
else {
res.status(400).json({ status: 'error', message: 'Unable to load config.xml' });
}
}));
app.get('/cordova.js', servePlatformResource, serveMockCordovaJS);
app.get('/cordova_plugins.js', servePlatformResource);
app.get('/plugins/*', servePlatformResource);
if (!options.noproxy) {
yield setupProxies(env.project, app);
}
return app;
});
}
exports.createHttpServer = createHttpServer;
function setupProxies(project, app) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const projectConfig = yield project.load();
for (const proxy of projectConfig.proxies || []) {
let opts = url.parse(proxy.proxyUrl);
if (proxy.proxyNoAgent) {
opts.agent = false;
}
opts.rejectUnauthorized = !(proxy.rejectUnauthorized === false);
const proxyMiddleware = yield Promise.resolve().then(function () { return require('proxy-middleware'); });
app.use(proxy.path, proxyMiddleware(opts));
console.log('Proxy added:' + proxy.path + ' => ' + url.format(opts));
}
});
}
/**
* http responder for /index.html base entrypoint
*/
function serveIndex(req, res) {
const options = req.app.get('serveOptions');
// respond with the index.html file
const indexFileName = path.join(options.wwwDir, 'index.html');
fs.readFile(indexFileName, (err, indexHtml) => {
if (!options.nolivereload) {
indexHtml = live_reload_1.injectLiveReloadScript(indexHtml, options.externalAddress, options.livereloadPort);
}
res.set('Content-Type', 'text/html');
res.send(indexHtml);
});
}
/**
* http responder for cordova.js file
*/
function serveMockCordovaJS(req, res) {
res.set('Content-Type', 'application/javascript');
res.send('// mock cordova file during development');
}
/**
* Middleware to serve platform resources
*/
function servePlatformResource(req, res, next) {
const options = req.app.get('serveOptions');
const userAgent = req.header('user-agent') || '';
let resourcePath = options.wwwDir;
if (!options.iscordovaserve) {
return next();
}
if (isUserAgentIOS(userAgent)) {
resourcePath = path.join(options.projectRoot, config_1.IOS_PLATFORM_PATH);
}
else if (isUserAgentAndroid(userAgent)) {
resourcePath = path.join(options.projectRoot, config_1.ANDROID_PLATFORM_PATH);
}
fs.stat(path.join(resourcePath, req.url), (err, stats) => {
if (err) {
return next();
}
res.sendFile(req.url, { root: resourcePath });
});
}
function isUserAgentIOS(ua) {
ua = ua.toLowerCase();
return (ua.indexOf('iphone') > -1 || ua.indexOf('ipad') > -1 || ua.indexOf('ipod') > -1);
}
function isUserAgentAndroid(ua) {
ua = ua.toLowerCase();
return ua.indexOf('android') > -1;
}