-
Notifications
You must be signed in to change notification settings - Fork 69
/
global.ts
110 lines (99 loc) · 3.29 KB
/
global.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
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import { env } from '@salesforce/kit';
import { SfError } from './sfError';
/**
* Represents an environment mode. Supports `production`, `development`, `demo`, and `test`
* with the default mode being `production`.
*
* To set the mode, `export SFDX_ENV=<mode>` in your current environment.
*/
export enum Mode {
PRODUCTION = 'production',
DEVELOPMENT = 'development',
DEMO = 'demo',
TEST = 'test',
}
/**
* Global constants, methods, and configuration.
*/
export class Global {
/**
* Enable interoperability between `.sfdx` and `.sf`.
*
* When @salesforce/core@v2 is deprecated and no longer used, this can be removed.
*/
public static SFDX_INTEROPERABILITY = env.getBoolean('SF_SFDX_INTEROPERABILITY', true);
/**
* The global folder in which sfdx state is stored.
*/
public static readonly SFDX_STATE_FOLDER = '.sfdx';
/**
* The global folder in which sf state is stored.
*/
public static readonly SF_STATE_FOLDER = '.sf';
/**
* The preferred global folder in which state is stored.
*/
public static readonly STATE_FOLDER = Global.SFDX_STATE_FOLDER;
/**
* The full system path to the global sfdx state folder.
*
* **See** {@link Global.SFDX_STATE_FOLDER}
*/
public static get SFDX_DIR(): string {
return path.join(os.homedir(), Global.SFDX_STATE_FOLDER);
}
/**
* The full system path to the global sf state folder.
*
* **See** {@link Global.SF_STATE_FOLDER}
*/
public static get SF_DIR(): string {
return path.join(os.homedir(), Global.SF_STATE_FOLDER);
}
/**
* The full system path to the preferred global state folder
*/
public static get DIR(): string {
return path.join(os.homedir(), Global.SFDX_STATE_FOLDER);
}
/**
* Gets the current mode environment variable as a {@link Mode} instance.
*
* ```
* console.log(Global.getEnvironmentMode() === Mode.PRODUCTION);
* ```
*/
public static getEnvironmentMode(): Mode {
const envValue = env.getString('SF_ENV') ?? env.getString('SFDX_ENV', Mode.PRODUCTION);
return envValue in Mode || envValue.toUpperCase() in Mode
? Mode[envValue.toUpperCase() as keyof typeof Mode]
: Mode.PRODUCTION;
}
/**
* Creates a directory within {@link Global.SFDX_DIR}, or {@link Global.SFDX_DIR} itself if the `dirPath` param
* is not provided. This is resolved or rejected when the directory creation operation has completed.
*
* @param dirPath The directory path to be created within {@link Global.SFDX_DIR}.
*/
public static async createDir(dirPath?: string): Promise<void> {
const resolvedPath = dirPath ? path.join(Global.SFDX_DIR, dirPath) : Global.SFDX_DIR;
try {
if (process.platform === 'win32') {
await fs.promises.mkdir(resolvedPath, { recursive: true });
} else {
await fs.promises.mkdir(resolvedPath, { recursive: true, mode: 0o700 });
}
} catch (error) {
throw new SfError(`Failed to create directory or set permissions for: ${resolvedPath}`);
}
}
}