-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cpa): initialize git repo on project creation (#6342)
- Loading branch information
Showing
7 changed files
with
71 additions
and
8 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
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
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
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,50 @@ | ||
import type { ExecSyncOptions } from 'child_process' | ||
|
||
import { execSync } from 'child_process' | ||
|
||
import { warning } from './log.js' | ||
|
||
export function tryInitRepoAndCommit(args: { cwd: string }): void { | ||
const execOpts: ExecSyncOptions = { cwd: args.cwd, stdio: 'ignore' } | ||
try { | ||
// Check if git is available | ||
execSync('git -v', execOpts) | ||
|
||
// Do nothing if already in a git repo | ||
if (isGitRepo(execOpts)) { | ||
return | ||
} | ||
|
||
// Initialize | ||
execSync('git init', execOpts) | ||
if (!ensureHasDefaultBranch(execOpts)) { | ||
execSync('git checkout -b main', execOpts) | ||
} | ||
|
||
// Add and commit files | ||
execSync('git add -A', execOpts) | ||
execSync('git commit -m "feat: initial commit"', execOpts) | ||
} catch (_) { | ||
warning('Failed to initialize git repository.') | ||
} | ||
} | ||
|
||
function isGitRepo(opts: ExecSyncOptions): boolean { | ||
try { | ||
execSync('git rev-parse --is-inside-work-tree', opts) | ||
return true | ||
} catch (_) { | ||
// Ignore errors | ||
} | ||
return false | ||
} | ||
|
||
function ensureHasDefaultBranch(opts: ExecSyncOptions): boolean { | ||
try { | ||
execSync(`git config init.defaultBranch`, opts) | ||
return true | ||
} catch (_) { | ||
// Ignore errros | ||
} | ||
return false | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.