forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToolPaths.ts
More file actions
73 lines (57 loc) · 2.41 KB
/
ToolPaths.ts
File metadata and controls
73 lines (57 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { PackageJsonLookup, IPackageJson, JsonFile } from '@rushstack/node-core-library';
import * as path from 'path';
/**
* @beta
*/
export class ToolPaths {
private static _typescriptPackagePath: string | undefined;
private static _eslintPackagePath: string | undefined;
private static _tslintPackagePath: string | undefined;
public static get typescriptPackagePath(): string {
if (!ToolPaths._typescriptPackagePath) {
ToolPaths._typescriptPackagePath = ToolPaths._getPackagePath('typescript');
if (!ToolPaths._typescriptPackagePath) {
throw new Error('Unable to find "typescript" package.');
}
}
return ToolPaths._typescriptPackagePath;
}
public static get typescriptPackageJson(): IPackageJson {
return JsonFile.load(path.join(ToolPaths.typescriptPackagePath, 'package.json'));
}
public static get eslintPackagePath(): string {
if (!ToolPaths._eslintPackagePath) {
ToolPaths._eslintPackagePath = ToolPaths._getPackagePath('eslint');
if (!ToolPaths._eslintPackagePath) {
throw new Error('Unable to find "eslint" package.');
}
}
return ToolPaths._eslintPackagePath;
}
public static get eslintPackageJson(): IPackageJson {
return JsonFile.load(path.join(ToolPaths.eslintPackagePath, 'package.json'));
}
public static get tslintPackagePath(): string {
if (!ToolPaths._tslintPackagePath) {
ToolPaths._tslintPackagePath = ToolPaths._getPackagePath('tslint');
if (!ToolPaths._tslintPackagePath) {
throw new Error('Unable to find "tslint" package.');
}
}
return ToolPaths._tslintPackagePath;
}
public static get tslintPackageJson(): IPackageJson {
return JsonFile.load(path.join(ToolPaths.tslintPackagePath, 'package.json'));
}
private static _getPackagePath(packageName: string): string | undefined {
const packageJsonPath: string | undefined = ToolPaths._getPackageJsonPath(packageName);
return packageJsonPath ? path.dirname(packageJsonPath) : undefined;
}
private static _getPackageJsonPath(packageName: string): string | undefined {
const lookup: PackageJsonLookup = new PackageJsonLookup();
const mainEntryPath: string = require.resolve(packageName);
return lookup.tryGetPackageJsonFilePathFor(mainEntryPath);
}
}