diff --git a/src/common.ts b/src/common.ts
index 16645f87..aaa344b6 100644
--- a/src/common.ts
+++ b/src/common.ts
@@ -13,9 +13,11 @@
* this program. If not, see .
*/
import {Logger} from '@openstapps/logger';
-import {mkdir, PathLike, readFile, unlink, writeFile} from 'fs';
+import {existsSync, mkdir, PathLike, readFile, unlink, writeFile} from 'fs';
import * as glob from 'glob';
import {Schema as JSONSchema, ValidationError} from 'jsonschema';
+import {platform} from 'os';
+import {join, sep} from 'path';
import {Definition} from 'ts-json-schema-generator';
import {Application, ProjectReflection} from 'typedoc';
import {promisify} from 'util';
@@ -154,3 +156,31 @@ export function isSchemaWithDefinitions(schema: JSONSchema): schema is SchemaWit
export function isThingWithType(thing: any): thing is { type: string } {
return typeof thing.type === 'string';
}
+
+/**
+ * Get path that contains a tsconfig.json
+ *
+ * @param startPath Path from where to start searching "upwards"
+ */
+export function getTsconfigPath(startPath: string): string {
+ let tsconfigPath = startPath;
+
+ // see https://stackoverflow.com/questions/9652043/identifying-the-file-system-root-with-node-js
+ const root = (platform() === 'win32') ? process.cwd().split(sep)[0] : '/';
+
+ // repeat until a tsconfig.json is found
+ while (!existsSync(join(tsconfigPath, 'tsconfig.json'))) {
+ if (tsconfigPath === root) {
+ throw new Error(`Reached file system root ${root} while searching for 'tsconfig.json' in ${startPath}!`);
+ }
+
+ // pop last directory
+ const tsconfigPathParts = tsconfigPath.split(sep);
+ tsconfigPathParts.pop();
+ tsconfigPath = tsconfigPathParts.join(sep);
+ }
+
+ logger.info(`Using 'tsconfig.json' from ${tsconfigPath}.`);
+
+ return tsconfigPath;
+}
diff --git a/test/Common.spec.ts b/test/Common.spec.ts
new file mode 100644
index 00000000..7e2a7256
--- /dev/null
+++ b/test/Common.spec.ts
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2018-2019 StApps
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation, version 3.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program. If not, see .
+ */
+import {expect} from 'chai';
+import {slow, suite, test, timeout} from 'mocha-typescript';
+import {cwd} from 'process';
+import {getTsconfigPath, logger} from '../src/common';
+
+process.on('unhandledRejection', (err) => {
+ logger.error('UNHANDLED REJECTION', err.stack);
+ process.exit(1);
+});
+
+@suite(timeout(10000), slow(5000))
+export class CommonSpec {
+ @test
+ async getTsconfigPath() {
+ expect(getTsconfigPath(__dirname)).to.be.equal(cwd());
+ }
+}