Skip to content

Commit f531de5

Browse files
committed
Extract shared expandHomePrefix utility to eliminate duplication
Move the tilde expansion logic into a shared utils/expand-home.ts module and use it from all four call sites: build-utils.ts, project-config.ts, build-settings.ts, and cli/commands/init.ts. The shared version handles both ~/ and ~\ (Windows) consistently, matching the behavior that was previously only in init.ts.
1 parent e414185 commit f531de5

5 files changed

Lines changed: 34 additions & 46 deletions

File tree

src/cli/commands/init.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as os from 'node:os';
55
import * as clack from '@clack/prompts';
66
import { getResourceRoot } from '../../core/resource-root.ts';
77
import { createPrompter, isInteractiveTTY, type Prompter } from '../interactive/prompts.ts';
8+
import { expandHomePrefix } from '../../utils/expand-home.ts';
89

910
type SkillType = 'mcp' | 'cli';
1011

@@ -72,18 +73,6 @@ function readSkillContent(skillType: SkillType): string {
7273
return fs.readFileSync(sourcePath, 'utf8');
7374
}
7475

75-
function expandHomePrefix(inputPath: string): string {
76-
if (inputPath === '~') {
77-
return os.homedir();
78-
}
79-
80-
if (inputPath.startsWith('~/') || inputPath.startsWith('~\\')) {
81-
return path.join(os.homedir(), inputPath.slice(2));
82-
}
83-
84-
return inputPath;
85-
}
86-
8776
function resolveDestinationPath(inputPath: string): string {
8877
return path.resolve(expandHomePrefix(inputPath));
8978
}

src/mcp/tools/device/build-settings.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
11
import path from 'node:path';
2-
import { homedir } from 'node:os';
32
import { XcodePlatform } from '../../../types/common.ts';
43
import type { CommandExecutor } from '../../../utils/execution/index.ts';
4+
import { expandHomePrefix } from '../../../utils/expand-home.ts';
55

66
function resolvePathFromCwd(pathValue?: string): string | undefined {
77
if (!pathValue) {
88
return undefined;
99
}
1010

11-
if (pathValue === '~') {
12-
return homedir();
11+
const expanded = expandHomePrefix(pathValue);
12+
if (path.isAbsolute(expanded)) {
13+
return expanded;
1314
}
1415

15-
if (pathValue.startsWith('~/')) {
16-
return path.join(homedir(), pathValue.slice(2));
17-
}
18-
19-
if (path.isAbsolute(pathValue)) {
20-
return pathValue;
21-
}
22-
23-
return path.resolve(process.cwd(), pathValue);
16+
return path.resolve(process.cwd(), expanded);
2417
}
2518

2619
export type DevicePlatform = 'iOS' | 'watchOS' | 'tvOS' | 'visionOS';

src/utils/build-utils.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,14 @@ import {
3232
} from './xcodemake.ts';
3333
import { sessionStore } from './session-store.ts';
3434
import path from 'path';
35-
import { homedir } from 'os';
35+
import { expandHomePrefix } from './expand-home.ts';
3636

3737
function resolvePathFromCwd(pathValue: string): string {
38-
if (pathValue === '~') {
39-
return homedir();
38+
const expanded = expandHomePrefix(pathValue);
39+
if (path.isAbsolute(expanded)) {
40+
return expanded;
4041
}
41-
if (pathValue.startsWith('~/')) {
42-
return path.join(homedir(), pathValue.slice(2));
43-
}
44-
if (path.isAbsolute(pathValue)) {
45-
return pathValue;
46-
}
47-
return path.resolve(process.cwd(), pathValue);
42+
return path.resolve(process.cwd(), expanded);
4843
}
4944

5045
/**

src/utils/expand-home.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import path from 'node:path';
2+
import { homedir } from 'node:os';
3+
4+
/**
5+
* Expand a leading ~ or ~/ (or ~\ on Windows) prefix to the user's home directory.
6+
* Returns the path unchanged if it does not start with ~.
7+
*/
8+
export function expandHomePrefix(inputPath: string): string {
9+
if (inputPath === '~') {
10+
return homedir();
11+
}
12+
13+
if (inputPath.startsWith('~/') || inputPath.startsWith('~\\')) {
14+
return path.join(homedir(), inputPath.slice(2));
15+
}
16+
17+
return inputPath;
18+
}

src/utils/project-config.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import path from 'node:path';
2-
import { homedir } from 'node:os';
32
import { fileURLToPath } from 'node:url';
43
import { parse as parseYaml, stringify as stringifyYaml } from 'yaml';
54
import type { FileSystemExecutor } from './FileSystemExecutor.ts';
65
import type { SessionDefaults } from './session-store.ts';
76
import { log } from './logger.ts';
7+
import { expandHomePrefix } from './expand-home.ts';
88
import { removeUndefined } from './remove-undefined.ts';
99
import { runtimeConfigFileSchema, type RuntimeConfigFile } from './runtime-config-schema.ts';
1010
import { normalizeSessionDefaultsProfileName } from './session-defaults-profile.ts';
@@ -126,19 +126,12 @@ function normalizePathValue(value: string, cwd: string): string {
126126
return fileUrlPath;
127127
}
128128

129-
if (value === '~') {
130-
return homedir();
129+
const expanded = expandHomePrefix(value);
130+
if (path.isAbsolute(expanded)) {
131+
return expanded;
131132
}
132133

133-
if (value.startsWith('~/')) {
134-
return path.join(homedir(), value.slice(2));
135-
}
136-
137-
if (path.isAbsolute(value)) {
138-
return value;
139-
}
140-
141-
return path.resolve(cwd, value);
134+
return path.resolve(cwd, expanded);
142135
}
143136

144137
function resolveRelativeSessionPaths(

0 commit comments

Comments
 (0)