This repository has been archived by the owner on Nov 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
ImperativeConfig.ts
214 lines (189 loc) · 6.87 KB
/
ImperativeConfig.ts
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/
import { Constants } from "../../constants";
import { join } from "path";
import { IImperativeConfig } from "../../imperative/src/doc/IImperativeConfig";
import { ImperativeError } from "../../error";
import { EnvironmentalVariableSettings } from "../../imperative/src/env/EnvironmentalVariableSettings";
/**
* This class is used to contain all configuration being set by Imperative.
* It is a singleton and should be accessed via ImperativeConfig.instance.
*/
export class ImperativeConfig {
/**
* This is the variable that stores the specific instance of Imperative Config.
* Defined as static so that it can be accessed from anywhere.
*/
private static mInstance: ImperativeConfig = null;
/**
* This parameter is used as the container of all loaded configuration for
* Imperative.
*/
private mLoadedConfig: IImperativeConfig = null;
/**
* This parameter is used to contain the caller location of imperative configuration file.
*/
private mCallerLocation: string = null;
/**
* This is the package name of the host application. It will only be set once accessed to
* lessen loads to the host package.json.
*/
private mHostPackageName: string;
/**
* This is the name of our imperative package. It will only be set once accessed to
* lessen loads to the imperative package.json.
*
* It isn't hardcoded so that the name of our package can change without affecting
* modules dependent on it.
*/
private mImperativePackageName: string;
/**
* This is our calling CLI's command name (taken from package.json: bin).
*/
private mRootCommandName: string;
/**
* Gets a single instance of the PluginIssues. On the first call of
* ImperativeConfig.instance, a new Plugin Issues object is initialized and returned.
* Every subsequent call will use the one that was first created.
*
* @returns {ImperativeConfig} The newly initialized PMF object.
*/
public static get instance(): ImperativeConfig {
if (this.mInstance == null) {
this.mInstance = new ImperativeConfig();
}
return this.mInstance;
}
/**
* Set the caller location.
* @param {string} location new location to be updated with
*/
public set callerLocation(location: string) {
this.mCallerLocation = location;
}
/**
* Return file location of imperative configuration file.
* @returns {streturnsring} - location of configuration file
*/
public get callerLocation(): string {
return this.mCallerLocation;
}
/**
* Set the loaded config data.
* @param {IImperativeConfig} config to be set.
*/
public set loadedConfig(config: IImperativeConfig) {
this.mLoadedConfig = config;
}
/**
* Retrieve the loaded config (if init has
* @returns {IImperativeConfig} - the config that has been loaded, if any
*/
public get loadedConfig(): IImperativeConfig {
return this.mLoadedConfig;
}
/**
* Set our root command name.
* @param rootCommandName - The name of our calling CLI's command.
*/
public set rootCommandName(rootCommandName: string) {
this.mRootCommandName = rootCommandName;
}
/**
* Get our root command name.
* @returns The name of our calling CLI's command.
*/
public get rootCommandName(): string {
return this.mRootCommandName;
}
/**
* Retrieve the host package name from which imperative was called.
*/
public get hostPackageName(): string {
if (!this.mHostPackageName) {
this.mHostPackageName = this.callerPackageJson.name;
}
return this.mHostPackageName;
}
/**
* Retrieve the package name of the imperative application.
*/
public get imperativePackageName(): string {
if (!this.mImperativePackageName) {
this.mImperativePackageName = require(join(__dirname, "../../../package.json")).name;
}
return this.mImperativePackageName;
}
/**
* Parses the package.json file and searches for the symlink name used under "bin".
* @returns {string} - return bin symlink name if present, otherwise null
*/
public findPackageBinName(): string {
const pkg = this.callerPackageJson;
if (typeof pkg.bin === "string") {
return pkg.name;
} else if (typeof pkg.bin === "object") {
return Object.keys(pkg.bin).pop();
}
return null;
}
/**
* Return the cli Home path.
* @return {string} path to cli Home.
*/
public get cliHome(): string {
const settings = EnvironmentalVariableSettings.read(this.loadedConfig.envVariablePrefix || this.loadedConfig.name);
if (settings.cliHome.value != null) {
return settings.cliHome.value;
}
return this.loadedConfig.defaultHome;
}
/**
* Return profile Directory.
* @return {string} profile directory.
*/
public get profileDir(): string {
return this.loadedConfig.defaultHome + Constants.PROFILES_DIR + "/";
}
/**
* Return package.json of the imperative user
* @returns {any} - package.json file of caller
*/
public get callerPackageJson(): any {
return this.getCallerFile("package.json");
}
/**
* Require a file from a project using imperative accounting for imperative being contained
* separately from the current implementers directory.
* @param {string} file - the file to require from project using imperative
*/
public getCallerFile(file: string): any {
// try to locate the file using find-up first
let findupErr: Error;
try {
const filePath = require("find-up").sync(file, {cwd: ImperativeConfig.instance.callerLocation});
return require(filePath);
} catch (e) {
// couldn't locate using find-up, try to require directly
findupErr = e;
}
// if we couldn't find the file path through find-up, try requiring the string directly
try {
return require(file);
} catch (e) {
e.message = "Could not locate the specified module through requiring directly, nor through " +
"searching the directories above " + ImperativeConfig.instance.callerLocation +
". 'require()' error message: " + e.message +
" \n 'find-up' (directory search) error message:" + findupErr.message;
throw new ImperativeError({msg: e.message});
}
}
}