Skip to content

Commit 5a4e36c

Browse files
committed
Use Git to resolve root path in no-VCS mode
1 parent 574bde9 commit 5a4e36c

File tree

3 files changed

+83
-12
lines changed

3 files changed

+83
-12
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import spawnAsync from '@expo/spawn-async';
2+
import fs from 'fs/promises';
3+
import os from 'os';
4+
import path from 'path';
5+
6+
import NoVcsClient from '../noVcs';
7+
8+
describe('noVcs', () => {
9+
describe('NoVcsClient', () => {
10+
let vcs: NoVcsClient;
11+
let repoRoot: string;
12+
let globalEasProjectRoot: string | undefined;
13+
14+
afterAll(async () => {
15+
await fs.rm(repoRoot, { recursive: true, force: true });
16+
process.env.EAS_PROJECT_ROOT = globalEasProjectRoot;
17+
});
18+
19+
beforeAll(async () => {
20+
repoRoot = await fs.mkdtemp(path.join(os.tmpdir(), 'eas-cli-git-test-'));
21+
22+
vcs = new NoVcsClient({ cwdOverride: repoRoot });
23+
globalEasProjectRoot = process.env.EAS_PROJECT_ROOT;
24+
delete process.env.EAS_PROJECT_ROOT;
25+
});
26+
27+
it('should return the current working directory when not in Git repository', async () => {
28+
expect(await vcs.getRootPathAsync()).toBe(process.cwd());
29+
});
30+
31+
it('should return the Git root when in Git repository', async () => {
32+
await spawnAsync('git', ['init'], { cwd: repoRoot });
33+
expect(await fs.realpath(await vcs.getRootPathAsync())).toBe(await fs.realpath(repoRoot));
34+
});
35+
36+
it('should return the project root when EAS_PROJECT_ROOT is set', async () => {
37+
process.env.EAS_PROJECT_ROOT = 'project-root';
38+
expect(await vcs.getRootPathAsync()).toBe(path.resolve(process.cwd(), 'project-root'));
39+
40+
process.env.EAS_PROJECT_ROOT = '/app';
41+
expect(await vcs.getRootPathAsync()).toBe('/app');
42+
});
43+
});
44+
});

packages/eas-cli/src/vcs/clients/noVcs.ts

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,53 @@
1-
import { Ignore, getRootPath, makeShallowCopyAsync } from '../local';
1+
import spawnAsync from '@expo/spawn-async';
2+
import path from 'path';
3+
4+
import Log from '../../log';
5+
import { Ignore, makeShallowCopyAsync } from '../local';
26
import { Client } from '../vcs';
37

48
export default class NoVcsClient extends Client {
9+
private readonly cwdOverride?: string;
10+
11+
constructor(options: { cwdOverride?: string } = {}) {
12+
super();
13+
this.cwdOverride = options.cwdOverride;
14+
}
15+
516
public async getRootPathAsync(): Promise<string> {
6-
return getRootPath();
17+
// Honor `EAS_PROJECT_ROOT` if it is set.
18+
if (process.env.EAS_PROJECT_ROOT) {
19+
const rootPath = process.env.EAS_PROJECT_ROOT;
20+
// `path.resolve()` will return `rootPath` if it is absolute
21+
// (which is what we want).
22+
return path.resolve(process.cwd(), rootPath);
23+
}
24+
25+
// If `EAS_PROJECT_ROOT` is not set, try to get the root path from Git.
26+
try {
27+
return (
28+
await spawnAsync('git', ['rev-parse', '--show-toplevel'], {
29+
cwd: this.cwdOverride,
30+
})
31+
).stdout.trim();
32+
} catch (err) {
33+
Log.warn(`Failed to get Git root path with \`git rev-parse --show-toplevel\`.`, err);
34+
Log.warn('Falling back to using current working directory as project root.');
35+
Log.warn(
36+
'You can set `EAS_PROJECT_ROOT` environment variable to let eas-cli know where your project is located.'
37+
);
38+
39+
return process.cwd();
40+
}
741
}
842

943
public async makeShallowCopyAsync(destinationPath: string): Promise<void> {
10-
const srcPath = getRootPath();
44+
const srcPath = path.normalize(await this.getRootPathAsync());
1145
await makeShallowCopyAsync(srcPath, destinationPath);
1246
}
1347

1448
public override async isFileIgnoredAsync(filePath: string): Promise<boolean> {
15-
const ignore = await Ignore.createAsync(getRootPath());
49+
const rootPath = path.normalize(await this.getRootPathAsync());
50+
const ignore = await Ignore.createAsync(rootPath);
1651
return ignore.ignores(filePath);
1752
}
1853

packages/eas-cli/src/vcs/local.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,6 @@ const DEFAULT_IGNORE = `
1414
node_modules
1515
`;
1616

17-
export function getRootPath(): string {
18-
const rootPath = process.env.EAS_PROJECT_ROOT ?? process.cwd();
19-
if (!path.isAbsolute(rootPath)) {
20-
return path.resolve(process.cwd(), rootPath);
21-
}
22-
return rootPath;
23-
}
24-
2517
/**
2618
* Ignore wraps the 'ignore' package to support multiple .gitignore files
2719
* in subdirectories.

0 commit comments

Comments
 (0)