diff --git a/lib/services/project-service.ts b/lib/services/project-service.ts index 4e4930a273..3c24ece6f7 100644 --- a/lib/services/project-service.ts +++ b/lib/services/project-service.ts @@ -24,6 +24,7 @@ import { IFileSystem, IProjectHelper, IStringDictionary, + IChildProcess, } from "../common/declarations"; import * as _ from "lodash"; import { injector } from "../common/yok"; @@ -42,7 +43,8 @@ export class ProjectService implements IProjectService { private $projectNameService: IProjectNameService, private $projectTemplatesService: IProjectTemplatesService, private $tempService: ITempService, - private $staticConfig: IStaticConfig + private $staticConfig: IStaticConfig, + private $childProcess: IChildProcess ) {} public async validateProjectName(opts: { @@ -103,6 +105,17 @@ export class ProjectService implements IProjectService { projectName, }); + try { + await this.$childProcess.exec(`git init ${projectDir}`); + await this.$childProcess.exec(`git -C ${projectDir} add --all`); + await this.$childProcess.exec(`git -C ${projectDir} commit --no-verify -m "init"`); + } catch (err) { + this.$logger.trace( + "Unable to initialize git repository. Error is: ", + err + ); + } + this.$logger.info(); this.$logger.printMarkdown( "__Project `%s` was successfully created.__", diff --git a/test/project-service.ts b/test/project-service.ts index 5c986a4865..0fea4151ba 100644 --- a/test/project-service.ts +++ b/test/project-service.ts @@ -87,6 +87,13 @@ describe("projectService", () => { extractPackage: () => Promise.resolve(), }); testInjector.register("tempService", TempServiceStub); + const executedCommands: string[] = []; + testInjector.register("childProcess", { + _getExecutedCommands: () => executedCommands, + exec: (executedCommand: string) => { + executedCommands.push(executedCommand); + }, + }); return testInjector; }; @@ -98,16 +105,27 @@ describe("projectService", () => { const projectService = testInjector.resolve( ProjectServiceLib.ProjectService ); + const projectDir = path.join(dirToCreateProject, projectName); const projectCreationData = await projectService.createProject({ projectName: projectName, pathToProject: dirToCreateProject, force: true, template: constants.RESERVED_TEMPLATE_NAMES["default"], }); + assert.deepStrictEqual(projectCreationData, { projectName, - projectDir: path.join(dirToCreateProject, projectName), + projectDir, }); + + assert.deepEqual( + testInjector.resolve("childProcess")._getExecutedCommands(), + [ + `git init ${projectDir}`, + `git -C ${projectDir} add --all`, + `git -C ${projectDir} commit --no-verify -m "init"`, + ] + ); }); it("fails when invalid name is passed when projectNameService fails", async () => { @@ -188,6 +206,7 @@ describe("projectService", () => { downloadAndExtract: () => Promise.resolve(), }); testInjector.register("tempService", TempServiceStub); + testInjector.register("childProcess", {}); return testInjector; };