diff --git a/src/commands/create.ts b/src/commands/create.ts index 8324d0a2b..d96f3d9bf 100644 --- a/src/commands/create.ts +++ b/src/commands/create.ts @@ -57,6 +57,10 @@ export class CreateCommand extends ApifyCommand { description: 'Skip installing optional dependencies.', required: false, }), + 'skip-git-init': Flags.boolean({ + description: 'Skip initializing a git repository in the Actor directory.', + required: false, + }), }; static override args = { @@ -68,7 +72,7 @@ export class CreateCommand extends ApifyCommand { async run() { let { actorName } = this.args; - const { template: templateName, skipDependencyInstall } = this.flags; + const { template: templateName, skipDependencyInstall, skipGitInit } = this.flags; // --template-archive-url is an internal, undocumented flag that's used // for testing of templates that are not yet published in the manifest @@ -308,6 +312,20 @@ export class CreateCommand extends ApifyCommand { }); } + // Initialize git repository before reporting success, but store result for later + let gitInitResult: { success: boolean; error?: Error } = { success: true }; + if (!skipGitInit) { + try { + await execWithLog({ + cmd: 'git', + args: ['init'], + opts: { cwd: actFolderDir }, + }); + } catch (err) { + gitInitResult = { success: false, error: err as Error }; + } + } + if (dependenciesInstalled) { success({ message: `Actor '${actorName}' was created. To run it, run "cd ${actorName}" and "apify run".` }); 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 { message: `Actor '${actorName}' was created. Please install its dependencies to be able to run it using "apify run".`, }); } + + // Report git initialization result after actor creation success + if (!skipGitInit) { + if (gitInitResult.success) { + info({ + message: `Git repository initialized in '${actorName}'. You can now commit and push your Actor to Git.`, + }); + } else { + // Git init is not critical, so we just warn if it fails + warning({ message: `Failed to initialize git repository: ${gitInitResult.error!.message}` }); + warning({ message: 'You can manually run "git init" in the Actor directory if needed.' }); + } + } } } diff --git a/test/local/commands/create.test.ts b/test/local/commands/create.test.ts index cc274c91c..d70f6a3df 100644 --- a/test/local/commands/create.test.ts +++ b/test/local/commands/create.test.ts @@ -118,4 +118,36 @@ describe('apify create', () => { expect(existsSync(joinPath('node_modules', 'cheerio'))).toBeTruthy(); expect(existsSync(joinPath('node_modules', 'playwright'))).toBeFalsy(); }); + + it('should initialize git repository by default', async () => { + const ACT_TEMPLATE = 'project_empty'; + + await testRunCommand(CreateCommand, { + args_actorName: actName, + flags_template: ACT_TEMPLATE, + flags_skipDependencyInstall: true, + }); + + toggleCwdBetweenFullAndParentPath(); + + // Check that .git directory exists + expect(existsSync(joinPath('.git'))).toBeTruthy(); + expect(existsSync(joinPath('.git', 'config'))).toBeTruthy(); + }); + + it('should skip git initialization when --skip-git-init flag is used', async () => { + const ACT_TEMPLATE = 'project_empty'; + + await testRunCommand(CreateCommand, { + args_actorName: actName, + flags_template: ACT_TEMPLATE, + flags_skipDependencyInstall: true, + flags_skipGitInit: true, + }); + + toggleCwdBetweenFullAndParentPath(); + + // Check that .git directory does not exist + expect(existsSync(joinPath('.git'))).toBeFalsy(); + }); });