Skip to content

Commit

Permalink
feat(nx-plugin): add proxy config support to file-server executor
Browse files Browse the repository at this point in the history
  • Loading branch information
austinhappel committed Mar 7, 2022
1 parent fd64546 commit c6c794d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 7 deletions.
6 changes: 6 additions & 0 deletions docs/generated/api-web/executors/file-server.md
Expand Up @@ -47,6 +47,12 @@ Type: `number`

Port to listen on.

### proxyConfig

Type: `string`

Path to the proxy configuration.

### proxyUrl

Type: `string`
Expand Down
38 changes: 31 additions & 7 deletions packages/web/src/executors/file-server/file-server.impl.ts
@@ -1,12 +1,28 @@
import { ExecutorContext, joinPathFragments, logger } from '@nrwl/devkit';
import { workspaceLayout } from '@nrwl/workspace/src/core/file-utils';
import { exec, execSync } from 'child_process';
import { ExecutorContext, joinPathFragments } from '@nrwl/devkit';
import ignore from 'ignore';
import { readFileSync } from 'fs';
import { Schema } from './schema';
import { watch } from 'chokidar';
import { workspaceLayout } from '@nrwl/workspace/src/core/file-utils';
import { existsSync, readFileSync } from 'fs';
import ignore from 'ignore';
import { Schema, ProxyConfig } from './schema';
import { join } from 'path';

function getHttpServerArgs(options: Schema) {
// Find and import proxyConfig configuration if supplied
function getProxyConfig(proxyConfigPath, context) {
let proxyConfig: ProxyConfig;

const fullProxyConfigPath = proxyConfigPath
? join(context.root, proxyConfigPath)
: join(context.root, 'proxy.conf.json');

if (existsSync(proxyConfigPath)) {
logger.info(`[info] found proxy configuration at ${proxyConfigPath}`);
proxyConfig = require(fullProxyConfigPath);
}
return proxyConfig;
}

function getHttpServerArgs(options: Schema, context: ExecutorContext) {
const args = ['-c-1'];
if (options.port) {
args.push(`-p ${options.port}`);
Expand All @@ -26,6 +42,14 @@ function getHttpServerArgs(options: Schema) {
if (options.proxyUrl) {
args.push(`-P ${options.proxyUrl}`);
}

const proxyConfigData = getProxyConfig(options.proxyConfig, context);

if (proxyConfigData) {
Object.keys(proxyConfigData).forEach((key) => {
args.push(`--proxy-options.${key}`, proxyConfigData[key]);
});
}
return args;
}

Expand Down Expand Up @@ -123,7 +147,7 @@ export default async function* fileServerExecutor(
run();

const outputPath = getBuildTargetOutputPath(options, context);
const args = getHttpServerArgs(options);
const args = getHttpServerArgs(options, context);

const serve = exec(`npx http-server ${outputPath} ${args.join(' ')}`, {
cwd: context.root,
Expand Down
31 changes: 31 additions & 0 deletions packages/web/src/executors/file-server/schema.d.ts
@@ -1,3 +1,5 @@
import { ProxyOptions } from './ProxyConfig';

export interface Schema {
host: string;
port: number;
Expand All @@ -9,4 +11,33 @@ export interface Schema {
parallel: boolean;
maxParallel?: number;
withDeps: boolean;
proxyConfig?: string;
}

export interface ProxyConfig {
// target: string; Used by http-server internally
// changeOrigin: boolean; Used by http-server internally
// ssl: object; Use Schema ssl/sslKey/sslCert instead
// toProxy: boolean; Http-server does not proxy to another proxy
agent?: object;
forward?: string;
secure?: boolean;
ws?: boolean;
xfwd?: boolean;
prependPath?: boolean;
ignorePath?: boolean;
localAddress?: string;
changeOrigin?: boolean;
preserveHeaderKeyCase?: boolean;
auth?: string;
hostRewrite?: string;
autoRewrite?: boolean;
protocolRewrite?: string;
cookieDomainRewrite?: boolean | string | object;
cookiePathRewrite?: boolean | string | object;
headers?: object;
proxyTimeout?: number;
followRedirects?: boolean;
selfHandleResponse?: boolean;
buffer?: Buffer;
}
5 changes: 5 additions & 0 deletions packages/web/src/executors/file-server/schema.json
Expand Up @@ -48,6 +48,11 @@
"proxyUrl": {
"type": "string",
"description": "URL to proxy unhandled requests to."
},
"proxyConfig": {
"type": "string",
"description": "Path to the proxy configuration.",
"default": ""
}
},
"additionalProperties": false,
Expand Down

0 comments on commit c6c794d

Please sign in to comment.