Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions web/packages/teleterm/src/mainProcess/fixtures/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ export const makeRuntimeSettings = (
insecure: true,
userDataDir: '',
sessionDataDir: '',
homeDir: '',
tempDataDir: '',
agentBinaryPath: '',
binDir: '',
Expand Down
2 changes: 2 additions & 0 deletions web/packages/teleterm/src/mainProcess/runtimeSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const insecure =
(dev && !!env.CONNECT_INSECURE);

export async function getRuntimeSettings(): Promise<RuntimeSettings> {
const homeDir = app.getPath('home');
const userDataDir = app.getPath('userData');
const sessionDataDir = app.getPath('sessionData');
const tempDataDir = app.getPath('temp');
Expand Down Expand Up @@ -106,6 +107,7 @@ export async function getRuntimeSettings(): Promise<RuntimeSettings> {
tshd,
sharedProcess,
tshdEvents,
homeDir,
userDataDir,
sessionDataDir,
tempDataDir,
Expand Down
2 changes: 2 additions & 0 deletions web/packages/teleterm/src/mainProcess/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export type RuntimeSettings = {
* - Starting the app in dev mode with the CONNECT_INSECURE env var.
*/
insecure: boolean;
/** User's home directory. */
homeDir: string;
userDataDir: string;
sessionDataDir: string;
tempDataDir: string;
Expand Down
39 changes: 29 additions & 10 deletions web/packages/teleterm/src/services/config/appConfigSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const createAppConfigSchema = (settings: RuntimeSettings) => {
CUSTOM_SHELL_ID,
];

const pathSchema = tildeExpandingPathSchema(settings.homeDir);
const shortcutSchema = createKeyboardShortcutSchema(settings.platform);

// `keymap.` prefix is used in `initUi.ts` in a predicate function.
Expand All @@ -70,10 +71,10 @@ export const createAppConfigSchema = (settings: RuntimeSettings) => {
.describe(
'Keeps the app running in the menu bar/system tray even when the main window is closed. On Linux, displaying the system tray icon may require installing shell extensions.'
),
tshHome: z
.string()
.default(settings.tshd.defaultHomeDir)
.describe('Home location for tsh configuration and data.'),
tshHome: pathSchema({
defaultPath: settings.tshd.defaultHomeDir,
description: 'Home location for tsh configuration and data.',
}),
/**
* This value can be provided by the user and is unsanitized. This means that it cannot be directly interpolated
* in a styled component or used in CSS, as it may inject malicious CSS code.
Expand Down Expand Up @@ -112,12 +113,11 @@ export const createAppConfigSchema = (settings: RuntimeSettings) => {
`Cannot find the shell "${iss.input}". Available options are: ${availableShellIdsWithCustom.join(', ')}. Using platform default.`,
}
),
'terminal.customShell': z
.string()
.default('')
.describe(
'Path to the custom shell that is used when `terminal.shell` is set to `custom`. It is best to configure it through UI (right click on a terminal tab > Custom Shell…).'
),
'terminal.customShell': pathSchema({
defaultPath: '',
description:
'Path to the custom shell that is used when `terminal.shell` is set to `custom`. It is best to configure it through UI (right click on a terminal tab > Custom Shell…).',
}),
'terminal.rightClick': z
.enum(['paste', 'copyPaste', 'menu'])
.default(settings.platform === 'win32' ? 'copyPaste' : 'menu')
Expand Down Expand Up @@ -375,3 +375,22 @@ z.config({
}
},
});

/**
* Creates a Zod string schema for filesystem paths that automatically expand
* leading "~" to the provided home directory.
*/
function tildeExpandingPathSchema(homeDir: string) {
return ({
defaultPath,
description,
}: {
defaultPath: string;
description: string;
}) =>
z
.string()
.default(defaultPath)
.describe(description)
.transform(p => p.replace(/^~/, homeDir));
}
15 changes: 15 additions & 0 deletions web/packages/teleterm/src/services/config/configService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,18 @@ test(`enum validation`, () => {
path: expect.arrayContaining(['theme']),
});
});

test('tilde expanding in path schema', () => {
const configFile = createMockFileStorage();
configFile.replace({
tshHome: '~/.~tsh-dev',
});
const configService = createConfigService({
configFile,
jsonSchemaFile: createMockFileStorage(),
settings: makeRuntimeSettings({ homeDir: '/Users/testuser' }),
});
expect(configService.get('tshHome')).toMatchObject({
value: '/Users/testuser/.~tsh-dev',
});
});
Loading