Skip to content

Commit c379a89

Browse files
committed
Fixed bug with github command when creating a repo.
1 parent 48503ae commit c379a89

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

src/__tests__/github.test.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,27 +140,38 @@ describe('createRepo', () => {
140140
mockExeca.mockResolvedValue({ stdout: '', stderr: '', exitCode: 0 });
141141
});
142142

143-
it('calls gh repo create with expected args and returns repo URL', async () => {
143+
it('initializes git and calls gh repo create with expected args and returns repo URL', async () => {
144144
const url = await createRepo({
145145
repoName: 'my-app',
146146
projectPath,
147147
username,
148148
});
149149

150150
expect(url).toBe('https://github.com/octocat/my-app.git');
151-
expect(mockExeca).toHaveBeenCalledWith(
151+
expect(mockExeca).toHaveBeenCalledTimes(4);
152+
expect(mockExeca).toHaveBeenNthCalledWith(1, 'git', ['init'], {
153+
stdio: 'inherit',
154+
cwd: projectPath,
155+
});
156+
expect(mockExeca).toHaveBeenNthCalledWith(2, 'git', ['add', '.'], {
157+
stdio: 'inherit',
158+
cwd: projectPath,
159+
});
160+
expect(mockExeca).toHaveBeenNthCalledWith(3, 'git', ['commit', '-m', 'Initial commit'], {
161+
stdio: 'inherit',
162+
cwd: projectPath,
163+
});
164+
expect(mockExeca).toHaveBeenNthCalledWith(
165+
4,
152166
'gh',
153167
[
154168
'repo',
155169
'create',
156170
'my-app',
157171
'--public',
158-
'--source',
159-
projectPath,
160-
'--description',
161-
'',
162-
'--remote',
163-
'origin',
172+
`--source=${projectPath}`,
173+
'--remote=origin',
174+
'--push',
164175
],
165176
{ stdio: 'inherit', cwd: projectPath },
166177
);
@@ -174,9 +185,10 @@ describe('createRepo', () => {
174185
description: 'My cool project',
175186
});
176187

177-
expect(mockExeca).toHaveBeenCalledWith(
188+
expect(mockExeca).toHaveBeenNthCalledWith(
189+
4,
178190
'gh',
179-
expect.arrayContaining(['--description', 'My cool project']),
191+
expect.arrayContaining(['--description=My cool project']),
180192
expect.any(Object),
181193
);
182194
});

src/github.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import fs from 'fs-extra';
2+
import path from 'path';
13
import { execa } from 'execa';
24

35
/**
@@ -56,13 +58,28 @@ export async function createRepo(options: {
5658
username: string;
5759
description?: string;
5860
}): Promise<string> {
61+
const gitDir = path.join(options.projectPath, '.git');
62+
if (!(await fs.pathExists(gitDir))) {
63+
await execa('git', ['init'], { stdio: 'inherit', cwd: options.projectPath });
64+
await execa('git', ['add', '.'], { stdio: 'inherit', cwd: options.projectPath });
65+
await execa('git', ['commit', '-m', 'Initial commit'], {
66+
stdio: 'inherit',
67+
cwd: options.projectPath,
68+
});
69+
}
70+
5971
const args = [
60-
'repo', 'create', options.repoName,
72+
'repo',
73+
'create',
74+
options.repoName,
6175
'--public',
62-
'--source', options.projectPath,
63-
'--description', options.description || '',
64-
'--remote', 'origin',
76+
`--source=${options.projectPath}`,
77+
'--remote=origin',
78+
'--push',
6579
];
80+
if (options.description) {
81+
args.push(`--description=${options.description}`);
82+
}
6683
await execa('gh', args, { stdio: 'inherit', cwd: options.projectPath });
6784
return `https://github.com/${options.username}/${options.repoName}.git`;
6885
}

0 commit comments

Comments
 (0)