-
Notifications
You must be signed in to change notification settings - Fork 682
Expand file tree
/
Copy pathRushUserConfiguration.ts
More file actions
85 lines (72 loc) · 2.81 KB
/
RushUserConfiguration.ts
File metadata and controls
85 lines (72 loc) · 2.81 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
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as path from 'node:path';
import { FileSystem, JsonFile, JsonSchema, User } from '@rushstack/node-core-library';
import { RushConstants } from '../logic/RushConstants';
import schemaJson from '../schemas/rush-user-settings.schema.json';
interface IRushUserSettingsJson {
buildCacheFolder?: string;
pnpm?: {
storePath?: string;
};
}
/**
* Rush per-user configuration data.
*
* @beta
*/
export class RushUserConfiguration {
private static _schema: JsonSchema = JsonSchema.fromLoadedObject(schemaJson);
/**
* If provided, store build cache in the specified folder. Must be an absolute path.
*/
public readonly buildCacheFolder: string | undefined;
/**
* If provided, specifies the path for the PNPM store directory.
* Can be overridden by the RUSH_PNPM_STORE_PATH environment variable.
*/
public readonly pnpmStorePath: string | undefined;
private constructor(rushUserConfigurationJson: IRushUserSettingsJson | undefined) {
this.buildCacheFolder = rushUserConfigurationJson?.buildCacheFolder;
if (this.buildCacheFolder && !path.isAbsolute(this.buildCacheFolder)) {
throw new Error('buildCacheFolder must be an absolute path');
}
this.pnpmStorePath = rushUserConfigurationJson?.pnpm?.storePath;
}
public static async initializeAsync(): Promise<RushUserConfiguration> {
const rushUserFolderPath: string = RushUserConfiguration.getRushUserFolderPath();
const rushUserSettingsFilePath: string = path.join(rushUserFolderPath, 'settings.json');
let rushUserSettingsJson: IRushUserSettingsJson | undefined;
try {
rushUserSettingsJson = await JsonFile.loadAndValidateAsync(
rushUserSettingsFilePath,
RushUserConfiguration._schema
);
} catch (e) {
if (!FileSystem.isNotExistError(e as Error)) {
throw e;
}
}
return new RushUserConfiguration(rushUserSettingsJson);
}
public static initialize(): RushUserConfiguration {
const rushUserFolderPath: string = RushUserConfiguration.getRushUserFolderPath();
const rushUserSettingsFilePath: string = path.join(rushUserFolderPath, 'settings.json');
let rushUserSettingsJson: IRushUserSettingsJson | undefined;
try {
rushUserSettingsJson = JsonFile.loadAndValidate(
rushUserSettingsFilePath,
RushUserConfiguration._schema
);
} catch (e) {
if (!FileSystem.isNotExistError(e as Error)) {
throw e;
}
}
return new RushUserConfiguration(rushUserSettingsJson);
}
public static getRushUserFolderPath(): string {
const homeFolderPath: string = User.getHomeFolder();
return `${homeFolderPath}/${RushConstants.rushUserConfigurationFolderName}`;
}
}