Skip to content

Commit 30d42d2

Browse files
committed
feat(create-project): allow --force to forcefully create git repo inside another one
1 parent 7c8741e commit 30d42d2

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

lib/services/project-service.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,12 @@ export class ProjectService implements IProjectService {
114114
// sub projects in an existing git repo.
115115
if (this.$options.git) {
116116
try {
117-
const git: SimpleGit = simpleGit(projectDir);
118-
if (await git.checkIsRepo()) {
119-
// throwing here since we're catching below.
120-
throw new Error("Already part of a git repository.");
117+
if (!this.$options.force) {
118+
const git: SimpleGit = simpleGit(projectDir);
119+
if (await git.checkIsRepo()) {
120+
// throwing here since we're catching below.
121+
throw new Error("Already part of a git repository.");
122+
}
121123
}
122124
await this.$childProcess.exec(`git init ${projectDir}`);
123125
await this.$childProcess.exec(`git -C ${projectDir} add --all`);

test/project-service.ts

+45-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
IProjectDataService,
1111
IProjectService,
1212
} from "../lib/definitions/project";
13-
import { IProjectNameService } from "../lib/declarations";
13+
import { IOptions, IProjectNameService } from "../lib/declarations";
1414
import { IInjector } from "../lib/common/definitions/yok";
1515
import { IDictionary, IFileSystem } from "../lib/common/declarations";
1616
import { ProjectConfigService } from "../lib/services/project-config-service";
@@ -118,6 +118,26 @@ describe("projectService", () => {
118118
projectName,
119119
projectDir,
120120
});
121+
});
122+
123+
it("creates a git repo", async () => {
124+
const projectName = invalidProjectName;
125+
const testInjector = getTestInjector({ projectName });
126+
const options = testInjector.resolve<IOptions>("options");
127+
const projectService = testInjector.resolve<IProjectService>(
128+
ProjectServiceLib.ProjectService
129+
);
130+
const projectDir = path.join(dirToCreateProject, projectName);
131+
132+
// force creation to skip git repo check...
133+
options.force = true;
134+
135+
await projectService.createProject({
136+
projectName: projectName,
137+
pathToProject: dirToCreateProject,
138+
force: true,
139+
template: constants.RESERVED_TEMPLATE_NAMES["default"],
140+
});
121141

122142
assert.deepEqual(
123143
testInjector.resolve("childProcess")._getExecutedCommands(),
@@ -129,6 +149,30 @@ describe("projectService", () => {
129149
);
130150
});
131151

152+
it("does not create a git repo with --no-git", async () => {
153+
const projectName = invalidProjectName;
154+
const testInjector = getTestInjector({ projectName });
155+
const options = testInjector.resolve<IOptions>("options");
156+
const projectService = testInjector.resolve<IProjectService>(
157+
ProjectServiceLib.ProjectService
158+
);
159+
160+
// simulate --no-git
161+
options.git = false;
162+
163+
await projectService.createProject({
164+
projectName: projectName,
165+
pathToProject: dirToCreateProject,
166+
force: true,
167+
template: constants.RESERVED_TEMPLATE_NAMES["default"],
168+
});
169+
170+
assert.deepEqual(
171+
testInjector.resolve("childProcess")._getExecutedCommands(),
172+
[]
173+
);
174+
});
175+
132176
it("fails when invalid name is passed when projectNameService fails", async () => {
133177
const projectName = invalidProjectName;
134178
const testInjector = getTestInjector({ projectName });

0 commit comments

Comments
 (0)