Skip to content

Commit df95e03

Browse files
committed
get rid of chdir
1 parent 9f99b1d commit df95e03

10 files changed

Lines changed: 63 additions & 21 deletions

File tree

packages/plugin-platform-apple/src/lib/commands/build/buildProject.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import spawn, { SubprocessError } from 'nano-spawn';
88

99
export const buildProject = async (
1010
xcodeProject: XcodeProjectInfo,
11+
sourceDir: string,
1112
platformName: ApplePlatform,
1213
udid: string | undefined,
1314
scheme: string,
@@ -66,7 +67,9 @@ export const buildProject = async (
6667
);
6768
logger.debug(`Running "xcodebuild ${xcodebuildArgs.join(' ')}.`);
6869
try {
69-
const { output } = await spawn('xcodebuild', xcodebuildArgs);
70+
const { output } = await spawn('xcodebuild', xcodebuildArgs, {
71+
cwd: sourceDir,
72+
});
7073
loader.stop(
7174
`Built the app with xcodebuild for ${scheme} scheme in ${mode} mode.`
7275
);

packages/plugin-platform-apple/src/lib/commands/build/createBuild.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@ export const createBuild = async (
2929
}
3030

3131
normalizeArgs(args, xcodeProject);
32-
// @todo replace chdir with running the command in the {cwd: sourceDir}
33-
process.chdir(sourceDir);
3432

3533
const { scheme, mode } = args.interactive
3634
? await selectFromInteractiveMode(xcodeProject, args.scheme, args.mode)
3735
: await getConfiguration(
3836
xcodeProject,
37+
sourceDir,
3938
args.scheme,
4039
args.mode,
4140
platformName
@@ -44,6 +43,7 @@ export const createBuild = async (
4443
try {
4544
await buildProject(
4645
xcodeProject,
46+
sourceDir,
4747
platformName,
4848
undefined,
4949
scheme,

packages/plugin-platform-apple/src/lib/commands/build/getConfiguration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import color from 'picocolors';
77

88
export async function getConfiguration(
99
xcodeProject: XcodeProjectInfo,
10+
sourceDir: string,
1011
inputScheme: string,
1112
inputMode: string,
1213
platformName: ApplePlatform
1314
) {
14-
const sourceDir = process.cwd();
1515
const info = await getInfo(xcodeProject, sourceDir);
1616
checkIfConfigurationExists(info?.configurations ?? [], inputMode);
1717
let scheme = inputScheme;

packages/plugin-platform-apple/src/lib/commands/run/createRun.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,30 @@ export const createRun = async (
3838
}
3939

4040
normalizeArgs(args, projectRoot, xcodeProject);
41-
// @todo replace chdir with running the command in the {cwd: sourceDir}
42-
process.chdir(sourceDir);
4341

4442
const { scheme, mode } = args.interactive
4543
? await selectFromInteractiveMode(xcodeProject, args.scheme, args.mode)
4644
: await getConfiguration(
4745
xcodeProject,
46+
sourceDir,
4847
args.scheme,
4948
args.mode,
5049
platformName
5150
);
5251

5352
if (platformName === 'macos') {
54-
await runOnMac(xcodeProject, mode, scheme, args);
53+
await runOnMac(xcodeProject, sourceDir, mode, scheme, args);
5554
outro('Success 🎉.');
5655
return;
5756
} else if (args.catalyst) {
58-
await runOnMacCatalyst(platformName, mode, scheme, xcodeProject, args);
57+
await runOnMacCatalyst(
58+
platformName,
59+
mode,
60+
scheme,
61+
xcodeProject,
62+
sourceDir,
63+
args
64+
);
5965
outro('Success 🎉.');
6066
return;
6167
}
@@ -76,13 +82,22 @@ export const createRun = async (
7682
await runOnSimulator(
7783
device,
7884
xcodeProject,
85+
sourceDir,
7986
platformName,
8087
mode,
8188
scheme,
8289
args
8390
);
8491
} else if (device.type === 'device') {
85-
await runOnDevice(device, platformName, mode, scheme, xcodeProject, args);
92+
await runOnDevice(
93+
device,
94+
platformName,
95+
mode,
96+
scheme,
97+
xcodeProject,
98+
sourceDir,
99+
args
100+
);
86101
}
87102
outro('Success 🎉.');
88103
return;
@@ -116,6 +131,7 @@ export const createRun = async (
116131
await runOnSimulator(
117132
simulator,
118133
xcodeProject,
134+
sourceDir,
119135
platformName,
120136
mode,
121137
scheme,

packages/plugin-platform-apple/src/lib/commands/run/getBuildSettings.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,28 @@ export type BuildSettings = {
1212

1313
export async function getBuildSettings(
1414
xcodeProject: XcodeProjectInfo,
15+
sourceDir: string,
1516
mode: string,
1617
buildOutput: string,
1718
scheme: string,
1819
target?: string
1920
): Promise<BuildSettings | null> {
20-
const { stdout: buildSettings } = await spawn('xcodebuild', [
21-
xcodeProject.isWorkspace ? '-workspace' : '-project',
22-
xcodeProject.name,
23-
'-scheme',
24-
scheme,
25-
'-sdk',
26-
getPlatformName(buildOutput),
27-
'-configuration',
28-
mode,
29-
'-showBuildSettings',
30-
'-json',
31-
]);
21+
const { stdout: buildSettings } = await spawn(
22+
'xcodebuild',
23+
[
24+
xcodeProject.isWorkspace ? '-workspace' : '-project',
25+
xcodeProject.name,
26+
'-scheme',
27+
scheme,
28+
'-sdk',
29+
getPlatformName(buildOutput),
30+
'-configuration',
31+
mode,
32+
'-showBuildSettings',
33+
'-json',
34+
],
35+
{ cwd: sourceDir }
36+
);
3237

3338
const settings = JSON.parse(buildSettings);
3439

packages/plugin-platform-apple/src/lib/commands/run/installApp.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import spawn, { SubprocessError } from 'nano-spawn';
88
type Options = {
99
buildOutput: string;
1010
xcodeProject: XcodeProjectInfo;
11+
sourceDir: string;
1112
mode: string;
1213
scheme: string;
1314
target?: string;
@@ -19,6 +20,7 @@ type Options = {
1920
export default async function installApp({
2021
buildOutput,
2122
xcodeProject,
23+
sourceDir,
2224
mode,
2325
scheme,
2426
target,
@@ -30,6 +32,7 @@ export default async function installApp({
3032

3133
const buildSettings = await getBuildSettings(
3234
xcodeProject,
35+
sourceDir,
3336
mode,
3437
buildOutput,
3538
scheme,

packages/plugin-platform-apple/src/lib/commands/run/runOnDevice.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export async function runOnDevice(
1313
mode: string,
1414
scheme: string,
1515
xcodeProject: XcodeProjectInfo,
16+
sourceDir: string,
1617
args: RunFlags
1718
) {
1819
try {
@@ -29,6 +30,7 @@ export async function runOnDevice(
2930
if (!args.binaryPath) {
3031
buildOutput = await buildProject(
3132
xcodeProject,
33+
sourceDir,
3234
platform,
3335
selectedDevice.udid,
3436
scheme,
@@ -38,6 +40,7 @@ export async function runOnDevice(
3840

3941
const buildSettings = await getBuildSettings(
4042
xcodeProject,
43+
sourceDir,
4144
mode,
4245
buildOutput,
4346
scheme

packages/plugin-platform-apple/src/lib/commands/run/runOnMac.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ import { RunFlags } from './runOptions.js';
99

1010
export async function runOnMac(
1111
xcodeProject: XcodeProjectInfo,
12+
sourceDir: string,
1213
mode: string,
1314
scheme: string,
1415
args: RunFlags
1516
) {
1617
const buildOutput = await buildProject(
1718
xcodeProject,
19+
sourceDir,
1820
'macos',
1921
undefined,
2022
scheme,
@@ -25,6 +27,7 @@ export async function runOnMac(
2527
await openApp({
2628
buildOutput,
2729
xcodeProject,
30+
sourceDir,
2831
mode,
2932
scheme,
3033
target: args.target,
@@ -35,6 +38,7 @@ export async function runOnMac(
3538
type Options = {
3639
buildOutput: string;
3740
xcodeProject: XcodeProjectInfo;
41+
sourceDir: string;
3842
mode: string;
3943
scheme: string;
4044
target?: string;
@@ -44,6 +48,7 @@ type Options = {
4448
async function openApp({
4549
buildOutput,
4650
xcodeProject,
51+
sourceDir,
4752
mode,
4853
scheme,
4954
target,
@@ -53,6 +58,7 @@ async function openApp({
5358

5459
const buildSettings = await getBuildSettings(
5560
xcodeProject,
61+
sourceDir,
5662
mode,
5763
buildOutput,
5864
scheme,

packages/plugin-platform-apple/src/lib/commands/run/runOnMacCatalyst.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export async function runOnMacCatalyst(
1010
mode: string,
1111
scheme: string,
1212
xcodeProject: XcodeProjectInfo,
13+
sourceDir: string,
1314
args: RunFlags
1415
) {
1516
if (args.binaryPath) {
@@ -19,6 +20,7 @@ export async function runOnMacCatalyst(
1920
}
2021
const buildOutput = await buildProject(
2122
xcodeProject,
23+
sourceDir,
2224
platform,
2325
undefined,
2426
scheme,
@@ -28,6 +30,7 @@ export async function runOnMacCatalyst(
2830

2931
const buildSettings = await getBuildSettings(
3032
xcodeProject,
33+
sourceDir,
3134
mode,
3235
buildOutput,
3336
scheme

packages/plugin-platform-apple/src/lib/commands/run/runOnSimulator.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { spinner } from '@clack/prompts';
88
export async function runOnSimulator(
99
simulator: Device,
1010
xcodeProject: XcodeProjectInfo,
11+
sourceDir: string,
1112
platform: ApplePlatform,
1213
mode: string,
1314
scheme: string,
@@ -46,6 +47,7 @@ export async function runOnSimulator(
4647
if (!binaryPath) {
4748
buildOutput = await buildProject(
4849
xcodeProject,
50+
sourceDir,
4951
platform,
5052
simulator.udid,
5153
scheme,
@@ -58,6 +60,7 @@ export async function runOnSimulator(
5860
await installApp({
5961
buildOutput: buildOutput ?? '',
6062
xcodeProject,
63+
sourceDir,
6164
mode,
6265
scheme,
6366
target,

0 commit comments

Comments
 (0)