Skip to content

Commit da41728

Browse files
committed
save new cli config to different location, misc fixes
1 parent 0b37539 commit da41728

File tree

1 file changed

+45
-34
lines changed

1 file changed

+45
-34
lines changed

packages/cli-v3/src/utilities/configFiles.ts

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { mkdirSync, writeFileSync } from "node:fs";
1+
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
22
import path from "node:path";
33
import { z } from "zod";
44
import { xdgAppPaths } from "../imports/xdg-app-paths.js";
@@ -13,6 +13,9 @@ function getGlobalConfigFolderPath() {
1313

1414
export const DEFFAULT_PROFILE = "default";
1515

16+
const CONFIG_FILE = "config.json";
17+
const OLD_CONFIG_FILE = "default.json";
18+
1619
const CliConfigProfileSettings = z.object({
1720
accessToken: z.string().optional(),
1821
apiUrl: z.string().optional(),
@@ -29,44 +32,60 @@ const CliConfigFile = z.object({
2932
});
3033
type CliConfigFile = z.infer<typeof CliConfigFile>;
3134

35+
function getOldAuthConfigFilePath() {
36+
return path.join(getGlobalConfigFolderPath(), OLD_CONFIG_FILE);
37+
}
38+
3239
function getAuthConfigFilePath() {
33-
return path.join(getGlobalConfigFolderPath(), "default.json");
40+
return path.join(getGlobalConfigFolderPath(), CONFIG_FILE);
3441
}
3542

3643
function getAuthConfigFileBackupPath() {
3744
// Multiple calls won't overwrite old backups
38-
return path.join(getGlobalConfigFolderPath(), `default.json.bak-${Date.now()}`);
45+
return path.join(getGlobalConfigFolderPath(), `${CONFIG_FILE}.bak-${Date.now()}`);
46+
}
47+
48+
function getBlankConfig(): CliConfigFile {
49+
return {
50+
version: 2,
51+
currentProfile: DEFFAULT_PROFILE,
52+
profiles: {},
53+
};
54+
}
55+
56+
function getConfig() {
57+
return readAuthConfigFile() ?? getBlankConfig();
3958
}
4059

4160
export function writeAuthConfigCurrentProfileName(profile: string) {
42-
const existingConfig = readAuthConfigFile();
61+
const config = getConfig();
4362

44-
existingConfig.currentProfile = profile;
63+
config.currentProfile = profile;
4564

46-
writeAuthConfigFile(existingConfig);
65+
writeAuthConfigFile(config);
4766
}
4867

4968
export function readAuthConfigCurrentProfileName(): string {
50-
const existingConfig = readAuthConfigFile();
51-
return existingConfig.currentProfile;
69+
const config = getConfig();
70+
return config.currentProfile;
5271
}
5372

5473
export function writeAuthConfigProfile(
5574
settings: CliConfigProfileSettings,
5675
profile: string = DEFFAULT_PROFILE
5776
) {
58-
const existingConfig = readAuthConfigFile();
77+
const config = getConfig();
5978

60-
existingConfig.profiles[profile] = settings;
79+
config.profiles[profile] = settings;
6180

62-
writeAuthConfigFile(existingConfig);
81+
writeAuthConfigFile(config);
6382
}
6483

6584
export function readAuthConfigProfile(
6685
profile: string = DEFFAULT_PROFILE
6786
): CliConfigProfileSettings | undefined {
6887
try {
69-
const config = readAuthConfigFile();
88+
const config = getConfig();
7089
return config.profiles[profile];
7190
} catch (error) {
7291
logger.debug(`Error reading auth config file: ${error}`);
@@ -75,20 +94,25 @@ export function readAuthConfigProfile(
7594
}
7695

7796
export function deleteAuthConfigProfile(profile: string = DEFFAULT_PROFILE) {
78-
const existingConfig = readAuthConfigFile();
97+
const config = getConfig();
7998

80-
delete existingConfig.profiles[profile];
99+
delete config.profiles[profile];
100+
101+
if (config.currentProfile === profile) {
102+
config.currentProfile = DEFFAULT_PROFILE;
103+
}
81104

82-
writeAuthConfigFile(existingConfig);
105+
writeAuthConfigFile(config);
83106
}
84107

85-
export function readAuthConfigFile(): CliConfigFile {
108+
export function readAuthConfigFile(): CliConfigFile | null {
86109
try {
87-
const authConfigFilePath = getAuthConfigFilePath();
110+
const configFilePath = getAuthConfigFilePath();
111+
const configFileExists = existsSync(configFilePath);
88112

89-
logger.debug(`Reading auth config file`, { authConfigFilePath });
113+
logger.debug(`Reading auth config file`, { configFilePath, configFileExists });
90114

91-
const json = readJSONFileSync(authConfigFilePath);
115+
const json = readJSONFileSync(configFileExists ? configFilePath : getOldAuthConfigFilePath());
92116

93117
if ("currentProfile" in json) {
94118
// This is the new format
@@ -105,16 +129,13 @@ export function readAuthConfigFile(): CliConfigFile {
105129
profiles: oldConfigFormat,
106130
} satisfies CliConfigFile;
107131

108-
// Save a backup
109-
backupOldConfigFile(oldConfigFormat);
110-
111-
// Then overwrite the old config with the new format
132+
// Save to new config file location, the old file will remain untouched
112133
writeAuthConfigFile(newConfigFormat);
113134

114135
return newConfigFormat;
115136
} catch (error) {
116137
logger.debug(`Error reading auth config file: ${error}`);
117-
throw new Error(`Error reading auth config file: ${error}`);
138+
return null;
118139
}
119140
}
120141

@@ -127,13 +148,3 @@ export function writeAuthConfigFile(config: CliConfigFile) {
127148
encoding: "utf-8",
128149
});
129150
}
130-
131-
export function backupOldConfigFile(config: OldCliConfigFile) {
132-
const authConfigFilePath = getAuthConfigFileBackupPath();
133-
mkdirSync(path.dirname(authConfigFilePath), {
134-
recursive: true,
135-
});
136-
writeFileSync(path.join(authConfigFilePath), JSON.stringify(config, undefined, 2), {
137-
encoding: "utf-8",
138-
});
139-
}

0 commit comments

Comments
 (0)