mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-10 19:52:53 +00:00
feat: add function to find tsconfig.json
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user