Skip to content

Commit bd1f0a1

Browse files
Copilotvladfrangu
andauthored
feat: Add automatic git repository initialization to apify create command (#919)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: vladfrangu <[email protected]> Co-authored-by: Vlad Frangu <[email protected]>
1 parent a05fad5 commit bd1f0a1

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

src/commands/create.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ export class CreateCommand extends ApifyCommand<typeof CreateCommand> {
5757
description: 'Skip installing optional dependencies.',
5858
required: false,
5959
}),
60+
'skip-git-init': Flags.boolean({
61+
description: 'Skip initializing a git repository in the Actor directory.',
62+
required: false,
63+
}),
6064
};
6165

6266
static override args = {
@@ -68,7 +72,7 @@ export class CreateCommand extends ApifyCommand<typeof CreateCommand> {
6872

6973
async run() {
7074
let { actorName } = this.args;
71-
const { template: templateName, skipDependencyInstall } = this.flags;
75+
const { template: templateName, skipDependencyInstall, skipGitInit } = this.flags;
7276

7377
// --template-archive-url is an internal, undocumented flag that's used
7478
// for testing of templates that are not yet published in the manifest
@@ -308,6 +312,20 @@ export class CreateCommand extends ApifyCommand<typeof CreateCommand> {
308312
});
309313
}
310314

315+
// Initialize git repository before reporting success, but store result for later
316+
let gitInitResult: { success: boolean; error?: Error } = { success: true };
317+
if (!skipGitInit) {
318+
try {
319+
await execWithLog({
320+
cmd: 'git',
321+
args: ['init'],
322+
opts: { cwd: actFolderDir },
323+
});
324+
} catch (err) {
325+
gitInitResult = { success: false, error: err as Error };
326+
}
327+
}
328+
311329
if (dependenciesInstalled) {
312330
success({ message: `Actor '${actorName}' was created. To run it, run "cd ${actorName}" and "apify run".` });
313331
info({ message: 'To run your code in the cloud, run "apify push" and deploy your code to Apify Console.' });
@@ -319,5 +337,18 @@ export class CreateCommand extends ApifyCommand<typeof CreateCommand> {
319337
message: `Actor '${actorName}' was created. Please install its dependencies to be able to run it using "apify run".`,
320338
});
321339
}
340+
341+
// Report git initialization result after actor creation success
342+
if (!skipGitInit) {
343+
if (gitInitResult.success) {
344+
info({
345+
message: `Git repository initialized in '${actorName}'. You can now commit and push your Actor to Git.`,
346+
});
347+
} else {
348+
// Git init is not critical, so we just warn if it fails
349+
warning({ message: `Failed to initialize git repository: ${gitInitResult.error!.message}` });
350+
warning({ message: 'You can manually run "git init" in the Actor directory if needed.' });
351+
}
352+
}
322353
}
323354
}

test/local/commands/create.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,36 @@ describe('apify create', () => {
118118
expect(existsSync(joinPath('node_modules', 'cheerio'))).toBeTruthy();
119119
expect(existsSync(joinPath('node_modules', 'playwright'))).toBeFalsy();
120120
});
121+
122+
it('should initialize git repository by default', async () => {
123+
const ACT_TEMPLATE = 'project_empty';
124+
125+
await testRunCommand(CreateCommand, {
126+
args_actorName: actName,
127+
flags_template: ACT_TEMPLATE,
128+
flags_skipDependencyInstall: true,
129+
});
130+
131+
toggleCwdBetweenFullAndParentPath();
132+
133+
// Check that .git directory exists
134+
expect(existsSync(joinPath('.git'))).toBeTruthy();
135+
expect(existsSync(joinPath('.git', 'config'))).toBeTruthy();
136+
});
137+
138+
it('should skip git initialization when --skip-git-init flag is used', async () => {
139+
const ACT_TEMPLATE = 'project_empty';
140+
141+
await testRunCommand(CreateCommand, {
142+
args_actorName: actName,
143+
flags_template: ACT_TEMPLATE,
144+
flags_skipDependencyInstall: true,
145+
flags_skipGitInit: true,
146+
});
147+
148+
toggleCwdBetweenFullAndParentPath();
149+
150+
// Check that .git directory does not exist
151+
expect(existsSync(joinPath('.git'))).toBeFalsy();
152+
});
121153
});

0 commit comments

Comments
 (0)