feat: add function to find tsconfig.json

This commit is contained in:
Karl-Philipp Wulfert
2019-01-30 14:17:54 +01:00
parent 466cc2579c
commit aa645a2cc4
2 changed files with 62 additions and 1 deletions

View File

@@ -13,9 +13,11 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
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;
}