Skip to content

Commit 9f78c15

Browse files
feat: initialize git repo on project create (#5468)
* 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 <[email protected]>
1 parent c8447cd commit 9f78c15

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

lib/services/project-service.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
IFileSystem,
2525
IProjectHelper,
2626
IStringDictionary,
27+
IChildProcess,
2728
} from "../common/declarations";
2829
import * as _ from "lodash";
2930
import { injector } from "../common/yok";
@@ -42,7 +43,8 @@ export class ProjectService implements IProjectService {
4243
private $projectNameService: IProjectNameService,
4344
private $projectTemplatesService: IProjectTemplatesService,
4445
private $tempService: ITempService,
45-
private $staticConfig: IStaticConfig
46+
private $staticConfig: IStaticConfig,
47+
private $childProcess: IChildProcess
4648
) {}
4749

4850
public async validateProjectName(opts: {
@@ -103,6 +105,17 @@ export class ProjectService implements IProjectService {
103105
projectName,
104106
});
105107

108+
try {
109+
await this.$childProcess.exec(`git init ${projectDir}`);
110+
await this.$childProcess.exec(`git -C ${projectDir} add --all`);
111+
await this.$childProcess.exec(`git -C ${projectDir} commit --no-verify -m "init"`);
112+
} catch (err) {
113+
this.$logger.trace(
114+
"Unable to initialize git repository. Error is: ",
115+
err
116+
);
117+
}
118+
106119
this.$logger.info();
107120
this.$logger.printMarkdown(
108121
"__Project `%s` was successfully created.__",

test/project-service.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ describe("projectService", () => {
8787
extractPackage: () => Promise.resolve(),
8888
});
8989
testInjector.register("tempService", TempServiceStub);
90+
const executedCommands: string[] = [];
91+
testInjector.register("childProcess", {
92+
_getExecutedCommands: () => executedCommands,
93+
exec: (executedCommand: string) => {
94+
executedCommands.push(executedCommand);
95+
},
96+
});
9097

9198
return testInjector;
9299
};
@@ -98,16 +105,27 @@ describe("projectService", () => {
98105
const projectService = testInjector.resolve<IProjectService>(
99106
ProjectServiceLib.ProjectService
100107
);
108+
const projectDir = path.join(dirToCreateProject, projectName);
101109
const projectCreationData = await projectService.createProject({
102110
projectName: projectName,
103111
pathToProject: dirToCreateProject,
104112
force: true,
105113
template: constants.RESERVED_TEMPLATE_NAMES["default"],
106114
});
115+
107116
assert.deepStrictEqual(projectCreationData, {
108117
projectName,
109-
projectDir: path.join(dirToCreateProject, projectName),
118+
projectDir,
110119
});
120+
121+
assert.deepEqual(
122+
testInjector.resolve("childProcess")._getExecutedCommands(),
123+
[
124+
`git init ${projectDir}`,
125+
`git -C ${projectDir} add --all`,
126+
`git -C ${projectDir} commit --no-verify -m "init"`,
127+
]
128+
);
111129
});
112130

113131
it("fails when invalid name is passed when projectNameService fails", async () => {
@@ -188,6 +206,7 @@ describe("projectService", () => {
188206
downloadAndExtract: () => Promise.resolve(),
189207
});
190208
testInjector.register("tempService", TempServiceStub);
209+
testInjector.register("childProcess", {});
191210

192211
return testInjector;
193212
};

0 commit comments

Comments
 (0)