Skip to content

Commit

Permalink
feat: initialize git repo on project create (#5468)
Browse files Browse the repository at this point in the history
* feat: initialize git repo on project create

* refactor: use child process

* test: git init commands

* fix: ignore commit hooks

* test: update test with --no-verify

Co-authored-by: Igor Randjelovic <rigor789@gmail.com>
  • Loading branch information
janoshrubos and rigor789 authored Jan 14, 2021
1 parent c8447cd commit 9f78c15
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
15 changes: 14 additions & 1 deletion lib/services/project-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
IFileSystem,
IProjectHelper,
IStringDictionary,
IChildProcess,
} from "../common/declarations";
import * as _ from "lodash";
import { injector } from "../common/yok";
Expand All @@ -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: {
Expand Down Expand Up @@ -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.__",
Expand Down
21 changes: 20 additions & 1 deletion test/project-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand All @@ -98,16 +105,27 @@ describe("projectService", () => {
const projectService = testInjector.resolve<IProjectService>(
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 () => {
Expand Down Expand Up @@ -188,6 +206,7 @@ describe("projectService", () => {
downloadAndExtract: () => Promise.resolve(),
});
testInjector.register("tempService", TempServiceStub);
testInjector.register("childProcess", {});

return testInjector;
};
Expand Down

0 comments on commit 9f78c15

Please sign in to comment.