From 03393283e457ee1185d4b87b528edf9889e3ad3c Mon Sep 17 00:00:00 2001
From: Paul Makles <paulmakles@gmail.com>
Date: Thu, 21 Feb 2019 15:02:51 +0000
Subject: [PATCH] Add environment and PATH options.

---
 README.md            |  5 +++++
 package-lock.json    |  2 +-
 package.json         |  6 ++++--
 src/Config.ts        | 14 ++++++++++++++
 src/Options.ts       |  6 +++++-
 src/Runner.ts        |  2 +-
 src/compilers/G++.ts |  6 ++++--
 src/compilers/GCC.ts |  6 ++++--
 8 files changed, 38 insertions(+), 9 deletions(-)

diff --git a/README.md b/README.md
index 20fd3b6..ae74072 100644
--- a/README.md
+++ b/README.md
@@ -5,6 +5,7 @@
 - Simplified options and usage
 - Types and intellisense support
 - Supports compilation of C and C++ files through `gcc` and `g++`
+- Also can be easily used with emscripten.
 
 ## Quick Start
 
@@ -33,6 +34,10 @@ await gnucc({
   includes: [
     'src/headers'
   ],
+  binaries: {
+    "gcc": 'gcc',
+    "g++": 'g++'
+  },
   optimisation: OPTIMISATION.HIGH,
   warning: [WARN.ALL]
 });
diff --git a/package-lock.json b/package-lock.json
index fac5403..9ea52bc 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,6 +1,6 @@
 {
   "name": "gnucc",
-  "version": "1.0.3",
+  "version": "1.1.5",
   "lockfileVersion": 1,
   "requires": true,
   "dependencies": {
diff --git a/package.json b/package.json
index 3b131c4..da69bc6 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "gnucc",
-  "version": "1.1.3",
+  "version": "1.1.5",
   "description": "Wrapper for GCC and G++",
   "main": "dist/index.js",
   "types": "dist/index.d.ts",
@@ -36,10 +36,12 @@
     "url": "https://gitlab.insrt.uk/insert/gnucc.git"
   },
   "keywords": [
+    "gnu",
     "gcc",
     "g++",
     "c++",
     "c",
-    "compiler"
+    "compiler",
+    "emscripten"
   ]
 }
diff --git a/src/Config.ts b/src/Config.ts
index 9526bd8..c2f690d 100644
--- a/src/Config.ts
+++ b/src/Config.ts
@@ -54,4 +54,18 @@ export function ProcessGppOpt(opts: GPPOptions): string[] {
 	opts.std && args.push(`--std=${opts.std}`);
 
 	return args;
+}
+
+export function ProcessEnv(opts: ProjectOptions): NodeJS.ProcessEnv {
+	let env = process.env;
+
+	if (opts.env) {
+		Object.assign(env, opts.env);
+	}
+
+	if (opts.path) {
+		opts.path.forEach(x => env['PATH'] = x + ';' + env['PATH']);
+	}
+
+	return env;
 }
\ No newline at end of file
diff --git a/src/Options.ts b/src/Options.ts
index 064f80b..b9d87ad 100644
--- a/src/Options.ts
+++ b/src/Options.ts
@@ -242,7 +242,11 @@ export interface ProjectOptions extends LinkerOptions {
 	/** Link libraries, -l */
 	link?: string[],
 	/** Output logs when compiling */
-	log?: boolean
+	log?: boolean,
+	/** Environment variables to attach */
+	env?: NodeJS.ProcessEnv,
+	/** PATH paths to prepend */
+	path?: string[]
 };
 
 export interface GCCOptions extends ProjectOptions {
diff --git a/src/Runner.ts b/src/Runner.ts
index 3235696..e3cbcb3 100644
--- a/src/Runner.ts
+++ b/src/Runner.ts
@@ -8,7 +8,7 @@ export interface Result {
 	exitCode: Number
 };
 
-export default function Run(args: string[], log?: boolean): Promise<Result> {
+export default function Run(args: string[], log?: boolean, env?: NodeJS.ProcessEnv): Promise<Result> {
 	let command = args.join(' ');
 	log && LogOutput(command);
 	const proc = spawn(args.shift() || 'echo', args);
diff --git a/src/compilers/G++.ts b/src/compilers/G++.ts
index edfdc3a..b8869d1 100644
--- a/src/compilers/G++.ts
+++ b/src/compilers/G++.ts
@@ -1,6 +1,6 @@
 import Run, { Result } from '../Runner';
 import { GPPOptions } from '../Options';
-import { ProcessGppOpt } from '../Config';
+import { ProcessGppOpt, ProcessEnv } from '../Config';
 
 /**
  * Compiles a source file with G++
@@ -18,16 +18,18 @@ export default async function gpp(opt: GPPOptions): Promise<Result>;
 export default async function gpp(optOrInput: GPPOptions | string, output?: string, log: boolean = true) {
 	let binary = 'g++';
 	let args: string[] = [];
+	let env;
 
 	if (typeof optOrInput === 'string') {
 		args.push(optOrInput);
 		output && args.push('-o', output);
 	} else {
+		env = ProcessEnv(optOrInput);
 		if (optOrInput.binaries && optOrInput.binaries["g++"]) binary = optOrInput.binaries["g++"];
 		args.push(...ProcessGppOpt(optOrInput));
 		log = optOrInput.log || false;
 	}
 
 	args.unshift(binary);
-	return await Run(args, log);
+	return await Run(args, log, env);
 };
\ No newline at end of file
diff --git a/src/compilers/GCC.ts b/src/compilers/GCC.ts
index 553e4ab..39ae24b 100644
--- a/src/compilers/GCC.ts
+++ b/src/compilers/GCC.ts
@@ -1,6 +1,6 @@
 import Run, { Result } from '../Runner';
 import { GCCOptions } from '../Options';
-import { ProcessGccOpt } from '../Config';
+import { ProcessGccOpt, ProcessEnv } from '../Config';
 
 /**
  * Compiles a source file with GCC
@@ -18,16 +18,18 @@ export default async function gcc(opt: GCCOptions): Promise<Result>;
 export default async function gcc(optOrInput: GCCOptions | string, output?: string, log: boolean = true) {
 	let binary = 'gcc';
 	let args: string[] = [];
+	let env;
 
 	if (typeof optOrInput === 'string') {
 		args.push(optOrInput);
 		output && args.push('-o', output);
 	} else {
+		env = ProcessEnv(optOrInput);
 		if (optOrInput.binaries && optOrInput.binaries.gcc) binary = optOrInput.binaries.gcc;
 		args.push(...ProcessGccOpt(optOrInput));
 		log = optOrInput.log || false;
 	}
 
 	args.unshift(binary);
-	return await Run(args, log);
+	return await Run(args, log, env);
 };
\ No newline at end of file
-- 
GitLab