Skip to content
This repository has been archived by the owner on May 1, 2020. It is now read-only.

Commit

Permalink
fix(live-server): update android platform path (#1407)
Browse files Browse the repository at this point in the history
  • Loading branch information
itslenny authored and imhoffd committed Apr 18, 2018
1 parent 718d2fc commit 1591c81
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 20 deletions.
63 changes: 45 additions & 18 deletions src/dev-server/http-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
ServeConfig,
LOGGER_DIR,
IONIC_LAB_URL,
IOS_PLATFORM_PATH,
ANDROID_PLATFORM_PATH
IOS_PLATFORM_PATHS,
ANDROID_PLATFORM_PATHS
} from './serve-config';
import { Logger } from '../logger/logger';
import * as proxyMiddleware from 'proxy-middleware';
Expand All @@ -36,7 +36,7 @@ export function createHttpServer(config: ServeConfig): express.Application {
// Lab routes
app.use(IONIC_LAB_URL + '/static', express.static(path.join(__dirname, '..', '..', 'lab', 'static')));
app.get(IONIC_LAB_URL, LabAppView);
app.get(IONIC_LAB_URL + '/api/v1/cordova', ApiCordovaProject );
app.get(IONIC_LAB_URL + '/api/v1/cordova', ApiCordovaProject);
app.get(IONIC_LAB_URL + '/api/v1/app-config', ApiPackageJson);

app.get('/cordova.js', servePlatformResource, serveMockCordovaJS);
Expand All @@ -52,7 +52,7 @@ export function createHttpServer(config: ServeConfig): express.Application {

function setupProxies(app: express.Application) {
if (getBooleanPropertyValue(Constants.ENV_READ_CONFIG_JSON)) {
getProjectJson().then(function(projectConfig: IonicProject) {
getProjectJson().then(function (projectConfig: IonicProject) {
for (const proxy of projectConfig.proxies || []) {
let opts: any = url.parse(proxy.proxyUrl);
if (proxy.proxyNoAgent) {
Expand All @@ -74,7 +74,7 @@ function setupProxies(app: express.Application) {
/**
* http responder for /index.html base entrypoint
*/
function serveIndex(req: express.Request, res: express.Response) {
function serveIndex(req: express.Request, res: express.Response) {
const config: ServeConfig = req.app.get('serveConfig');

// respond with the index.html file
Expand Down Expand Up @@ -108,37 +108,64 @@ function serveMockCordovaJS(req: express.Request, res: express.Response) {
/**
* Middleware to serve platform resources
*/
function servePlatformResource(req: express.Request, res: express.Response, next: express.NextFunction) {
async function servePlatformResource(req: express.Request, res: express.Response, next: express.NextFunction) {
const config: ServeConfig = req.app.get('serveConfig');
const userAgent = req.header('user-agent');
let resourcePath = config.wwwDir;

if (!config.isCordovaServe) {
return next();
}

let root = await getResourcePath(req.url, config, userAgent);
if (root) {
res.sendFile(req.url, { root });
} else {
next();
}
}

/**
* Determines the appropriate resource path, and checks if the specified url
*
* @returns string of the resource path or undefined if there is no match
*/
async function getResourcePath(url: string, config: ServeConfig, userAgent: string): Promise<string> {
let searchPaths: string[] = [config.wwwDir];
if (isUserAgentIOS(userAgent)) {
resourcePath = path.join(config.rootDir, IOS_PLATFORM_PATH);
searchPaths = IOS_PLATFORM_PATHS.map(resourcePath => path.join(config.rootDir, resourcePath));
} else if (isUserAgentAndroid(userAgent)) {
resourcePath = path.join(config.rootDir, ANDROID_PLATFORM_PATH);
searchPaths = ANDROID_PLATFORM_PATHS.map(resourcePath => path.join(config.rootDir, resourcePath));
}

fs.stat(path.join(resourcePath, req.url), (err, stats) => {
if (err) {
return next();
}
res.sendFile(req.url, { root: resourcePath });
});
for (let i = 0; i < searchPaths.length; i++) {
let checkPath = path.join(searchPaths[i], url);
try {
let result = await checkFile(checkPath);
return searchPaths[i];
} catch (e) { }
}
}

/**
* Checks if a file exists (responds to stat)
*/
function checkFile(filePath: string): Promise<void> {
return new Promise((resolve, reject) => {
fs.stat(filePath, (err: Error, stats: any) => {
if (err) {
return reject();
}
resolve();
});
});
}


function isUserAgentIOS(ua: string) {
function isUserAgentIOS(ua: string): boolean {
ua = ua.toLowerCase();
return (ua.indexOf('iphone') > -1 || ua.indexOf('ipad') > -1 || ua.indexOf('ipod') > -1);
}

function isUserAgentAndroid(ua: string) {
function isUserAgentAndroid(ua: string): boolean {
ua = ua.toLowerCase();
return ua.indexOf('android') > -1;
}
7 changes: 5 additions & 2 deletions src/dev-server/serve-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ export interface ServeConfig {
export const LOGGER_DIR = '__ion-dev-server';
export const IONIC_LAB_URL = '/ionic-lab';

export const IOS_PLATFORM_PATH = path.join('platforms', 'ios', 'www');
export const ANDROID_PLATFORM_PATH = path.join('platforms', 'android', 'assets', 'www');
export const IOS_PLATFORM_PATHS = [path.join('platforms', 'ios', 'www')];
export const ANDROID_PLATFORM_PATHS = [
path.join('platforms', 'android', 'assets', 'www'),
path.join('platforms', 'android', 'app', 'src', 'main', 'assets', 'www')
];

0 comments on commit 1591c81

Please sign in to comment.