-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e725f28
commit a438004
Showing
3 changed files
with
75 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'sv': patch | ||
--- | ||
|
||
add prompt to setup git repo if already not initialized |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { execSync } from 'node:child_process'; | ||
import * as p from '@sveltejs/clack-prompts'; | ||
|
||
function hasGitInstalled() { | ||
try { | ||
execSync('git --version', { stdio: 'ignore' }); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
|
||
function isGitRepo(cwd: string) { | ||
try { | ||
return ( | ||
execSync('git rev-parse --is-inside-work-tree', { cwd, stdio: 'pipe' }).toString().trim() === | ||
'true' | ||
); | ||
} catch { | ||
return false; | ||
} | ||
} | ||
|
||
export async function initGitRepo(cwd: string) { | ||
execSync('git init', { cwd, stdio: 'ignore' }); | ||
execSync('git add -A', { cwd, stdio: 'ignore' }); | ||
} | ||
|
||
export async function gitInitPrompt(cwd: string): Promise<boolean> { | ||
const hasGit = hasGitInstalled(); | ||
if (!hasGit) return false; | ||
const alreadyGitRepo = isGitRepo(cwd); | ||
if (alreadyGitRepo) return false; | ||
|
||
const shouldInit = await p.confirm({ | ||
message: 'Do you want to initialize a git repository?', | ||
active: 'yes', | ||
inactive: 'no', | ||
initialValue: true | ||
}); | ||
|
||
if (p.isCancel(shouldInit)) { | ||
p.cancel('Operation cancelled.'); | ||
process.exit(1); | ||
} | ||
|
||
return shouldInit; | ||
} |