Skip to content

Commit 6d9b7df

Browse files
committed
feat(create-project): detect git repo before initializing and --no-git support
1 parent 9705645 commit 6d9b7df

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

lib/declarations.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,7 @@ interface IOptions
683683
appleApplicationSpecificPassword: string;
684684
appleSessionBase64: string;
685685
markingMode: boolean;
686+
git: boolean;
686687
}
687688

688689
interface IEnvOptions {

lib/options.ts

+5
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,11 @@ export class Options {
225225
jar: { type: OptionType.String, hasSensitiveValue: true },
226226
aar: { type: OptionType.String, hasSensitiveValue: true },
227227
filter: { type: OptionType.String, hasSensitiveValue: true },
228+
git: {
229+
type: OptionType.Boolean,
230+
hasSensitiveValue: false,
231+
default: true,
232+
},
228233
};
229234
}
230235

lib/services/project-service.ts

+25-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as constants from "../constants";
22
import * as path from "path";
3+
import simpleGit, { SimpleGit } from "simple-git";
34
import { exported } from "../common/decorators";
45
import { Hooks } from "../constants";
56
import { performanceLog } from "../common/decorators";
@@ -15,6 +16,7 @@ import {
1516
} from "../definitions/project";
1617
import {
1718
INodePackageManager,
19+
IOptions,
1820
IProjectNameService,
1921
IStaticConfig,
2022
} from "../declarations";
@@ -32,6 +34,7 @@ import { ITempService } from "../definitions/temp-service";
3234

3335
export class ProjectService implements IProjectService {
3436
constructor(
37+
private $options: IOptions,
3538
private $hooksService: IHooksService,
3639
private $packageManager: INodePackageManager,
3740
private $errors: IErrors,
@@ -106,24 +109,30 @@ export class ProjectService implements IProjectService {
106109
projectName,
107110
});
108111

109-
try {
110-
await this.$childProcess.exec(`git init ${projectDir}`);
111-
await this.$childProcess.exec(`git -C ${projectDir} add --all`);
112-
await this.$childProcess.exec(
113-
`git -C ${projectDir} commit --no-verify -m "init"`
114-
);
115-
} catch (err) {
116-
this.$logger.trace(
117-
"Unable to initialize git repository. Error is: ",
118-
err
119-
);
112+
// can pass --no-git to skip creating a git repo
113+
// useful in monorepos where we're creating
114+
// sub projects in an existing git repo.
115+
if (this.$options.git) {
116+
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.");
121+
}
122+
await this.$childProcess.exec(`git init ${projectDir}`);
123+
await this.$childProcess.exec(`git -C ${projectDir} add --all`);
124+
await this.$childProcess.exec(
125+
`git -C ${projectDir} commit --no-verify -m "init"`
126+
);
127+
} catch (err) {
128+
this.$logger.trace(
129+
"Unable to initialize git repository. Error is: ",
130+
err
131+
);
132+
}
120133
}
121134

122-
this.$logger.info();
123-
this.$logger.printMarkdown(
124-
"__Project `%s` was successfully created.__",
125-
projectName
126-
);
135+
this.$logger.trace(`Project ${projectName} was successfully created.`);
127136

128137
return projectCreationData;
129138
}

0 commit comments

Comments
 (0)