Skip to content

Commit 9b720e0

Browse files
authored
feat(cli): add --skip-git option (#3860)
* feat(cli): add `--skip-git` option * add test
1 parent 64714a4 commit 9b720e0

File tree

6 files changed

+63
-12
lines changed

6 files changed

+63
-12
lines changed

packages/api/cli/src/electron-forge-init.ts

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ program
1313
.option('-t, --template [name]', 'Name of the Forge template to use.', 'base')
1414
.option('-c, --copy-ci-files', 'Whether to copy the templated CI files.', false)
1515
.option('-f, --force', 'Whether to overwrite an existing directory.', false)
16+
.option('--skip-git', 'Skip initializing a git repository in the initialized project.', false)
1617
.action(async (dir) => {
1718
const workingDir = resolveWorkingDir(dir, false);
1819

packages/api/cli/src/electron-forge.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import './util/terminate';
88

99
import packageJSON from '../package.json';
1010

11-
import { checkSystem } from './util/check-system';
11+
import { checkSystem, SystemCheckContext } from './util/check-system';
1212

1313
program
1414
.version(packageJSON.version, '-V, --version', 'Output the current version.')
@@ -21,14 +21,13 @@ program
2121
.command('publish', 'Publish the current Electron application.')
2222
.hook('preSubcommand', async (_command, subcommand) => {
2323
if (!process.argv.includes('--help') && !process.argv.includes('-h')) {
24-
const runner = new Listr<{
25-
command: string;
26-
}>(
24+
const runner = new Listr<SystemCheckContext>(
2725
[
2826
{
2927
title: 'Checking your system',
3028
task: async (ctx, task) => {
3129
ctx.command = subcommand.name();
30+
ctx.git = !process.argv.includes('--skip-git');
3231
return await checkSystem(task);
3332
},
3433
},

packages/api/cli/src/util/check-system.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,22 @@ export async function checkPackageManager() {
105105
*/
106106
const SKIP_SYSTEM_CHECK = path.resolve(os.homedir(), '.skip-forge-system-check');
107107

108-
type SystemCheckContext = {
108+
export type SystemCheckContext = {
109+
command: string;
109110
git: boolean;
110111
node: boolean;
111112
packageManager: boolean;
112113
};
113114

114-
export async function checkSystem(callerTask: ForgeListrTask<{ command: string }>) {
115+
export async function checkSystem(callerTask: ForgeListrTask<SystemCheckContext>) {
115116
if (!(await fs.pathExists(SKIP_SYSTEM_CHECK))) {
116117
d('checking system, create ~/.skip-forge-system-check to stop doing this');
117-
return callerTask.newListr<{ command: string } & SystemCheckContext>(
118+
return callerTask.newListr<SystemCheckContext>(
118119
[
119120
{
120121
title: 'Checking git exists',
121122
// We only call the `initGit` helper in the `init` and `import` commands
122-
enabled: (ctx): boolean => ctx.command === 'init' || ctx.command === 'import',
123+
enabled: (ctx): boolean => (ctx.command === 'init' || ctx.command === 'import') && ctx.git,
123124
task: async (_, task) => {
124125
const gitVersion = await getGitVersion();
125126
if (gitVersion) {

packages/api/core/spec/slow/api.slow.spec.ts

+18
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,24 @@ describe.each([PACKAGE_MANAGERS['npm'], PACKAGE_MANAGERS['yarn'], PACKAGE_MANAGE
5555
});
5656
};
5757

58+
describe('init (with skipGit)', () => {
59+
beforeAll(async () => {
60+
dir = await ensureTestDirIsNonexistent();
61+
62+
return async () => {
63+
await fs.promises.rm(dir, { recursive: true, force: true });
64+
};
65+
});
66+
67+
it('should not initialize a git repo if passed the skipGit option', async () => {
68+
await api.init({
69+
dir,
70+
skipGit: true,
71+
});
72+
expect(fs.existsSync(path.join(dir, '.git'))).toEqual(false);
73+
});
74+
});
75+
5876
describe('init', () => {
5977
beforeInitTest();
6078

packages/api/core/src/api/import.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,26 @@ export interface ImportOptions {
5050
* The path to the directory containing generated distributables
5151
*/
5252
outDir?: string;
53+
/**
54+
* By default, Forge initializes a git repository in the project directory. Set this option to `true` to skip this step.
55+
*/
56+
skipGit?: boolean;
5357
}
5458

5559
export default autoTrace(
5660
{ name: 'import()', category: '@electron-forge/core' },
5761
async (
5862
childTrace,
59-
{ dir = process.cwd(), interactive = false, confirmImport, shouldContinueOnExisting, shouldRemoveDependency, shouldUpdateScript, outDir }: ImportOptions
63+
{
64+
dir = process.cwd(),
65+
interactive = false,
66+
confirmImport,
67+
shouldContinueOnExisting,
68+
shouldRemoveDependency,
69+
shouldUpdateScript,
70+
outDir,
71+
skipGit = false,
72+
}: ImportOptions
6073
): Promise<void> => {
6174
const listrOptions: ForgeListrOptions<{ pm: PMDetails }> = {
6275
concurrent: false,
@@ -86,7 +99,9 @@ export default autoTrace(
8699
}
87100
}
88101

89-
await initGit(dir);
102+
if (!skipGit) {
103+
await initGit(dir);
104+
}
90105
}),
91106
},
92107
{

packages/api/core/src/api/init.ts

+19-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ export interface InitOptions {
3939
* The custom template to use. If left empty, the default template is used
4040
*/
4141
template?: string;
42+
/**
43+
* By default, Forge initializes a git repository in the project directory. Set this option to `true` to skip this step.
44+
*/
45+
skipGit?: boolean;
4246
}
4347

4448
async function validateTemplate(template: string, templateModule: ForgeTemplate): Promise<void> {
@@ -54,7 +58,14 @@ async function validateTemplate(template: string, templateModule: ForgeTemplate)
5458
}
5559
}
5660

57-
export default async ({ dir = process.cwd(), interactive = false, copyCIFiles = false, force = false, template = 'base' }: InitOptions): Promise<void> => {
61+
export default async ({
62+
dir = process.cwd(),
63+
interactive = false,
64+
copyCIFiles = false,
65+
force = false,
66+
template = 'base',
67+
skipGit = false,
68+
}: InitOptions): Promise<void> => {
5869
d(`Initializing in: ${dir}`);
5970

6071
const runner = new Listr<{
@@ -79,10 +90,16 @@ export default async ({ dir = process.cwd(), interactive = false, copyCIFiles =
7990
title: 'Initializing directory',
8091
task: async (_, task) => {
8192
await initDirectory(dir, task, force);
82-
await initGit(dir);
8393
},
8494
rendererOptions: { persistentOutput: true },
8595
},
96+
{
97+
title: 'Initializing git repository',
98+
enabled: !skipGit,
99+
task: async () => {
100+
await initGit(dir);
101+
},
102+
},
86103
{
87104
title: 'Preparing template',
88105
task: async ({ templateModule }) => {

0 commit comments

Comments
 (0)